1// Copyright 2015 The Go Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style 3// license that can be found in the LICENSE file. 4 5package big 6 7import ( 8 "bytes" 9 "encoding/gob" 10 "encoding/json" 11 "io" 12 "testing" 13) 14 15var floatVals = []string{ 16 "0", 17 "1", 18 "0.1", 19 "2.71828", 20 "1234567890", 21 "3.14e1234", 22 "3.14e-1234", 23 "0.738957395793475734757349579759957975985497e100", 24 "0.73895739579347546656564656573475734957975995797598589749859834759476745986795497e100", 25 "inf", 26 "Inf", 27} 28 29func TestFloatGobEncoding(t *testing.T) { 30 var medium bytes.Buffer 31 enc := gob.NewEncoder(&medium) 32 dec := gob.NewDecoder(&medium) 33 for _, test := range floatVals { 34 for _, sign := range []string{"", "+", "-"} { 35 for _, prec := range []uint{0, 1, 2, 10, 53, 64, 100, 1000} { 36 for _, mode := range []RoundingMode{ToNearestEven, ToNearestAway, ToZero, AwayFromZero, ToNegativeInf, ToPositiveInf} { 37 medium.Reset() // empty buffer for each test case (in case of failures) 38 x := sign + test 39 40 var tx Float 41 _, _, err := tx.SetPrec(prec).SetMode(mode).Parse(x, 0) 42 if err != nil { 43 t.Errorf("parsing of %s (%dbits, %v) failed (invalid test case): %v", x, prec, mode, err) 44 continue 45 } 46 47 // If tx was set to prec == 0, tx.Parse(x, 0) assumes precision 64. Correct it. 48 if prec == 0 { 49 tx.SetPrec(0) 50 } 51 52 if err := enc.Encode(&tx); err != nil { 53 t.Errorf("encoding of %v (%dbits, %v) failed: %v", &tx, prec, mode, err) 54 continue 55 } 56 57 var rx Float 58 if err := dec.Decode(&rx); err != nil { 59 t.Errorf("decoding of %v (%dbits, %v) failed: %v", &tx, prec, mode, err) 60 continue 61 } 62 63 if rx.Cmp(&tx) != 0 { 64 t.Errorf("transmission of %s failed: got %s want %s", x, rx.String(), tx.String()) 65 continue 66 } 67 68 if rx.Prec() != prec { 69 t.Errorf("transmission of %s's prec failed: got %d want %d", x, rx.Prec(), prec) 70 } 71 72 if rx.Mode() != mode { 73 t.Errorf("transmission of %s's mode failed: got %s want %s", x, rx.Mode(), mode) 74 } 75 76 if rx.Acc() != tx.Acc() { 77 t.Errorf("transmission of %s's accuracy failed: got %s want %s", x, rx.Acc(), tx.Acc()) 78 } 79 } 80 } 81 } 82 } 83} 84 85func TestFloatCorruptGob(t *testing.T) { 86 var buf bytes.Buffer 87 tx := NewFloat(4 / 3).SetPrec(1000).SetMode(ToPositiveInf) 88 if err := gob.NewEncoder(&buf).Encode(tx); err != nil { 89 t.Fatal(err) 90 } 91 b := buf.Bytes() 92 93 var rx Float 94 if err := gob.NewDecoder(bytes.NewReader(b)).Decode(&rx); err != nil { 95 t.Fatal(err) 96 } 97 98 if err := gob.NewDecoder(bytes.NewReader(b[:10])).Decode(&rx); err != io.ErrUnexpectedEOF { 99 t.Errorf("got %v want EOF", err) 100 } 101 102 b[1] = 0 103 if err := gob.NewDecoder(bytes.NewReader(b)).Decode(&rx); err == nil { 104 t.Fatal("got nil want version error") 105 } 106} 107 108func TestFloatJSONEncoding(t *testing.T) { 109 for _, test := range floatVals { 110 for _, sign := range []string{"", "+", "-"} { 111 for _, prec := range []uint{0, 1, 2, 10, 53, 64, 100, 1000} { 112 if prec > 53 && testing.Short() { 113 continue 114 } 115 x := sign + test 116 var tx Float 117 _, _, err := tx.SetPrec(prec).Parse(x, 0) 118 if err != nil { 119 t.Errorf("parsing of %s (prec = %d) failed (invalid test case): %v", x, prec, err) 120 continue 121 } 122 b, err := json.Marshal(&tx) 123 if err != nil { 124 t.Errorf("marshaling of %v (prec = %d) failed: %v", &tx, prec, err) 125 continue 126 } 127 var rx Float 128 rx.SetPrec(prec) 129 if err := json.Unmarshal(b, &rx); err != nil { 130 t.Errorf("unmarshaling of %v (prec = %d) failed: %v", &tx, prec, err) 131 continue 132 } 133 if rx.Cmp(&tx) != 0 { 134 t.Errorf("JSON encoding of %v (prec = %d) failed: got %v want %v", &tx, prec, &rx, &tx) 135 } 136 } 137 } 138 } 139} 140