Skip to content

Commit 9d71baf

Browse files
committed
Test: Human encoding: Test finalized expressions
1 parent ac74f89 commit 9d71baf

File tree

1 file changed

+69
-9
lines changed

1 file changed

+69
-9
lines changed

src/human_encoding/mod.rs

+69-9
Original file line numberDiff line numberDiff line change
@@ -236,30 +236,43 @@ mod tests {
236236
use std::collections::HashMap;
237237
use std::sync::Arc;
238238

239+
fn get_root(input: &Option<Arc<Value>>) -> &'static str {
240+
if input.is_none() {
241+
"main"
242+
} else {
243+
"expr"
244+
}
245+
}
246+
239247
fn assert_finalize_ok<J: Jet>(
240248
s: &str,
241249
witness: &HashMap<Arc<str>, Arc<Value>>,
242250
env: &J::Environment,
251+
input: Option<Arc<Value>>,
243252
) {
244253
let program = Forest::<J>::parse(s)
245254
.expect("Failed to parse human encoding")
246-
.to_witness_node(witness)
255+
.to_witness_node_expression(witness, get_root(&input))
247256
.expect("Forest is missing expected root")
248257
.finalize()
249258
.expect("Failed to finalize");
250259
let mut mac = BitMachine::for_program(&program);
260+
if let Some(value) = input {
261+
mac.input(value).expect("Failed to provide input");
262+
}
251263
mac.exec(&program, env).expect("Failed to run program");
252264
}
253265

254266
fn assert_finalize_err<J: Jet>(
255267
s: &str,
256268
witness: &HashMap<Arc<str>, Arc<Value>>,
257269
env: &J::Environment,
270+
input: Option<Arc<Value>>,
258271
err_msg: &'static str,
259272
) {
260273
let program = match Forest::<J>::parse(s)
261274
.expect("Failed to parse human encoding")
262-
.to_witness_node(witness)
275+
.to_witness_node_expression(witness, get_root(&input))
263276
.expect("Forest is missing expected root")
264277
.finalize()
265278
{
@@ -270,6 +283,12 @@ mod tests {
270283
}
271284
};
272285
let mut mac = BitMachine::for_program(&program);
286+
if let Some(value) = input {
287+
if let Err(error) = mac.input(value) {
288+
assert_eq!(error.to_string().as_str(), err_msg);
289+
return;
290+
}
291+
}
273292
match mac.exec(&program, env) {
274293
Ok(_) => panic!("Execution is expected to fail"),
275294
Err(error) => assert_eq!(&error.to_string(), err_msg),
@@ -292,13 +311,19 @@ mod tests {
292311
(Arc::from("a"), Value::u8(0x00)),
293312
(Arc::from("b"), Value::u8(0x01)),
294313
]);
295-
assert_finalize_ok::<Core>(s, &a_less_than_b, &());
314+
assert_finalize_ok::<Core>(s, &a_less_than_b, &(), None);
296315

297316
let b_greater_equal_a = HashMap::from([
298317
(Arc::from("a"), Value::u8(0x01)),
299318
(Arc::from("b"), Value::u8(0x01)),
300319
]);
301-
assert_finalize_err::<Core>(s, &b_greater_equal_a, &(), "Jet failed during execution");
320+
assert_finalize_err::<Core>(
321+
s,
322+
&b_greater_equal_a,
323+
&(),
324+
None,
325+
"Jet failed during execution",
326+
);
302327
}
303328

304329
#[test]
@@ -314,6 +339,7 @@ mod tests {
314339
",
315340
&witness,
316341
&(),
342+
None,
317343
"unable to satisfy program",
318344
);
319345
}
@@ -326,7 +352,7 @@ mod tests {
326352
main := comp (pair wit1 unit) case unit wit2
327353
";
328354
let wit2_is_pruned = HashMap::from([(Arc::from("wit1"), Value::u1(0))]);
329-
assert_finalize_ok::<Core>(s, &wit2_is_pruned, &());
355+
assert_finalize_ok::<Core>(s, &wit2_is_pruned, &(), None);
330356

331357
let wit2_is_missing = HashMap::from([(Arc::from("wit1"), Value::u1(1))]);
332358
// FIXME The finalization should fail
@@ -335,15 +361,15 @@ mod tests {
335361
assert_finalize_err::<Core>(
336362
s,
337363
&wit2_is_missing,
338-
&(),
364+
&(), None,
339365
"Execution reached a pruned branch: bf12681a76fc7c00c63e583c25cc97237337d6aca30d3f4a664075445385c648"
340366
);
341367

342368
let wit2_is_present = HashMap::from([
343369
(Arc::from("wit1"), Value::u1(1)),
344370
(Arc::from("wit2"), Value::unit()),
345371
]);
346-
assert_finalize_ok::<Core>(s, &wit2_is_present, &());
372+
assert_finalize_ok::<Core>(s, &wit2_is_present, &(), None);
347373
}
348374

349375
#[test]
@@ -357,6 +383,7 @@ mod tests {
357383
",
358384
&empty,
359385
&(),
386+
None,
360387
);
361388
}
362389

@@ -370,6 +397,7 @@ mod tests {
370397
",
371398
&empty,
372399
&(),
400+
None,
373401
"unable to satisfy program",
374402
);
375403
}
@@ -382,9 +410,41 @@ mod tests {
382410
main := comp wit2 iden
383411
";
384412
let wit1_populated = HashMap::from([(Arc::from("wit1"), Value::unit())]);
385-
assert_finalize_err::<Core>(s, &wit1_populated, &(), "unable to satisfy program");
413+
assert_finalize_err::<Core>(s, &wit1_populated, &(), None, "unable to satisfy program");
386414

387415
let wit2_populated = HashMap::from([(Arc::from("wit2"), Value::unit())]);
388-
assert_finalize_ok::<Core>(s, &wit2_populated, &());
416+
assert_finalize_ok::<Core>(s, &wit2_populated, &(), None);
417+
}
418+
419+
#[test]
420+
fn expression() {
421+
let s = "
422+
expr := iden
423+
";
424+
let empty = HashMap::new();
425+
assert_finalize_ok::<Core>(s, &empty, &(), Some(Value::unit()));
426+
427+
let s = "
428+
expr := comp
429+
comp
430+
pair iden (comp unit const 0xff)
431+
jet_eq_8
432+
jet_verify
433+
";
434+
assert_finalize_err::<Core>(
435+
s,
436+
&empty,
437+
&(),
438+
Some(Value::unit()),
439+
"Expected input of type: 2^8",
440+
);
441+
assert_finalize_ok::<Core>(s, &empty, &(), Some(Value::u8(0xff)));
442+
assert_finalize_err::<Core>(
443+
s,
444+
&empty,
445+
&(),
446+
Some(Value::u8(0x00)),
447+
"Jet failed during execution",
448+
);
389449
}
390450
}

0 commit comments

Comments
 (0)