|
| 1 | +# Tests for symbolic comparison operators (064-symbolic-comparison-operators) |
| 2 | +# This file tests <, >, <=, >= operators returning GiacExpr for symbolic inequalities. |
| 3 | + |
| 4 | +using Test |
| 5 | +using Giac |
| 6 | +using Giac.Commands: assume, about, sign, purge, additionally |
| 7 | + |
| 8 | +@testset "Comparison Operators (064-symbolic-comparison-operators)" begin |
| 9 | + |
| 10 | + # ======================================================================== |
| 11 | + # User Story 1: Build symbolic inequalities (Priority: P1) - MVP |
| 12 | + # ======================================================================== |
| 13 | + |
| 14 | + @testset "US1: Less-than operator (<) between GiacExpr" begin |
| 15 | + x = giac_eval("x") |
| 16 | + y = giac_eval("y") |
| 17 | + result = x < y |
| 18 | + @test result isa GiacExpr |
| 19 | + # GIAC may normalize x<y to y>x — check both representations |
| 20 | + s = string(result) |
| 21 | + @test occursin("<", s) || occursin(">", s) |
| 22 | + end |
| 23 | + |
| 24 | + @testset "US1: Greater-than operator (>) between GiacExpr" begin |
| 25 | + x = giac_eval("x") |
| 26 | + y = giac_eval("y") |
| 27 | + result = x > y |
| 28 | + @test result isa GiacExpr |
| 29 | + @test occursin(">", string(result)) |
| 30 | + end |
| 31 | + |
| 32 | + @testset "US1: Less-than-or-equal operator (<=) between GiacExpr" begin |
| 33 | + x = giac_eval("x") |
| 34 | + y = giac_eval("y") |
| 35 | + result = x <= y |
| 36 | + @test result isa GiacExpr |
| 37 | + # GIAC may normalize x<=y to y>=x — check both representations |
| 38 | + s = string(result) |
| 39 | + @test occursin("<=", s) || occursin(">=", s) |
| 40 | + end |
| 41 | + |
| 42 | + @testset "US1: Greater-than-or-equal operator (>=) between GiacExpr" begin |
| 43 | + x = giac_eval("x") |
| 44 | + y = giac_eval("y") |
| 45 | + result = x >= y |
| 46 | + @test result isa GiacExpr |
| 47 | + @test occursin(">=", string(result)) |
| 48 | + end |
| 49 | + |
| 50 | + @testset "US1: Regression — existing operators unaffected" begin |
| 51 | + x = giac_eval("x") |
| 52 | + y = giac_eval("y") |
| 53 | + |
| 54 | + # == still returns Bool |
| 55 | + @test (x == x) isa Bool |
| 56 | + @test x == x |
| 57 | + @test !(x == y) |
| 58 | + |
| 59 | + # ~ still returns GiacExpr |
| 60 | + @test (x ~ y) isa GiacExpr |
| 61 | + |
| 62 | + # hash still works for Dict/Set |
| 63 | + d = Dict(x => 1, y => 2) |
| 64 | + @test d[x] == 1 |
| 65 | + @test d[y] == 2 |
| 66 | + s = Set([x, y]) |
| 67 | + @test length(s) == 2 |
| 68 | + end |
| 69 | + |
| 70 | + # ======================================================================== |
| 71 | + # User Story 2: Use with assume and additionally (Priority: P1) |
| 72 | + # ======================================================================== |
| 73 | + |
| 74 | + @testset "US2: assume(x > 0) sets assumption" begin |
| 75 | + x = giac_eval("x") |
| 76 | + assume(x > 0) |
| 77 | + @test sign(x) == giac_eval("1") |
| 78 | + about_str = string(about(x)) |
| 79 | + @test occursin("assume", about_str) |
| 80 | + purge(x) |
| 81 | + end |
| 82 | + |
| 83 | + @testset "US2: purge clears assumption" begin |
| 84 | + x = giac_eval("x") |
| 85 | + assume(x > 0) |
| 86 | + purge(x) |
| 87 | + @test string(about(x)) == "x" |
| 88 | + end |
| 89 | + |
| 90 | + @testset "US2: assume + additionally for interval constraints" begin |
| 91 | + x = giac_eval("x") |
| 92 | + assume(x > giac_eval("-1")) |
| 93 | + additionally(x < giac_eval("1")) |
| 94 | + about_str = string(about(x)) |
| 95 | + @test occursin("assume", about_str) |
| 96 | + purge(x) |
| 97 | + end |
| 98 | + |
| 99 | + # ======================================================================== |
| 100 | + # User Story 3: Mixed-type comparisons (Priority: P2) |
| 101 | + # ======================================================================== |
| 102 | + |
| 103 | + @testset "US3: GiacExpr vs Int" begin |
| 104 | + x = giac_eval("x") |
| 105 | + @test (x > 0) isa GiacExpr |
| 106 | + @test (x < 0) isa GiacExpr |
| 107 | + @test (x >= 1) isa GiacExpr |
| 108 | + @test (x <= -1) isa GiacExpr |
| 109 | + end |
| 110 | + |
| 111 | + @testset "US3: Int vs GiacExpr" begin |
| 112 | + x = giac_eval("x") |
| 113 | + @test (0 < x) isa GiacExpr |
| 114 | + @test (0 > x) isa GiacExpr |
| 115 | + @test (1 <= x) isa GiacExpr |
| 116 | + @test (-1 >= x) isa GiacExpr |
| 117 | + end |
| 118 | + |
| 119 | + @testset "US3: GiacExpr vs Float64" begin |
| 120 | + x = giac_eval("x") |
| 121 | + @test (x > 1.5) isa GiacExpr |
| 122 | + @test (x < 1.5) isa GiacExpr |
| 123 | + end |
| 124 | + |
| 125 | + @testset "US3: GiacExpr vs Rational" begin |
| 126 | + x = giac_eval("x") |
| 127 | + @test (x > 1//2) isa GiacExpr |
| 128 | + @test (x >= 1//3) isa GiacExpr |
| 129 | + end |
| 130 | + |
| 131 | + @testset "US3: GiacExpr vs Irrational" begin |
| 132 | + x = giac_eval("x") |
| 133 | + @test (x > π) isa GiacExpr |
| 134 | + @test (x <= ℯ) isa GiacExpr |
| 135 | + end |
| 136 | + |
| 137 | + @testset "US3: Edge cases" begin |
| 138 | + x = giac_eval("x") |
| 139 | + |
| 140 | + # Self-comparison returns GiacExpr (not Bool) |
| 141 | + result = x > x |
| 142 | + @test result isa GiacExpr |
| 143 | + |
| 144 | + # Infinity comparisons |
| 145 | + @test (x > Inf) isa GiacExpr |
| 146 | + @test (x < -Inf) isa GiacExpr |
| 147 | + end |
| 148 | + |
| 149 | + # ======================================================================== |
| 150 | + # Logical operators & and | for combining inequalities |
| 151 | + # ======================================================================== |
| 152 | + |
| 153 | + @testset "Logical AND (&) between GiacExpr" begin |
| 154 | + x = giac_eval("x") |
| 155 | + result = (x > 0) & (x < 10) |
| 156 | + @test result isa GiacExpr |
| 157 | + s = string(result) |
| 158 | + @test occursin("and", s) |
| 159 | + end |
| 160 | + |
| 161 | + @testset "Logical OR (|) between GiacExpr" begin |
| 162 | + x = giac_eval("x") |
| 163 | + result = (x < -1) | (x > 1) |
| 164 | + @test result isa GiacExpr |
| 165 | + s = string(result) |
| 166 | + @test occursin("or", s) |
| 167 | + end |
| 168 | + |
| 169 | + @testset "assume with interval via & operator" begin |
| 170 | + x = giac_eval("x") |
| 171 | + assume((x > 0) & (x < 10)) |
| 172 | + @test sign(x) == giac_eval("1") |
| 173 | + about_str = string(about(x)) |
| 174 | + @test occursin("assume", about_str) |
| 175 | + purge(x) |
| 176 | + end |
| 177 | + |
| 178 | +end |
0 commit comments