asahi: Add LOD clamp packing unit tests
authorAlyssa Rosenzweig <alyssa@rosenzweig.io>
Sun, 6 Feb 2022 20:03:25 +0000 (15:03 -0500)
committerMarge Bot <emma+marge@anholt.net>
Fri, 18 Feb 2022 23:48:32 +0000 (23:48 +0000)
With GTest.

Signed-off-by: Alyssa Rosenzweig <alyssa@rosenzweig.io>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/14903>

src/asahi/lib/meson.build
src/asahi/lib/tests/test-lod-clamps.cpp [new file with mode: 0644]

index 5860526..e921db1 100644 (file)
@@ -64,3 +64,22 @@ libasahi_decode = static_library(
   gnu_symbol_visibility : 'hidden',
   build_by_default : false,
 )
+
+if with_tests
+  test(
+    'libasahi_tests',
+    executable(
+      'libasahi_tests',
+      files(
+        'tests/test-lod-clamps.cpp',
+      ),
+      c_args : [c_msvc_compat_args, no_override_init_args],
+      gnu_symbol_visibility : 'hidden',
+      include_directories : [inc_include, inc_src, inc_mesa],
+      dependencies: [idep_gtest, idep_agx_pack],
+      link_with : [],
+    ),
+    suite : ['asahi'],
+    protocol : gtest_test_protocol,
+  )
+endif
diff --git a/src/asahi/lib/tests/test-lod-clamps.cpp b/src/asahi/lib/tests/test-lod-clamps.cpp
new file mode 100644 (file)
index 0000000..3bee31f
--- /dev/null
@@ -0,0 +1,68 @@
+/*
+ * Copyright (C) 2022 Alyssa Rosenzweig
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "agx_pack.h"
+
+#include <gtest/gtest.h>
+
+const struct {
+   float f;
+   uint16_t encoded;
+   bool inexact;
+} lod_cases[] = {
+   /* Lower bound clamp */
+   { -INFINITY, 0x00, true },
+   { -0.1, 0x00, true },
+   { -0.0, 0x00, true },
+
+   /* Exact bounds */
+   { 0.0, 0x00 },
+   { 14.0, 0x380 },
+
+   /* Upper bound clamp */
+   { 14.1, 0x380, true },
+   { 18.1, 0x380, true },
+   { INFINITY, 0x380, true },
+};
+
+class LODClamp : public testing::Test {
+};
+
+TEST_F(LODClamp, Encode)
+{
+   for (unsigned i = 0; i < ARRAY_SIZE(lod_cases); ++i)
+      ASSERT_EQ(__float_to_lod(lod_cases[i].f), lod_cases[i].encoded);
+}
+
+TEST_F(LODClamp, Decode)
+{
+   for (unsigned i = 0; i < ARRAY_SIZE(lod_cases); ++i) {
+      if (lod_cases[i].inexact)
+         continue;
+
+      uint8_t cl[4] = { 0 };
+      memcpy(cl, &lod_cases[i].encoded, sizeof(lod_cases[i].encoded));
+
+      ASSERT_EQ(__gen_unpack_lod(cl, 0, 10), lod_cases[i].f);
+   }
+}