Repository: gfx-rs/wgpu
Pull request #5911 (2024-07-04 22:29:05)

refactor: `vertex_index_common`: remove footguns in `tests` matrix init. (again)
Re-file of <https://github.com/gfx-rs/wgpu/pull/5840>, but actually targeting `trunk` this time. 😅


Patch:
diff --git a/Cargo.lock b/Cargo.lock
index f491e23fe..1fd477852 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -4384,6 +4384,7 @@ dependencies = [
  "env_logger",
  "futures-lite",
  "image",
+ "itertools",
  "js-sys",
  "libtest-mimic",
  "log",
@@ -4394,6 +4395,7 @@ dependencies = [
  "profiling",
  "serde",
  "serde_json",
+ "strum",
  "wasm-bindgen",
  "wasm-bindgen-futures",
  "wasm-bindgen-test",
diff --git a/Cargo.toml b/Cargo.toml
index 988b92f7f..ce5ebcce1 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -91,6 +91,7 @@ getrandom = "0.2"
 glam = "0.27"
 heck = "0.5.0"
 image = { version = "0.24", default-features = false, features = ["png"] }
+itertools = { version = "0.10.5" }
 ktx2 = "0.3"
 libc = "0.2"
 # libloading 0.8 switches from `winapi` to `windows-sys`; permit either
@@ -121,6 +122,7 @@ serde = "1"
 serde_json = "1.0.119"
 smallvec = "1"
 static_assertions = "1.1.0"
+strum = { version = "0.25.0", features = ["derive"] }
 tracy-client = "0.17"
 thiserror = "1"
 wgpu = { version = "0.20.0", path = "./wgpu", default-features = false }


Test patch:
diff --git a/tests/Cargo.toml b/tests/Cargo.toml
index 0e509c712..d6b5ae672 100644
--- a/tests/Cargo.toml
+++ b/tests/Cargo.toml
@@ -27,6 +27,7 @@ bytemuck.workspace = true
 cfg-if.workspace = true
 ctor.workspace = true
 futures-lite.workspace = true
+itertools.workspace = true
 libtest-mimic.workspace = true
 log.workspace = true
 parking_lot.workspace = true
@@ -35,6 +36,7 @@ pollster.workspace = true
 profiling.workspace = true
 serde_json.workspace = true
 serde.workspace = true
+strum = { workspace = true, features = ["derive"] }
 wgpu-macros.workspace = true
 wgpu.workspace = true
 wgt = { workspace = true, features = ["serde"] }
diff --git a/tests/tests/vertex_indices/mod.rs b/tests/tests/vertex_indices/mod.rs
index b85f3274e..59048ef31 100644
--- a/tests/tests/vertex_indices/mod.rs
+++ b/tests/tests/vertex_indices/mod.rs
@@ -5,8 +5,9 @@
 
 use std::{num::NonZeroU64, ops::Range};
 
+use itertools::Itertools;
+use strum::IntoEnumIterator;
 use wgpu::util::{BufferInitDescriptor, DeviceExt, RenderEncoder};
-
 use wgpu_test::{gpu_test, GpuTestConfiguration, TestParameters, TestingContext};
 use wgt::RenderBundleDescriptor;
 
@@ -79,7 +80,7 @@ impl Draw {
     }
 }
 
-#[derive(Debug, Copy, Clone)]
+#[derive(Debug, Copy, Clone, strum::EnumIter)]
 enum TestCase {
     /// A single draw call with 6 vertices
     Draw,
@@ -94,14 +95,6 @@ enum TestCase {
 }
 
 impl TestCase {
-    const ARRAY: [Self; 5] = [
-        Self::Draw,
-        Self::DrawNonZeroFirstVertex,
-        Self::DrawBaseVertex,
-        Self::DrawInstanced,
-        Self::DrawNonZeroFirstInstance,
-    ];
-
     // Get the draw calls for this test case
     fn draws(&self) -> &'static [Draw] {
         match self {
@@ -148,7 +141,7 @@ impl TestCase {
     }
 }
 
-#[derive(Debug, Copy, Clone)]
+#[derive(Debug, Copy, Clone, strum::EnumIter)]
 enum IdSource {
     /// Use buffers to load the vertex and instance index
     Buffers,
@@ -156,30 +149,18 @@ enum IdSource {
     Builtins,
 }
 
-impl IdSource {
-    const ARRAY: [Self; 2] = [Self::Buffers, Self::Builtins];
-}
-
-#[derive(Debug, Copy, Clone)]
+#[derive(Debug, Copy, Clone, strum::EnumIter)]
 enum DrawCallKind {
     Direct,
     Indirect,
 }
 
-impl DrawCallKind {
-    const ARRAY: [Self; 2] = [Self::Direct, Self::Indirect];
-}
-
-#[derive(Debug, Copy, Clone)]
+#[derive(Debug, Copy, Clone, strum::EnumIter)]
 enum EncoderKind {
     RenderPass,
     RenderBundle,
 }
 
-impl EncoderKind {
-    const ARRAY: [Self; 2] = [Self::RenderPass, Self::RenderBundle];
-}
-
 struct Test {
     case: TestCase,
     id_source: IdSource,
@@ -356,24 +337,23 @@ async fn vertex_index_common(ctx: TestingContext) {
         )
         .create_view(&wgpu::TextureViewDescriptor::default());
 
-    let mut tests = Vec::with_capacity(5 * 2 * 2 * 2);
-    for case in TestCase::ARRAY {
-        for id_source in IdSource::ARRAY {
-            for draw_call_kind in DrawCallKind::ARRAY {
-                for encoder_kind in EncoderKind::ARRAY {
-                    for vertex_pulling_transform in [false, true] {
-                        tests.push(Test {
-                            case,
-                            id_source,
-                            draw_call_kind,
-                            encoder_kind,
-                            vertex_pulling_transform,
-                        })
-                    }
+    let tests = TestCase::iter()
+        .cartesian_product(IdSource::iter())
+        .cartesian_product(DrawCallKind::iter())
+        .cartesian_product(EncoderKind::iter())
+        .cartesian_product([false, true])
+        .map(
+            |((((case, id_source), draw_call_kind), encoder_kind), vertex_pulling_transform)| {
+                Test {
+                    case,
+                    id_source,
+                    draw_call_kind,
+                    encoder_kind,
+                    vertex_pulling_transform,
                 }
-            }
-        }
-    }
+            },
+        )
+        .collect::<Vec<_>>();
 
     let features = ctx.adapter.features();
 
@@ -398,7 +378,7 @@ async fn vertex_index_common(ctx: TestingContext) {
 
         let expected = test.expectation(&ctx);
 
-        let buffer_size = 4 * expected.len() as u64;
+        let buffer_size = (std::mem::size_of_val(&expected[0]) * expected.len()) as u64;
         let cpu_buffer = ctx.device.create_buffer(&wgpu::BufferDescriptor {
             label: None,
             size: buffer_size,