Explorar el Código

Restructure Numbat integration tests

David Peter hace 1 año
padre
commit
83c8507c0f

+ 0 - 170
examples/prelude_tests.nbt

@@ -1,170 +0,0 @@
-# unit_of
-
-assert_eq(unit_of(0),            1)
-
-assert_eq(unit_of(1),            1)
-assert_eq(unit_of(1.2345),       1)
-
-assert_eq(unit_of(1 m),          m)
-assert_eq(unit_of(1.2345 m),     m)
-
-assert_eq(unit_of(1 m^2/s),      m^2/s)
-assert_eq(unit_of(1.2345 m^2/s), m^2/s)
-
-# value_of
-
-assert_eq(value_of(0),            0)
-
-assert_eq(value_of(1),            1)
-assert_eq(value_of(1.2345),       1.2345)
-
-assert_eq(value_of(1 m),          1)
-assert_eq(value_of(1.2345 m),     1.2345)
-
-assert_eq(value_of(1 m^2/s),      1)
-assert_eq(value_of(1.2345 m^2/s), 1.2345)
-
-# hypot
-
-assert_eq(hypot2(3, 4), 5)
-assert_eq(hypot2(3 m, 4 m), 5 m)
-
-assert_eq(hypot3(8, 9, 12), 17)
-assert_eq(hypot3(8 m, 9 m, 12 m), 17 m)
-
-# trigonometry_extra
-
-assert_eq(cot(0.3), 3.2327281, 1e-5)
-assert_eq(acot(0.3), 1.2793395, 1e-5)
-
-assert_eq(coth(0.3), 3.4327384, 1e-5)
-assert_eq(acoth(1.3), 1.018441, 1e-5)
-
-assert_eq(secant(0.3), 1.0467516, 1e-5)
-assert_eq(arcsecant(1.3), 0.693160, 1e-5)
-
-assert_eq(csc(0.3), 3.3838634, 1e-5)
-assert_eq(acsc(1.3), 0.877636, 1e-5)
-
-assert_eq(sech(1.3), 0.507379, 1e-5)
-assert_eq(asech(0.3), 1.873820, 1e-5)
-
-assert_eq(csch(0.3), 3.283853, 1e-5)
-assert_eq(acsch(0.3), 1.918896, 1e-5)
-
-# strings
-
-assert_eq(str_length(""), 0)
-assert_eq(str_length("foo"), 3)
-
-assert_eq(str_slice("hello world", 0, 5), "hello")
-assert_eq(str_slice("hello world", 6, 11), "world")
-assert_eq(str_slice("hello world", 0, 0), "")
-assert_eq(str_slice("hello world", 0, 100), "")
-
-assert_eq(str_append("foo", "bar"), "foobar")
-
-assert(str_contains("hello world", "hello"))
-assert(str_contains("hello world", "world"))
-assert_eq(str_contains("hello world", "HELLO"), false)
-
-assert_eq(str_replace("hello world", "hello", "HEY"), "HEY world")
-assert_eq(str_replace("xxx", "x", "yY"), "yYyYyY")
-assert_eq(str_replace("a b", " ", "   "), "a   b")
-
-assert_eq(str_repeat("xy", 3), "xyxyxy")
-
-assert_eq((0b0 -> bin), "0b0")
-assert_eq((0b1 -> bin), "0b1")
-assert_eq((0b10 -> bin), "0b10")
-assert_eq((0b11 -> bin), "0b11")
-assert_eq((0b10101010101010101010101010101010 -> bin), "0b10101010101010101010101010101010")
-assert_eq((-0b11110000 -> bin), "-0b11110000")
-
-assert_eq((0o0 -> oct), "0o0")
-assert_eq((0o1 -> oct), "0o1")
-assert_eq((0o7 -> oct), "0o7")
-assert_eq((0o10 -> oct), "0o10")
-assert_eq((0o77 -> oct), "0o77")
-assert_eq((0o12345670 -> oct), "0o12345670")
-assert_eq((-0o12345670 -> oct), "-0o12345670")
-
-assert_eq((0x0 -> hex), "0x0")
-assert_eq((0x1 -> hex), "0x1")
-assert_eq((0x9 -> hex), "0x9")
-assert_eq((0xa -> hex), "0xa")
-assert_eq((0xf -> hex), "0xf")
-assert_eq((0xabc1234567890 -> hex), "0xabc1234567890")
-assert_eq((-0xc0ffee -> hex), "-0xc0ffee")
-
-# maximum
-
-assert_eq(maximum([1]), 1)
-assert_eq(maximum([1, 3]), 3)
-assert_eq(maximum([3, 1]), 3)
-assert_eq(maximum([100 cm, 3 m]), 3 m)
-
-# minimum
-
-assert_eq(minimum([1]), 1)
-assert_eq(minimum([1, 3]), 1)
-assert_eq(minimum([3, 1]), 1)
-assert_eq(minimum([100 cm, 3 m]), 100 cm)
-
-# mean
-
-assert_eq(mean([]), 0)
-assert_eq(mean([1]), 1)
-assert_eq(mean([1, 3]), 2)
-assert_eq(mean([1 m, 300 cm]), 2 m)
-
-# variance
-
-assert_eq(variance([]), 0)
-assert_eq(variance([1]), 0)
-assert_eq(variance([1, 1, 1, 1]), 0)
-assert_eq(variance([1, 2, 3, 4, 5]), 2.0)
-assert_eq(variance([1, -1, 1, -1]), 1.0)
-assert_eq(variance([-2, -4, -6, -8]), 5.0)
-
-# standard deviation
-
-assert_eq(stdev([]), 0)
-assert_eq(stdev([1]), 0)
-assert_eq(stdev([1, 1, 1, 1]), 0)
-assert_eq(stdev([1, 2, 3, 4, 5]), sqrt(2.0))
-
-# median
-
-assert_eq(median([1]), 1)
-assert_eq(median([1, 2]), 1.5)
-assert_eq(median([1, 2, 3]), 2)
-assert_eq(median([1, 2, 3, 4]), 2.5)
-assert_eq(median([1, 2, 3, 4, 5]), 3)
-
-# gcd
-
-assert_eq(gcd(0, 0), 0)
-assert_eq(gcd(0, 1), 1)
-assert_eq(gcd(1, 0), 1)
-assert_eq(gcd(1, 1), 1)
-assert_eq(gcd(2, 3), 1)
-assert_eq(gcd(2, 4), 2)
-assert_eq(gcd(3, 6), 3)
-assert_eq(gcd(4, 6), 2)
-assert_eq(gcd(4, 8), 4)
-assert_eq(gcd(6, 35), 1)
-assert_eq(gcd(1071, 462), 21)
-assert_eq(gcd(-8, 4), 4)
-assert_eq(gcd(-8, -4), 4)
-assert_eq(gcd(8, -4), 4)
-
-# lcm
-
-assert_eq(lcm(1, 1), 1)
-assert_eq(lcm(2, 3), 6)
-assert_eq(lcm(2, 4), 4)
-assert_eq(lcm(3, 6), 6)
-assert_eq(lcm(21, 6), 42)
-assert_eq(lcm(-8, 4), 8)
-assert_eq(lcm(8, -4), 8)

+ 2 - 0
examples/assert_eq_success.nbt → examples/tests/assert_eq.nbt

@@ -1,3 +1,5 @@
 let length = 2 m
 assert_eq(length, 1 m + 1 m)
 assert_eq(1.0 m, 1.001 m, 0.01 m)
+
+assert_eq(2 min + 30 s, 2.5 min, 0.01 s)

+ 22 - 0
examples/tests/base_conversion.nbt

@@ -0,0 +1,22 @@
+assert_eq((0b0 -> bin), "0b0")
+assert_eq((0b1 -> bin), "0b1")
+assert_eq((0b10 -> bin), "0b10")
+assert_eq((0b11 -> bin), "0b11")
+assert_eq((0b10101010101010101010101010101010 -> bin), "0b10101010101010101010101010101010")
+assert_eq((-0b11110000 -> bin), "-0b11110000")
+
+assert_eq((0o0 -> oct), "0o0")
+assert_eq((0o1 -> oct), "0o1")
+assert_eq((0o7 -> oct), "0o7")
+assert_eq((0o10 -> oct), "0o10")
+assert_eq((0o77 -> oct), "0o77")
+assert_eq((0o12345670 -> oct), "0o12345670")
+assert_eq((-0o12345670 -> oct), "-0o12345670")
+
+assert_eq((0x0 -> hex), "0x0")
+assert_eq((0x1 -> hex), "0x1")
+assert_eq((0x9 -> hex), "0x9")
+assert_eq((0xa -> hex), "0xa")
+assert_eq((0xf -> hex), "0xf")
+assert_eq((0xabc1234567890 -> hex), "0xabc1234567890")
+assert_eq((-0xc0ffee -> hex), "-0xc0ffee")

+ 0 - 0
examples/booleans.nbt → examples/tests/booleans.nbt


+ 0 - 0
examples/chemistry_tests.nbt → examples/tests/chemistry.nbt


+ 0 - 0
examples/codata2018_consistency.nbt → examples/tests/codata2018.nbt


+ 25 - 0
examples/tests/core.nbt

@@ -0,0 +1,25 @@
+# unit_of
+
+assert_eq(unit_of(0),            1)
+
+assert_eq(unit_of(1),            1)
+assert_eq(unit_of(1.2345),       1)
+
+assert_eq(unit_of(1 m),          m)
+assert_eq(unit_of(1.2345 m),     m)
+
+assert_eq(unit_of(1 m^2/s),      m^2/s)
+assert_eq(unit_of(1.2345 m^2/s), m^2/s)
+
+# value_of
+
+assert_eq(value_of(0),            0)
+
+assert_eq(value_of(1),            1)
+assert_eq(value_of(1.2345),       1.2345)
+
+assert_eq(value_of(1 m),          1)
+assert_eq(value_of(1.2345 m),     1.2345)
+
+assert_eq(value_of(1 m^2/s),      1)
+assert_eq(value_of(1.2345 m^2/s), 1.2345)

+ 0 - 0
examples/datetime_tests.nbt → examples/tests/datetime.nbt


+ 0 - 0
examples/datetime_human_tests.nbt → examples/tests/human.nbt


+ 0 - 0
examples/list_tests.nbt → examples/tests/lists.nbt


+ 196 - 0
examples/tests/math_functions.nbt

@@ -0,0 +1,196 @@
+# sqrt
+
+assert_eq(sqrt(0), 0)
+assert_eq(sqrt(1), 1)
+assert_eq(sqrt(4), 2)
+
+assert_eq(sqrt(0 m²), 0 m)
+assert_eq(sqrt(1 m²), 1 m)
+assert_eq(sqrt(4 m²), 2 m)
+
+# sqr
+
+assert_eq(sqr(0), 0)
+assert_eq(sqr(1), 1)
+assert_eq(sqr(4), 16)
+
+assert_eq(sqr(0 m), 0 m²)
+assert_eq(sqr(1 m), 1 m²)
+assert_eq(sqr(4 m), 16 m²)
+
+# exp
+
+assert_eq(exp(0), 1)
+assert_eq(exp(1), e)
+assert_eq(exp(2), e * e, 1e-8)
+
+# ln/log
+
+assert_eq(ln(1), 0)
+assert_eq(ln(e), 1)
+assert_eq(ln(e * e), 2, 1e-8)
+
+assert_eq(log(1), 0)
+assert_eq(log(e), 1)
+assert_eq(log(e * e), 2, 1e-8)
+
+# log10
+
+assert_eq(log10(1), 0)
+assert_eq(log10(10), 1)
+assert_eq(log10(100), 2)
+
+# log2
+
+assert_eq(log2(1), 0)
+assert_eq(log2(2), 1)
+assert_eq(log2(4), 2)
+assert_eq(log2(1024), 10)
+
+# sin, cos, tan
+
+assert_eq(sin(0), 0, 1e-8)
+assert_eq(sin(π / 2), 1, 1e-8)
+assert_eq(sin(π), 0, 1e-8)
+
+assert_eq(cos(0), 1, 1e-8)
+assert_eq(cos(π / 2), 0, 1e-8)
+assert_eq(cos(π), -1, 1e-8)
+
+assert_eq(tan(0), 0, 1e-8)
+assert_eq(tan(π / 4), 1, 1e-8)
+
+# asin, acos, atan
+
+assert_eq(asin(0), 0, 1e-8)
+assert_eq(asin(1), π / 2, 1e-8)
+
+assert_eq(acos(0), π / 2, 1e-8)
+assert_eq(acos(1), 0, 1e-8)
+
+assert_eq(atan(0), 0, 1e-8)
+assert_eq(atan(1), π / 4, 1e-8)
+
+# atan2
+
+assert_eq(atan2(0, 1), 0, 1e-8)
+assert_eq(atan2(1, 0), π / 2, 1e-8)
+assert_eq(atan2(0, -1), π, 1e-8)
+assert_eq(atan2(-1, 0), -π / 2, 1e-8)
+
+# sinh, cosh, tanh
+
+assert_eq(sinh(0), 0, 1e-8)
+assert_eq(sinh(1), (e - 1 / e) / 2, 1e-8)
+
+assert_eq(cosh(0), 1, 1e-8)
+assert_eq(cosh(1), (e + 1 / e) / 2, 1e-8)
+
+assert_eq(tanh(0), 0, 1e-8)
+assert_eq(tanh(1), (e * e - 1) / (e * e + 1), 1e-8)
+
+# asinh, acosh, atanh
+
+assert_eq(asinh(0), 0, 1e-8)
+assert_eq(asinh(1), ln(1 + sqrt(2)), 1e-8)
+
+assert_eq(acosh(1), 0, 1e-8)
+assert_eq(acosh(2), ln(2 + sqrt(3)), 1e-8)
+
+assert_eq(atanh(0), 0, 1e-8)
+assert_eq(atanh(0.5), 0.549_306_144, 1e-8)
+
+# gamma
+
+assert_eq(gamma(1), 1)
+assert_eq(gamma(2), 1)
+assert_eq(gamma(2.5), 1.329_340_388, 1e-8)
+assert_eq(gamma(3), 2)
+assert_eq(gamma(4), 6)
+
+# maximum
+
+assert_eq(maximum([1]), 1)
+assert_eq(maximum([1, 3]), 3)
+assert_eq(maximum([3, 1]), 3)
+assert_eq(maximum([100 cm, 3 m]), 3 m)
+
+# minimum
+
+assert_eq(minimum([1]), 1)
+assert_eq(minimum([1, 3]), 1)
+assert_eq(minimum([3, 1]), 1)
+assert_eq(minimum([100 cm, 3 m]), 100 cm)
+
+# mean
+
+assert_eq(mean([]), 0)
+assert_eq(mean([1]), 1)
+assert_eq(mean([1, 3]), 2)
+assert_eq(mean([1 m, 300 cm]), 2 m)
+
+# variance
+
+assert_eq(variance([]), 0)
+assert_eq(variance([1]), 0)
+assert_eq(variance([1, 1, 1, 1]), 0)
+assert_eq(variance([1, 2, 3, 4, 5]), 2.0)
+assert_eq(variance([1, -1, 1, -1]), 1.0)
+assert_eq(variance([-2, -4, -6, -8]), 5.0)
+
+# standard deviation
+
+assert_eq(stdev([]), 0)
+assert_eq(stdev([1]), 0)
+assert_eq(stdev([1, 1, 1, 1]), 0)
+assert_eq(stdev([1, 2, 3, 4, 5]), sqrt(2.0))
+
+# median
+
+assert_eq(median([1]), 1)
+assert_eq(median([1, 2]), 1.5)
+assert_eq(median([1, 2, 3]), 2)
+assert_eq(median([1, 2, 3, 4]), 2.5)
+assert_eq(median([1, 2, 3, 4, 5]), 3)
+
+# gcd
+
+assert_eq(gcd(0, 0), 0)
+assert_eq(gcd(0, 1), 1)
+assert_eq(gcd(1, 0), 1)
+assert_eq(gcd(1, 1), 1)
+assert_eq(gcd(2, 3), 1)
+assert_eq(gcd(2, 4), 2)
+assert_eq(gcd(3, 6), 3)
+assert_eq(gcd(4, 6), 2)
+assert_eq(gcd(4, 8), 4)
+assert_eq(gcd(6, 35), 1)
+assert_eq(gcd(1071, 462), 21)
+assert_eq(gcd(-8, 4), 4)
+assert_eq(gcd(-8, -4), 4)
+assert_eq(gcd(8, -4), 4)
+
+# lcm
+
+assert_eq(lcm(1, 1), 1)
+assert_eq(lcm(2, 3), 6)
+assert_eq(lcm(2, 4), 4)
+assert_eq(lcm(3, 6), 6)
+assert_eq(lcm(21, 6), 42)
+assert_eq(lcm(-8, 4), 8)
+assert_eq(lcm(8, -4), 8)
+
+# hypot2, hypot3
+
+assert_eq(hypot2(3, 4), 5)
+assert_eq(hypot2(3 m, 4 m), 5 m)
+
+assert_eq(hypot3(8, 9, 12), 17)
+assert_eq(hypot3(8 m, 9 m, 12 m), 17 m)
+
+# circle_area, circle_circumference, sphere_area, sphere_volume
+
+assert_eq(circle_area(2 m), 4 π m²)
+assert_eq(circle_circumference(2 m), 4 π m)
+assert_eq(sphere_area(2 m), 16 π m²)
+assert_eq(sphere_volume(2 m), 32/3 * π m³)

+ 0 - 0
examples/numerics_tests.nbt → examples/tests/numerics.nbt


+ 19 - 0
examples/tests/strings.nbt

@@ -0,0 +1,19 @@
+assert_eq(str_length(""), 0)
+assert_eq(str_length("foo"), 3)
+
+assert_eq(str_slice("hello world", 0, 5), "hello")
+assert_eq(str_slice("hello world", 6, 11), "world")
+assert_eq(str_slice("hello world", 0, 0), "")
+assert_eq(str_slice("hello world", 0, 100), "")
+
+assert_eq(str_append("foo", "bar"), "foobar")
+
+assert(str_contains("hello world", "hello"))
+assert(str_contains("hello world", "world"))
+assert_eq(str_contains("hello world", "HELLO"), false)
+
+assert_eq(str_replace("hello world", "hello", "HEY"), "HEY world")
+assert_eq(str_replace("xxx", "x", "yY"), "yYyYyY")
+assert_eq(str_replace("a b", " ", "   "), "a   b")
+
+assert_eq(str_repeat("xy", 3), "xyxyxy")

+ 17 - 0
examples/tests/trigonometry_extra.nbt

@@ -0,0 +1,17 @@
+assert_eq(cot(0.3), 3.2327281, 1e-5)
+assert_eq(acot(0.3), 1.2793395, 1e-5)
+
+assert_eq(coth(0.3), 3.4327384, 1e-5)
+assert_eq(acoth(1.3), 1.018441, 1e-5)
+
+assert_eq(secant(0.3), 1.0467516, 1e-5)
+assert_eq(arcsecant(1.3), 0.693160, 1e-5)
+
+assert_eq(csc(0.3), 3.3838634, 1e-5)
+assert_eq(acsc(1.3), 0.877636, 1e-5)
+
+assert_eq(sech(1.3), 0.507379, 1e-5)
+assert_eq(asech(0.3), 1.873820, 1e-5)
+
+assert_eq(csch(0.3), 3.283853, 1e-5)
+assert_eq(acsch(0.3), 1.918896, 1e-5)

+ 0 - 0
examples/consistency_others.nbt → examples/tests/unit_consistency_misc.nbt


+ 0 - 0
examples/consistency_si.nbt → examples/tests/unit_consistency_si.nbt


+ 0 - 1
examples/time_units.nbt

@@ -1 +0,0 @@
-assert_eq(2 min + 30 s, 2.5 min, 0.01 s)

+ 5 - 0
numbat/tests/prelude_and_examples.rs

@@ -82,6 +82,11 @@ fn examples_can_be_parsed_and_interpreted() {
     run_for_each_file("../examples/*.nbt", assert_runs);
 }
 
+#[test]
+fn numbat_tests_are_executed_successfully() {
+    run_for_each_file("../examples/tests/*.nbt", assert_runs);
+}
+
 #[test]
 fn parse_error_examples_fail_as_expected() {
     run_for_each_file("../examples/parse_error/*.nbt", assert_parse_error);