From 6b990a757186da0f2766fefac53ce5de140d9174 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Sun, 28 Feb 2021 13:08:54 +0200 Subject: test(tazjin/rlox): Add some tests for numerical operations If I was adding any dependencies, this might be a good one for a property-based test thing, but I'm not going to. Change-Id: Ia801d041479d1a88c59ef9e0fe1460b3640382e3 Reviewed-on: https://cl.tvl.fyi/c/depot/+/2569 Reviewed-by: tazjin Tested-by: BuildkiteCI --- users/tazjin/rlox/src/bytecode/mod.rs | 3 +++ users/tazjin/rlox/src/bytecode/tests.rs | 48 +++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 users/tazjin/rlox/src/bytecode/tests.rs (limited to 'users/tazjin/rlox/src/bytecode') diff --git a/users/tazjin/rlox/src/bytecode/mod.rs b/users/tazjin/rlox/src/bytecode/mod.rs index 0776225bec91..3c7dad97be3e 100644 --- a/users/tazjin/rlox/src/bytecode/mod.rs +++ b/users/tazjin/rlox/src/bytecode/mod.rs @@ -9,6 +9,9 @@ mod opcode; mod value; mod vm; +#[cfg(test)] +mod tests; + use chunk::Chunk; pub struct Interpreter {} diff --git a/users/tazjin/rlox/src/bytecode/tests.rs b/users/tazjin/rlox/src/bytecode/tests.rs new file mode 100644 index 000000000000..543d9cab515d --- /dev/null +++ b/users/tazjin/rlox/src/bytecode/tests.rs @@ -0,0 +1,48 @@ +use super::*; + +use crate::Lox; + +fn expect(code: &str, value: value::Value) { + let result = Interpreter::create() + .interpret(code.into()) + .expect("evaluation failed"); + assert_eq!(result, value); +} + +#[test] +fn numbers() { + expect("1", 1.0); + expect("13.37", 13.37); +} + +#[test] +fn negative_numbers() { + // Note: This technically tests unary operators. + expect("-1", -1.0); + expect("-13.37", -13.37); +} + +#[test] +fn terms() { + expect("1 + 2", 3.0); + expect("3 - 1", 2.0); + expect("0.7 + 0.3", 1.0); + expect("1 + -3", -2.0); + expect("-1 - -1", 0.0); + expect("10 - -10 + 10", 30.0); +} + +#[test] +fn factors() { + expect("1 * 2", 2.0); + expect("10 / 5", 2.0); + expect("0.7 * 4 / 1.4", 2.0); + expect("10 * -10 / 10", -10.0); +} + +#[test] +fn arithmetic() { + expect("10 - 3 * 2", 4.0); + expect("-4 * -4 + (14 - 5)", 25.0); + expect("(702 + 408) - ((239 - 734) / -5) + -4", 1007.0); +} -- cgit 1.4.1