|
| 1 | +package gp |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | +) |
| 6 | + |
| 7 | +func TestFloat(t *testing.T) { |
| 8 | + t.Run("MakeFloat and conversions", func(t *testing.T) { |
| 9 | + // Test creating float and converting back |
| 10 | + f := MakeFloat(3.14159) |
| 11 | + |
| 12 | + // Test Float64 conversion |
| 13 | + if got := f.Float64(); got != 3.14159 { |
| 14 | + t.Errorf("Float64() = %v, want %v", got, 3.14159) |
| 15 | + } |
| 16 | + |
| 17 | + // Test Float32 conversion |
| 18 | + if got := f.Float32(); float64(got) != float64(float32(3.14159)) { |
| 19 | + t.Errorf("Float32() = %v, want %v", got, float32(3.14159)) |
| 20 | + } |
| 21 | + }) |
| 22 | + |
| 23 | + t.Run("IsInteger", func(t *testing.T) { |
| 24 | + // Test integer float |
| 25 | + intFloat := MakeFloat(5.0) |
| 26 | + |
| 27 | + if !intFloat.IsInteger().Bool() { |
| 28 | + t.Errorf("IsInteger() for 5.0 = false, want true") |
| 29 | + } |
| 30 | + |
| 31 | + // Test non-integer float |
| 32 | + fracFloat := MakeFloat(5.5) |
| 33 | + |
| 34 | + if fracFloat.IsInteger().Bool() { |
| 35 | + t.Errorf("IsInteger() for 5.5 = true, want false") |
| 36 | + } |
| 37 | + }) |
| 38 | + |
| 39 | + t.Run("Zero and special values", func(t *testing.T) { |
| 40 | + // Test zero |
| 41 | + zero := MakeFloat(0.0) |
| 42 | + |
| 43 | + if got := zero.Float64(); got != 0.0 { |
| 44 | + t.Errorf("Float64() = %v, want 0.0", got) |
| 45 | + } |
| 46 | + |
| 47 | + // Test very large number |
| 48 | + large := MakeFloat(1e308) |
| 49 | + |
| 50 | + if got := large.Float64(); got != 1e308 { |
| 51 | + t.Errorf("Float64() = %v, want 1e308", got) |
| 52 | + } |
| 53 | + }) |
| 54 | +} |
0 commit comments