From 1738000d407ebba32f4e0d4e9f959855194a7f0e Mon Sep 17 00:00:00 2001 From: Nicolas Silva Date: Tue, 11 Oct 2022 16:04:43 +0200 Subject: [PATCH] Add a test. --- wgpu/tests/common/mod.rs | 18 ++++++++++++++ wgpu/tests/resource_error.rs | 47 ++++++++++++++++++++++++++++++++++++ wgpu/tests/root.rs | 1 + 3 files changed, 66 insertions(+) create mode 100644 wgpu/tests/resource_error.rs diff --git a/wgpu/tests/common/mod.rs b/wgpu/tests/common/mod.rs index b0df54d2f19..9b12f46726c 100644 --- a/wgpu/tests/common/mod.rs +++ b/wgpu/tests/common/mod.rs @@ -307,3 +307,21 @@ pub fn initialize_test(parameters: TestParameters, test_function: impl FnOnce(Te panic!("UNEXPECTED TEST FAILURE DUE TO {}", failure_cause) } } + +// Run some code in an error scope and assert that validation fails. +pub fn fail(device: &wgpu::Device, callback: impl FnOnce() -> T) -> T { + device.push_error_scope(wgpu::ErrorFilter::Validation); + let result = callback(); + assert!(pollster::block_on(device.pop_error_scope()).is_some()); + + result +} + +// Run some code in an error scope and assert that validation succeeds. +pub fn valid(device: &wgpu::Device, callback: impl FnOnce() -> T) -> T { + device.push_error_scope(wgpu::ErrorFilter::Validation); + let result = callback(); + assert!(pollster::block_on(device.pop_error_scope()).is_none()); + + result +} diff --git a/wgpu/tests/resource_error.rs b/wgpu/tests/resource_error.rs new file mode 100644 index 00000000000..cc2d6c3b1d9 --- /dev/null +++ b/wgpu/tests/resource_error.rs @@ -0,0 +1,47 @@ +use crate::common::{initialize_test, TestParameters, fail, valid}; + +#[test] +fn bad_buffer() { + // Create a buffer with bad parameters and call a few methods. + // Validation should fail but there should be not panic. + initialize_test(TestParameters::default(), |ctx| { + let buffer = fail(&ctx.device, || ctx.device.create_buffer(&wgpu::BufferDescriptor { + label: None, + size: 99999999, + usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::STORAGE, + mapped_at_creation: false, + })); + + fail(&ctx.device, || buffer.slice(..).map_async(wgpu::MapMode::Write, |_| {})); + fail(&ctx.device, || buffer.unmap()); + valid(&ctx.device, || buffer.destroy()); + valid(&ctx.device, || buffer.destroy()); + }); +} + +#[test] +fn bad_texture() { + // Create a texture with bad parameters and call a few methods. + // Validation should fail but there should be not panic. + initialize_test(TestParameters::default(), |ctx| { + let texture = fail(&ctx.device, || ctx.device.create_texture(&wgpu::TextureDescriptor { + label: None, + size: wgpu::Extent3d { + width: 0, + height: 12345678, + depth_or_array_layers: 9001, + }, + mip_level_count: 2000, + sample_count: 27, + dimension: wgpu::TextureDimension::D2, + format: wgpu::TextureFormat::Rgba8UnormSrgb, + usage: wgpu::TextureUsages::all(), + })); + + fail(&ctx.device, || { + let _ = texture.create_view(&wgpu::TextureViewDescriptor::default()); + }); + valid(&ctx.device, || texture.destroy()); + valid(&ctx.device, || texture.destroy()); + }); +} diff --git a/wgpu/tests/root.rs b/wgpu/tests/root.rs index 9151d18e9ee..9f5068b723c 100644 --- a/wgpu/tests/root.rs +++ b/wgpu/tests/root.rs @@ -10,6 +10,7 @@ mod example_wgsl; mod instance; mod poll; mod resource_descriptor_accessor; +mod resource_error; mod shader_primitive_index; mod texture_bounds; mod vertex_indices;