diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2026-01-11 15:21:11 -0500 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2026-01-11 15:21:11 -0500 |
| commit | bb4c931fc660eca5cc28f637873386bfb3a6b56b (patch) | |
| tree | 6d5c1caf73c63775913001ee71698272c1690225 | |
| parent | 144b93491a76b969e531743f48a21897f9c5e8bc (diff) | |
Added/updated reals test
| -rw-r--r-- | src/compile/text.c | 3 | ||||
| -rw-r--r-- | test/nums.tm | 52 | ||||
| -rw-r--r-- | test/optionals.tm | 28 | ||||
| -rw-r--r-- | test/reals.tm | 140 |
4 files changed, 96 insertions, 127 deletions
diff --git a/src/compile/text.c b/src/compile/text.c index 7f11169b..1a9d492e 100644 --- a/src/compile/text.c +++ b/src/compile/text.c @@ -23,7 +23,8 @@ Text_t expr_as_text(Text_t expr, type_t *t, Text_t color) { case BigIntType: case IntType: case ByteType: - case FloatType: { + case FloatType: + case RealType: { Text_t name = type_to_text(t); return Texts(name, "$as_text(stack(", expr, "), ", color, ", &", name, "$info)"); } diff --git a/test/nums.tm b/test/nums.tm deleted file mode 100644 index 9077a67f..00000000 --- a/test/nums.tm +++ /dev/null @@ -1,52 +0,0 @@ -func main() - n := 1.5 - assert n == 1.5 - - assert n + n == 3. - - assert n * 2 == 3. - - assert n - n == 0. - - assert Float64.PI == 3.141592653589793 - - assert Float64.PI.with_precision(0.01) == 3.14 - - assert Float64.INF == Float64.INF - assert Float64.INF.isinf() - - none_num : Float64? = none - assert none_num == none - assert none_num == none_num - assert (none_num < none_num) == no - assert (none_num > none_num) == no - assert (none_num != none_num) == no - assert (none_num <> none_num) == Int32(0) - assert (none_num == 0.0) == no - assert none_num < 0.0 - assert (none_num > 0.0) == no - assert none_num != 0.0 - assert (none_num <> 0.0) == Int32(-1) - - # >> nan + 1 - # = none - - >> 0./0. - - # >> 0./0. - # = none - - assert Float64.PI.cos()!.near(-1) - assert Float64.PI.sin()!.near(0) - - assert Float64.INF.near(-Float64.INF) == no - - assert Float32.sqrt(16) == Float32(4) - assert Float32.sqrt(-1) == none - - assert (0.25).mix(10, 20) == 12.5 - assert (2.0).mix(10, 20) == 30. - - assert Float64(5) == 5. - - assert (0.5).percent() == "50%" diff --git a/test/optionals.tm b/test/optionals.tm index 53741748..db8c41e6 100644 --- a/test/optionals.tm +++ b/test/optionals.tm @@ -43,7 +43,13 @@ func maybe_text(should_i:Bool->Text?) else return none -func maybe_num(should_i:Bool->Float64?) +func maybe_float(should_i:Bool->Float64?) + if should_i + return Float64(12.3) + else + return none + +func maybe_real(should_i:Bool->Real?) if should_i return 12.3 else @@ -157,10 +163,24 @@ func main() do say("...") - say("Nums:") - yep := maybe_num(yes) + say("Floats:") + yep := maybe_float(yes) + assert yep == Float64(12.3) + nope := maybe_float(no) + assert nope == none + >> if yep + assert yep == Float64(12.3) + else fail("Falsey: $yep") + >> if nope + fail("Truthy: $nope") + else say("Falsey: $nope") + + do + say("...") + say("Reals:") + yep := maybe_real(yes) assert yep == 12.3 - nope := maybe_num(no) + nope := maybe_float(no) assert nope == none >> if yep assert yep == 12.3 diff --git a/test/reals.tm b/test/reals.tm index dae4a465..fc28b474 100644 --- a/test/reals.tm +++ b/test/reals.tm @@ -1,119 +1,119 @@ func main() # Basic arithmetic - n := Real(1.5) + n := 1.5 assert n == 1.5 assert n + n == 3.0 - assert n * 2 == 3.0 + assert n * 2. == 3.0 assert n - n == 0.0 - assert n / 2 == 0.75 + assert n / 2. == 0.75 # Exact rational arithmetic - third := Real(1) / Real(3) - assert third * 3 == 1 - assert (third + third + third) == 1 + third := 1. / 3. + assert third * 3. == 1. + assert (third + third + third) == 1. # Decimal representation - assert Real(0.1) + Real(0.2) == Real(0.3) - assert (Real(0.1) + Real(0.2)).value_as_text() == "0.3" + assert 0.1 + 0.2 == 0.3 + assert Text(0.1 + 0.2) == "0.3" # Fraction representation - assert (Real(1) / Real(3)).value_as_text() == "1/3" - assert (Real(5) / Real(7)).value_as_text() == "5/7" + assert Text(1. / 3.) == "1/3" + assert Text(5. / 7.) == "5/7" # Terminating decimals - assert (Real(3) / Real(4)).value_as_text() == "0.75" - assert (Real(1) / Real(8)).value_as_text() == "0.125" + assert Text(3. / 4.) == "0.75" + assert Text(1. / 8.) == "0.125" # Square root - assert Real(4).sqrt() == 2 - assert Real(9).sqrt() == 3 - assert (Real(1) / Real(4)).sqrt() == Real(1) / Real(2) + assert (4.).sqrt() == 2. + assert (9.).sqrt() == 3. + assert (1. / 4.).sqrt() == 1. / 2. # Symbolic sqrt simplification - sqrt2 := Real(2).sqrt() - assert (sqrt2 * sqrt2) == 2 + sqrt2 := (2.).sqrt() + assert (sqrt2 * sqrt2) == 2. # Rounding assert sqrt2.rounded_to(0.01) == 1.41 assert sqrt2.rounded_to(0.001) == 1.414 - assert Real(1234).rounded_to(100) == 1200 - assert Real(1250).rounded_to(100) == 1300 + assert (1234.).rounded_to(100.) == 1200. + assert (1250.).rounded_to(100.) == 1300. # Trigonometric functions - assert Real(0).sin().value_as_text() == "0" - assert Real(0).cos().value_as_text() == "1" - assert Real(0).tan().value_as_text() == "0" + assert (0.).sin() == 0. + assert (0.).cos() == 1. + assert (0.).tan() == 0. # Exponential and logarithm - assert Real(0).exp() == 1 + assert (0.).exp() == 1. # Absolute value - assert Real(-5).abs() == 5 - assert Real(5).abs() == 5 - assert (Real(-3) / Real(4)).abs() == Real(3) / Real(4) + assert (-5.).abs() == 5. + assert (5.).abs() == 5. + assert (-3. / 4.).abs() == 3. / 4. # Floor and ceiling - assert Real(2.7).floor() == 2 - assert Real(2.3).floor() == 2 - assert Real(-2.3).floor() == -3 - assert Real(2.3).ceil() == 3 - assert Real(2.7).ceil() == 3 - assert Real(-2.3).ceil() == -2 + assert (2.7).floor() == 2. + assert (2.3).floor() == 2. + assert (-2.3).floor() == -3. + assert (2.3).ceil() == 3. + assert (2.7).ceil() == 3. + assert (-2.3).ceil() == -2. # Floor/ceil on rationals - assert (Real(7) / Real(3)).floor() == 2 - assert (Real(7) / Real(3)).ceil() == 3 - assert (Real(-7) / Real(3)).floor() == -3 - assert (Real(-7) / Real(3)).ceil() == -2 + assert (7. / 3.).floor() == 2. + assert (7. / 3.).ceil() == 3. + assert (-7. / 3.).floor() == -3. + assert (-7. / 3.).ceil() == -2. # Modulo - assert Real(7).mod(3) == 1 - assert Real(-7).mod(3) == 2 # Euclidean division - assert Real(7).mod(-3) == -2 - assert Real(2.5).mod(1.0) == 0.5 + assert 7. mod 3. == 1. + assert -7. mod 3. == 2. # Euclidean division + assert 7. mod -3. == -2. + assert 2.5 mod 1.0 == 0.5 # Mod1 - assert Real(3.7).mod1(2) == 1.7 + assert 3.7 mod1 2. == 1.7 # Mix (linear interpolation) - assert Real(0.25).mix(10, 20) == 12.5 - assert Real(0.0).mix(10, 20) == 10 - assert Real(1.0).mix(10, 20) == 20 - assert Real(0.5).mix(10, 20) == 15 + assert (0.25).mix(10., 20.) == 12.5 + assert (0.0).mix(10., 20.) == 10. + assert (1.0).mix(10., 20.) == 20. + assert (0.5).mix(10., 20.) == 15. # Is between - assert Real(5).is_between(1, 10) - assert Real(1).is_between(1, 10) - assert Real(10).is_between(1, 10) - assert Real(0).is_between(1, 10) == no - assert Real(11).is_between(1, 10) == no + assert (5.).is_between(1, 10) + assert (1.).is_between(1, 10) + assert (10.).is_between(1, 10) + assert (0.).is_between(1, 10) == no + assert (11.).is_between(1, 10) == no # Clamped - assert Real(5).clamped(1, 10) == 5 - assert Real(0).clamped(1, 10) == 1 - assert Real(15).clamped(1, 10) == 10 - assert Real(1).clamped(1, 10) == 1 - assert Real(10).clamped(1, 10) == 10 + assert (5.).clamped(1, 10) == 5 + assert (0.).clamped(1, 10) == 1 + assert (15.).clamped(1, 10) == 10 + assert (1.).clamped(1, 10) == 1 + assert (10.).clamped(1, 10) == 10 # Comparison - assert Real(3) > Real(2) - assert Real(2) < Real(3) - assert Real(3) >= Real(3) - assert Real(3) <= Real(3) - assert (Real(1) / Real(3)) < (Real(1) / Real(2)) + assert 3. > 2. + assert 2. < 3. + assert 3. >= 3. + assert 3. <= 3. + assert (1. / 3.) < (1. / 2.) # Negation - assert -Real(5) == Real(-5) - assert -(Real(1) / Real(3)) == Real(-1) / Real(3) + assert -(5.) == -5. + assert -(1. / 3.) == -1. / 3. # Power - assert Real(2).power(3) == 8 - assert Real(2).power(0) == 1 + assert (2.).power(3) == 8 + assert (2.).power(0) == 1 # Parsing assert Real.parse("123") == 123 assert Real.parse("1.5") == 1.5 - assert Real.parse("0.3333333333333333") == Real(0.3333333333333333) + assert Real.parse("0.3333333333333333") == 0.3333333333333333 assert Real.parse("-5") == -5 assert Real.parse("-2.5") == -2.5 @@ -124,15 +124,15 @@ func main() assert Real.parse("") == none # Large integers - big := Real(9999999999999999) - assert big + 1 == Real(10000000000000000) + big := (9999999999999999.) + assert big + 1 == (10000000000000000.) # Mixed operations preserve exactness - result := Real(1) / Real(3) + Real(2) / Real(3) + result := 1. / 3. + 2. / 3. assert result == 1 # Symbolic expressions maintain structure - expr := sqrt2 + Real(1) - assert expr.value_as_text().contains("sqrt") + expr := sqrt2 + 1. + assert Text(expr) == "sqrt(2) + 1" print("All Real tests passed!") |
