|
| 1 | +package gp |
| 2 | + |
| 3 | +import ( |
| 4 | + "math" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func TestLongCreation(t *testing.T) { |
| 9 | + tests := []struct { |
| 10 | + name string |
| 11 | + input int64 |
| 12 | + expected int64 |
| 13 | + }{ |
| 14 | + {"zero", 0, 0}, |
| 15 | + {"positive", 42, 42}, |
| 16 | + {"negative", -42, -42}, |
| 17 | + {"max_int64", math.MaxInt64, math.MaxInt64}, |
| 18 | + {"min_int64", math.MinInt64, math.MinInt64}, |
| 19 | + } |
| 20 | + |
| 21 | + for _, tt := range tests { |
| 22 | + t.Run(tt.name, func(t *testing.T) { |
| 23 | + l := MakeLong(tt.input) |
| 24 | + if got := l.Int64(); got != tt.expected { |
| 25 | + t.Errorf("MakeLong(%d) = %d; want %d", tt.input, got, tt.expected) |
| 26 | + } |
| 27 | + }) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +func TestLongFromFloat64(t *testing.T) { |
| 32 | + tests := []struct { |
| 33 | + name string |
| 34 | + input float64 |
| 35 | + expected int64 |
| 36 | + }{ |
| 37 | + {"integer_float", 42.0, 42}, |
| 38 | + {"truncated_float", 42.9, 42}, |
| 39 | + {"negative_float", -42.9, -42}, |
| 40 | + } |
| 41 | + |
| 42 | + for _, tt := range tests { |
| 43 | + t.Run(tt.name, func(t *testing.T) { |
| 44 | + l := LongFromFloat64(tt.input) |
| 45 | + if got := l.Int64(); got != tt.expected { |
| 46 | + t.Errorf("LongFromFloat64(%f) = %d; want %d", tt.input, got, tt.expected) |
| 47 | + } |
| 48 | + }) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +func TestLongFromString(t *testing.T) { |
| 53 | + tests := []struct { |
| 54 | + name string |
| 55 | + input string |
| 56 | + base int |
| 57 | + expected int64 |
| 58 | + }{ |
| 59 | + {"decimal", "42", 10, 42}, |
| 60 | + {"hex", "2A", 16, 42}, |
| 61 | + {"binary", "101010", 2, 42}, |
| 62 | + {"octal", "52", 8, 42}, |
| 63 | + {"negative", "-42", 10, -42}, |
| 64 | + } |
| 65 | + |
| 66 | + for _, tt := range tests { |
| 67 | + t.Run(tt.name, func(t *testing.T) { |
| 68 | + l := LongFromString(tt.input, tt.base) |
| 69 | + if got := l.Int64(); got != tt.expected { |
| 70 | + t.Errorf("LongFromString(%q, %d) = %d; want %d", tt.input, tt.base, got, tt.expected) |
| 71 | + } |
| 72 | + }) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +func TestLongConversions(t *testing.T) { |
| 77 | + l := MakeLong(42) |
| 78 | + |
| 79 | + t.Run("Int", func(t *testing.T) { |
| 80 | + if got := l.Int(); got != 42 { |
| 81 | + t.Errorf("Int() = %d; want 42", got) |
| 82 | + } |
| 83 | + }) |
| 84 | + |
| 85 | + t.Run("Uint", func(t *testing.T) { |
| 86 | + if got := l.Uint(); got != 42 { |
| 87 | + t.Errorf("Uint() = %d; want 42", got) |
| 88 | + } |
| 89 | + }) |
| 90 | + |
| 91 | + t.Run("Uint64", func(t *testing.T) { |
| 92 | + if got := l.Uint64(); got != 42 { |
| 93 | + t.Errorf("Uint64() = %d; want 42", got) |
| 94 | + } |
| 95 | + }) |
| 96 | + |
| 97 | + t.Run("Float64", func(t *testing.T) { |
| 98 | + if got := l.Float64(); got != 42.0 { |
| 99 | + t.Errorf("Float64() = %f; want 42.0", got) |
| 100 | + } |
| 101 | + }) |
| 102 | +} |
| 103 | + |
| 104 | +func TestLongFromUintptr(t *testing.T) { |
| 105 | + tests := []struct { |
| 106 | + name string |
| 107 | + input uintptr |
| 108 | + expected int64 |
| 109 | + }{ |
| 110 | + {"zero", 0, 0}, |
| 111 | + {"positive", 42, 42}, |
| 112 | + {"large_number", 1 << 30, 1 << 30}, |
| 113 | + } |
| 114 | + |
| 115 | + for _, tt := range tests { |
| 116 | + t.Run(tt.name, func(t *testing.T) { |
| 117 | + l := LongFromUintptr(tt.input) |
| 118 | + if got := l.Int64(); got != tt.expected { |
| 119 | + t.Errorf("LongFromUintptr(%d) = %d; want %d", tt.input, got, tt.expected) |
| 120 | + } |
| 121 | + }) |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +func TestLongFromUnicode(t *testing.T) { |
| 126 | + tests := []struct { |
| 127 | + name string |
| 128 | + input string |
| 129 | + base int |
| 130 | + expected int64 |
| 131 | + }{ |
| 132 | + {"unicode_decimal", "42", 10, 42}, |
| 133 | + {"unicode_hex", "2A", 16, 42}, |
| 134 | + } |
| 135 | + |
| 136 | + for _, tt := range tests { |
| 137 | + t.Run(tt.name, func(t *testing.T) { |
| 138 | + // Create Unicode object from string |
| 139 | + u := MakeStr(tt.input) |
| 140 | + l := LongFromUnicode(u.Object, tt.base) |
| 141 | + if got := l.Int64(); got != tt.expected { |
| 142 | + t.Errorf("LongFromUnicode(%q, %d) = %d; want %d", tt.input, tt.base, got, tt.expected) |
| 143 | + } |
| 144 | + }) |
| 145 | + } |
| 146 | +} |
0 commit comments