ail: Restructure generated tests
authorAlyssa Rosenzweig <alyssa@rosenzweig.io>
Fri, 10 Mar 2023 02:01:02 +0000 (21:01 -0500)
committerMarge Bot <emma+marge@anholt.net>
Sat, 11 Mar 2023 20:45:42 +0000 (20:45 +0000)
Currently, the generated tests consist of some boilerplate, generated
test cases, and at the very end the actual test. This is bad for readability,
because the actual code is all the way at the bottom. It's also bad for
clang-format linting: even though the test cases are /* clang-format off */,
they still take an exceptionally long time to parse when linting. I suspect this
is a clang-format bug, but it's easy enough to workaround.

To solve these issues, restructure so that the test cases are in separate files
(containing the actual data), but the manually written test functions are
consolidated into a new family of generated layout tests. This is probably
cleaner.

Parallel clang-format linting is now 10x faster on the M1, which means it's
now practical to lint in my "publish branch" hook.

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

src/asahi/layout/meson.build
src/asahi/layout/tests/comp-twiddled.txt [moved from src/asahi/layout/tests/test-comp-twiddled.cpp with 99% similarity]
src/asahi/layout/tests/miptree.txt [moved from src/asahi/layout/tests/test-miptree.cpp with 88% similarity]
src/asahi/layout/tests/test-generated.cpp [new file with mode: 0644]
src/asahi/layout/tests/uncomp-twiddled.txt [moved from src/asahi/layout/tests/test-uncomp-twiddled.cpp with 99% similarity]

index b6a8c00..484a32d 100644 (file)
@@ -39,9 +39,7 @@ if with_tests
       'asahi_layout_tests',
       files(
         'tests/test-layout.cpp',
-        'tests/test-miptree.cpp',
-        'tests/test-comp-twiddled.cpp',
-        'tests/test-uncomp-twiddled.cpp',
+        'tests/test-generated.cpp',
       ),
       c_args : [c_msvc_compat_args, no_override_init_args],
       gnu_symbol_visibility : 'hidden',
similarity index 99%
rename from src/asahi/layout/tests/test-comp-twiddled.cpp
rename to src/asahi/layout/tests/comp-twiddled.txt
index 22ae0d5..64ea353 100644 (file)
@@ -1,25 +1,3 @@
-/*
- * Copyright 2022 Asahi Lina
- * SPDX-License-Identifier: MIT
- */
-
-#include <gtest/gtest.h>
-#include "layout.h"
-
-/*
- * Test compressed texture sizes. All test cases in this file are extracted from
- * texture structure dumps in Metal.
- */
-struct sizetest {
-   enum pipe_format format;
-   uint32_t width, height, depth;
-   uint8_t levels;
-   uint32_t size;
-};
-
-/* clang-format off */
-/* Sort: `sort -t"," -k1,1 -k2,5n | uniq` */
-static struct sizetest comptests[] = {
    { PIPE_FORMAT_R16G16B16A16_UNORM, 16, 16, 1, 1, 0x880 },
    { PIPE_FORMAT_R16G16B16A16_UNORM, 16, 24, 1, 1, 0x1080 },
    { PIPE_FORMAT_R16G16B16A16_UNORM, 16, 32, 1, 1, 0x1080 },
@@ -14309,30 +14287,3 @@ static struct sizetest comptests[] = {
    { PIPE_FORMAT_Z32_FLOAT, 4097, 4097, 1, 13, 0x5cbeb00 },
    { PIPE_FORMAT_Z32_FLOAT, 4097, 4097, 2, 1, 0x8808000 },
    { PIPE_FORMAT_Z32_FLOAT, 4097, 4097, 2, 13, 0xb97d600 },
-};
-/* clang-format on */
-
-TEST(CompTwiddled, SizeTests)
-{
-   for (unsigned i = 0; i < ARRAY_SIZE(comptests); ++i) {
-      struct sizetest test = comptests[i];
-
-      struct ail_layout layout = {
-         .width_px = test.width,
-         .height_px = test.height,
-         .depth_px = test.depth,
-         .sample_count_sa = 1,
-         .levels = test.levels,
-         .tiling = AIL_TILING_TWIDDLED_COMPRESSED,
-         .format = test.format,
-      };
-
-      ail_make_miptree(&layout);
-
-      EXPECT_EQ(layout.size_B, test.size)
-         << test.width << "x" << test.height << "x" << test.depth << " "
-         << (int)test.levels << "L " << util_format_short_name(test.format)
-         << " compressed texture has wrong allocation size, off by "
-         << ((int)layout.size_B - (int)test.size);
-   }
-}
similarity index 88%
rename from src/asahi/layout/tests/test-miptree.cpp
rename to src/asahi/layout/tests/miptree.txt
index 380f643..6f6e55d 100644 (file)
@@ -1,42 +1,3 @@
-/*
- * 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 <gtest/gtest.h>
-#include "layout.h"
-
-/*
- * Test miptree layouts. All test cases in this file are extracted from memory
- * dumps of a test pattern ran through Metal.
- */
-struct miptest {
-   enum pipe_format format;
-   uint32_t width, height;
-   uint8_t levels;
-   uint32_t offsets[16];
-};
-
-/* clang-format off */
-static struct miptest miptests[] = {
    {
       PIPE_FORMAT_R8G8B8A8_UNORM,
       1024,
@@ -1256,32 +1217,3 @@ static struct miptest miptests[] = {
          0x32D700,
       }
    },
-};
-/* clang-format on */
-
-TEST(Miptree, Tests2D)
-{
-   for (unsigned i = 0; i < ARRAY_SIZE(miptests); ++i) {
-      struct miptest test = miptests[i];
-
-      struct ail_layout layout = {
-         .width_px = test.width,
-         .height_px = test.height,
-         .depth_px = 1,
-         .sample_count_sa = 1,
-         .levels = test.levels,
-         .tiling = AIL_TILING_TWIDDLED,
-         .format = test.format,
-      };
-
-      ail_make_miptree(&layout);
-
-      for (unsigned l = 0; l < test.levels; ++l) {
-         EXPECT_EQ(ail_get_level_offset_B(&layout, l), test.offsets[l])
-            << test.width << "x" << test.height << " "
-            << util_format_short_name(test.format)
-            << " texture has wrong offset at level " << l << ", off by "
-            << test.offsets[l] - ail_get_level_offset_B(&layout, l);
-      }
-   }
-}
diff --git a/src/asahi/layout/tests/test-generated.cpp b/src/asahi/layout/tests/test-generated.cpp
new file mode 100644 (file)
index 0000000..597e16f
--- /dev/null
@@ -0,0 +1,119 @@
+/*
+ * Copyright 2022 Alyssa Rosenzweig
+ * Copyright 2022 Asahi Lina
+ * SPDX-License-Identifier: MIT
+ */
+
+#include <gtest/gtest.h>
+#include "layout.h"
+
+/*
+ * Test texture layouts with test cases extracted from texture structure dumps
+ * produced by Metal.
+ *
+ * The extracted test cases are stored in separate files which do not get
+ * clang-formatted. They do still get parsed as C code, though. They may be
+ * sorted with `sort -t"," -k1,1 -k2,5n | uniq`.
+ */
+struct sizetest {
+   enum pipe_format format;
+   uint32_t width, height, depth;
+   uint8_t levels;
+   uint32_t size;
+};
+
+struct miptest {
+   enum pipe_format format;
+   uint32_t width, height;
+   uint8_t levels;
+   uint32_t offsets[16];
+};
+
+static struct sizetest comptests[] = {
+#include "comp-twiddled.txt"
+};
+
+static struct sizetest sizetests[] = {
+#include "uncomp-twiddled.txt"
+};
+
+static struct miptest miptests[] = {
+#include "miptree.txt"
+};
+
+TEST(Generated, CompTwiddled)
+{
+   for (unsigned i = 0; i < ARRAY_SIZE(comptests); ++i) {
+      struct sizetest test = comptests[i];
+
+      struct ail_layout layout = {
+         .width_px = test.width,
+         .height_px = test.height,
+         .depth_px = test.depth,
+         .sample_count_sa = 1,
+         .levels = test.levels,
+         .tiling = AIL_TILING_TWIDDLED_COMPRESSED,
+         .format = test.format,
+      };
+
+      ail_make_miptree(&layout);
+
+      EXPECT_EQ(layout.size_B, test.size)
+         << test.width << "x" << test.height << "x" << test.depth << " "
+         << (int)test.levels << "L " << util_format_short_name(test.format)
+         << " compressed texture has wrong allocation size, off by "
+         << ((int)layout.size_B - (int)test.size);
+   }
+}
+
+TEST(Generated, UncompTwiddled)
+{
+   for (unsigned i = 0; i < ARRAY_SIZE(sizetests); ++i) {
+      struct sizetest test = sizetests[i];
+
+      struct ail_layout layout = {
+         .width_px = test.width,
+         .height_px = test.height,
+         .depth_px = test.depth,
+         .sample_count_sa = 1,
+         .levels = test.levels,
+         .tiling = AIL_TILING_TWIDDLED,
+         .format = test.format,
+      };
+
+      ail_make_miptree(&layout);
+
+      EXPECT_EQ(layout.size_B, test.size)
+         << test.width << "x" << test.height << "x" << test.depth << " "
+         << (int)test.levels << "L " << util_format_short_name(test.format)
+         << " uncompressed texture has wrong allocation size, off by "
+         << ((int)layout.size_B - (int)test.size);
+   }
+}
+
+TEST(Generated, Miptree2D)
+{
+   for (unsigned i = 0; i < ARRAY_SIZE(miptests); ++i) {
+      struct miptest test = miptests[i];
+
+      struct ail_layout layout = {
+         .width_px = test.width,
+         .height_px = test.height,
+         .depth_px = 1,
+         .sample_count_sa = 1,
+         .levels = test.levels,
+         .tiling = AIL_TILING_TWIDDLED,
+         .format = test.format,
+      };
+
+      ail_make_miptree(&layout);
+
+      for (unsigned l = 0; l < test.levels; ++l) {
+         EXPECT_EQ(ail_get_level_offset_B(&layout, l), test.offsets[l])
+            << test.width << "x" << test.height << " "
+            << util_format_short_name(test.format)
+            << " texture has wrong offset at level " << l << ", off by "
+            << test.offsets[l] - ail_get_level_offset_B(&layout, l);
+      }
+   }
+}
similarity index 99%
rename from src/asahi/layout/tests/test-uncomp-twiddled.cpp
rename to src/asahi/layout/tests/uncomp-twiddled.txt
index 60d5a21..7541934 100644 (file)
@@ -1,25 +1,3 @@
-/*
- * Copyright 2022 Asahi Lina
- * SPDX-License-Identifier: MIT
- */
-
-#include <gtest/gtest.h>
-#include "layout.h"
-
-/*
- * Test uncompressed texture sizes. All test cases in this file are extracted
- * from texture structure dumps in Metal.
- */
-struct sizetest {
-   enum pipe_format format;
-   uint32_t width, height, depth;
-   uint8_t levels;
-   uint32_t size;
-};
-
-/* clang-format off */
-/* Sort: `sort -t"," -k1,1 -k2,5n | uniq` */
-static struct sizetest sizetests[] = {
    { PIPE_FORMAT_R8_UNORM, 1, 1, 1, 1, 0x80 },
    { PIPE_FORMAT_R8_UNORM, 1, 1, 2, 1, 0x100 },
    { PIPE_FORMAT_R8_UNORM, 1, 2, 1, 2, 0x100 },
@@ -4746,30 +4724,3 @@ static struct sizetest sizetests[] = {
    { PIPE_FORMAT_Z32_FLOAT, 4097, 15, 1, 13, 0xac000 },
    { PIPE_FORMAT_Z32_FLOAT, 4097, 15, 2, 1, 0x100000 },
    { PIPE_FORMAT_Z32_FLOAT, 4097, 15, 2, 13, 0x158000 },
-};
-/* clang-format on */
-
-TEST(UncompTwiddled, SizeTests)
-{
-   for (unsigned i = 0; i < ARRAY_SIZE(sizetests); ++i) {
-      struct sizetest test = sizetests[i];
-
-      struct ail_layout layout = {
-         .width_px = test.width,
-         .height_px = test.height,
-         .depth_px = test.depth,
-         .sample_count_sa = 1,
-         .levels = test.levels,
-         .tiling = AIL_TILING_TWIDDLED,
-         .format = test.format,
-      };
-
-      ail_make_miptree(&layout);
-
-      EXPECT_EQ(layout.size_B, test.size)
-         << test.width << "x" << test.height << "x" << test.depth << " "
-         << (int)test.levels << "L " << util_format_short_name(test.format)
-         << " uncompressed texture has wrong allocation size, off by "
-         << ((int)layout.size_B - (int)test.size);
-   }
-}