|
| 1 | +package float16 |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | +) |
| 6 | + |
| 7 | +func TestAddWithMode(t *testing.T) { |
| 8 | + tests := []struct { |
| 9 | + name string |
| 10 | + a Float16 |
| 11 | + b Float16 |
| 12 | + mode ArithmeticMode |
| 13 | + rounding RoundingMode |
| 14 | + expect Float16 |
| 15 | + hasError bool |
| 16 | + }{ |
| 17 | + // Basic addition |
| 18 | + {"1.0 + 2.0", 0x3C00, 0x4000, ModeIEEEArithmetic, RoundNearestEven, 0x4200, false}, // 1.0 + 2.0 = 3.0 |
| 19 | + {"0.5 + 0.25", 0x3800, 0x3400, ModeIEEEArithmetic, RoundNearestEven, 0x3A00, false}, // 0.5 + 0.25 = 0.75 (0x3A00) |
| 20 | + |
| 21 | + // Special cases |
| 22 | + {"0 + x", 0x0000, 0x3C00, ModeIEEEArithmetic, RoundNearestEven, 0x3C00, false}, // 0 + 1.0 = 1.0 |
| 23 | + {"x + 0", 0x3C00, 0x0000, ModeIEEEArithmetic, RoundNearestEven, 0x3C00, false}, // 1.0 + 0 = 1.0 |
| 24 | + |
| 25 | + // Infinity handling |
| 26 | + {"Inf + 1", 0x7C00, 0x3C00, ModeIEEEArithmetic, RoundNearestEven, 0x7C00, false}, // +Inf + 1 = +Inf |
| 27 | + {"1 + Inf", 0x3C00, 0x7C00, ModeIEEEArithmetic, RoundNearestEven, 0x7C00, false}, // 1 + Inf = +Inf |
| 28 | + {"-Inf + Inf", 0xFC00, 0x7C00, ModeIEEEArithmetic, RoundNearestEven, 0x7E00, false}, // -Inf + Inf = NaN |
| 29 | + |
| 30 | + // NaN handling |
| 31 | + {"NaN + 1", 0x7E00, 0x3C00, ModeIEEEArithmetic, RoundNearestEven, 0x7E00, false}, // NaN + 1 = NaN |
| 32 | + {"1 + NaN", 0x3C00, 0x7E00, ModeIEEEArithmetic, RoundNearestEven, 0x7E00, false}, // 1 + NaN = NaN |
| 33 | + |
| 34 | + // Exact mode |
| 35 | + {"1.0 + 2.0 (exact)", 0x3C00, 0x4000, ModeExactArithmetic, RoundNearestEven, 0x4200, false}, // 1.0 + 2.0 = 3.0 (exact) |
| 36 | + {"0.1 + 0.2 (exact)", 0x2E66, 0x3266, ModeExactArithmetic, RoundNearestEven, 0x34CC, false}, // 0.1 + 0.2 = ~0.2998 (actual float16 result) |
| 37 | + |
| 38 | + // Rounding modes |
| 39 | + {"1.0 + 0.5 (toward zero)", 0x3C00, 0x3800, ModeIEEEArithmetic, RoundTowardZero, 0x3E00, false}, // 1.0 + 0.5 = 1.5 (0x3E00) |
| 40 | + } |
| 41 | + |
| 42 | + for _, tt := range tests { |
| 43 | + t.Run(tt.name, func(t *testing.T) { |
| 44 | + result, err := AddWithMode(tt.a, tt.b, tt.mode, tt.rounding) |
| 45 | + |
| 46 | + if tt.hasError { |
| 47 | + if err == nil { |
| 48 | + t.Errorf("Expected error, got nil") |
| 49 | + } |
| 50 | + return |
| 51 | + } |
| 52 | + |
| 53 | + if err != nil { |
| 54 | + t.Fatalf("Unexpected error: %v", err) |
| 55 | + } |
| 56 | + |
| 57 | + // For better debugging, show float32 values when the test fails |
| 58 | + if result != tt.expect { |
| 59 | + t.Errorf("AddWithMode(%v, %v) = %v (0x%04X, %f), want %v (0x%04X, %f)", |
| 60 | + tt.a, tt.b, result, uint16(result), result.ToFloat32(), |
| 61 | + tt.expect, uint16(tt.expect), tt.expect.ToFloat32()) |
| 62 | + } |
| 63 | + }) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +func TestAddWithMode_ErrorCases(t *testing.T) { |
| 68 | + tests := []struct { |
| 69 | + name string |
| 70 | + a Float16 |
| 71 | + b Float16 |
| 72 | + mode ArithmeticMode |
| 73 | + expect Float16 |
| 74 | + errCode ErrorCode |
| 75 | + }{ |
| 76 | + {"NaN in exact mode", 0x7E00, 0x3C00, ModeExactArithmetic, 0, ErrNaN}, |
| 77 | + {"Inf-Inf in exact mode", 0x7C00, 0xFC00, ModeExactArithmetic, 0, ErrInvalidOperation}, |
| 78 | + } |
| 79 | + |
| 80 | + for _, tt := range tests { |
| 81 | + t.Run(tt.name, func(t *testing.T) { |
| 82 | + result, err := AddWithMode(tt.a, tt.b, tt.mode, RoundNearestEven) |
| 83 | + |
| 84 | + if err == nil { |
| 85 | + t.Fatalf("Expected error, got nil") |
| 86 | + } |
| 87 | + |
| 88 | + err16, ok := err.(*Float16Error) |
| 89 | + if !ok { |
| 90 | + t.Fatalf("Expected Float16Error, got %T", err) |
| 91 | + } |
| 92 | + |
| 93 | + if err16.Code != tt.errCode { |
| 94 | + t.Errorf("Expected error code %v, got %v", tt.errCode, err16.Code) |
| 95 | + } |
| 96 | + |
| 97 | + if result != tt.expect { |
| 98 | + t.Errorf("Expected result %v, got %v", tt.expect, result) |
| 99 | + } |
| 100 | + }) |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +func TestAdd(t *testing.T) { |
| 105 | + tests := []struct { |
| 106 | + a Float16 |
| 107 | + b Float16 |
| 108 | + expect Float16 |
| 109 | + }{ |
| 110 | + {0x3C00, 0x4000, 0x4200}, // 1.0 + 2.0 = 3.0 |
| 111 | + {0x3800, 0x3400, 0x3A00}, // 0.5 + 0.25 = 0.75 (0x3A00) |
| 112 | + {0x7C00, 0x3C00, 0x7C00}, // +Inf + 1 = +Inf |
| 113 | + {0x3C00, 0x0000, 0x3C00}, // 1.0 + 0.0 = 1.0 |
| 114 | + {0x0000, 0x3C00, 0x3C00}, // 0.0 + 1.0 = 1.0 |
| 115 | + {0x3C00, 0xBC00, 0x0000}, // 1.0 + (-1.0) = 0.0 |
| 116 | + } |
| 117 | + |
| 118 | + for _, tt := range tests { |
| 119 | + t.Run(tt.a.String()+" + "+tt.b.String(), func(t *testing.T) { |
| 120 | + result := Add(tt.a, tt.b) |
| 121 | + if result != tt.expect { |
| 122 | + t.Errorf("Add(%v, %v) = %v (0x%04X), want %v (0x%04X)", |
| 123 | + tt.a, tt.b, result, uint16(result), tt.expect, uint16(tt.expect)) |
| 124 | + } |
| 125 | + }) |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +func TestSubWithModeBasic(t *testing.T) { |
| 130 | + tests := []struct { |
| 131 | + name string |
| 132 | + a Float16 |
| 133 | + b Float16 |
| 134 | + expect Float16 |
| 135 | + hasError bool |
| 136 | + }{ |
| 137 | + {name: "1.0 - 0.5", a: 0x3C00, b: 0x3800, expect: 0x3800, hasError: false}, // 1.0 - 0.5 = 0.5 |
| 138 | + {name: "0.5 - 0.25", a: 0x3800, b: 0x3400, expect: 0x3400, hasError: false}, // 0.5 - 0.25 = 0.25 |
| 139 | + {name: "1.0 - 1.0", a: 0x3C00, b: 0x3C00, expect: 0x0000, hasError: false}, // 1.0 - 1.0 = 0.0 |
| 140 | + {name: "1.0 - -1.0", a: 0x3C00, b: 0xBC00, expect: 0x4000, hasError: false}, // 1.0 - (-1.0) = 2.0 |
| 141 | + {name: "-1.0 - 1.0", a: 0xBC00, b: 0x3C00, expect: 0xC000, hasError: false}, // -1.0 - 1.0 = -2.0 |
| 142 | + {name: "0.0 - 0.0", a: 0x0000, b: 0x0000, expect: 0x0000, hasError: false}, // 0.0 - 0.0 = 0.0 (or -0.0 is also valid) |
| 143 | + {name: "Inf - 1.0", a: 0x7C00, b: 0x3C00, expect: 0x7C00, hasError: false}, // +Inf - 1.0 = +Inf |
| 144 | + {name: "1.0 - Inf", a: 0x3C00, b: 0x7C00, expect: 0xFC00, hasError: false}, // 1.0 - +Inf = -Inf |
| 145 | + {name: "NaN - 1.0", a: 0x7E00, b: 0x3C00, expect: 0x7E00, hasError: false}, // NaN - 1.0 = NaN |
| 146 | + {name: "1.0 - NaN", a: 0x3C00, b: 0x7E00, expect: 0x7E00, hasError: false}, // 1.0 - NaN = NaN |
| 147 | + } |
| 148 | + |
| 149 | + for _, tt := range tests { |
| 150 | + t.Run(tt.name, func(t *testing.T) { |
| 151 | + result, err := SubWithMode(tt.a, tt.b, ModeIEEEArithmetic, RoundNearestEven) |
| 152 | + |
| 153 | + if tt.hasError { |
| 154 | + if err == nil { |
| 155 | + t.Error("Expected error, got nil") |
| 156 | + } |
| 157 | + return |
| 158 | + } |
| 159 | + |
| 160 | + if err != nil { |
| 161 | + t.Fatalf("Unexpected error: %v", err) |
| 162 | + } |
| 163 | + |
| 164 | + // For 0.0 - 0.0, both 0.0 and -0.0 are valid results |
| 165 | + if tt.a == 0 && tt.b == 0 { |
| 166 | + // Check if the result is either 0.0 or -0.0 |
| 167 | + if result != 0 && result != Float16(0x8000) { |
| 168 | + t.Errorf("SubWithMode(%v, %v) = %v (0x%04X), want 0.0 (0x0000) or -0.0 (0x8000)", |
| 169 | + tt.a, tt.b, result, uint16(result)) |
| 170 | + } |
| 171 | + } else if result != tt.expect { |
| 172 | + t.Errorf("SubWithMode(%v, %v) = %v (0x%04X), want %v (0x%04X)", |
| 173 | + tt.a, tt.b, result, uint16(result), tt.expect, uint16(tt.expect)) |
| 174 | + } |
| 175 | + }) |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +func TestSub(t *testing.T) { |
| 180 | + tests := []struct { |
| 181 | + a Float16 |
| 182 | + b Float16 |
| 183 | + expect Float16 |
| 184 | + }{ |
| 185 | + {0x4200, 0x3C00, 0x4000}, // 3.0 - 1.0 = 2.0 |
| 186 | + {0x3C00, 0x3C00, 0x0000}, // 1.0 - 1.0 = 0.0 |
| 187 | + {0x3C00, 0x4000, 0xBC00}, // 1.0 - 2.0 = -1.0 |
| 188 | + } |
| 189 | + |
| 190 | + for _, tt := range tests { |
| 191 | + t.Run(tt.a.String()+" - "+tt.b.String(), func(t *testing.T) { |
| 192 | + result := Sub(tt.a, tt.b) |
| 193 | + if result != tt.expect { |
| 194 | + t.Errorf("Sub(%v, %v) = %v (0x%04X), want %v (0x%04X)", |
| 195 | + tt.a, tt.b, result, uint16(result), tt.expect, uint16(tt.expect)) |
| 196 | + } |
| 197 | + }) |
| 198 | + } |
| 199 | +} |
0 commit comments