intmarsh_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. package big
  5. import (
  6. "bytes"
  7. "encoding/gob"
  8. "encoding/json"
  9. "encoding/xml"
  10. "testing"
  11. )
  12. var encodingTests = []string{
  13. "0",
  14. "1",
  15. "2",
  16. "10",
  17. "1000",
  18. "1234567890",
  19. "298472983472983471903246121093472394872319615612417471234712061",
  20. }
  21. func TestIntGobEncoding(t *testing.T) {
  22. var medium bytes.Buffer
  23. enc := gob.NewEncoder(&medium)
  24. dec := gob.NewDecoder(&medium)
  25. for _, test := range encodingTests {
  26. for _, sign := range []string{"", "+", "-"} {
  27. x := sign + test
  28. medium.Reset() // empty buffer for each test case (in case of failures)
  29. var tx Int
  30. tx.SetString(x, 10)
  31. if err := enc.Encode(&tx); err != nil {
  32. t.Errorf("encoding of %s failed: %s", &tx, err)
  33. continue
  34. }
  35. var rx Int
  36. if err := dec.Decode(&rx); err != nil {
  37. t.Errorf("decoding of %s failed: %s", &tx, err)
  38. continue
  39. }
  40. if rx.Cmp(&tx) != 0 {
  41. t.Errorf("transmission of %s failed: got %s want %s", &tx, &rx, &tx)
  42. }
  43. }
  44. }
  45. }
  46. // Sending a nil Int pointer (inside a slice) on a round trip through gob should yield a zero.
  47. // TODO: top-level nils.
  48. func TestGobEncodingNilIntInSlice(t *testing.T) {
  49. buf := new(bytes.Buffer)
  50. enc := gob.NewEncoder(buf)
  51. dec := gob.NewDecoder(buf)
  52. var in = make([]*Int, 1)
  53. err := enc.Encode(&in)
  54. if err != nil {
  55. t.Errorf("gob encode failed: %q", err)
  56. }
  57. var out []*Int
  58. err = dec.Decode(&out)
  59. if err != nil {
  60. t.Fatalf("gob decode failed: %q", err)
  61. }
  62. if len(out) != 1 {
  63. t.Fatalf("wrong len; want 1 got %d", len(out))
  64. }
  65. var zero Int
  66. if out[0].Cmp(&zero) != 0 {
  67. t.Fatalf("transmission of (*Int)(nil) failed: got %s want 0", out)
  68. }
  69. }
  70. func TestIntJSONEncoding(t *testing.T) {
  71. for _, test := range encodingTests {
  72. for _, sign := range []string{"", "+", "-"} {
  73. x := sign + test
  74. var tx Int
  75. tx.SetString(x, 10)
  76. b, err := json.Marshal(&tx)
  77. if err != nil {
  78. t.Errorf("marshaling of %s failed: %s", &tx, err)
  79. continue
  80. }
  81. var rx Int
  82. if err := json.Unmarshal(b, &rx); err != nil {
  83. t.Errorf("unmarshaling of %s failed: %s", &tx, err)
  84. continue
  85. }
  86. if rx.Cmp(&tx) != 0 {
  87. t.Errorf("JSON encoding of %s failed: got %s want %s", &tx, &rx, &tx)
  88. }
  89. }
  90. }
  91. }
  92. func TestIntXMLEncoding(t *testing.T) {
  93. for _, test := range encodingTests {
  94. for _, sign := range []string{"", "+", "-"} {
  95. x := sign + test
  96. var tx Int
  97. tx.SetString(x, 0)
  98. b, err := xml.Marshal(&tx)
  99. if err != nil {
  100. t.Errorf("marshaling of %s failed: %s", &tx, err)
  101. continue
  102. }
  103. var rx Int
  104. if err := xml.Unmarshal(b, &rx); err != nil {
  105. t.Errorf("unmarshaling of %s failed: %s", &tx, err)
  106. continue
  107. }
  108. if rx.Cmp(&tx) != 0 {
  109. t.Errorf("XML encoding of %s failed: got %s want %s", &tx, &rx, &tx)
  110. }
  111. }
  112. }
  113. }