LEFT | RIGHT |
1 // Copyright 2010 The Go Authors. All rights reserved. | 1 // Copyright 2010 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package big | 5 package big |
6 | 6 |
7 import ( | 7 import ( |
8 "bytes" | 8 "bytes" |
9 "encoding/gob" | 9 "encoding/gob" |
10 "fmt" | 10 "fmt" |
(...skipping 369 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
380 if x.RatString() != test.out { | 380 if x.RatString() != test.out { |
381 t.Errorf("#%d got %s want %s", i, x.RatString(), test.ou
t) | 381 t.Errorf("#%d got %s want %s", i, x.RatString(), test.ou
t) |
382 } | 382 } |
383 } | 383 } |
384 } | 384 } |
385 | 385 |
386 func TestRatGobEncoding(t *testing.T) { | 386 func TestRatGobEncoding(t *testing.T) { |
387 var medium bytes.Buffer | 387 var medium bytes.Buffer |
388 enc := gob.NewEncoder(&medium) | 388 enc := gob.NewEncoder(&medium) |
389 dec := gob.NewDecoder(&medium) | 389 dec := gob.NewDecoder(&medium) |
390 » for i, test := range encodingTests { | 390 » for _, test := range encodingTests { |
391 » » for j := 0; j < 4; j++ { | 391 » » medium.Reset() // empty buffer for each test case (in case of fa
ilures) |
392 » » » medium.Reset() // empty buffer for each test case (in ca
se of failures) | 392 » » var tx Rat |
393 » » » stest := test | 393 » » tx.SetString(test + ".14159265") |
394 » » » if j&1 != 0 { | 394 » » if err := enc.Encode(&tx); err != nil { |
395 » » » » // negative numbers | 395 » » » t.Errorf("encoding of %s failed: %s", &tx, err) |
396 » » » » stest = "-" + test | 396 » » } |
397 » » » } | 397 » » var rx Rat |
398 » » » if j%2 != 0 { | 398 » » if err := dec.Decode(&rx); err != nil { |
399 » » » » // fractions | 399 » » » t.Errorf("decoding of %s failed: %s", &tx, err) |
400 » » » » stest = stest + "." + test | 400 » » } |
401 » » » } | 401 » » if rx.Cmp(&tx) != 0 { |
402 » » » var tx Rat | 402 » » » t.Errorf("transmission of %s failed: got %s want %s", &t
x, &rx, &tx) |
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 wan
t %s", i, 'a'+j, &rx, &tx) | |
413 » » » } | |
414 } | 403 } |
415 } | 404 } |
416 } | 405 } |
417 | 406 |
418 func TestIssue2379(t *testing.T) { | 407 func TestIssue2379(t *testing.T) { |
419 // 1) no aliasing | 408 // 1) no aliasing |
420 q := NewRat(3, 2) | 409 q := NewRat(3, 2) |
421 x := new(Rat) | 410 x := new(Rat) |
422 x.SetFrac(NewInt(3), NewInt(2)) | 411 x.SetFrac(NewInt(3), NewInt(2)) |
423 if x.Cmp(q) != 0 { | 412 if x.Cmp(q) != 0 { |
(...skipping 23 matching lines...) Expand all Loading... |
447 | 436 |
448 // 5) numerator and denominator are the same | 437 // 5) numerator and denominator are the same |
449 q = NewRat(1, 1) | 438 q = NewRat(1, 1) |
450 x = new(Rat) | 439 x = new(Rat) |
451 n := NewInt(7) | 440 n := NewInt(7) |
452 x.SetFrac(n, n) | 441 x.SetFrac(n, n) |
453 if x.Cmp(q) != 0 { | 442 if x.Cmp(q) != 0 { |
454 t.Errorf("5) got %s want %s", x, q) | 443 t.Errorf("5) got %s want %s", x, q) |
455 } | 444 } |
456 } | 445 } |
LEFT | RIGHT |