MCPcopy
hub / github.com/shopspring/decimal / TestNewFromFloatWithExponent

Function TestNewFromFloatWithExponent

decimal_test.go:407–475  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

405}
406
407func TestNewFromFloatWithExponent(t *testing.T) {
408 type Inp struct {
409 float float64
410 exp int32
411 }
412 // some tests are taken from here https://www.cockroachlabs.com/blog/rounding-implementations-in-go/
413 tests := map[Inp]string{
414 Inp{123.4, -3}: "123.4",
415 Inp{123.4, -1}: "123.4",
416 Inp{123.412345, 1}: "120",
417 Inp{123.412345, 0}: "123",
418 Inp{123.412345, -5}: "123.41235",
419 Inp{123.412345, -6}: "123.412345",
420 Inp{123.412345, -7}: "123.412345",
421 Inp{123.412345, -28}: "123.4123450000000019599610823207",
422 Inp{1230000000, 3}: "1230000000",
423 Inp{123.9999999999999999, -7}: "124",
424 Inp{123.8989898999999999, -7}: "123.8989899",
425 Inp{0.49999999999999994, 0}: "0",
426 Inp{0.5, 0}: "1",
427 Inp{0., -1000}: "0",
428 Inp{0.5000000000000001, 0}: "1",
429 Inp{1.390671161567e-309, 0}: "0",
430 Inp{4.503599627370497e+15, 0}: "4503599627370497",
431 Inp{4.503599627370497e+60, 0}: "4503599627370497110902645731364739935039854989106233267453952",
432 Inp{4.503599627370497e+60, 1}: "4503599627370497110902645731364739935039854989106233267453950",
433 Inp{4.503599627370497e+60, -1}: "4503599627370497110902645731364739935039854989106233267453952",
434 Inp{50, 2}: "100",
435 Inp{49, 2}: "0",
436 Inp{50, 3}: "0",
437 // subnormals
438 Inp{1.390671161567e-309, -2000}: "0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001390671161567000864431395448332752540137009987788957394095829635554502771758698872408926974382819387852542087331897381878220271350970912568035007740861074263206736245957501456549756342151614772544950978154339064833880234531754156635411349342950306987480369774780312897442981323940546749863054846093718407237782253156822124910364044261653195961209878120072488178603782495270845071470243842997312255994555557251870400944414666445871039673491570643357351279578519863428540219295076767898526278029257129758694673164251056158277568765100904638511604478844087596428177947970563689475826736810456067108202083804368114484417399279328807983736233036662284338182105684628835292230438999173947056675615385756827890872955322265625",
439 Inp{1.390671161567e-309, -862}: "0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000013906711615670008644313954483327525401370099877889573940958296355545027717586988724089269743828193878525420873318973818782202713509709125680350077408610742632067362459575014565497563421516147725449509781543390648338802345317541566354113493429503069874803697747803128974429813239405467498630548460937184072377822531568221249103640442616531959612098781200724881786037824952708450714702438429973122559945555572518704009444146664458710396734915706433573512795785198634285402192950767678985262780292571297586946731642510561582775687651009046385116044788440876",
440 Inp{1.390671161567e-309, -863}: "0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000013906711615670008644313954483327525401370099877889573940958296355545027717586988724089269743828193878525420873318973818782202713509709125680350077408610742632067362459575014565497563421516147725449509781543390648338802345317541566354113493429503069874803697747803128974429813239405467498630548460937184072377822531568221249103640442616531959612098781200724881786037824952708450714702438429973122559945555572518704009444146664458710396734915706433573512795785198634285402192950767678985262780292571297586946731642510561582775687651009046385116044788440876",
441 }
442
443 // add negatives
444 for p, s := range tests {
445 if p.float > 0 {
446 if s != "0" {
447 tests[Inp{-p.float, p.exp}] = "-" + s
448 } else {
449 tests[Inp{-p.float, p.exp}] = "0"
450 }
451 }
452 }
453
454 for input, s := range tests {
455 d := NewFromFloatWithExponent(input.float, input.exp)
456 if d.String() != s {
457 t.Errorf("expected %s, got %s (%s, %d)",
458 s, d.String(),
459 d.value.String(), d.exp)
460 }
461 }
462
463 shouldPanicOn := []float64{
464 math.NaN(),

Callers

nothing calls this directly

Calls 3

StringMethod · 0.95
NewFromFloatWithExponentFunction · 0.85
didPanicFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…