Browse code

Add test

Robert Cranston authored on 16/05/2020 09:52:54
Showing 4 changed files

1 1
new file mode 100644
... ...
@@ -0,0 +1 @@
1
+/test/test
0 2
new file mode 100644
... ...
@@ -0,0 +1,61 @@
1
+## Project specific variables
2
+PROG = test/test
3
+CXXSTD = c++11
4
+HDRDIR = .
5
+
6
+## Implicit variable defaults
7
+WARNFLAGS += -pedantic -Wall -Wextra -Werror
8
+CXXFLAGS  += $(WARNFLAGS) -Weffc++
9
+CXXFLAGS  += -I$(HDRDIR) -std=$(CXXSTD)
10
+LDLIBS    += -lglfw -lGLEW -lGL
11
+
12
+## Build type
13
+CXXFLAGS += -g
14
+
15
+## Files
16
+SRCS =
17
+HDRS =
18
+
19
+## Standard targets
20
+
21
+.PHONY: all
22
+all: $(PROG)
23
+
24
+.PHONY: check
25
+check: test
26
+
27
+.PHONY: test
28
+test: $(PROG)
29
+	cd $(<D) && ./$(<F)
30
+
31
+.PHONY: distclean
32
+distclean:
33
+	$(RM) $(PROG)
34
+
35
+## Main targets
36
+
37
+test/test: CXXFLAGS += -DGLSHADER_TEST
38
+test/test: test/test.cpp $(SRCS)
39
+
40
+## Header dependency targets
41
+
42
+$(SRCS): $(HDRS)
43
+	touch $@
44
+
45
+## Auxiliary targets
46
+
47
+.PHONY: install-deps-build
48
+install-deps-build:
49
+	sudo apt-get install \
50
+	    libgl1-mesa-dev \
51
+	    libglew-dev \
52
+	    libglm-dev
53
+
54
+.PHONY: install-deps-test
55
+install-deps-test:
56
+	sudo apt-get install \
57
+	    libglfw3-dev
58
+
59
+.PHONY: debug
60
+debug: $(PROG)
61
+	cd $(<D) && gdb ./$(<F)
... ...
@@ -7,11 +7,36 @@ A C++11 helper for [OpenGL][] >=2.0 [shaders][Shader].
7 7
 [`std::runtime_error`]: https://en.cppreference.com/w/cpp/error/runtime_error
8 8
 [`what()`]: https://en.cppreference.com/w/cpp/error/exception/what
9 9
 
10
+## Tests
11
+
12
+A test, using [GLFW][], is provided in [`test/`](test/) and can be run with
13
+`make test` from the repository root.
14
+
15
+[GLFW]: https://www.glfw.org/
16
+
10 17
 ## Dependencies
11 18
 
19
+The [OpenGL Extension Wrangler (GLEW)][] library is used for extension loading.
20
+
12 21
 [`str.hpp`][] is bundled.
13 22
 
23
+Installing dependencies on a Linux distribution using the [APT package
24
+manager][] can be done with:
25
+
26
+```sh
27
+// Library dependencies.
28
+sudo apt-get install \
29
+    libgl1-mesa-dev \
30
+    libglew-dev
31
+
32
+// Test dependencies.
33
+sudo apt-get install \
34
+    libglfw3-dev
35
+```
36
+
37
+[OpenGL Extension Wrangler (GLEW)]: http://glew.sourceforge.net
14 38
 [`str.hpp`]: https://git.rcrnstn.net/rcrnstn/str.hpp
39
+[APT package manager]: https://en.wikipedia.org/wiki/APT_(software)
15 40
 
16 41
 ## License
17 42
 
18 43
new file mode 100644
... ...
@@ -0,0 +1,138 @@
1
+#ifdef GLSHADER_TEST
2
+
3
+
4
+#include <iostream>
5
+#include <iomanip>
6
+#include <stdexcept>
7
+
8
+#include <GL/glew.h>
9
+#include <GLFW/glfw3.h>
10
+
11
+#include "str.hpp"
12
+
13
+
14
+static auto debugMessageDisable = false;
15
+
16
+
17
+// Only check the first line, the others are from the driver dependent OpenGL
18
+// info log.
19
+#define EXPECT_EXCEPTION(CODE, WHAT) \
20
+    { \
21
+        std::string what; \
22
+        debugMessageDisable = true; \
23
+        try { \
24
+            CODE; \
25
+        } catch (std::exception const & e) { \
26
+            what = e.what(); \
27
+            what = what.substr(0, what.find("\n")); \
28
+        } \
29
+        debugMessageDisable = false; \
30
+        if (what.empty()) \
31
+            throw std::runtime_error{STR( \
32
+                "Expected exception \"" << WHAT << "\", got none." \
33
+            )}; \
34
+        if (what != WHAT) \
35
+            throw std::runtime_error{STR( \
36
+                "Expected exception \"" << WHAT << "\", got \"" << what \
37
+                << "\"." \
38
+            )}; \
39
+    }
40
+
41
+
42
+void test() {
43
+}
44
+
45
+
46
+void errorCallback(int /* error */, char const * description) {
47
+    std::cerr << "GLFW ERROR: " << description << std::endl;
48
+    exit(1);
49
+}
50
+
51
+
52
+void APIENTRY debugMessageCallback(
53
+    GLenum /* source */,
54
+    GLenum /* type */,
55
+    GLuint /* id */,
56
+    GLenum severity,
57
+    GLsizei /* length */,
58
+    const GLchar *message,
59
+    const void * /* userParam */
60
+) {
61
+    if (debugMessageDisable)
62
+        return;
63
+    std::cerr << "GL DEBUG MESSAGE: " << message << std::endl;
64
+    if (
65
+        severity == GL_DEBUG_SEVERITY_HIGH ||
66
+        severity == GL_DEBUG_SEVERITY_MEDIUM
67
+    )
68
+        exit(1);
69
+}
70
+
71
+
72
+int main() {
73
+    try {
74
+        glfwInit();
75
+        glfwSetErrorCallback(errorCallback);
76
+        glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
77
+        glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
78
+        glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GLFW_TRUE);
79
+        auto window = glfwCreateWindow(
80
+            640, 480, "glshader test", nullptr, nullptr
81
+        );
82
+        glfwMakeContextCurrent(window);
83
+        glfwSwapInterval(0);
84
+
85
+        glewInit();
86
+        #define GLSHADER_TEST_PRINT(NAME) \
87
+            std::cout << std::setw(24+2) << std::left << #NAME ": " << \
88
+                glGetString(GL_##NAME) << std::endl;
89
+        GLSHADER_TEST_PRINT(VENDOR);
90
+        GLSHADER_TEST_PRINT(RENDERER);
91
+        GLSHADER_TEST_PRINT(VERSION);
92
+        GLSHADER_TEST_PRINT(SHADING_LANGUAGE_VERSION);
93
+        glEnable(GL_DEBUG_OUTPUT);
94
+        glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
95
+        glDebugMessageControl(
96
+            GL_DONT_CARE,
97
+            GL_DONT_CARE,
98
+            GL_DEBUG_SEVERITY_NOTIFICATION,
99
+            0,
100
+            nullptr,
101
+            GL_FALSE
102
+        );
103
+        glDebugMessageCallback(debugMessageCallback, 0);
104
+
105
+        std::cout << "---\n";
106
+
107
+        int frame = 0;
108
+        glfwSetTime(0);
109
+        while (glfwGetTime() < 1) {
110
+            ++frame;
111
+            test();
112
+
113
+            glBegin(GL_TRIANGLE_STRIP);
114
+            glVertex3f(-0.5, -0.5, 0);
115
+            glVertex3f(-0.5, +0.5, 0);
116
+            glVertex3f(+0.5, +0.5, 0);
117
+            glVertex3f(+0.5, -0.5, 0);
118
+            glVertex3f(-0.5, -0.5, 0);
119
+            glEnd();
120
+
121
+            glfwSwapBuffers(window);
122
+            glfwPollEvents();
123
+        }
124
+
125
+        std::cout << "---\n";
126
+
127
+        std::cout << "Frames run: " << frame << "\n";
128
+
129
+        glfwDestroyWindow(window);
130
+        glfwTerminate();
131
+    } catch (std::exception const & e) {
132
+        std::cerr << e.what() << std::endl;
133
+        return 1;
134
+    }
135
+}
136
+
137
+
138
+#endif // GLSHADER_TEST