pnc-engine 0.2.1

Core engine for PNC: arbitrary-precision RPN evaluation, units, and persistence
Documentation
use num_rational::BigRational;
use pnc_engine::unit::{UnitId, convert};
use pnc_engine::value::Value;

#[test]
fn convert_100f_to_c() {
    let v = Value::Unit {
        number: Box::new(Value::Int(100.into())),
        unit: UnitId::Fahrenheit,
    };
    let out = convert(&v, UnitId::Celsius).unwrap();
    // 100 F = 340/9 C
    match out {
        Value::Unit { number, unit } => {
            assert_eq!(unit, UnitId::Celsius);
            assert_eq!(
                *number,
                Value::Rational(BigRational::new(340.into(), 9.into()))
            );
        }
        _ => panic!("expected unit value"),
    }
}

#[test]
fn convert_round_trip_f_to_c_to_f() {
    let v = Value::Unit {
        number: Box::new(Value::Int(100.into())),
        unit: UnitId::Fahrenheit,
    };
    let c = convert(&v, UnitId::Celsius).unwrap();
    let f = convert(&c, UnitId::Fahrenheit).unwrap();
    match f {
        Value::Unit { number, unit } => {
            assert_eq!(unit, UnitId::Fahrenheit);
            assert_eq!(*number, Value::Int(100.into()));
        }
        _ => panic!("expected unit value"),
    }
}