github.com/enetx/g@v1.0.80/tests/float_test.go (about) 1 package g_test 2 3 import ( 4 "math" 5 "math/big" 6 "testing" 7 8 "github.com/enetx/g" 9 ) 10 11 func TestFloatBytes(t *testing.T) { 12 // Test case for positive float 13 f := g.Float(3.14) 14 expected := []byte{64, 9, 30, 184, 81, 235, 133, 31} // Bytes representation of 3.14 in big-endian 15 actual := f.Bytes() 16 if actual.Ne(expected) { 17 t.Errorf("Bytes representation of positive float incorrect. Expected: %v, Got: %v", expected, actual) 18 } 19 20 // Test case for negative float 21 f = g.Float(-3.14) 22 expected = []byte{192, 9, 30, 184, 81, 235, 133, 31} // Bytes representation of -3.14 in big-endian 23 actual = f.Bytes() 24 if actual.Ne(expected) { 25 t.Errorf("Bytes representation of negative float incorrect. Expected: %v, Got: %v", expected, actual) 26 } 27 28 // Test case for infinity 29 f = g.Float(math.Inf(1)) 30 expected = []byte{127, 240, 0, 0, 0, 0, 0, 0} // Bytes representation of positive infinity in big-endian 31 actual = f.Bytes() 32 if actual.Ne(expected) { 33 t.Errorf("Bytes representation of positive infinity incorrect. Expected: %v, Got: %v", expected, actual) 34 } 35 36 // Test case for negative infinity 37 f = g.Float(math.Inf(-1)) 38 expected = []byte{255, 240, 0, 0, 0, 0, 0, 0} // Bytes representation of negative infinity in big-endian 39 actual = f.Bytes() 40 if actual.Ne(expected) { 41 t.Errorf("Bytes representation of negative infinity incorrect. Expected: %v, Got: %v", expected, actual) 42 } 43 } 44 45 func TestFloatCompare(t *testing.T) { 46 testCases := []struct { 47 f1 g.Float 48 f2 g.Float 49 expected int 50 }{ 51 {3.14, 6.28, -1}, 52 {6.28, 3.14, 1}, 53 {1.23, 1.23, 0}, 54 {-2.5, 2.5, -1}, 55 } 56 57 for _, tc := range testCases { 58 result := int(tc.f1.Cmp(tc.f2)) 59 if result != tc.expected { 60 t.Errorf("Compare(%f, %f): expected %d, got %d", tc.f1, tc.f2, tc.expected, result) 61 } 62 } 63 } 64 65 func TestFloatEq(t *testing.T) { 66 testCases := []struct { 67 f1 g.Float 68 f2 g.Float 69 expected bool 70 }{ 71 {3.14, 6.28, false}, 72 {1.23, 1.23, true}, 73 {0.0, 0.0, true}, 74 {-2.5, 2.5, false}, 75 } 76 77 for _, tc := range testCases { 78 result := tc.f1.Eq(tc.f2) 79 if result != tc.expected { 80 t.Errorf("Eq(%f, %f): expected %t, got %t", tc.f1, tc.f2, tc.expected, result) 81 } 82 } 83 } 84 85 func TestFloatNe(t *testing.T) { 86 testCases := []struct { 87 f1 g.Float 88 f2 g.Float 89 expected bool 90 }{ 91 {3.14, 6.28, true}, 92 {1.23, 1.23, false}, 93 {0.0, 0.0, false}, 94 {-2.5, 2.5, true}, 95 } 96 97 for _, tc := range testCases { 98 result := tc.f1.Ne(tc.f2) 99 if result != tc.expected { 100 t.Errorf("Ne(%f, %f): expected %t, got %t", tc.f1, tc.f2, tc.expected, result) 101 } 102 } 103 } 104 105 func TestFloatGt(t *testing.T) { 106 testCases := []struct { 107 f1 g.Float 108 f2 g.Float 109 expected bool 110 }{ 111 {3.14, 6.28, false}, 112 {6.28, 3.14, true}, 113 {1.23, 1.23, false}, 114 {-2.5, 2.5, false}, 115 } 116 117 for _, tc := range testCases { 118 result := tc.f1.Gt(tc.f2) 119 if result != tc.expected { 120 t.Errorf("Gt(%f, %f): expected %t, got %t", tc.f1, tc.f2, tc.expected, result) 121 } 122 } 123 } 124 125 func TestFloatLt(t *testing.T) { 126 testCases := []struct { 127 f1 g.Float 128 f2 g.Float 129 expected bool 130 }{ 131 {3.14, 6.28, true}, 132 {6.28, 3.14, false}, 133 {1.23, 1.23, false}, 134 {-2.5, 2.5, true}, 135 } 136 for _, tc := range testCases { 137 result := tc.f1.Lt(tc.f2) 138 if result != tc.expected { 139 t.Errorf("Lt(%f, %f): expected %t, got %t", tc.f1, tc.f2, tc.expected, result) 140 } 141 } 142 } 143 144 func TestFloatRound(t *testing.T) { 145 // Test cases for positive numbers 146 positiveTests := []struct { 147 input g.Float 148 expected g.Int 149 }{ 150 {1.1, 1}, 151 {1.5, 2}, 152 {1.9, 2}, 153 } 154 155 for _, tc := range positiveTests { 156 result := tc.input.Round() 157 if result != tc.expected { 158 t.Errorf("Round(%f) = %d; expected %d", tc.input, result, tc.expected) 159 } 160 } 161 162 // Test cases for negative numbers 163 negativeTests := []struct { 164 input g.Float 165 expected g.Int 166 }{ 167 {-1.1, -1}, 168 {-1.5, -2}, 169 {-1.9, -2}, 170 } 171 172 for _, tc := range negativeTests { 173 result := tc.input.Round() 174 if result != tc.expected { 175 t.Errorf("Round(%f) = %d; expected %d", tc.input, result, tc.expected) 176 } 177 } 178 } 179 180 func TestFloatRoundDecimal(t *testing.T) { 181 testCases := []struct { 182 value g.Float 183 decimals g.Int 184 expected g.Float 185 }{ 186 {3.1415926535, 2, 3.14}, 187 {3.1415926535, 3, 3.142}, 188 {100.123456789, 4, 100.1235}, 189 {-5.6789, 1, -5.7}, 190 {12345.6789, 0, 12346}, 191 {12345.6789, -1, 12345.6789}, 192 } 193 194 for _, testCase := range testCases { 195 result := testCase.value.RoundDecimal(testCase.decimals) 196 if result != testCase.expected { 197 t.Errorf( 198 "Failed: value=%.10f decimals=%d, expected=%.10f, got=%.10f\n", 199 testCase.value, 200 testCase.decimals, 201 testCase.expected, 202 result, 203 ) 204 } 205 } 206 } 207 208 func TestFloatMax(t *testing.T) { 209 if max := g.NewFloat(2.2).Max(2.8, 2.1, 2.7); max != 2.8 { 210 t.Errorf("Max() = %f, want: %f.", max, 2.8) 211 } 212 } 213 214 func TestFloatMin(t *testing.T) { 215 if min := g.NewFloat(2.2).Min(2.8, 2.1, 2.7); min != 2.1 { 216 t.Errorf("Min() = %f; want: %f", min, 2.1) 217 } 218 } 219 220 func TestFloatAbs(t *testing.T) { 221 // Test case for positive float 222 f := g.Float(3.14) 223 expected := g.Float(3.14) 224 actual := f.Abs() 225 if actual != expected { 226 t.Errorf("Absolute value of positive float incorrect. Expected: %v, Got: %v", expected, actual) 227 } 228 229 // Test case for negative float 230 f = g.Float(-3.14) 231 expected = g.Float(3.14) 232 actual = f.Abs() 233 if actual != expected { 234 t.Errorf("Absolute value of negative float incorrect. Expected: %v, Got: %v", expected, actual) 235 } 236 237 // Test case for zero float 238 f = g.Float(0) 239 expected = g.Float(0) 240 actual = f.Abs() 241 if actual != expected { 242 t.Errorf("Absolute value of zero float incorrect. Expected: %v, Got: %v", expected, actual) 243 } 244 } 245 246 func TestFloatAdd(t *testing.T) { 247 // Test case for addition of positive floats 248 f1 := g.Float(3.14) 249 f2 := g.Float(1.23) 250 expected := g.Float(4.37) 251 actual := f1.Add(f2) 252 if actual != expected { 253 t.Errorf("Addition of positive floats incorrect. Expected: %v, Got: %v", expected, actual) 254 } 255 256 // Test case for addition of negative floats 257 f1 = g.Float(-3.14) 258 f2 = g.Float(-1.23) 259 expected = g.Float(-4.37) 260 actual = f1.Add(f2) 261 if actual != expected { 262 t.Errorf("Addition of negative floats incorrect. Expected: %v, Got: %v", expected, actual) 263 } 264 } 265 266 func TestFloatToBigFloat(t *testing.T) { 267 // Test case for converting positive float to *big.Float 268 f := g.Float(3.14) 269 expected := big.NewFloat(3.14) 270 actual := f.ToBigFloat() 271 if actual.Cmp(expected) != 0 { 272 t.Errorf("Conversion of positive float to *big.Float incorrect. Expected: %v, Got: %v", expected, actual) 273 } 274 275 // Test case for converting negative float to *big.Float 276 f = g.Float(-3.14) 277 expected = big.NewFloat(-3.14) 278 actual = f.ToBigFloat() 279 if actual.Cmp(expected) != 0 { 280 t.Errorf("Conversion of negative float to *big.Float incorrect. Expected: %v, Got: %v", expected, actual) 281 } 282 283 // Test case for converting zero float to *big.Float 284 f = g.Float(0) 285 expected = big.NewFloat(0) 286 actual = f.ToBigFloat() 287 if actual.Cmp(expected) != 0 { 288 t.Errorf("Conversion of zero float to *big.Float incorrect. Expected: %v, Got: %v", expected, actual) 289 } 290 } 291 292 func TestFloatIsZero(t *testing.T) { 293 // Test case for zero float 294 f := g.Float(0) 295 if !f.IsZero() { 296 t.Errorf("IsZero method failed to identify zero float.") 297 } 298 299 // Test case for positive non-zero float 300 f = g.Float(3.14) 301 if f.IsZero() { 302 t.Errorf("IsZero method incorrectly identified positive non-zero float as zero.") 303 } 304 305 // Test case for negative non-zero float 306 f = g.Float(-3.14) 307 if f.IsZero() { 308 t.Errorf("IsZero method incorrectly identified negative non-zero float as zero.") 309 } 310 } 311 312 func TestFloatToInt(t *testing.T) { 313 // Test case for positive float 314 f := g.Float(3.14) 315 expected := g.Int(3) 316 actual := f.ToInt() 317 if actual != expected { 318 t.Errorf("ToInt method failed to convert positive float. Expected: %d, Got: %d", expected, actual) 319 } 320 321 // Test case for negative float 322 f = g.Float(-3.14) 323 expected = g.Int(-3) 324 actual = f.ToInt() 325 if actual != expected { 326 t.Errorf("ToInt method failed to convert negative float. Expected: %d, Got: %d", expected, actual) 327 } 328 329 // Test case for zero float 330 f = g.Float(0) 331 expected = g.Int(0) 332 actual = f.ToInt() 333 if actual != expected { 334 t.Errorf("ToInt method failed to convert zero float. Expected: %d, Got: %d", expected, actual) 335 } 336 } 337 338 func TestFloatToString(t *testing.T) { 339 // Test case for positive float 340 f := g.Float(3.14) 341 expected := g.String("3.14") 342 actual := f.ToString() 343 if actual != expected { 344 t.Errorf("ToString method failed to convert positive float. Expected: %s, Got: %s", expected, actual) 345 } 346 347 // Test case for negative float 348 f = g.Float(-3.14) 349 expected = g.String("-3.14") 350 actual = f.ToString() 351 if actual != expected { 352 t.Errorf("ToString method failed to convert negative float. Expected: %s, Got: %s", expected, actual) 353 } 354 355 // Test case for zero float 356 f = g.Float(0) 357 expected = g.String("0") 358 actual = f.ToString() 359 if actual != expected { 360 t.Errorf("ToString method failed to convert zero float. Expected: %s, Got: %s", expected, actual) 361 } 362 } 363 364 func TestFloatAsFloat32(t *testing.T) { 365 // Test case for positive float 366 f := g.Float(3.14) 367 expected := float32(3.14) 368 actual := f.AsFloat32() 369 if actual != expected { 370 t.Errorf("AsFloat32 method failed to convert positive float. Expected: %f, Got: %f", expected, actual) 371 } 372 373 // Test case for negative float 374 f = g.Float(-3.14) 375 expected = float32(-3.14) 376 actual = f.AsFloat32() 377 if actual != expected { 378 t.Errorf("AsFloat32 method failed to convert negative float. Expected: %f, Got: %f", expected, actual) 379 } 380 381 // Test case for zero float 382 f = g.Float(0) 383 expected = float32(0) 384 actual = f.AsFloat32() 385 if actual != expected { 386 t.Errorf("AsFloat32 method failed to convert zero float. Expected: %f, Got: %f", expected, actual) 387 } 388 } 389 390 func TestFloatHashing(t *testing.T) { 391 // Test case for a positive float 392 f := g.Float(3.14) 393 fh := f.Hash() 394 395 // Test MD5 396 expectedMD5 := g.String("32200b8781d6e8f31543da4cf19ff307") 397 actualMD5 := fh.MD5() 398 if actualMD5 != expectedMD5 { 399 t.Errorf("MD5 hash mismatch for positive float. Expected: %s, Got: %s", expectedMD5, actualMD5) 400 } 401 402 // Test SHA1 403 expectedSHA1 := g.String("8d3ad0b5fdf81c2de3656ebe8d8b0f14e1431438") 404 actualSHA1 := fh.SHA1() 405 if actualSHA1 != expectedSHA1 { 406 t.Errorf("SHA1 hash mismatch for positive float. Expected: %s, Got: %s", expectedSHA1, actualSHA1) 407 } 408 409 // Test SHA256 410 expectedSHA256 := g.String("a7c511f4744a60f88b6a88fbbb1ed7c79820e028f841c50843963bbb1dcdd9f6") 411 actualSHA256 := fh.SHA256() 412 if actualSHA256 != expectedSHA256 { 413 t.Errorf("SHA256 hash mismatch for positive float. Expected: %s, Got: %s", expectedSHA256, actualSHA256) 414 } 415 416 // Test SHA512 417 expectedSHA512 := g.String( 418 "a86ec42eec985ea198240622e13ddfdbd25bee28007d4ee7b17058292dc46ef51e5b107ab44d70ae14300d88bf71a4cda93851ab920f5eeef8bc1531cd451063", 419 ) 420 actualSHA512 := fh.SHA512() 421 if actualSHA512 != expectedSHA512 { 422 t.Errorf("SHA512 hash mismatch for positive float. Expected: %s, Got: %s", expectedSHA512, actualSHA512) 423 } 424 }