@@ -1294,6 +1294,7 @@ func TestNegateRoundTrip(t *testing.T) {
1294
1294
}
1295
1295
1296
1296
func TestQuantityAsApproximateFloat64 (t * testing.T ) {
1297
+ // NOTE: this table should be kept in sync with TestQuantityAsFloat64Slow
1297
1298
table := []struct {
1298
1299
in Quantity
1299
1300
out float64
@@ -1344,11 +1345,11 @@ func TestQuantityAsApproximateFloat64(t *testing.T) {
1344
1345
{decQuantity (- 12 , 500 , DecimalSI ), math .Inf (- 1 )},
1345
1346
}
1346
1347
1347
- for _ , item := range table {
1348
+ for i , item := range table {
1348
1349
t .Run (fmt .Sprintf ("%s %s" , item .in .Format , item .in .String ()), func (t * testing.T ) {
1349
1350
out := item .in .AsApproximateFloat64 ()
1350
1351
if out != item .out {
1351
- t .Fatalf ("expected %v, got %v" , item .out , out )
1352
+ t .Fatalf ("test %d expected %v, got %v" , i + 1 , item .out , out )
1352
1353
}
1353
1354
if item .in .d .Dec != nil {
1354
1355
if i , ok := item .in .AsInt64 (); ok {
@@ -1363,6 +1364,77 @@ func TestQuantityAsApproximateFloat64(t *testing.T) {
1363
1364
}
1364
1365
}
1365
1366
1367
+ func TestQuantityAsFloat64Slow (t * testing.T ) {
1368
+ // NOTE: this table should be kept in sync with TestQuantityAsApproximateFloat64
1369
+ table := []struct {
1370
+ in Quantity
1371
+ out float64
1372
+ }{
1373
+ {decQuantity (0 , 0 , DecimalSI ), 0.0 },
1374
+ {decQuantity (0 , 0 , DecimalExponent ), 0.0 },
1375
+ {decQuantity (0 , 0 , BinarySI ), 0.0 },
1376
+
1377
+ {decQuantity (1 , 0 , DecimalSI ), 1 },
1378
+ {decQuantity (1 , 0 , DecimalExponent ), 1 },
1379
+ {decQuantity (1 , 0 , BinarySI ), 1 },
1380
+
1381
+ // Binary suffixes
1382
+ {decQuantity (1024 , 0 , BinarySI ), 1024 },
1383
+ {decQuantity (8 * 1024 , 0 , BinarySI ), 8 * 1024 },
1384
+ {decQuantity (7 * 1024 * 1024 , 0 , BinarySI ), 7 * 1024 * 1024 },
1385
+ {decQuantity (7 * 1024 * 1024 , 1 , BinarySI ), (7 * 1024 * 1024 ) * 10 },
1386
+ {decQuantity (7 * 1024 * 1024 , 4 , BinarySI ), (7 * 1024 * 1024 ) * 10000 },
1387
+ {decQuantity (7 * 1024 * 1024 , 8 , BinarySI ), (7 * 1024 * 1024 ) * 100000000 },
1388
+ {decQuantity (7 * 1024 * 1024 , - 1 , BinarySI ), (7 * 1024 * 1024 ) / float64 (10 )},
1389
+ {decQuantity (7 * 1024 * 1024 , - 8 , BinarySI ), (7 * 1024 * 1024 ) / float64 (100000000 )},
1390
+
1391
+ {decQuantity (1024 , 0 , DecimalSI ), 1024 },
1392
+ {decQuantity (8 * 1024 , 0 , DecimalSI ), 8 * 1024 },
1393
+ {decQuantity (7 * 1024 * 1024 , 0 , DecimalSI ), 7 * 1024 * 1024 },
1394
+ {decQuantity (7 * 1024 * 1024 , 1 , DecimalSI ), (7 * 1024 * 1024 ) * 10 },
1395
+ {decQuantity (7 * 1024 * 1024 , 4 , DecimalSI ), (7 * 1024 * 1024 ) * 10000 },
1396
+ {decQuantity (7 * 1024 * 1024 , 8 , DecimalSI ), (7 * 1024 * 1024 ) * 100000000 },
1397
+ {decQuantity (7 * 1024 * 1024 , - 1 , DecimalSI ), (7 * 1024 * 1024 ) / float64 (10 )},
1398
+ {decQuantity (7 * 1024 * 1024 , - 8 , DecimalSI ), (7 * 1024 * 1024 ) / float64 (100000000 )},
1399
+
1400
+ {decQuantity (1024 , 0 , DecimalExponent ), 1024 },
1401
+ {decQuantity (8 * 1024 , 0 , DecimalExponent ), 8 * 1024 },
1402
+ {decQuantity (7 * 1024 * 1024 , 0 , DecimalExponent ), 7 * 1024 * 1024 },
1403
+ {decQuantity (7 * 1024 * 1024 , 1 , DecimalExponent ), (7 * 1024 * 1024 ) * 10 },
1404
+ {decQuantity (7 * 1024 * 1024 , 4 , DecimalExponent ), (7 * 1024 * 1024 ) * 10000 },
1405
+ {decQuantity (7 * 1024 * 1024 , 8 , DecimalExponent ), (7 * 1024 * 1024 ) * 100000000 },
1406
+ {decQuantity (7 * 1024 * 1024 , - 1 , DecimalExponent ), (7 * 1024 * 1024 ) / float64 (10 )},
1407
+ {decQuantity (7 * 1024 * 1024 , - 8 , DecimalExponent ), (7 * 1024 * 1024 ) / float64 (100000000 )},
1408
+
1409
+ // very large numbers
1410
+ {Quantity {d : maxAllowed , Format : DecimalSI }, math .MaxInt64 },
1411
+ {Quantity {d : maxAllowed , Format : BinarySI }, math .MaxInt64 },
1412
+ {decQuantity (12 , 18 , DecimalSI ), 1.2e19 },
1413
+
1414
+ // infinities caused due to float64 overflow
1415
+ {decQuantity (12 , 500 , DecimalSI ), math .Inf (0 )},
1416
+ {decQuantity (- 12 , 500 , DecimalSI ), math .Inf (- 1 )},
1417
+ }
1418
+
1419
+ for i , item := range table {
1420
+ t .Run (fmt .Sprintf ("%s %s" , item .in .Format , item .in .String ()), func (t * testing.T ) {
1421
+ out := item .in .AsFloat64Slow ()
1422
+ if out != item .out {
1423
+ t .Fatalf ("test %d expected %v, got %v" , i + 1 , item .out , out )
1424
+ }
1425
+ if item .in .d .Dec != nil {
1426
+ if i , ok := item .in .AsInt64 (); ok {
1427
+ q := intQuantity (i , 0 , item .in .Format )
1428
+ out := q .AsFloat64Slow ()
1429
+ if out != item .out {
1430
+ t .Fatalf ("as int quantity: expected %v, got %v" , item .out , out )
1431
+ }
1432
+ }
1433
+ }
1434
+ })
1435
+ }
1436
+ }
1437
+
1366
1438
func TestStringQuantityAsApproximateFloat64 (t * testing.T ) {
1367
1439
table := []struct {
1368
1440
in string
@@ -1397,6 +1469,40 @@ func TestStringQuantityAsApproximateFloat64(t *testing.T) {
1397
1469
}
1398
1470
}
1399
1471
1472
+ func TestStringQuantityAsFloat64Slow (t * testing.T ) {
1473
+ table := []struct {
1474
+ in string
1475
+ out float64
1476
+ }{
1477
+ {"2Ki" , 2048 },
1478
+ {"1.1Ki" , 1126.4e+0 },
1479
+ {"1Mi" , 1.048576e+06 },
1480
+ {"2Gi" , 2.147483648e+09 },
1481
+ }
1482
+
1483
+ for _ , item := range table {
1484
+ t .Run (item .in , func (t * testing.T ) {
1485
+ in , err := ParseQuantity (item .in )
1486
+ if err != nil {
1487
+ t .Fatal (err )
1488
+ }
1489
+ out := in .AsFloat64Slow ()
1490
+ if out != item .out {
1491
+ t .Fatalf ("expected %v, got %v" , item .out , out )
1492
+ }
1493
+ if in .d .Dec != nil {
1494
+ if i , ok := in .AsInt64 (); ok {
1495
+ q := intQuantity (i , 0 , in .Format )
1496
+ out := q .AsFloat64Slow ()
1497
+ if out != item .out {
1498
+ t .Fatalf ("as int quantity: expected %v, got %v" , item .out , out )
1499
+ }
1500
+ }
1501
+ }
1502
+ })
1503
+ }
1504
+ }
1505
+
1400
1506
func benchmarkQuantities () []Quantity {
1401
1507
return []Quantity {
1402
1508
intQuantity (1024 * 1024 * 1024 , 0 , BinarySI ),
@@ -1579,6 +1685,18 @@ func BenchmarkQuantityAsApproximateFloat64(b *testing.B) {
1579
1685
b .StopTimer ()
1580
1686
}
1581
1687
1688
+ func BenchmarkQuantityAsFloat64Slow (b * testing.B ) {
1689
+ values := benchmarkQuantities ()
1690
+ b .ResetTimer ()
1691
+ for i := 0 ; i < b .N ; i ++ {
1692
+ q := values [i % len (values )]
1693
+ if q .AsFloat64Slow () == - 1 {
1694
+ b .Fatal (q )
1695
+ }
1696
+ }
1697
+ b .StopTimer ()
1698
+ }
1699
+
1582
1700
var _ pflag.Value = & QuantityValue {}
1583
1701
1584
1702
func TestQuantityValueSet (t * testing.T ) {
0 commit comments