OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [math/] [big/] [rat_test.go] - Blame information for rev 747

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// Copyright 2010 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
 
5
package big
6
 
7
import (
8
        "bytes"
9
        "encoding/gob"
10
        "fmt"
11
        "testing"
12
)
13
 
14
func TestZeroRat(t *testing.T) {
15
        var x, y, z Rat
16
        y.SetFrac64(0, 42)
17
 
18
        if x.Cmp(&y) != 0 {
19
                t.Errorf("x and y should be both equal and zero")
20
        }
21
 
22
        if s := x.String(); s != "0/1" {
23
                t.Errorf("got x = %s, want 0/1", s)
24
        }
25
 
26
        if s := x.RatString(); s != "0" {
27
                t.Errorf("got x = %s, want 0", s)
28
        }
29
 
30
        z.Add(&x, &y)
31
        if s := z.RatString(); s != "0" {
32
                t.Errorf("got x+y = %s, want 0", s)
33
        }
34
 
35
        z.Sub(&x, &y)
36
        if s := z.RatString(); s != "0" {
37
                t.Errorf("got x-y = %s, want 0", s)
38
        }
39
 
40
        z.Mul(&x, &y)
41
        if s := z.RatString(); s != "0" {
42
                t.Errorf("got x*y = %s, want 0", s)
43
        }
44
 
45
        // check for division by zero
46
        defer func() {
47
                if s := recover(); s == nil || s.(string) != "division by zero" {
48
                        panic(s)
49
                }
50
        }()
51
        z.Quo(&x, &y)
52
}
53
 
54
var setStringTests = []struct {
55
        in, out string
56
        ok      bool
57
}{
58
        {"0", "0", true},
59
        {"-0", "0", true},
60
        {"1", "1", true},
61
        {"-1", "-1", true},
62
        {"1.", "1", true},
63
        {"1e0", "1", true},
64
        {"1.e1", "10", true},
65
        {in: "1e", ok: false},
66
        {in: "1.e", ok: false},
67
        {in: "1e+14e-5", ok: false},
68
        {in: "1e4.5", ok: false},
69
        {in: "r", ok: false},
70
        {in: "a/b", ok: false},
71
        {in: "a.b", ok: false},
72
        {"-0.1", "-1/10", true},
73
        {"-.1", "-1/10", true},
74
        {"2/4", "1/2", true},
75
        {".25", "1/4", true},
76
        {"-1/5", "-1/5", true},
77
        {"8129567.7690E14", "812956776900000000000", true},
78
        {"78189e+4", "781890000", true},
79
        {"553019.8935e+8", "55301989350000", true},
80
        {"98765432109876543210987654321e-10", "98765432109876543210987654321/10000000000", true},
81
        {"9877861857500000E-7", "3951144743/4", true},
82
        {"2169378.417e-3", "2169378417/1000000", true},
83
        {"884243222337379604041632732738665534", "884243222337379604041632732738665534", true},
84
        {"53/70893980658822810696", "53/70893980658822810696", true},
85
        {"106/141787961317645621392", "53/70893980658822810696", true},
86
        {"204211327800791583.81095", "4084226556015831676219/20000", true},
87
}
88
 
89
func TestRatSetString(t *testing.T) {
90
        for i, test := range setStringTests {
91
                x, ok := new(Rat).SetString(test.in)
92
 
93
                if ok {
94
                        if !test.ok {
95
                                t.Errorf("#%d SetString(%q) expected failure", i, test.in)
96
                        } else if x.RatString() != test.out {
97
                                t.Errorf("#%d SetString(%q) got %s want %s", i, test.in, x.RatString(), test.out)
98
                        }
99
                } else if x != nil {
100
                        t.Errorf("#%d SetString(%q) got %p want nil", i, test.in, x)
101
                }
102
        }
103
}
104
 
105
func TestRatScan(t *testing.T) {
106
        var buf bytes.Buffer
107
        for i, test := range setStringTests {
108
                x := new(Rat)
109
                buf.Reset()
110
                buf.WriteString(test.in)
111
 
112
                _, err := fmt.Fscanf(&buf, "%v", x)
113
                if err == nil != test.ok {
114
                        if test.ok {
115
                                t.Errorf("#%d error: %s", i, err)
116
                        } else {
117
                                t.Errorf("#%d expected error", i)
118
                        }
119
                        continue
120
                }
121
                if err == nil && x.RatString() != test.out {
122
                        t.Errorf("#%d got %s want %s", i, x.RatString(), test.out)
123
                }
124
        }
125
}
126
 
127
var floatStringTests = []struct {
128
        in   string
129
        prec int
130
        out  string
131
}{
132
        {"0", 0, "0"},
133
        {"0", 4, "0.0000"},
134
        {"1", 0, "1"},
135
        {"1", 2, "1.00"},
136
        {"-1", 0, "-1"},
137
        {".25", 2, "0.25"},
138
        {".25", 1, "0.3"},
139
        {".25", 3, "0.250"},
140
        {"-1/3", 3, "-0.333"},
141
        {"-2/3", 4, "-0.6667"},
142
        {"0.96", 1, "1.0"},
143
        {"0.999", 2, "1.00"},
144
        {"0.9", 0, "1"},
145
        {".25", -1, "0"},
146
        {".55", -1, "1"},
147
}
148
 
149
func TestFloatString(t *testing.T) {
150
        for i, test := range floatStringTests {
151
                x, _ := new(Rat).SetString(test.in)
152
 
153
                if x.FloatString(test.prec) != test.out {
154
                        t.Errorf("#%d got %s want %s", i, x.FloatString(test.prec), test.out)
155
                }
156
        }
157
}
158
 
159
func TestRatSign(t *testing.T) {
160
        zero := NewRat(0, 1)
161
        for _, a := range setStringTests {
162
                x, ok := new(Rat).SetString(a.in)
163
                if !ok {
164
                        continue
165
                }
166
                s := x.Sign()
167
                e := x.Cmp(zero)
168
                if s != e {
169
                        t.Errorf("got %d; want %d for z = %v", s, e, &x)
170
                }
171
        }
172
}
173
 
174
var ratCmpTests = []struct {
175
        rat1, rat2 string
176
        out        int
177
}{
178
        {"0", "0/1", 0},
179
        {"1/1", "1", 0},
180
        {"-1", "-2/2", 0},
181
        {"1", "0", 1},
182
        {"0/1", "1/1", -1},
183
        {"-5/1434770811533343057144", "-5/1434770811533343057145", -1},
184
        {"49832350382626108453/8964749413", "49832350382626108454/8964749413", -1},
185
        {"-37414950961700930/7204075375675961", "37414950961700930/7204075375675961", -1},
186
        {"37414950961700930/7204075375675961", "74829901923401860/14408150751351922", 0},
187
}
188
 
189
func TestRatCmp(t *testing.T) {
190
        for i, test := range ratCmpTests {
191
                x, _ := new(Rat).SetString(test.rat1)
192
                y, _ := new(Rat).SetString(test.rat2)
193
 
194
                out := x.Cmp(y)
195
                if out != test.out {
196
                        t.Errorf("#%d got out = %v; want %v", i, out, test.out)
197
                }
198
        }
199
}
200
 
201
func TestIsInt(t *testing.T) {
202
        one := NewInt(1)
203
        for _, a := range setStringTests {
204
                x, ok := new(Rat).SetString(a.in)
205
                if !ok {
206
                        continue
207
                }
208
                i := x.IsInt()
209
                e := x.Denom().Cmp(one) == 0
210
                if i != e {
211
                        t.Errorf("got IsInt(%v) == %v; want %v", x, i, e)
212
                }
213
        }
214
}
215
 
216
func TestRatAbs(t *testing.T) {
217
        zero := new(Rat)
218
        for _, a := range setStringTests {
219
                x, ok := new(Rat).SetString(a.in)
220
                if !ok {
221
                        continue
222
                }
223
                e := new(Rat).Set(x)
224
                if e.Cmp(zero) < 0 {
225
                        e.Sub(zero, e)
226
                }
227
                z := new(Rat).Abs(x)
228
                if z.Cmp(e) != 0 {
229
                        t.Errorf("got Abs(%v) = %v; want %v", x, z, e)
230
                }
231
        }
232
}
233
 
234
func TestRatNeg(t *testing.T) {
235
        zero := new(Rat)
236
        for _, a := range setStringTests {
237
                x, ok := new(Rat).SetString(a.in)
238
                if !ok {
239
                        continue
240
                }
241
                e := new(Rat).Sub(zero, x)
242
                z := new(Rat).Neg(x)
243
                if z.Cmp(e) != 0 {
244
                        t.Errorf("got Neg(%v) = %v; want %v", x, z, e)
245
                }
246
        }
247
}
248
 
249
func TestRatInv(t *testing.T) {
250
        zero := new(Rat)
251
        for _, a := range setStringTests {
252
                x, ok := new(Rat).SetString(a.in)
253
                if !ok {
254
                        continue
255
                }
256
                if x.Cmp(zero) == 0 {
257
                        continue // avoid division by zero
258
                }
259
                e := new(Rat).SetFrac(x.Denom(), x.Num())
260
                z := new(Rat).Inv(x)
261
                if z.Cmp(e) != 0 {
262
                        t.Errorf("got Inv(%v) = %v; want %v", x, z, e)
263
                }
264
        }
265
}
266
 
267
type ratBinFun func(z, x, y *Rat) *Rat
268
type ratBinArg struct {
269
        x, y, z string
270
}
271
 
272
func testRatBin(t *testing.T, i int, name string, f ratBinFun, a ratBinArg) {
273
        x, _ := new(Rat).SetString(a.x)
274
        y, _ := new(Rat).SetString(a.y)
275
        z, _ := new(Rat).SetString(a.z)
276
        out := f(new(Rat), x, y)
277
 
278
        if out.Cmp(z) != 0 {
279
                t.Errorf("%s #%d got %s want %s", name, i, out, z)
280
        }
281
}
282
 
283
var ratBinTests = []struct {
284
        x, y      string
285
        sum, prod string
286
}{
287
        {"0", "0", "0", "0"},
288
        {"0", "1", "1", "0"},
289
        {"-1", "0", "-1", "0"},
290
        {"-1", "1", "0", "-1"},
291
        {"1", "1", "2", "1"},
292
        {"1/2", "1/2", "1", "1/4"},
293
        {"1/4", "1/3", "7/12", "1/12"},
294
        {"2/5", "-14/3", "-64/15", "-28/15"},
295
        {"4707/49292519774798173060", "-3367/70976135186689855734", "84058377121001851123459/1749296273614329067191168098769082663020", "-1760941/388732505247628681598037355282018369560"},
296
        {"-61204110018146728334/3", "-31052192278051565633/2", "-215564796870448153567/6", "950260896245257153059642991192710872711/3"},
297
        {"-854857841473707320655/4237645934602118692642972629634714039", "-18/31750379913563777419", "-27/133467566250814981", "15387441146526731771790/134546868362786310073779084329032722548987800600710485341"},
298
        {"618575745270541348005638912139/19198433543745179392300736", "-19948846211000086/637313996471", "27674141753240653/30123979153216", "-6169936206128396568797607742807090270137721977/6117715203873571641674006593837351328"},
299
        {"-3/26206484091896184128", "5/2848423294177090248", "15310893822118706237/9330894968229805033368778458685147968", "-5/24882386581946146755650075889827061248"},
300
        {"26946729/330400702820", "41563965/225583428284", "1238218672302860271/4658307703098666660055", "224002580204097/14906584649915733312176"},
301
        {"-8259900599013409474/7", "-84829337473700364773/56707961321161574960", "-468402123685491748914621885145127724451/396955729248131024720", "350340947706464153265156004876107029701/198477864624065512360"},
302
        {"575775209696864/1320203974639986246357", "29/712593081308", "410331716733912717985762465/940768218243776489278275419794956", "808/45524274987585732633"},
303
        {"1786597389946320496771/2066653520653241", "6269770/1992362624741777", "3559549865190272133656109052308126637/4117523232840525481453983149257", "8967230/3296219033"},
304
        {"-36459180403360509753/32150500941194292113930", "9381566963714/9633539", "301622077145533298008420642898530153/309723104686531919656937098270", "-3784609207827/3426986245"},
305
}
306
 
307
func TestRatBin(t *testing.T) {
308
        for i, test := range ratBinTests {
309
                arg := ratBinArg{test.x, test.y, test.sum}
310
                testRatBin(t, i, "Add", (*Rat).Add, arg)
311
 
312
                arg = ratBinArg{test.y, test.x, test.sum}
313
                testRatBin(t, i, "Add symmetric", (*Rat).Add, arg)
314
 
315
                arg = ratBinArg{test.sum, test.x, test.y}
316
                testRatBin(t, i, "Sub", (*Rat).Sub, arg)
317
 
318
                arg = ratBinArg{test.sum, test.y, test.x}
319
                testRatBin(t, i, "Sub symmetric", (*Rat).Sub, arg)
320
 
321
                arg = ratBinArg{test.x, test.y, test.prod}
322
                testRatBin(t, i, "Mul", (*Rat).Mul, arg)
323
 
324
                arg = ratBinArg{test.y, test.x, test.prod}
325
                testRatBin(t, i, "Mul symmetric", (*Rat).Mul, arg)
326
 
327
                if test.x != "0" {
328
                        arg = ratBinArg{test.prod, test.x, test.y}
329
                        testRatBin(t, i, "Quo", (*Rat).Quo, arg)
330
                }
331
 
332
                if test.y != "0" {
333
                        arg = ratBinArg{test.prod, test.y, test.x}
334
                        testRatBin(t, i, "Quo symmetric", (*Rat).Quo, arg)
335
                }
336
        }
337
}
338
 
339
func TestIssue820(t *testing.T) {
340
        x := NewRat(3, 1)
341
        y := NewRat(2, 1)
342
        z := y.Quo(x, y)
343
        q := NewRat(3, 2)
344
        if z.Cmp(q) != 0 {
345
                t.Errorf("got %s want %s", z, q)
346
        }
347
 
348
        y = NewRat(3, 1)
349
        x = NewRat(2, 1)
350
        z = y.Quo(x, y)
351
        q = NewRat(2, 3)
352
        if z.Cmp(q) != 0 {
353
                t.Errorf("got %s want %s", z, q)
354
        }
355
 
356
        x = NewRat(3, 1)
357
        z = x.Quo(x, x)
358
        q = NewRat(3, 3)
359
        if z.Cmp(q) != 0 {
360
                t.Errorf("got %s want %s", z, q)
361
        }
362
}
363
 
364
var setFrac64Tests = []struct {
365
        a, b int64
366
        out  string
367
}{
368
        {0, 1, "0"},
369
        {0, -1, "0"},
370
        {1, 1, "1"},
371
        {-1, 1, "-1"},
372
        {1, -1, "-1"},
373
        {-1, -1, "1"},
374
        {-9223372036854775808, -9223372036854775808, "1"},
375
}
376
 
377
func TestRatSetFrac64Rat(t *testing.T) {
378
        for i, test := range setFrac64Tests {
379
                x := new(Rat).SetFrac64(test.a, test.b)
380
                if x.RatString() != test.out {
381
                        t.Errorf("#%d got %s want %s", i, x.RatString(), test.out)
382
                }
383
        }
384
}
385
 
386
func TestRatGobEncoding(t *testing.T) {
387
        var medium bytes.Buffer
388
        enc := gob.NewEncoder(&medium)
389
        dec := gob.NewDecoder(&medium)
390
        for i, test := range gobEncodingTests {
391
                for j := 0; j < 4; j++ {
392
                        medium.Reset() // empty buffer for each test case (in case of failures)
393
                        stest := test
394
                        if j&1 != 0 {
395
                                // negative numbers
396
                                stest = "-" + test
397
                        }
398
                        if j%2 != 0 {
399
                                // fractions
400
                                stest = stest + "." + test
401
                        }
402
                        var tx Rat
403
                        tx.SetString(stest)
404
                        if err := enc.Encode(&tx); err != nil {
405
                                t.Errorf("#%d%c: encoding failed: %s", i, 'a'+j, err)
406
                        }
407
                        var rx Rat
408
                        if err := dec.Decode(&rx); err != nil {
409
                                t.Errorf("#%d%c: decoding failed: %s", i, 'a'+j, err)
410
                        }
411
                        if rx.Cmp(&tx) != 0 {
412
                                t.Errorf("#%d%c: transmission failed: got %s want %s", i, 'a'+j, &rx, &tx)
413
                        }
414
                }
415
        }
416
}
417
 
418
func TestIssue2379(t *testing.T) {
419
        // 1) no aliasing
420
        q := NewRat(3, 2)
421
        x := new(Rat)
422
        x.SetFrac(NewInt(3), NewInt(2))
423
        if x.Cmp(q) != 0 {
424
                t.Errorf("1) got %s want %s", x, q)
425
        }
426
 
427
        // 2) aliasing of numerator
428
        x = NewRat(2, 3)
429
        x.SetFrac(NewInt(3), x.Num())
430
        if x.Cmp(q) != 0 {
431
                t.Errorf("2) got %s want %s", x, q)
432
        }
433
 
434
        // 3) aliasing of denominator
435
        x = NewRat(2, 3)
436
        x.SetFrac(x.Denom(), NewInt(2))
437
        if x.Cmp(q) != 0 {
438
                t.Errorf("3) got %s want %s", x, q)
439
        }
440
 
441
        // 4) aliasing of numerator and denominator
442
        x = NewRat(2, 3)
443
        x.SetFrac(x.Denom(), x.Num())
444
        if x.Cmp(q) != 0 {
445
                t.Errorf("4) got %s want %s", x, q)
446
        }
447
 
448
        // 5) numerator and denominator are the same
449
        q = NewRat(1, 1)
450
        x = new(Rat)
451
        n := NewInt(7)
452
        x.SetFrac(n, n)
453
        if x.Cmp(q) != 0 {
454
                t.Errorf("5) got %s want %s", x, q)
455
        }
456
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.