Skip to content

Commit

Permalink
Add some basic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
myaaaaaaaaa committed Jul 5, 2024
1 parent 6ad0279 commit fd53ebf
Showing 1 changed file with 156 additions and 0 deletions.
156 changes: 156 additions & 0 deletions gl41_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
package gl_test

import (
"errors"
"runtime"
"testing"

"github.com/go-gl/gl/v4.1-core/gl"
"github.com/go-gl/glfw/v3.3/glfw"

Check failure on line 9 in gl41_test.go

View workflow job for this annotation

GitHub Actions / stable macos-latest

no required module provides package github.com/go-gl/glfw/v3.3/glfw; to add it:

Check failure on line 9 in gl41_test.go

View workflow job for this annotation

GitHub Actions / stable windows-latest

no required module provides package github.com/go-gl/glfw/v3.3/glfw; to add it:

Check failure on line 9 in gl41_test.go

View workflow job for this annotation

GitHub Actions / stable ubuntu-latest

no required module provides package github.com/go-gl/glfw/v3.3/glfw; to add it:
)

func checkIntegers(t *testing.T) {
// See https://registry.khronos.org/OpenGL-Refpages/gl4/html/glGet.xhtml
var data int32
gl.GetIntegerv(gl.MAJOR_VERSION, &data)
if data != 4 {
// OpenGL 5 with raytracing...?
t.Error("invalid GL_MAJOR_VERSION:", data)
}
gl.GetIntegerv(gl.MAX_TEXTURE_SIZE, &data)
if data < 1024 {
// Guaranteed by spec
t.Error("invalid GL_MAX_TEXTURE_SIZE:", data)
}

if err := gl.GetError(); err != gl.NO_ERROR {
t.Error("glGetIntegerv():", err)
}
}
func checkStrings(t *testing.T) {
// See https://registry.khronos.org/OpenGL-Refpages/gl4/html/glGetString.xhtml
gl.GetString(gl.VENDOR)
gl.GetString(gl.RENDERER)
gl.GetString(gl.VERSION)
gl.GetString(gl.SHADING_LANGUAGE_VERSION)
if err := gl.GetError(); err != gl.NO_ERROR {
t.Error("glGetString():", err)
}

gl.GetString(gl.MAX_TEXTURE_SIZE)
if err := gl.GetError(); err != gl.INVALID_ENUM {
t.Error("glGetString() failed to return GL_INVALID_ENUM:", err)
}
}
func checkTextures(t *testing.T) {
var texture uint32
gl.GenTextures(1, &texture)
if texture == 0 {
t.Error("glGenTextures() returned zero")
}

// Textures must be bound before glIsTexture will recognize them.
// See https://registry.khronos.org/OpenGL-Refpages/gl4/html/glIsTexture.xhtml
gl.BindTexture(gl.TEXTURE_2D, texture)
if !gl.IsTexture(texture) {
t.Error("glIsTexture() failed to recognize a texture returned by glGenTextures()")
}

gl.DeleteTextures(1, &texture)
if gl.IsTexture(texture) {
t.Error("glDeleteTextures() did not delete texture")
}

if err := gl.GetError(); err != gl.NO_ERROR {
t.Error("texture error:", err)
}

gl.GenTextures(-1, &texture)
if err := gl.GetError(); err != gl.INVALID_VALUE {
t.Error("glGenTextures() failed to return GL_INVALID_VALUE:", err)
}
}
func checkShader(t *testing.T, src string) error {
csrc, free := gl.Strs(src + "\x00")
defer free()

shader := gl.CreateShader(gl.VERTEX_SHADER)
if shader == 0 {
t.Error("glCreateShader() returned zero")
}
defer gl.DeleteShader(shader)

gl.ShaderSource(shader, 1, csrc, nil)
gl.CompileShader(shader)
if !gl.IsShader(shader) {
t.Error("glIsShader() failed to recognize a shader returned by glCreateShader()")
}

if err := gl.GetError(); err != gl.NO_ERROR {
t.Error("shader error:", err)
}

var data int32
gl.GetShaderiv(shader, gl.COMPILE_STATUS, &data)
if data == gl.TRUE {
return nil
}

gl.GetShaderiv(shader, gl.INFO_LOG_LENGTH, &data)
infoLog := make([]byte, data+1)
gl.GetShaderInfoLog(shader, data, nil, &infoLog[0])
return errors.New(src + "\n" + string(infoLog))
}

func TestBasic(t *testing.T) {
// Each test runs in its own goroutine, so we need to lock OS threads here
// rather than in the init() function.
runtime.LockOSThread()
defer runtime.UnlockOSThread()

if err := glfw.Init(); err != nil {
t.Fatal("failed to initialize glfw:", err)
}
defer glfw.Terminate()

glfw.WindowHint(glfw.Visible, glfw.False)
glfw.WindowHint(glfw.ContextVersionMajor, 4)
glfw.WindowHint(glfw.ContextVersionMinor, 1)

window, err := glfw.CreateWindow(800, 600, "Test", nil, nil)
if err != nil {
t.Fatal("failed to create glfw window:", err)
}
defer window.Destroy()
window.MakeContextCurrent()

if err := gl.Init(); err != nil {
t.Fatal("failed to initialize opengl:", err)
}

checkIntegers(t)
checkStrings(t)
checkTextures(t)

err = checkShader(t, `
#version 410
void main() {
gl_Position = vec4(0, 0, 0, 1);
}
`)
if err != nil {
t.Error("unexpected compile error:", err)
}

err = checkShader(t, `
#version 410
void main() {
gl_Undefined = vec5(0, 0, 0, 1, 1);
}
`)
if err == nil {
t.Error("unexpected successful compilation of invalid shader")
} else {
t.Log(err)
}
}

0 comments on commit fd53ebf

Please sign in to comment.