| Sketch🕴️ | 586f28e | 2026-02-28 19:14:46 +0400 | [diff] [blame] | 1 | package evaluator |
| 2 | |
| 3 | import ( |
| 4 | "math" |
| 5 | "matheval/ast" |
| 6 | "matheval/token" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | func TestEvalNumberLit(t *testing.T) { |
| 11 | result, err := Eval(&ast.NumberLit{Value: 42.5}) |
| 12 | if err != nil { |
| 13 | t.Fatalf("unexpected error: %v", err) |
| 14 | } |
| 15 | if result != 42.5 { |
| 16 | t.Fatalf("expected 42.5, got %v", result) |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | func TestEvalAddition(t *testing.T) { |
| 21 | node := &ast.BinaryExpr{ |
| 22 | Op: token.Plus, |
| 23 | Left: &ast.NumberLit{Value: 1}, |
| 24 | Right: &ast.NumberLit{Value: 2}, |
| 25 | } |
| 26 | result, err := Eval(node) |
| 27 | if err != nil { |
| 28 | t.Fatalf("unexpected error: %v", err) |
| 29 | } |
| 30 | if result != 3 { |
| 31 | t.Fatalf("expected 3, got %v", result) |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | func TestEvalSubtraction(t *testing.T) { |
| 36 | node := &ast.BinaryExpr{ |
| 37 | Op: token.Minus, |
| 38 | Left: &ast.NumberLit{Value: 10}, |
| 39 | Right: &ast.NumberLit{Value: 4}, |
| 40 | } |
| 41 | result, err := Eval(node) |
| 42 | if err != nil { |
| 43 | t.Fatalf("unexpected error: %v", err) |
| 44 | } |
| 45 | if result != 6 { |
| 46 | t.Fatalf("expected 6, got %v", result) |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | func TestEvalMultiplication(t *testing.T) { |
| 51 | node := &ast.BinaryExpr{ |
| 52 | Op: token.Star, |
| 53 | Left: &ast.NumberLit{Value: 3}, |
| 54 | Right: &ast.NumberLit{Value: 7}, |
| 55 | } |
| 56 | result, err := Eval(node) |
| 57 | if err != nil { |
| 58 | t.Fatalf("unexpected error: %v", err) |
| 59 | } |
| 60 | if result != 21 { |
| 61 | t.Fatalf("expected 21, got %v", result) |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | func TestEvalDivision(t *testing.T) { |
| 66 | node := &ast.BinaryExpr{ |
| 67 | Op: token.Slash, |
| 68 | Left: &ast.NumberLit{Value: 10}, |
| 69 | Right: &ast.NumberLit{Value: 4}, |
| 70 | } |
| 71 | result, err := Eval(node) |
| 72 | if err != nil { |
| 73 | t.Fatalf("unexpected error: %v", err) |
| 74 | } |
| 75 | if result != 2.5 { |
| 76 | t.Fatalf("expected 2.5, got %v", result) |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | func TestEvalDivisionByZero(t *testing.T) { |
| 81 | node := &ast.BinaryExpr{ |
| 82 | Op: token.Slash, |
| 83 | Left: &ast.NumberLit{Value: 5}, |
| 84 | Right: &ast.NumberLit{Value: 0}, |
| 85 | } |
| 86 | _, err := Eval(node) |
| 87 | if err == nil { |
| 88 | t.Fatal("expected division by zero error") |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | func TestEvalNestedExpr(t *testing.T) { |
| 93 | // (1 + 2) * (8 / 4) = 3 * 2 = 6 |
| 94 | node := &ast.BinaryExpr{ |
| 95 | Op: token.Star, |
| 96 | Left: &ast.BinaryExpr{ |
| 97 | Op: token.Plus, |
| 98 | Left: &ast.NumberLit{Value: 1}, |
| 99 | Right: &ast.NumberLit{Value: 2}, |
| 100 | }, |
| 101 | Right: &ast.BinaryExpr{ |
| 102 | Op: token.Slash, |
| 103 | Left: &ast.NumberLit{Value: 8}, |
| 104 | Right: &ast.NumberLit{Value: 4}, |
| 105 | }, |
| 106 | } |
| 107 | result, err := Eval(node) |
| 108 | if err != nil { |
| 109 | t.Fatalf("unexpected error: %v", err) |
| 110 | } |
| 111 | if result != 6 { |
| 112 | t.Fatalf("expected 6, got %v", result) |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | func TestEvalDeeplyNested(t *testing.T) { |
| 117 | // ((2 + 3) * 4) - (10 / 5) = 20 - 2 = 18 |
| 118 | node := &ast.BinaryExpr{ |
| 119 | Op: token.Minus, |
| 120 | Left: &ast.BinaryExpr{ |
| 121 | Op: token.Star, |
| 122 | Left: &ast.BinaryExpr{ |
| 123 | Op: token.Plus, |
| 124 | Left: &ast.NumberLit{Value: 2}, |
| 125 | Right: &ast.NumberLit{Value: 3}, |
| 126 | }, |
| 127 | Right: &ast.NumberLit{Value: 4}, |
| 128 | }, |
| 129 | Right: &ast.BinaryExpr{ |
| 130 | Op: token.Slash, |
| 131 | Left: &ast.NumberLit{Value: 10}, |
| 132 | Right: &ast.NumberLit{Value: 5}, |
| 133 | }, |
| 134 | } |
| 135 | result, err := Eval(node) |
| 136 | if err != nil { |
| 137 | t.Fatalf("unexpected error: %v", err) |
| 138 | } |
| 139 | if result != 18 { |
| 140 | t.Fatalf("expected 18, got %v", result) |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | func TestEvalDivisionByZeroInSubExpr(t *testing.T) { |
| 145 | // 1 + (2 / 0) — error should propagate |
| 146 | node := &ast.BinaryExpr{ |
| 147 | Op: token.Plus, |
| 148 | Left: &ast.NumberLit{Value: 1}, |
| 149 | Right: &ast.BinaryExpr{ |
| 150 | Op: token.Slash, |
| 151 | Left: &ast.NumberLit{Value: 2}, |
| 152 | Right: &ast.NumberLit{Value: 0}, |
| 153 | }, |
| 154 | } |
| 155 | _, err := Eval(node) |
| 156 | if err == nil { |
| 157 | t.Fatal("expected division by zero error from sub-expression") |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | func TestEvalFloatingPoint(t *testing.T) { |
| 162 | // 1.5 + 2.3 = 3.8 |
| 163 | node := &ast.BinaryExpr{ |
| 164 | Op: token.Plus, |
| 165 | Left: &ast.NumberLit{Value: 1.5}, |
| 166 | Right: &ast.NumberLit{Value: 2.3}, |
| 167 | } |
| 168 | result, err := Eval(node) |
| 169 | if err != nil { |
| 170 | t.Fatalf("unexpected error: %v", err) |
| 171 | } |
| 172 | if math.Abs(result-3.8) > 1e-12 { |
| 173 | t.Fatalf("expected 3.8, got %v", result) |
| 174 | } |
| 175 | } |