assert_eq!(@textTrim@(" 雪 \n"), "雪"); assert_eq!(@textLower@("İABC雪"), "i\u{307}abc雪"); assert_eq!(@textUpper@("straße雪"), "STRASSE雪"); for value in ["", "ab", "e\u{301}"] { assert_eq!(@textCodepoint@(value), None); } for point in [0, 0x7f, 0x80, 0x7ff, 0x800, 0xd7ff, 0xe000, 0xffff, 0x10000, 0x10ffff] { let text = char::from_u32(point).unwrap().to_string(); assert_eq!(@textCodepoint@(&text), Some(i64::from(point))); assert_eq!(@textFromCodepoint@(i64::from(point)), Some(text)); } for point in [-1, 0xd800, 0xdfff, 0x110000, i64::MAX] { assert_eq!(@textFromCodepoint@(point), None); } let text = "a雪😀e\u{301}"; assert_eq!(@textAt@(text, 2), Some("😀".into())); for index in [-1, 5, i64::MAX] { assert_eq!(@textAt@(text, index), None); } assert_eq!(@textSlice@(text, 1, 3), Some("雪😀".into())); assert_eq!(@textSlice@(text, 5, 5), Some(String::new())); assert_eq!(@textSlice@("", 0, 0), Some(String::new())); for (start, end) in [(-1, 2), (0, -1), (3, 2), (0, 6), (6, 6), (i64::MAX, i64::MAX)] { assert_eq!(@textSlice@(text, start, end), None); } assert_eq!(@textIndexOf@(text, "😀"), Some(2)); assert_eq!(@textIndexOf@(text, "missing"), None); assert_eq!(@textIndexOf@(text, ""), Some(0)); assert!(@textContains@(text, "雪😀")); assert!(@textStartsWith@(text, "a雪")); assert!(@textEndsWith@(text, "e\u{301}")); assert!(!@textIsEmpty@(text)); assert!(@textIsEmpty@("")); assert_eq!(@textSplit@("雪😀", ""), ["雪", "😀"]); assert!(@textSplit@("", "").is_empty()); assert_eq!(@textSplit@(",a,,", ","), ["", "a", "", ""]); assert_eq!(@textReplace@("雪雪", "雪", "😀"), "😀😀"); assert_eq!(@textJoin@(&["雪".into(), "😀".into()], ","), "雪,😀"); for value in [i64::MIN, -1, 0, 1, i64::MAX] { assert_eq!(@intParse@(&@intToText@(value)), Some(value)); assert_eq!(@decimalFromInt@(value), Decimal::from_int(value)); assert_eq!(@floatFromInt@(value), value as f64); } for value in ["", " 1", "1 ", "1.0", "9223372036854775808"] { assert_eq!(@intParse@(value), None); } for value in ["", "nan", "NAN", "+inf", "Infinity", " 1", "1 ", "1_0", "1e", "--1"] { assert_eq!(@floatParse@(value), None); } for value in [0.0, -0.0, 0.1, f64::MIN_POSITIVE, f64::MAX, f64::INFINITY, f64::NEG_INFINITY] { assert_eq!(@floatParse@(&@floatToText@(value)).unwrap().to_bits(), value.to_bits()); } assert!(@floatParse@("NaN").unwrap().is_nan()); assert_eq!(@floatParse@("1e9999"), Some(f64::INFINITY)); for value in ["", "NaN", "inf", " 1", "1 ", "1_0", "1e", "1e6145", "1e-6177", "1.0000000000000000000000000000000001"] { assert_eq!(@decimalParse@(value), None, "{value}"); } for value in ["0", "0.1", "1e6144", "1e-6176"] { let parsed = @decimalParse@(value).unwrap(); assert_eq!(@decimalParse@(&@decimalToText@(parsed)), Some(parsed)); } for (value, places, expected) in [("1.25", 1, "1.2"), ("1.35", 1, "1.4"), ("250", -2, "200"), ("-1.25", 1, "-1.2")] { assert_eq!(@decimalRound@(Decimal::parse(value).unwrap(), places), Decimal::parse(expected).unwrap()); } for places in [i64::MIN, -6112, 6177, i64::MAX] { let failure = std::panic::catch_unwind(|| @decimalRound@(Decimal::from_int(1), places)).unwrap_err(); let info = failure.downcast_ref::<&'static dever_runtime::value::FaultInfo>().unwrap(); assert!(info.source.ends_with("lib/decimal.dv")); assert_eq!(info.line, 2); assert!(info.column > 0); assert_eq!(info.message, "decimal rounding fault"); } assert_eq!(@mathSqrt@(4.0), 2.0); assert_eq!(@mathSin@(0.0), 0.0); assert_eq!(@mathCos@(0.0), 1.0); assert_eq!(@mathLog@(1.0), 0.0); assert_eq!(@mathPow@(2.0, 3.0), 8.0); assert!(@mathIsNan@(@mathSqrt@(-1.0))); assert!(@mathIsFinite@(1.0)); assert!(!@mathIsFinite@(f64::INFINITY)); assert!(@mathIsInfinite@(f64::INFINITY)); @allocation_counter@ let before = ALLOCATIONS.load(Ordering::Relaxed); assert_eq!(@textIndexOf@(std::hint::black_box(text), "😀"), Some(2)); assert!(@textContains@(std::hint::black_box(text), "雪")); assert_eq!(@intParse@(std::hint::black_box("123")), Some(123)); assert_eq!(@floatParse@(std::hint::black_box("0.125")), Some(0.125)); assert_eq!(ALLOCATIONS.load(Ordering::Relaxed) - before, 0); assert_eq!(@readFloat@("0.125").unwrap(), 0.125); assert!(@readFloat@("NaN").unwrap().is_nan()); assert!(@readFloat@("bad").is_err()); assert_eq!(@readDecimal@("0.125").unwrap(), Decimal::parse("0.125").unwrap()); assert!(@readDecimal@("bad").is_err()); for parts in [vec![], vec![""], vec!["", "a", "", "雪🙂", ""], vec!["ab", "好🙂", "cd"]] { let parts = parts.into_iter().map(str::to_owned).collect::>(); let original = parts.clone(); assert_eq!(@joinChunks@(&parts), parts.concat()); assert_eq!(@prefixChunks@(&parts), "前缀:".to_owned() + &parts.concat()); assert_eq!(parts, original, "joining borrows and preserves its input"); }