about summary refs log blame commit diff
path: root/users/tazjin/rlox/src/bytecode/tests.rs
blob: 5a0901be6d79e8f7bba1ebff8c7820da9326f253 (plain) (tree)
1
2
3
4
5
6
7
8
9



               
                                       


                                      
                                                    



              

                               




                                                    

                                 



            





                                      



              



                                       



                 


                                                                
 
use super::*;

use crate::Lox;

fn expect_num(code: &str, value: f64) {
    let result = Interpreter::create()
        .interpret(code.into())
        .expect("evaluation failed");
    assert_eq!(result, value::Value::Number(value));
}

#[test]
fn numbers() {
    expect_num("1", 1.0);
    expect_num("13.37", 13.37);
}

#[test]
fn negative_numbers() {
    // Note: This technically tests unary operators.
    expect_num("-1", -1.0);
    expect_num("-13.37", -13.37);
}

#[test]
fn terms() {
    expect_num("1 + 2", 3.0);
    expect_num("3 - 1", 2.0);
    expect_num("0.7 + 0.3", 1.0);
    expect_num("1 + -3", -2.0);
    expect_num("-1 - -1", 0.0);
    expect_num("10 - -10 + 10", 30.0);
}

#[test]
fn factors() {
    expect_num("1 * 2", 2.0);
    expect_num("10 / 5", 2.0);
    expect_num("0.7 * 4 / 1.4", 2.0);
    expect_num("10 * -10 / 10", -10.0);
}

#[test]
fn arithmetic() {
    expect_num("10 - 3 * 2", 4.0);
    expect_num("-4 * -4 + (14 - 5)", 25.0);
    expect_num("(702 + 408) - ((239 - 734) / -5) + -4", 1007.0);
}