From 20e3663cbd04d81a8680c70bb20ef9fb058c170c Mon Sep 17 00:00:00 2001 From: David Peter Date: Tue, 2 Jul 2024 18:43:12 +0200 Subject: [PATCH 1/3] =?UTF-8?q?Use=20assert=5Feq=20instead=20of=20assert(?= =?UTF-8?q?=E2=80=A6=20=3D=3D=20=E2=80=A6)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/chemistry_tests.nbt | 12 ++--- examples/datetime_human_tests.nbt | 76 +++++++++++++++---------------- examples/datetime_tests.nbt | 6 +-- examples/prelude_tests.nbt | 70 ++++++++++++++-------------- 4 files changed, 82 insertions(+), 82 deletions(-) diff --git a/examples/chemistry_tests.nbt b/examples/chemistry_tests.nbt index 7a801118..7731a64e 100644 --- a/examples/chemistry_tests.nbt +++ b/examples/chemistry_tests.nbt @@ -7,8 +7,8 @@ # - https://en.wikipedia.org/wiki/Enthalpy_of_vaporization let w = element("tungsten") -assert(w.symbol == "W") -assert(w.name == "Tungsten") +assert_eq(w.symbol, "W") +assert_eq(w.name, "Tungsten") assert_eq(w.atomic_number, 74) assert_eq(w.group, 6) assert_eq(w.period, 6) @@ -21,8 +21,8 @@ assert_eq(w.vaporization_heat, 807 kJ/mol, 20 kJ/mol) let chl = element("chlorine") -assert(chl.symbol == "Cl") -assert(chl.name == "Chlorine") +assert_eq(chl.symbol, "Cl") +assert_eq(chl.name, "Chlorine") assert_eq(chl.atomic_number, 17) assert_eq(chl.group, 17) assert_eq(chl.period, 3) @@ -35,8 +35,8 @@ assert_eq(chl.vaporization_heat, 20.4 kJ/mol, 0.1 kJ/mol) let rb = element("rubidium") -assert(rb.symbol == "Rb") -assert(rb.name == "Rubidium") +assert_eq(rb.symbol, "Rb") +assert_eq(rb.name, "Rubidium") assert_eq(rb.atomic_number, 37) assert_eq(rb.group, 1) assert_eq(rb.period, 5) diff --git a/examples/datetime_human_tests.nbt b/examples/datetime_human_tests.nbt index 9857f400..4d51538d 100644 --- a/examples/datetime_human_tests.nbt +++ b/examples/datetime_human_tests.nbt @@ -1,38 +1,38 @@ -assert((0 second -> human) == "0 seconds") -assert((1 second -> human) == "1 second") -assert((5 second -> human) == "5 seconds") -assert((1.5 second -> human) == "1.500 seconds") - -assert((60 seconds -> human) == "1 minute") -assert((73 seconds -> human) == "1 minute + 13 seconds") -assert((120 seconds -> human) == "2 minutes") -assert((60.1 seconds -> human) == "1 minute + 0.100 seconds") -assert((1 minute -> human) == "1 minute") -assert((1.25 minute -> human) == "1 minute + 15 seconds") -assert((2.5 minute -> human) == "2 minutes + 30 seconds") - -assert((1 hour -> human) == "1 hour") -assert((1.5 hour -> human) == "1 hour + 30 minutes") -assert((2 hour -> human) == "2 hours") -assert((1 hour + 1 sec -> human) == "1 hour + 1 second") - -assert((1 day -> human) == "1 day") -assert((1.37 day -> human) == "1 day + 8 hours + 52 minutes + 48 seconds") - -assert((1 week -> human) == "7 days") -assert((1.5 weeks -> human) == "10 days + 12 hours") -assert((2 weeks -> human) == "14 days") - -assert((1 sidereal_day -> human) == "23 hours + 56 minutes + 4.090500 seconds") - -assert((10000 days -> human) == "10000 days") -assert((50 million days -> human) == "50_000_000 days") - -assert((1e12 days -> human) == "1_000_000_000_000 days") -assert((1e15 days -> human) == "1.0e+15 days") - -assert((1 ms -> human) == "0.001 seconds") -assert((1 µs -> human) == "0.000001 seconds") -assert((1 ns -> human) == "0.000000001 seconds") -assert((1234 ns -> human) == "0.000001234 seconds") -assert((1s + 1234 ns -> human) == "1.000001234 seconds") +assert_eq((0 second -> human), "0 seconds") +assert_eq((1 second -> human), "1 second") +assert_eq((5 second -> human), "5 seconds") +assert_eq((1.5 second -> human), "1.500 seconds") + +assert_eq((60 seconds -> human), "1 minute") +assert_eq((73 seconds -> human), "1 minute + 13 seconds") +assert_eq((120 seconds -> human), "2 minutes") +assert_eq((60.1 seconds -> human), "1 minute + 0.100 seconds") +assert_eq((1 minute -> human), "1 minute") +assert_eq((1.25 minute -> human), "1 minute + 15 seconds") +assert_eq((2.5 minute -> human), "2 minutes + 30 seconds") + +assert_eq((1 hour -> human), "1 hour") +assert_eq((1.5 hour -> human), "1 hour + 30 minutes") +assert_eq((2 hour -> human), "2 hours") +assert_eq((1 hour + 1 sec -> human), "1 hour + 1 second") + +assert_eq((1 day -> human), "1 day") +assert_eq((1.37 day -> human), "1 day + 8 hours + 52 minutes + 48 seconds") + +assert_eq((1 week -> human), "7 days") +assert_eq((1.5 weeks -> human), "10 days + 12 hours") +assert_eq((2 weeks -> human), "14 days") + +assert_eq((1 sidereal_day -> human), "23 hours + 56 minutes + 4.090500 seconds") + +assert_eq((10000 days -> human), "10000 days") +assert_eq((50 million days -> human), "50_000_000 days") + +assert_eq((1e12 days -> human), "1_000_000_000_000 days") +assert_eq((1e15 days -> human), "1.0e+15 days") + +assert_eq((1 ms -> human), "0.001 seconds") +assert_eq((1 µs -> human), "0.000001 seconds") +assert_eq((1 ns -> human), "0.000000001 seconds") +assert_eq((1234 ns -> human), "0.000001234 seconds") +assert_eq((1s + 1234 ns -> human), "1.000001234 seconds") diff --git a/examples/datetime_tests.nbt b/examples/datetime_tests.nbt index 23f1f10a..66f62de9 100644 --- a/examples/datetime_tests.nbt +++ b/examples/datetime_tests.nbt @@ -10,10 +10,10 @@ assert_eq(from_unixtime(1658346725) -> unixtime, 1658346725) # 2020 was a leap year let y = datetime("2020-02-28 20:00 UTC") -assert(format_datetime("%Y/%m/%d", y + 12 hours) == "2020/02/29") +assert_eq(format_datetime("%Y/%m/%d", y + 12 hours), "2020/02/29") let z = datetime("2021-02-28 20:00 UTC") -assert(format_datetime("%Y/%m/%d", z + 12 hours) == "2021/03/01") +assert_eq(format_datetime("%Y/%m/%d", z + 12 hours), "2021/03/01") # Regression test for #376 let dt_376 = datetime("Fri, 23 Feb 2024 14:01:54 -0800") -assert(format_datetime("%Y-%m-%dT%H:%M:%S%:z", dt_376) == "2024-02-23T14:01:54-08:00") +assert_eq(format_datetime("%Y-%m-%dT%H:%M:%S%:z", dt_376), "2024-02-23T14:01:54-08:00") diff --git a/examples/prelude_tests.nbt b/examples/prelude_tests.nbt index c05ac796..3bc36585 100644 --- a/examples/prelude_tests.nbt +++ b/examples/prelude_tests.nbt @@ -57,45 +57,45 @@ assert_eq(acsch(0.3), 1.918896, 1e-5) assert_eq(str_length(""), 0) assert_eq(str_length("foo"), 3) -assert(str_slice("hello world", 0, 5) == "hello") -assert(str_slice("hello world", 6, 11) == "world") -assert(str_slice("hello world", 0, 0) == "") -assert(str_slice("hello world", 0, 100) == "") +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(str_append("foo", "bar") == "foobar") +assert_eq(str_append("foo", "bar"), "foobar") assert(str_contains("hello world", "hello")) assert(str_contains("hello world", "world")) -assert(str_contains("hello world", "HELLO") == false) - -assert(str_replace("hello world", "hello", "HEY") == "HEY world") -assert(str_replace("xxx", "x", "yY") == "yYyYyY") -assert(str_replace("a b", " ", " ") == "a b") - -assert(str_repeat("xy", 3) == "xyxyxy") - -assert((0b0 -> bin) == "0b0") -assert((0b1 -> bin) == "0b1") -assert((0b10 -> bin) == "0b10") -assert((0b11 -> bin) == "0b11") -assert((0b10101010101010101010101010101010 -> bin) == "0b10101010101010101010101010101010") -assert((-0b11110000 -> bin) == "-0b11110000") - -assert((0o0 -> oct) == "0o0") -assert((0o1 -> oct) == "0o1") -assert((0o7 -> oct) == "0o7") -assert((0o10 -> oct) == "0o10") -assert((0o77 -> oct) == "0o77") -assert((0o12345670 -> oct) == "0o12345670") -assert((-0o12345670 -> oct) == "-0o12345670") - -assert((0x0 -> hex) == "0x0") -assert((0x1 -> hex) == "0x1") -assert((0x9 -> hex) == "0x9") -assert((0xa -> hex) == "0xa") -assert((0xf -> hex) == "0xf") -assert((0xabc1234567890 -> hex) == "0xabc1234567890") -assert((-0xc0ffee -> hex) == "-0xc0ffee") +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 From 8c9654f2bf03c44da02d88fedb43ff7d86d6beef Mon Sep 17 00:00:00 2001 From: David Peter Date: Tue, 2 Jul 2024 18:47:39 +0200 Subject: [PATCH 2/3] Fix insta warnings --- numbat/tests/interpreter.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/numbat/tests/interpreter.rs b/numbat/tests/interpreter.rs index 77557852..dd292a79 100644 --- a/numbat/tests/interpreter.rs +++ b/numbat/tests/interpreter.rs @@ -561,14 +561,14 @@ fn test_logical() { expect_output("false || true && !false", "true"); // Errors - insta::assert_display_snapshot!(fail("1 || 2"), @"Expected boolean value"); - insta::assert_display_snapshot!(fail("true || 2"), @"Expected boolean value"); - insta::assert_display_snapshot!(fail("1 || true"), @"Expected boolean value"); - insta::assert_display_snapshot!(fail("1 && 2"), @"Expected boolean value"); - insta::assert_display_snapshot!(fail("true && 2"), @"Expected boolean value"); - insta::assert_display_snapshot!(fail("1 && true"), @"Expected boolean value"); - insta::assert_display_snapshot!(fail("!1"), @"Expected boolean value"); - insta::assert_display_snapshot!(fail("!1 || true"), @"Expected boolean value"); + insta::assert_snapshot!(fail("1 || 2"), @"Expected boolean value"); + insta::assert_snapshot!(fail("true || 2"), @"Expected boolean value"); + insta::assert_snapshot!(fail("1 || true"), @"Expected boolean value"); + insta::assert_snapshot!(fail("1 && 2"), @"Expected boolean value"); + insta::assert_snapshot!(fail("true && 2"), @"Expected boolean value"); + insta::assert_snapshot!(fail("1 && true"), @"Expected boolean value"); + insta::assert_snapshot!(fail("!1"), @"Expected boolean value"); + insta::assert_snapshot!(fail("!1 || true"), @"Expected boolean value"); } #[test] From ad6cc3a2aa9b26b250345181a3fa02bc070af07f Mon Sep 17 00:00:00 2001 From: David Peter Date: Tue, 2 Jul 2024 19:37:30 +0200 Subject: [PATCH 3/3] Restructure Numbat integration tests --- examples/prelude_tests.nbt | 170 --------------- .../assert_eq.nbt} | 2 + examples/tests/base_conversion.nbt | 22 ++ examples/{ => tests}/booleans.nbt | 0 .../chemistry.nbt} | 0 .../codata2018.nbt} | 0 examples/tests/core.nbt | 25 +++ .../datetime.nbt} | 0 .../human.nbt} | 0 examples/{list_tests.nbt => tests/lists.nbt} | 0 examples/tests/math_functions.nbt | 196 ++++++++++++++++++ .../numerics.nbt} | 0 examples/tests/strings.nbt | 19 ++ examples/tests/trigonometry_extra.nbt | 17 ++ .../unit_consistency_misc.nbt} | 0 .../unit_consistency_si.nbt} | 0 examples/time_units.nbt | 1 - numbat/tests/prelude_and_examples.rs | 5 + 18 files changed, 286 insertions(+), 171 deletions(-) delete mode 100644 examples/prelude_tests.nbt rename examples/{assert_eq_success.nbt => tests/assert_eq.nbt} (65%) create mode 100644 examples/tests/base_conversion.nbt rename examples/{ => tests}/booleans.nbt (100%) rename examples/{chemistry_tests.nbt => tests/chemistry.nbt} (100%) rename examples/{codata2018_consistency.nbt => tests/codata2018.nbt} (100%) create mode 100644 examples/tests/core.nbt rename examples/{datetime_tests.nbt => tests/datetime.nbt} (100%) rename examples/{datetime_human_tests.nbt => tests/human.nbt} (100%) rename examples/{list_tests.nbt => tests/lists.nbt} (100%) create mode 100644 examples/tests/math_functions.nbt rename examples/{numerics_tests.nbt => tests/numerics.nbt} (100%) create mode 100644 examples/tests/strings.nbt create mode 100644 examples/tests/trigonometry_extra.nbt rename examples/{consistency_others.nbt => tests/unit_consistency_misc.nbt} (100%) rename examples/{consistency_si.nbt => tests/unit_consistency_si.nbt} (100%) delete mode 100644 examples/time_units.nbt diff --git a/examples/prelude_tests.nbt b/examples/prelude_tests.nbt deleted file mode 100644 index 3bc36585..00000000 --- a/examples/prelude_tests.nbt +++ /dev/null @@ -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) diff --git a/examples/assert_eq_success.nbt b/examples/tests/assert_eq.nbt similarity index 65% rename from examples/assert_eq_success.nbt rename to examples/tests/assert_eq.nbt index 8eab26b8..0684e999 100644 --- a/examples/assert_eq_success.nbt +++ b/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) diff --git a/examples/tests/base_conversion.nbt b/examples/tests/base_conversion.nbt new file mode 100644 index 00000000..fc2bd931 --- /dev/null +++ b/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") diff --git a/examples/booleans.nbt b/examples/tests/booleans.nbt similarity index 100% rename from examples/booleans.nbt rename to examples/tests/booleans.nbt diff --git a/examples/chemistry_tests.nbt b/examples/tests/chemistry.nbt similarity index 100% rename from examples/chemistry_tests.nbt rename to examples/tests/chemistry.nbt diff --git a/examples/codata2018_consistency.nbt b/examples/tests/codata2018.nbt similarity index 100% rename from examples/codata2018_consistency.nbt rename to examples/tests/codata2018.nbt diff --git a/examples/tests/core.nbt b/examples/tests/core.nbt new file mode 100644 index 00000000..4aec8980 --- /dev/null +++ b/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) diff --git a/examples/datetime_tests.nbt b/examples/tests/datetime.nbt similarity index 100% rename from examples/datetime_tests.nbt rename to examples/tests/datetime.nbt diff --git a/examples/datetime_human_tests.nbt b/examples/tests/human.nbt similarity index 100% rename from examples/datetime_human_tests.nbt rename to examples/tests/human.nbt diff --git a/examples/list_tests.nbt b/examples/tests/lists.nbt similarity index 100% rename from examples/list_tests.nbt rename to examples/tests/lists.nbt diff --git a/examples/tests/math_functions.nbt b/examples/tests/math_functions.nbt new file mode 100644 index 00000000..c9e29848 --- /dev/null +++ b/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³) diff --git a/examples/numerics_tests.nbt b/examples/tests/numerics.nbt similarity index 100% rename from examples/numerics_tests.nbt rename to examples/tests/numerics.nbt diff --git a/examples/tests/strings.nbt b/examples/tests/strings.nbt new file mode 100644 index 00000000..d1f4fc80 --- /dev/null +++ b/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") diff --git a/examples/tests/trigonometry_extra.nbt b/examples/tests/trigonometry_extra.nbt new file mode 100644 index 00000000..719863cf --- /dev/null +++ b/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) diff --git a/examples/consistency_others.nbt b/examples/tests/unit_consistency_misc.nbt similarity index 100% rename from examples/consistency_others.nbt rename to examples/tests/unit_consistency_misc.nbt diff --git a/examples/consistency_si.nbt b/examples/tests/unit_consistency_si.nbt similarity index 100% rename from examples/consistency_si.nbt rename to examples/tests/unit_consistency_si.nbt diff --git a/examples/time_units.nbt b/examples/time_units.nbt deleted file mode 100644 index a25ab664..00000000 --- a/examples/time_units.nbt +++ /dev/null @@ -1 +0,0 @@ -assert_eq(2 min + 30 s, 2.5 min, 0.01 s) diff --git a/numbat/tests/prelude_and_examples.rs b/numbat/tests/prelude_and_examples.rs index 0e39b1f1..cc1b3ea5 100644 --- a/numbat/tests/prelude_and_examples.rs +++ b/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);