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 02b0488
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
61 changes: 61 additions & 0 deletions wgpu/tests/resource_error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use crate::common::{initialize_test, TestParameters};

#[cfg(test)]
// Run some code in an error scope and assert that validation failed.s
fn fail(device: &wgpu::Device, callback: &mut dyn FnMut()) {
device.push_error_scope(wgpu::ErrorFilter::Validation);
callback();
assert!(pollster::block_on(device.pop_error_scope()).is_some());
}

#[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| {
ctx.device.push_error_scope(wgpu::ErrorFilter::Validation);
let buffer = ctx.device.create_buffer(&wgpu::BufferDescriptor {
label: None,
size: 99999999,
usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::STORAGE,
mapped_at_creation: false,
});
assert!(pollster::block_on(ctx.device.pop_error_scope()).is_some());

fail(&ctx.device, &mut || {
buffer.slice(..).map_async(wgpu::MapMode::Write, |_| {})
});
fail(&ctx.device, &mut || buffer.unmap());
fail(&ctx.device, &mut || buffer.destroy());
fail(&ctx.device, &mut || 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| {
ctx.device.push_error_scope(wgpu::ErrorFilter::Validation);
let texture = 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(),
});
assert!(pollster::block_on(ctx.device.pop_error_scope()).is_some());

fail(&ctx.device, &mut || {
let _ = texture.create_view(&wgpu::TextureViewDescriptor::default());
});
fail(&ctx.device, &mut || texture.destroy());
fail(&ctx.device, &mut || 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 02b0488

Please sign in to comment.