Skip to content

Commit 70afa89

Browse files
committed
unify result writing
1 parent ab34663 commit 70afa89

File tree

3 files changed

+16
-13
lines changed

3 files changed

+16
-13
lines changed

tests/difftests/lib/src/config.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
use serde::{Deserialize, Serialize};
2+
use std::fs::File;
3+
use std::io::Write;
24
use std::{fs, path::Path};
35

46
#[derive(Debug, Deserialize, Serialize)]
@@ -7,6 +9,14 @@ pub struct Config {
79
pub metadata_path: std::path::PathBuf,
810
}
911

12+
impl Config {
13+
pub fn write_result(&self, output: &[u8]) -> anyhow::Result<()> {
14+
let mut f = File::create(&self.output_path)?;
15+
f.write_all(output)?;
16+
Ok(())
17+
}
18+
}
19+
1020
/// Test metadata that controls output comparison behavior
1121
///
1222
/// This metadata is written alongside test output to specify how the test harness

tests/difftests/lib/src/scaffold/compute/backend.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,7 @@ impl<B: ComputeBackend> ComputeTest<B> {
103103
// Write the first storage buffer output to the file
104104
for (output, buffer_config) in outputs.iter().zip(&buffers) {
105105
if matches!(buffer_config.usage, BufferUsage::Storage) && !output.is_empty() {
106-
use std::fs::File;
107-
use std::io::Write;
108-
let mut f = File::create(&config.output_path)?;
109-
f.write_all(output)?;
106+
config.write_result(&output)?;
110107
return Ok(());
111108
}
112109
}

tests/difftests/lib/src/scaffold/compute/wgpu.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::scaffold::shader::WgslComputeShader;
66
use anyhow::Context;
77
use bytemuck::Pod;
88
use futures::executor::block_on;
9-
use std::{borrow::Cow, fs::File, io::Write, sync::Arc};
9+
use std::{borrow::Cow, sync::Arc};
1010
use wgpu::{PipelineCompilationOptions, util::DeviceExt};
1111

1212
pub type BufferConfig = backend::BufferConfig;
@@ -217,8 +217,7 @@ where
217217
/// Runs the compute shader with no input and writes the output to a file.
218218
pub fn run_test(self, config: &Config) -> anyhow::Result<()> {
219219
let output = self.run()?;
220-
let mut f = File::create(&config.output_path)?;
221-
f.write_all(&output)?;
220+
config.write_result(&output)?;
222221
Ok(())
223222
}
224223

@@ -228,8 +227,7 @@ where
228227
I: Sized + Pod,
229228
{
230229
let output = self.run_with_input(input)?;
231-
let mut f = File::create(&config.output_path)?;
232-
f.write_all(&output)?;
230+
config.write_result(&output)?;
233231
Ok(())
234232
}
235233
}
@@ -565,8 +563,7 @@ where
565563
// Write the first storage buffer output to the file.
566564
for (output, buffer_config) in outputs.iter().zip(&buffers) {
567565
if matches!(buffer_config.usage, BufferUsage::Storage) && !output.is_empty() {
568-
let mut f = File::create(&config.output_path)?;
569-
f.write_all(output)?;
566+
config.write_result(output)?;
570567
return Ok(());
571568
}
572569
}
@@ -775,8 +772,7 @@ where
775772
// Write first storage buffer output to file.
776773
for (data, buffer_config) in results.iter().zip(&buffers) {
777774
if buffer_config.usage == BufferUsage::Storage && !data.is_empty() {
778-
let mut f = File::create(&config.output_path)?;
779-
f.write_all(data)?;
775+
config.write_result(&data)?;
780776
return Ok(());
781777
}
782778
}

0 commit comments

Comments
 (0)