annotate libgo/go/math/big/floatmarsh_test.go @ 158:494b0b89df80 default tip

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