From 392c5960068747223afcdd253d4d84c0df50fe3d 2021-05-31 11:37:55 From: mh Date: 2021-05-31 11:37:55 Subject: [PATCH] Test non-ascii string literals and recursion --- diff --git a/src/protocol/input_source.rs b/src/protocol/input_source.rs index e730f9ba3c38c91210d6eeea67924c7498124a88..0ea65391f07b7e6d4d08abf34978a09a61ff9ffd 100644 --- a/src/protocol/input_source.rs +++ b/src/protocol/input_source.rs @@ -168,7 +168,6 @@ impl InputSource { } lookup.push(self.input.len() as u32 + 1); // for lookup_line_end, intentionally adding one character - debug_assert_eq!(self.line as usize + 2, lookup.len(), "remove me: i am a testing assert and sometimes invalid"); // Return created lookup drop(lookup); diff --git a/src/protocol/tests/eval_calls.rs b/src/protocol/tests/eval_calls.rs index f9a1e3c7ff545504193df16b75442b85270fcb6e..9ed4d853b644228943a8fbf8ea4ba3c7eb4dd136 100644 --- a/src/protocol/tests/eval_calls.rs +++ b/src/protocol/tests/eval_calls.rs @@ -27,6 +27,58 @@ fn test_function_call() { }); } +#[test] +fn test_recursion() { + // Single-chain + Tester::new_single_source_expect_ok("factorial", " + func horribly_slow_factorial(u32 term) -> u32 { + if (term <= 0) { return 1; } + + return term * horribly_slow_factorial(term - 1); + } + func foo() -> u32 { + return horribly_slow_factorial(10); + } + ").for_function("foo", |f| { + f.call_ok(Some(Value::UInt32(3628800))); + }); + + // Multi-chain horribleness + Tester::new_single_source_expect_ok("fibonacci", " + func horribly_slow_fibo(u32 term) -> u32 { + if (term <= 1) { + return 1; + } + return horribly_slow_fibo(term - 2) + horribly_slow_fibo(term - 1); + } + func foo() -> u32 { + return horribly_slow_fibo(10); + }").for_function("foo", |f| { + f.call_ok(Some(Value::UInt32(89))); + }); + + // Mutual recursion (in a contrived fashion, ofcourse) + Tester::new_single_source_expect_ok("mutual recursion", " + func collatz_even(u32 iter, u32 value) -> u32 { + value = value / 2; + if (value % 2 == 0) return collatz_even(iter + 1, value); + else return collatz_odd(iter + 1, value); + } + func collatz_odd(u32 iter, u32 value) -> u32 { + if (value <= 1) return iter; + + value = 3 * value + 1; + if (value % 2 == 0) return collatz_even(iter + 1, value); + else return collatz_odd(iter + 1, value); + } + func foo() -> u32 { + return collatz_odd(1, 19); + } + ").for_function("foo", |f| { + f.call_ok(Some(Value::UInt32(21))); + }); +} + #[test] fn test_empty_blocks() { // Yes this is silly, but I managed to make this a bug before diff --git a/src/protocol/tests/parser_literals.rs b/src/protocol/tests/parser_literals.rs index cb21deeeac5996b32ef97fb50745c95619486f7c..29ab95774c778f867b8ecbc46072e01ff5e39f90 100644 --- a/src/protocol/tests/parser_literals.rs +++ b/src/protocol/tests/parser_literals.rs @@ -62,4 +62,9 @@ fn test_string_literals() { Tester::new_single_source_expect_err("invalid escaped character", " func test() -> string { return \"\\y\"; } ").error(|e| { e.assert_msg_has(0, "unsupported escape character 'y'"); }); + + // Note sure if this should always be in here... + Tester::new_single_source_expect_err("non-ASCII string", " + func test() -> string { return \"💧\"; } + ").error(|e| { e.assert_msg_has(0, "non-ASCII character in string literal"); }); } \ No newline at end of file