Skip to content

Commit

Permalink
Add a test.
Browse files Browse the repository at this point in the history
  • Loading branch information
nical committed Oct 11, 2022
1 parent 318cb7a commit 1738000
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
18 changes: 18 additions & 0 deletions wgpu/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(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<T>(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
}
47 changes: 47 additions & 0 deletions wgpu/tests/resource_error.rs
Original file line number Diff line number Diff line change
@@ -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());
});
}
1 change: 1 addition & 0 deletions wgpu/tests/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 1738000

Please sign in to comment.