Skip to content

Commit a649ff1

Browse files
authored
Fix private input test example (#336)
1 parent c55d2ac commit a649ff1

File tree

1 file changed

+70
-19
lines changed
  • tests/testing-framework/src

1 file changed

+70
-19
lines changed

tests/testing-framework/src/lib.rs

Lines changed: 70 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,14 @@ mod test {
133133

134134
/// Helper function to run emulator and check that the inputs and outputs are correct.
135135
fn emulate<
136-
T: Serialize,
137-
U: Serialize + DeserializeOwned + std::fmt::Debug + PartialEq + Clone,
136+
T: Serialize + std::fmt::Debug,
137+
U: Serialize + std::fmt::Debug,
138+
V: Serialize + DeserializeOwned + std::fmt::Debug + PartialEq + Clone,
138139
>(
139140
elfs: Vec<ElfFile>,
140141
input: Option<T>,
141-
expected_output: Option<U>,
142+
private_input: Option<U>,
143+
expected_output: Option<V>,
142144
expected_result: &Result<
143145
(Vec<InstructionResult>, MemoryTranscript),
144146
nexus_vm::error::VMError,
@@ -151,14 +153,20 @@ mod test {
151153
input_bytes = to_allocvec(input).expect("Serialization failed");
152154
}
153155

154-
let mut deserialized_output: Option<U> = None;
156+
let mut private_input_bytes = Vec::<u8>::new();
157+
if let Some(private_input) = &private_input {
158+
private_input_bytes = to_allocvec(private_input).expect("Serialization failed");
159+
}
160+
161+
let mut deserialized_output: Option<V> = None;
155162
let ad = vec![0u8; 0xbeef as usize]; // placeholder ad until we have use for it
156163

157164
for elf in elfs {
158165
match emulator_type {
159166
EmulatorType::Harvard | EmulatorType::TwoPass => {
160167
// Use elf file to build the harvard emulator.
161-
let mut emulator = HarvardEmulator::from_elf(elf.clone(), &input_bytes, &[]);
168+
let mut emulator =
169+
HarvardEmulator::from_elf(elf.clone(), &input_bytes, &private_input_bytes);
162170

163171
// Check that the program exits correctly.
164172
assert_eq!(&emulator.execute(), expected_result);
@@ -177,7 +185,8 @@ mod test {
177185

178186
// Use the data obtained from the harvard emulator to construct the linear emulator.
179187
let mut linear_emulator =
180-
LinearEmulator::from_harvard(emulator, elf, &ad, &[]).unwrap();
188+
LinearEmulator::from_harvard(emulator, elf, &ad, &private_input_bytes)
189+
.unwrap();
181190

182191
// Check that the program exits correctly.
183192
assert_eq!(&linear_emulator.execute(), expected_result);
@@ -210,8 +219,13 @@ mod test {
210219
.expect("Invalid memory layout");
211220

212221
// Construct the linear emulator.
213-
let mut emulator =
214-
LinearEmulator::from_elf(memory_layout, &ad, elf, &input_bytes, &[]);
222+
let mut emulator = LinearEmulator::from_elf(
223+
memory_layout,
224+
&ad,
225+
elf,
226+
&input_bytes,
227+
&private_input_bytes,
228+
);
215229

216230
// Check that the program exits correctly.
217231
assert_eq!(&emulator.execute(), expected_result);
@@ -232,14 +246,16 @@ mod test {
232246

233247
/// Helper function to run test accross multiple emulators, multiple opt levels, and multiple inputs.
234248
fn test_example_multi<
235-
T: Serialize + Clone,
236-
U: Serialize + DeserializeOwned + std::fmt::Debug + PartialEq + Clone,
249+
T: Serialize + Clone + std::fmt::Debug,
250+
U: Serialize + Clone + std::fmt::Debug,
251+
V: Serialize + DeserializeOwned + std::fmt::Debug + PartialEq + Clone,
237252
>(
238253
emulators: Vec<EmulatorType>,
239254
compile_flags: Vec<&str>,
240255
name: &str,
241256
inputs: Vec<T>,
242-
outputs: Vec<U>,
257+
private_inputs: Vec<U>,
258+
outputs: Vec<V>,
243259
expected_result: Result<
244260
(Vec<InstructionResult>, MemoryTranscript),
245261
nexus_vm::error::VMError,
@@ -248,10 +264,13 @@ mod test {
248264
let elfs = compile_multi(name, &compile_flags);
249265

250266
for emulator in &emulators {
251-
for (input, output) in inputs.iter().zip(outputs.iter()) {
252-
emulate::<T, U>(
267+
for ((input, private_input), output) in
268+
inputs.iter().zip(private_inputs.iter()).zip(outputs.iter())
269+
{
270+
emulate::<T, U, V>(
253271
elfs.clone(),
254272
Some(input.clone()),
273+
Some(private_input.clone()),
255274
Some(output.clone()),
256275
&expected_result,
257276
emulator.clone(),
@@ -273,6 +292,7 @@ mod test {
273292
"../../examples/src/fact",
274293
vec![()],
275294
vec![()],
295+
vec![()],
276296
Err(nexus_vm::error::VMError::VMExited(0)),
277297
);
278298
}
@@ -290,6 +310,7 @@ mod test {
290310
"../../examples/src/fib",
291311
vec![()],
292312
vec![()],
313+
vec![()],
293314
Err(nexus_vm::error::VMError::VMExited(0)),
294315
);
295316
}
@@ -307,6 +328,7 @@ mod test {
307328
"../../examples/src/fib1000",
308329
vec![()],
309330
vec![()],
331+
vec![()],
310332
Err(nexus_vm::error::VMError::VMExited(0)),
311333
);
312334
}
@@ -324,6 +346,7 @@ mod test {
324346
"../../examples/src/main",
325347
vec![()],
326348
vec![()],
349+
vec![()],
327350
Err(nexus_vm::error::VMError::VMExited(0)),
328351
);
329352
}
@@ -341,6 +364,7 @@ mod test {
341364
"../../examples/src/palindromes",
342365
vec![()],
343366
vec![()],
367+
vec![()],
344368
Err(nexus_vm::error::VMError::VMExited(0)),
345369
);
346370
}
@@ -358,6 +382,7 @@ mod test {
358382
"../../examples/src/galeshapley",
359383
vec![()],
360384
vec![()],
385+
vec![()],
361386
Err(nexus_vm::error::VMError::VMExited(0)),
362387
);
363388
}
@@ -375,6 +400,7 @@ mod test {
375400
"../../examples/src/lambda_calculus",
376401
vec![()],
377402
vec![()],
403+
vec![()],
378404
Err(nexus_vm::error::VMError::VMExited(0)),
379405
);
380406
}
@@ -392,10 +418,29 @@ mod test {
392418
"../../examples/src/fail",
393419
vec![()],
394420
vec![()],
421+
vec![()],
395422
Err(nexus_vm::error::VMError::VMExited(1)),
396423
);
397424
}
398425

426+
#[test]
427+
#[serial]
428+
fn test_input_output_example() {
429+
test_example_multi(
430+
vec![
431+
EmulatorType::Harvard,
432+
EmulatorType::default_linear(),
433+
EmulatorType::TwoPass,
434+
],
435+
vec!["-C opt-level=3"],
436+
"../../examples/src/input_output",
437+
vec![(3u32), (4u32), (5u32), (1_048_576u32)],
438+
vec![(4u32), (0u32), (6u32), (4u32)],
439+
vec![(12u32), (0u32), (30u32), (4_194_304u32)],
440+
Err(nexus_vm::error::VMError::VMExited(0)),
441+
);
442+
}
443+
399444
#[test]
400445
#[ignore]
401446
fn test_examples_all_opt_levels() {
@@ -426,9 +471,10 @@ mod test {
426471
let elfs = compile_multi(&example_path, &compile_flags);
427472

428473
for emulator in &emulators {
429-
emulate::<(), ()>(
474+
emulate::<(), (), ()>(
430475
elfs.clone(),
431476
None,
477+
None,
432478
Some(()),
433479
&Err(nexus_vm::error::VMError::VMExited(0)),
434480
emulator.clone(),
@@ -441,9 +487,10 @@ mod test {
441487
let fail_elfs = compile_multi(fail_path, &compile_flags);
442488

443489
for emulator in &emulators {
444-
emulate::<(), ()>(
490+
emulate::<(), (), ()>(
445491
fail_elfs.clone(),
446492
None,
493+
None,
447494
Some(()),
448495
&Err(nexus_vm::error::VMError::VMExited(1)),
449496
emulator.clone(),
@@ -465,23 +512,26 @@ mod test {
465512
let io_u128_elfs = compile_multi("io_u128", &compile_flags);
466513

467514
for emulator in emulators {
468-
emulate::<u32, u32>(
515+
emulate::<u32, (), u32>(
469516
io_u32_elfs.clone(),
470517
Some(123u32),
518+
None,
471519
Some(123u32),
472520
&Err(nexus_vm::error::VMError::VMExited(0)),
473521
emulator.clone(),
474522
);
475-
emulate::<u64, u64>(
523+
emulate::<u64, (), u64>(
476524
io_u64_elfs.clone(),
477525
Some(1u64 << 32),
526+
None,
478527
Some(1u64 << 32),
479528
&Err(nexus_vm::error::VMError::VMExited(0)),
480529
emulator.clone(),
481530
);
482-
emulate::<u128, u128>(
531+
emulate::<u128, (), u128>(
483532
io_u128_elfs.clone(),
484533
Some(332306998946228968225970211937533483u128),
534+
None,
485535
Some(332306998946228968225970211937533483u128),
486536
&Err(nexus_vm::error::VMError::VMExited(0)),
487537
emulator,
@@ -503,9 +553,10 @@ mod test {
503553

504554
for (input, output) in inputs.iter().zip(outputs.iter()) {
505555
for emulator in emulators.clone() {
506-
emulate::<u32, u32>(
556+
emulate::<u32, (), u32>(
507557
elfs.clone(),
508558
Some(input.clone()),
559+
None,
509560
Some(output.clone()),
510561
&Err(nexus_vm::error::VMError::VMExited(0)),
511562
emulator.clone(),

0 commit comments

Comments
 (0)