aboutsummaryrefslogtreecommitdiffstats
path: root/src/eval.rs
diff options
context:
space:
mode:
authorRose Hogenson <rosehogenson@posteo.net>2024-05-30 20:25:59 -0700
committerRose Hogenson <rosehogenson@posteo.net>2024-05-30 20:25:59 -0700
commit5f47e7b0613b7477ef91568f4a4ed1e10dc5f707 (patch)
tree022bb3ae92ead3ca11f68682e7e0dcf10c1f37bd /src/eval.rs
parent405e5eb597709eebeefdc679e4c63b396248c3bc (diff)
downloadqc-5f47e7b0613b7477ef91568f4a4ed1e10dc5f707.tar.zst
Write tests for eval.
Diffstat (limited to 'src/eval.rs')
-rw-r--r--src/eval.rs90
1 files changed, 89 insertions, 1 deletions
diff --git a/src/eval.rs b/src/eval.rs
index b08bf81..dce9d68 100644
--- a/src/eval.rs
+++ b/src/eval.rs
@@ -1,6 +1,6 @@
use std::fmt::{Display, Formatter};
-#[derive(Clone, Copy)]
+#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Num {
Int(i128),
Float(f64),
@@ -211,3 +211,91 @@ impl Display for Num {
}
}
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn pow_positive_int() {
+ assert_eq!(int(2).pow(int(16)), int(65536));
+ }
+
+ #[test]
+ fn pow_negative_int() {
+ assert_eq!(int(2).pow(int(-3)), float(0.125));
+ }
+
+ #[test]
+ fn pow_float() {
+ assert_eq!(int(4).pow(float(0.5)), float(2.));
+ }
+
+ #[test]
+ fn pow_overflow() {
+ assert_eq!(int(2).pow(int(1 << 126)), float(f64::INFINITY));
+ }
+
+ #[test]
+ fn pow_underflow() {
+ assert_eq!(int(2).pow(int(-(1 << 126))), float(0.));
+ }
+
+ #[test]
+ fn modulo_pos() {
+ assert_eq!(int(5).modulo(int(3)), int(2));
+ }
+
+ #[test]
+ fn modulo_pos_neg() {
+ assert_eq!(int(5).modulo(int(-3)), int(-1));
+ }
+
+ #[test]
+ fn modulo_neg_pos() {
+ assert_eq!(int(-5).modulo(int(3)), int(1));
+ }
+
+ #[test]
+ fn modulo_neg() {
+ assert_eq!(int(-5).modulo(int(-3)), int(-2));
+ }
+
+ #[test]
+ fn modulo_float_pos() {
+ assert_eq!(float(5.).modulo(int(3)), float(2.));
+ }
+
+ #[test]
+ fn modulo_float_pos_neg() {
+ assert_eq!(float(5.).modulo(int(-3)), float(-1.));
+ }
+
+ #[test]
+ fn modulo_float_neg_pos() {
+ assert_eq!(float(-5.).modulo(int(3)), float(1.));
+ }
+
+ #[test]
+ fn modulo_float_neg() {
+ assert_eq!(float(-5.).modulo(int(-3)), float(-2.));
+ }
+
+ #[test]
+ fn sqrt() {
+ for n in 0..65536 {
+ assert_eq!(
+ int(n * n).sqrt(),
+ int(n),
+ "int({}).sqrt() is not equal to {}",
+ n * n,
+ n
+ );
+ }
+ }
+
+ #[test]
+ fn sqrt_big() {
+ assert_eq!(int(1 << 126).sqrt(), int(1 << 63));
+ }
+}