gallium/osmesa: Convert osmesa test to gtest
authorDylan Baker <dylan@pnwbakers.com>
Mon, 21 Oct 2019 20:08:32 +0000 (13:08 -0700)
committerDylan Baker <dylan@pnwbakers.com>
Thu, 7 Nov 2019 14:11:19 +0000 (06:11 -0800)
This uses a bunch of additional C++ features for niceness and safety.

Reviewed-by: Brian Paul <brianp@vmware.com>
src/gallium/targets/osmesa/meson.build
src/gallium/targets/osmesa/test-render.c [deleted file]
src/gallium/targets/osmesa/test-render.cpp [new file with mode: 0644]

index 7792aa1e2fc18a5e74c75c533fe9c1c3bae1b2bf..8b2ba248b60907a5b7a22b012f3ea1096ec77ddc 100644 (file)
@@ -73,9 +73,13 @@ pkg.generate(
 
 if with_tests
   test('osmesa-render',
-       executable('osmesa-render',
-                  'test-render.c',
-                  include_directories : inc_common,
-                  link_with: libosmesa),
-       suite: 'gallium')
+    executable(
+      'osmesa-render',
+      'test-render.cpp',
+      include_directories : inc_common,
+      link_with: libosmesa,
+      dependencies : [idep_gtest],
+    ),
+    suite: 'gallium'
+  )
 endif
diff --git a/src/gallium/targets/osmesa/test-render.c b/src/gallium/targets/osmesa/test-render.c
deleted file mode 100644 (file)
index dc5cac2..0000000
+++ /dev/null
@@ -1,44 +0,0 @@
-#include <stdint.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include "GL/osmesa.h"
-
-static void
-render(void)
-{
-   glClearColor(0, 1, 0, 0);
-   glClear(GL_COLOR_BUFFER_BIT);
-}
-
-int
-main(int argc, char **argv)
-{
-   OSMesaContext ctx;
-   uint32_t pixel;
-   uint32_t green = 0xff << 8;
-   int w = 1, h = 1;
-
-   ctx = OSMesaCreateContext(GL_RGBA, NULL);
-   if (!ctx) {
-      fprintf(stderr, "Context create failed\n");
-      return 1;
-   }
-
-   if (!OSMesaMakeCurrent(ctx, &pixel, GL_UNSIGNED_BYTE, w, h )) {
-      fprintf(stderr, "MakeCurrent failed\n");
-      return 1;
-   }
-
-   render();
-   glFinish();
-
-   if (pixel != green) {
-      fprintf(stderr, "Expected: 0x%08x\n", green);
-      fprintf(stderr, "Probed: 0x%08x\n", pixel);
-      return 1;
-   }
-
-   OSMesaDestroyContext(ctx);
-
-   return 0;
-}
diff --git a/src/gallium/targets/osmesa/test-render.cpp b/src/gallium/targets/osmesa/test-render.cpp
new file mode 100644 (file)
index 0000000..1adffe7
--- /dev/null
@@ -0,0 +1,99 @@
+#include <cstdint>
+#include <cstdio>
+#include <cstdlib>
+#include <array>
+#include <memory>
+
+#include <gtest/gtest.h>
+
+#include "GL/osmesa.h"
+
+
+typedef std::array<GLenum, 2> Params;
+
+class OSMesaRenderTestFixture : public testing::TestWithParam<Params> {};
+
+std::string
+name_params(const testing::TestParamInfo<Params> params) {
+   auto p = params.param;
+   std::string first, second;
+   switch (p[0]) {
+   case OSMESA_RGBA:
+      first = "rgba";
+      break;
+   case OSMESA_BGRA:
+      first = "bgra";
+      break;
+   case OSMESA_RGB:
+      first = "rgb";
+      break;
+   case OSMESA_RGB_565:
+      first = "rgb_565";
+      break;
+   case OSMESA_ARGB:
+      first = "argb";
+      break;
+   }
+
+   switch (p[1]) {
+   case GL_UNSIGNED_SHORT:
+      second = "unsigned_short";
+      break;
+   case GL_UNSIGNED_BYTE:
+      second = "unsigned_byte";
+      break;
+   case GL_FLOAT:
+      second = "float";
+      break;
+   case GL_UNSIGNED_SHORT_5_6_5:
+      second = "unisgned_short_565";
+      break;
+   }
+
+   return first + "_" + second;
+};
+
+TEST_P(OSMesaRenderTestFixture, Render)
+{
+   auto params = GetParam();
+   uint32_t pixel = 0;
+   uint32_t expected;  // This should be green for the given color model
+   int w = 1, h = 1;
+
+   std::unique_ptr<osmesa_context, decltype(&OSMesaDestroyContext)> ctx{
+      OSMesaCreateContext(params[0], NULL), &OSMesaDestroyContext};
+   ASSERT_TRUE(ctx);
+
+   auto ret = OSMesaMakeCurrent(ctx.get(), &pixel, params[1], w, h);
+   ASSERT_EQ(ret, GL_TRUE);
+
+   switch (params[0]) {
+   case OSMESA_RGBA:
+   case OSMESA_BGRA:
+   case OSMESA_RGB:
+      expected = 0xff << 8;
+      glClearColor(0, 1, 0, 0);
+      break;
+   case OSMESA_RGB_565:
+      expected = 0x3f << 5;
+      glClearColor(0, 1, 0, 0);
+      break;
+   case OSMESA_ARGB:
+      expected = 0xff << 24;
+      glClearColor(0, 0, 1, 0);
+      break;
+   }
+   glClear(GL_COLOR_BUFFER_BIT);
+   glFinish();
+
+   ASSERT_EQ(expected, pixel);
+}
+
+INSTANTIATE_TEST_CASE_P(
+   OSMesaRenderTest,
+   OSMesaRenderTestFixture,
+   testing::Values(
+      Params{ OSMESA_RGBA, GL_UNSIGNED_BYTE }
+   ),
+   name_params
+);