func TestDecScan(t *testing.T) { tmp := new(inf.Dec) for i, test := range decStringTests { if test.scale < 0 { // SetString only supports scale >= 0 continue } // initialize to a non-zero value so that issues with parsing // 0 are detected tmp.Set(inf.NewDec(1234567890, 123)) n1, n2 := new(inf.Dec), tmp nn1, err1 := fmt.Sscan(test.in, n1) nn2, err2 := fmt.Sscan(test.in, n2) if !test.scanOk { if err1 == nil || err2 == nil { t.Errorf("#%d (input '%s') ok incorrect, should be %t", i, test.in, test.scanOk) } continue } expected := inf.NewDec(test.val, test.scale) if nn1 != 1 || err1 != nil || nn2 != 1 || err2 != nil { t.Errorf("#%d (input '%s') error %d %v, %d %v", i, test.in, nn1, err1, nn2, err2) continue } if n1.Cmp(expected) != 0 { t.Errorf("#%d (input '%s') got: %s want: %d", i, test.in, n1, test.val) } if n2.Cmp(expected) != 0 { t.Errorf("#%d (input '%s') got: %s want: %d", i, test.in, n2, test.val) } } }
func TestDecGobEncoding(t *testing.T) { var medium bytes.Buffer enc := gob.NewEncoder(&medium) dec := gob.NewDecoder(&medium) for i, test := range decGobEncodingTests { for j := 0; j < 2; j++ { for k := inf.Scale(-5); k <= 5; k++ { medium.Reset() // empty buffer for each test case (in case of failures) stest := test if j != 0 { // negative numbers stest = "-" + test } var tx inf.Dec tx.SetString(stest) tx.SetScale(k) // test with positive, negative, and zero scale if err := enc.Encode(&tx); err != nil { t.Errorf("#%d%c: encoding failed: %s", i, 'a'+j, err) } var rx inf.Dec if err := dec.Decode(&rx); err != nil { t.Errorf("#%d%c: decoding failed: %s", i, 'a'+j, err) } if rx.Cmp(&tx) != 0 { t.Errorf("#%d%c: transmission failed: got %s want %s", i, 'a'+j, &rx, &tx) } } } } }
func TestDecSetString(t *testing.T) { tmp := new(inf.Dec) for i, test := range decStringTests { if test.scale < 0 { // SetString only supports scale >= 0 continue } // initialize to a non-zero value so that issues with parsing // 0 are detected tmp.Set(inf.NewDec(1234567890, 123)) n1, ok1 := new(inf.Dec).SetString(test.in) n2, ok2 := tmp.SetString(test.in) expected := inf.NewDec(test.val, test.scale) if ok1 != test.ok || ok2 != test.ok { t.Errorf("#%d (input '%s') ok incorrect (should be %t)", i, test.in, test.ok) continue } if !ok1 { if n1 != nil { t.Errorf("#%d (input '%s') n1 != nil", i, test.in) } continue } if !ok2 { if n2 != nil { t.Errorf("#%d (input '%s') n2 != nil", i, test.in) } continue } if n1.Cmp(expected) != 0 { t.Errorf("#%d (input '%s') got: %s want: %d", i, test.in, n1, test.val) } if n2.Cmp(expected) != 0 { t.Errorf("#%d (input '%s') got: %s want: %d", i, test.in, n2, test.val) } } }
func TestDecGetString(t *testing.T) { z := new(inf.Dec) for i, test := range decStringTests { if !test.ok { continue } z.SetUnscaled(test.val) z.SetScale(test.scale) s := z.String() if s != test.out { t.Errorf("#%da got %s; want %s", i, s, test.out) } s = fmt.Sprintf("%d", z) if s != test.out { t.Errorf("#%db got %s; want %s", i, s, test.out) } } }
func TestDecAbsZ(t *testing.T) { var zero inf.Dec for _, a := range decSumZZ { var z inf.Dec z.Abs(a.z) var e inf.Dec e.Set(a.z) if e.Cmp(&zero) < 0 { e.Sub(&zero, &e) } if z.Cmp(&e) != 0 { t.Errorf("got z = %v; want %v", z, e) } } }
func ExampleDec_SetString() { d := new(inf.Dec) d.SetString("012345.67890") // decimal; leading 0 ignored; trailing 0 kept fmt.Println(d) // Output: 12345.67890 }