encode_test.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. // Copyright 2014 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package rlp
  17. import (
  18. "bytes"
  19. "errors"
  20. "fmt"
  21. "io"
  22. "math/big"
  23. "runtime"
  24. "sync"
  25. "testing"
  26. "github.com/ethereum/go-ethereum/common/math"
  27. )
  28. type testEncoder struct {
  29. err error
  30. }
  31. func (e *testEncoder) EncodeRLP(w io.Writer) error {
  32. if e == nil {
  33. panic("EncodeRLP called on nil value")
  34. }
  35. if e.err != nil {
  36. return e.err
  37. }
  38. w.Write([]byte{0, 1, 0, 1, 0, 1, 0, 1, 0, 1})
  39. return nil
  40. }
  41. type testEncoderValueMethod struct{}
  42. func (e testEncoderValueMethod) EncodeRLP(w io.Writer) error {
  43. w.Write([]byte{0xFA, 0xFE, 0xF0})
  44. return nil
  45. }
  46. type byteEncoder byte
  47. func (e byteEncoder) EncodeRLP(w io.Writer) error {
  48. w.Write(EmptyList)
  49. return nil
  50. }
  51. type undecodableEncoder func()
  52. func (f undecodableEncoder) EncodeRLP(w io.Writer) error {
  53. w.Write([]byte{0xF5, 0xF5, 0xF5})
  54. return nil
  55. }
  56. type encodableReader struct {
  57. A, B uint
  58. }
  59. func (e *encodableReader) Read(b []byte) (int, error) {
  60. panic("called")
  61. }
  62. type namedByteType byte
  63. var (
  64. _ = Encoder(&testEncoder{})
  65. _ = Encoder(byteEncoder(0))
  66. reader io.Reader = &encodableReader{1, 2}
  67. )
  68. type encTest struct {
  69. val interface{}
  70. output, error string
  71. }
  72. var encTests = []encTest{
  73. // booleans
  74. {val: true, output: "01"},
  75. {val: false, output: "80"},
  76. // integers
  77. {val: uint32(0), output: "80"},
  78. {val: uint32(127), output: "7F"},
  79. {val: uint32(128), output: "8180"},
  80. {val: uint32(256), output: "820100"},
  81. {val: uint32(1024), output: "820400"},
  82. {val: uint32(0xFFFFFF), output: "83FFFFFF"},
  83. {val: uint32(0xFFFFFFFF), output: "84FFFFFFFF"},
  84. {val: uint64(0xFFFFFFFF), output: "84FFFFFFFF"},
  85. {val: uint64(0xFFFFFFFFFF), output: "85FFFFFFFFFF"},
  86. {val: uint64(0xFFFFFFFFFFFF), output: "86FFFFFFFFFFFF"},
  87. {val: uint64(0xFFFFFFFFFFFFFF), output: "87FFFFFFFFFFFFFF"},
  88. {val: uint64(0xFFFFFFFFFFFFFFFF), output: "88FFFFFFFFFFFFFFFF"},
  89. // big integers (should match uint for small values)
  90. {val: big.NewInt(0), output: "80"},
  91. {val: big.NewInt(1), output: "01"},
  92. {val: big.NewInt(127), output: "7F"},
  93. {val: big.NewInt(128), output: "8180"},
  94. {val: big.NewInt(256), output: "820100"},
  95. {val: big.NewInt(1024), output: "820400"},
  96. {val: big.NewInt(0xFFFFFF), output: "83FFFFFF"},
  97. {val: big.NewInt(0xFFFFFFFF), output: "84FFFFFFFF"},
  98. {val: big.NewInt(0xFFFFFFFFFF), output: "85FFFFFFFFFF"},
  99. {val: big.NewInt(0xFFFFFFFFFFFF), output: "86FFFFFFFFFFFF"},
  100. {val: big.NewInt(0xFFFFFFFFFFFFFF), output: "87FFFFFFFFFFFFFF"},
  101. {
  102. val: new(big.Int).SetBytes(unhex("102030405060708090A0B0C0D0E0F2")),
  103. output: "8F102030405060708090A0B0C0D0E0F2",
  104. },
  105. {
  106. val: new(big.Int).SetBytes(unhex("0100020003000400050006000700080009000A000B000C000D000E01")),
  107. output: "9C0100020003000400050006000700080009000A000B000C000D000E01",
  108. },
  109. {
  110. val: new(big.Int).SetBytes(unhex("010000000000000000000000000000000000000000000000000000000000000000")),
  111. output: "A1010000000000000000000000000000000000000000000000000000000000000000",
  112. },
  113. {
  114. val: veryBigInt,
  115. output: "89FFFFFFFFFFFFFFFFFF",
  116. },
  117. {
  118. val: veryVeryBigInt,
  119. output: "B848FFFFFFFFFFFFFFFFF800000000000000001BFFFFFFFFFFFFFFFFC8000000000000000045FFFFFFFFFFFFFFFFC800000000000000001BFFFFFFFFFFFFFFFFF8000000000000000001",
  120. },
  121. // non-pointer big.Int
  122. {val: *big.NewInt(0), output: "80"},
  123. {val: *big.NewInt(0xFFFFFF), output: "83FFFFFF"},
  124. // negative ints are not supported
  125. {val: big.NewInt(-1), error: "rlp: cannot encode negative big.Int"},
  126. {val: *big.NewInt(-1), error: "rlp: cannot encode negative big.Int"},
  127. // byte arrays
  128. {val: [0]byte{}, output: "80"},
  129. {val: [1]byte{0}, output: "00"},
  130. {val: [1]byte{1}, output: "01"},
  131. {val: [1]byte{0x7F}, output: "7F"},
  132. {val: [1]byte{0x80}, output: "8180"},
  133. {val: [1]byte{0xFF}, output: "81FF"},
  134. {val: [3]byte{1, 2, 3}, output: "83010203"},
  135. {val: [57]byte{1, 2, 3}, output: "B839010203000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"},
  136. // named byte type arrays
  137. {val: [0]namedByteType{}, output: "80"},
  138. {val: [1]namedByteType{0}, output: "00"},
  139. {val: [1]namedByteType{1}, output: "01"},
  140. {val: [1]namedByteType{0x7F}, output: "7F"},
  141. {val: [1]namedByteType{0x80}, output: "8180"},
  142. {val: [1]namedByteType{0xFF}, output: "81FF"},
  143. {val: [3]namedByteType{1, 2, 3}, output: "83010203"},
  144. {val: [57]namedByteType{1, 2, 3}, output: "B839010203000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"},
  145. // byte slices
  146. {val: []byte{}, output: "80"},
  147. {val: []byte{0}, output: "00"},
  148. {val: []byte{0x7E}, output: "7E"},
  149. {val: []byte{0x7F}, output: "7F"},
  150. {val: []byte{0x80}, output: "8180"},
  151. {val: []byte{1, 2, 3}, output: "83010203"},
  152. // named byte type slices
  153. {val: []namedByteType{}, output: "80"},
  154. {val: []namedByteType{0}, output: "00"},
  155. {val: []namedByteType{0x7E}, output: "7E"},
  156. {val: []namedByteType{0x7F}, output: "7F"},
  157. {val: []namedByteType{0x80}, output: "8180"},
  158. {val: []namedByteType{1, 2, 3}, output: "83010203"},
  159. // strings
  160. {val: "", output: "80"},
  161. {val: "\x7E", output: "7E"},
  162. {val: "\x7F", output: "7F"},
  163. {val: "\x80", output: "8180"},
  164. {val: "dog", output: "83646F67"},
  165. {
  166. val: "Lorem ipsum dolor sit amet, consectetur adipisicing eli",
  167. output: "B74C6F72656D20697073756D20646F6C6F722073697420616D65742C20636F6E7365637465747572206164697069736963696E6720656C69",
  168. },
  169. {
  170. val: "Lorem ipsum dolor sit amet, consectetur adipisicing elit",
  171. output: "B8384C6F72656D20697073756D20646F6C6F722073697420616D65742C20636F6E7365637465747572206164697069736963696E6720656C6974",
  172. },
  173. {
  174. val: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur mauris magna, suscipit sed vehicula non, iaculis faucibus tortor. Proin suscipit ultricies malesuada. Duis tortor elit, dictum quis tristique eu, ultrices at risus. Morbi a est imperdiet mi ullamcorper aliquet suscipit nec lorem. Aenean quis leo mollis, vulputate elit varius, consequat enim. Nulla ultrices turpis justo, et posuere urna consectetur nec. Proin non convallis metus. Donec tempor ipsum in mauris congue sollicitudin. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Suspendisse convallis sem vel massa faucibus, eget lacinia lacus tempor. Nulla quis ultricies purus. Proin auctor rhoncus nibh condimentum mollis. Aliquam consequat enim at metus luctus, a eleifend purus egestas. Curabitur at nibh metus. Nam bibendum, neque at auctor tristique, lorem libero aliquet arcu, non interdum tellus lectus sit amet eros. Cras rhoncus, metus ac ornare cursus, dolor justo ultrices metus, at ullamcorper volutpat",
  175. output: "B904004C6F72656D20697073756D20646F6C6F722073697420616D65742C20636F6E73656374657475722061646970697363696E6720656C69742E20437572616269747572206D6175726973206D61676E612C20737573636970697420736564207665686963756C61206E6F6E2C20696163756C697320666175636962757320746F72746F722E2050726F696E20737573636970697420756C74726963696573206D616C6573756164612E204475697320746F72746F7220656C69742C2064696374756D2071756973207472697374697175652065752C20756C7472696365732061742072697375732E204D6F72626920612065737420696D70657264696574206D6920756C6C616D636F7270657220616C6971756574207375736369706974206E6563206C6F72656D2E2041656E65616E2071756973206C656F206D6F6C6C69732C2076756C70757461746520656C6974207661726975732C20636F6E73657175617420656E696D2E204E756C6C6120756C74726963657320747572706973206A7573746F2C20657420706F73756572652075726E6120636F6E7365637465747572206E65632E2050726F696E206E6F6E20636F6E76616C6C6973206D657475732E20446F6E65632074656D706F7220697073756D20696E206D617572697320636F6E67756520736F6C6C696369747564696E2E20566573746962756C756D20616E746520697073756D207072696D697320696E206661756369627573206F726369206C756374757320657420756C74726963657320706F737565726520637562696C69612043757261653B2053757370656E646973736520636F6E76616C6C69732073656D2076656C206D617373612066617563696275732C2065676574206C6163696E6961206C616375732074656D706F722E204E756C6C61207175697320756C747269636965732070757275732E2050726F696E20617563746F722072686F6E637573206E69626820636F6E64696D656E74756D206D6F6C6C69732E20416C697175616D20636F6E73657175617420656E696D206174206D65747573206C75637475732C206120656C656966656E6420707572757320656765737461732E20437572616269747572206174206E696268206D657475732E204E616D20626962656E64756D2C206E6571756520617420617563746F72207472697374697175652C206C6F72656D206C696265726F20616C697175657420617263752C206E6F6E20696E74657264756D2074656C6C7573206C65637475732073697420616D65742065726F732E20437261732072686F6E6375732C206D65747573206163206F726E617265206375727375732C20646F6C6F72206A7573746F20756C747269636573206D657475732C20617420756C6C616D636F7270657220766F6C7574706174",
  176. },
  177. // slices
  178. {val: []uint{}, output: "C0"},
  179. {val: []uint{1, 2, 3}, output: "C3010203"},
  180. {
  181. // [ [], [[]], [ [], [[]] ] ]
  182. val: []interface{}{[]interface{}{}, [][]interface{}{{}}, []interface{}{[]interface{}{}, [][]interface{}{{}}}},
  183. output: "C7C0C1C0C3C0C1C0",
  184. },
  185. {
  186. val: []string{"aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg", "hhh", "iii", "jjj", "kkk", "lll", "mmm", "nnn", "ooo"},
  187. output: "F83C836161618362626283636363836464648365656583666666836767678368686883696969836A6A6A836B6B6B836C6C6C836D6D6D836E6E6E836F6F6F",
  188. },
  189. {
  190. val: []interface{}{uint(1), uint(0xFFFFFF), []interface{}{[]uint{4, 5, 5}}, "abc"},
  191. output: "CE0183FFFFFFC4C304050583616263",
  192. },
  193. {
  194. val: [][]string{
  195. {"asdf", "qwer", "zxcv"},
  196. {"asdf", "qwer", "zxcv"},
  197. {"asdf", "qwer", "zxcv"},
  198. {"asdf", "qwer", "zxcv"},
  199. {"asdf", "qwer", "zxcv"},
  200. {"asdf", "qwer", "zxcv"},
  201. {"asdf", "qwer", "zxcv"},
  202. {"asdf", "qwer", "zxcv"},
  203. {"asdf", "qwer", "zxcv"},
  204. {"asdf", "qwer", "zxcv"},
  205. {"asdf", "qwer", "zxcv"},
  206. {"asdf", "qwer", "zxcv"},
  207. {"asdf", "qwer", "zxcv"},
  208. {"asdf", "qwer", "zxcv"},
  209. {"asdf", "qwer", "zxcv"},
  210. {"asdf", "qwer", "zxcv"},
  211. {"asdf", "qwer", "zxcv"},
  212. {"asdf", "qwer", "zxcv"},
  213. {"asdf", "qwer", "zxcv"},
  214. {"asdf", "qwer", "zxcv"},
  215. {"asdf", "qwer", "zxcv"},
  216. {"asdf", "qwer", "zxcv"},
  217. {"asdf", "qwer", "zxcv"},
  218. {"asdf", "qwer", "zxcv"},
  219. {"asdf", "qwer", "zxcv"},
  220. {"asdf", "qwer", "zxcv"},
  221. {"asdf", "qwer", "zxcv"},
  222. {"asdf", "qwer", "zxcv"},
  223. {"asdf", "qwer", "zxcv"},
  224. {"asdf", "qwer", "zxcv"},
  225. {"asdf", "qwer", "zxcv"},
  226. {"asdf", "qwer", "zxcv"},
  227. },
  228. output: "F90200CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376",
  229. },
  230. // RawValue
  231. {val: RawValue(unhex("01")), output: "01"},
  232. {val: RawValue(unhex("82FFFF")), output: "82FFFF"},
  233. {val: []RawValue{unhex("01"), unhex("02")}, output: "C20102"},
  234. // structs
  235. {val: simplestruct{}, output: "C28080"},
  236. {val: simplestruct{A: 3, B: "foo"}, output: "C50383666F6F"},
  237. {val: &recstruct{5, nil}, output: "C205C0"},
  238. {val: &recstruct{5, &recstruct{4, &recstruct{3, nil}}}, output: "C605C404C203C0"},
  239. {val: &intField{X: 3}, error: "rlp: type int is not RLP-serializable (struct field rlp.intField.X)"},
  240. // struct tag "-"
  241. {val: &ignoredField{A: 1, B: 2, C: 3}, output: "C20103"},
  242. // struct tag "tail"
  243. {val: &tailRaw{A: 1, Tail: []RawValue{unhex("02"), unhex("03")}}, output: "C3010203"},
  244. {val: &tailRaw{A: 1, Tail: []RawValue{unhex("02")}}, output: "C20102"},
  245. {val: &tailRaw{A: 1, Tail: []RawValue{}}, output: "C101"},
  246. {val: &tailRaw{A: 1, Tail: nil}, output: "C101"},
  247. // struct tag "optional"
  248. {val: &optionalFields{}, output: "C180"},
  249. {val: &optionalFields{A: 1}, output: "C101"},
  250. {val: &optionalFields{A: 1, B: 2}, output: "C20102"},
  251. {val: &optionalFields{A: 1, B: 2, C: 3}, output: "C3010203"},
  252. {val: &optionalFields{A: 1, B: 0, C: 3}, output: "C3018003"},
  253. {val: &optionalAndTailField{A: 1}, output: "C101"},
  254. {val: &optionalAndTailField{A: 1, B: 2}, output: "C20102"},
  255. {val: &optionalAndTailField{A: 1, Tail: []uint{5, 6}}, output: "C401800506"},
  256. {val: &optionalAndTailField{A: 1, Tail: []uint{5, 6}}, output: "C401800506"},
  257. {val: &optionalBigIntField{A: 1}, output: "C101"},
  258. {val: &optionalPtrField{A: 1}, output: "C101"},
  259. {val: &optionalPtrFieldNil{A: 1}, output: "C101"},
  260. // nil
  261. {val: (*uint)(nil), output: "80"},
  262. {val: (*string)(nil), output: "80"},
  263. {val: (*[]byte)(nil), output: "80"},
  264. {val: (*[10]byte)(nil), output: "80"},
  265. {val: (*big.Int)(nil), output: "80"},
  266. {val: (*[]string)(nil), output: "C0"},
  267. {val: (*[10]string)(nil), output: "C0"},
  268. {val: (*[]interface{})(nil), output: "C0"},
  269. {val: (*[]struct{ uint })(nil), output: "C0"},
  270. {val: (*interface{})(nil), output: "C0"},
  271. // nil struct fields
  272. {
  273. val: struct {
  274. X *[]byte
  275. }{},
  276. output: "C180",
  277. },
  278. {
  279. val: struct {
  280. X *[2]byte
  281. }{},
  282. output: "C180",
  283. },
  284. {
  285. val: struct {
  286. X *uint64
  287. }{},
  288. output: "C180",
  289. },
  290. {
  291. val: struct {
  292. X *uint64 `rlp:"nilList"`
  293. }{},
  294. output: "C1C0",
  295. },
  296. {
  297. val: struct {
  298. X *[]uint64
  299. }{},
  300. output: "C1C0",
  301. },
  302. {
  303. val: struct {
  304. X *[]uint64 `rlp:"nilString"`
  305. }{},
  306. output: "C180",
  307. },
  308. // interfaces
  309. {val: []io.Reader{reader}, output: "C3C20102"}, // the contained value is a struct
  310. // Encoder
  311. {val: (*testEncoder)(nil), output: "C0"},
  312. {val: &testEncoder{}, output: "00010001000100010001"},
  313. {val: &testEncoder{errors.New("test error")}, error: "test error"},
  314. {val: struct{ E testEncoderValueMethod }{}, output: "C3FAFEF0"},
  315. {val: struct{ E *testEncoderValueMethod }{}, output: "C1C0"},
  316. // Verify that the Encoder interface works for unsupported types like func().
  317. {val: undecodableEncoder(func() {}), output: "F5F5F5"},
  318. // Verify that pointer method testEncoder.EncodeRLP is called for
  319. // addressable non-pointer values.
  320. {val: &struct{ TE testEncoder }{testEncoder{}}, output: "CA00010001000100010001"},
  321. {val: &struct{ TE testEncoder }{testEncoder{errors.New("test error")}}, error: "test error"},
  322. // Verify the error for non-addressable non-pointer Encoder.
  323. {val: testEncoder{}, error: "rlp: unadressable value of type rlp.testEncoder, EncodeRLP is pointer method"},
  324. // Verify Encoder takes precedence over []byte.
  325. {val: []byteEncoder{0, 1, 2, 3, 4}, output: "C5C0C0C0C0C0"},
  326. }
  327. func runEncTests(t *testing.T, f func(val interface{}) ([]byte, error)) {
  328. for i, test := range encTests {
  329. output, err := f(test.val)
  330. if err != nil && test.error == "" {
  331. t.Errorf("test %d: unexpected error: %v\nvalue %#v\ntype %T",
  332. i, err, test.val, test.val)
  333. continue
  334. }
  335. if test.error != "" && fmt.Sprint(err) != test.error {
  336. t.Errorf("test %d: error mismatch\ngot %v\nwant %v\nvalue %#v\ntype %T",
  337. i, err, test.error, test.val, test.val)
  338. continue
  339. }
  340. if err == nil && !bytes.Equal(output, unhex(test.output)) {
  341. t.Errorf("test %d: output mismatch:\ngot %X\nwant %s\nvalue %#v\ntype %T",
  342. i, output, test.output, test.val, test.val)
  343. }
  344. }
  345. }
  346. func TestEncode(t *testing.T) {
  347. runEncTests(t, func(val interface{}) ([]byte, error) {
  348. b := new(bytes.Buffer)
  349. err := Encode(b, val)
  350. return b.Bytes(), err
  351. })
  352. }
  353. func TestEncodeToBytes(t *testing.T) {
  354. runEncTests(t, EncodeToBytes)
  355. }
  356. func TestEncodeAppendToBytes(t *testing.T) {
  357. buffer := make([]byte, 20)
  358. runEncTests(t, func(val interface{}) ([]byte, error) {
  359. w := NewEncoderBuffer(nil)
  360. defer w.Flush()
  361. err := Encode(w, val)
  362. if err != nil {
  363. return nil, err
  364. }
  365. output := w.AppendToBytes(buffer[:0])
  366. return output, nil
  367. })
  368. }
  369. func TestEncodeToReader(t *testing.T) {
  370. runEncTests(t, func(val interface{}) ([]byte, error) {
  371. _, r, err := EncodeToReader(val)
  372. if err != nil {
  373. return nil, err
  374. }
  375. return io.ReadAll(r)
  376. })
  377. }
  378. func TestEncodeToReaderPiecewise(t *testing.T) {
  379. runEncTests(t, func(val interface{}) ([]byte, error) {
  380. size, r, err := EncodeToReader(val)
  381. if err != nil {
  382. return nil, err
  383. }
  384. // read output piecewise
  385. output := make([]byte, size)
  386. for start, end := 0, 0; start < size; start = end {
  387. if remaining := size - start; remaining < 3 {
  388. end += remaining
  389. } else {
  390. end = start + 3
  391. }
  392. n, err := r.Read(output[start:end])
  393. end = start + n
  394. if err == io.EOF {
  395. break
  396. } else if err != nil {
  397. return nil, err
  398. }
  399. }
  400. return output, nil
  401. })
  402. }
  403. // This is a regression test verifying that encReader
  404. // returns its encbuf to the pool only once.
  405. func TestEncodeToReaderReturnToPool(t *testing.T) {
  406. buf := make([]byte, 50)
  407. wg := new(sync.WaitGroup)
  408. for i := 0; i < 5; i++ {
  409. wg.Add(1)
  410. go func() {
  411. for i := 0; i < 1000; i++ {
  412. _, r, _ := EncodeToReader("foo")
  413. io.ReadAll(r)
  414. r.Read(buf)
  415. r.Read(buf)
  416. r.Read(buf)
  417. r.Read(buf)
  418. }
  419. wg.Done()
  420. }()
  421. }
  422. wg.Wait()
  423. }
  424. var sink interface{}
  425. func BenchmarkIntsize(b *testing.B) {
  426. for i := 0; i < b.N; i++ {
  427. sink = intsize(0x12345678)
  428. }
  429. }
  430. func BenchmarkPutint(b *testing.B) {
  431. buf := make([]byte, 8)
  432. for i := 0; i < b.N; i++ {
  433. putint(buf, 0x12345678)
  434. sink = buf
  435. }
  436. }
  437. func BenchmarkEncodeBigInts(b *testing.B) {
  438. ints := make([]*big.Int, 200)
  439. for i := range ints {
  440. ints[i] = math.BigPow(2, int64(i))
  441. }
  442. out := bytes.NewBuffer(make([]byte, 0, 4096))
  443. b.ResetTimer()
  444. b.ReportAllocs()
  445. for i := 0; i < b.N; i++ {
  446. out.Reset()
  447. if err := Encode(out, ints); err != nil {
  448. b.Fatal(err)
  449. }
  450. }
  451. }
  452. func BenchmarkEncodeConcurrentInterface(b *testing.B) {
  453. type struct1 struct {
  454. A string
  455. B *big.Int
  456. C [20]byte
  457. }
  458. value := []interface{}{
  459. uint(999),
  460. &struct1{A: "hello", B: big.NewInt(0xFFFFFFFF)},
  461. [10]byte{1, 2, 3, 4, 5, 6},
  462. []string{"yeah", "yeah", "yeah"},
  463. }
  464. var wg sync.WaitGroup
  465. for cpu := 0; cpu < runtime.NumCPU(); cpu++ {
  466. wg.Add(1)
  467. go func() {
  468. defer wg.Done()
  469. var buffer bytes.Buffer
  470. for i := 0; i < b.N; i++ {
  471. buffer.Reset()
  472. err := Encode(&buffer, value)
  473. if err != nil {
  474. panic(err)
  475. }
  476. }
  477. }()
  478. }
  479. wg.Wait()
  480. }
  481. type byteArrayStruct struct {
  482. A [20]byte
  483. B [32]byte
  484. C [32]byte
  485. }
  486. func BenchmarkEncodeByteArrayStruct(b *testing.B) {
  487. var out bytes.Buffer
  488. var value byteArrayStruct
  489. b.ReportAllocs()
  490. for i := 0; i < b.N; i++ {
  491. out.Reset()
  492. if err := Encode(&out, &value); err != nil {
  493. b.Fatal(err)
  494. }
  495. }
  496. }
  497. type structSliceElem struct {
  498. X uint64
  499. Y uint64
  500. Z uint64
  501. }
  502. type structPtrSlice []*structSliceElem
  503. func BenchmarkEncodeStructPtrSlice(b *testing.B) {
  504. var out bytes.Buffer
  505. var value = structPtrSlice{
  506. &structSliceElem{1, 1, 1},
  507. &structSliceElem{2, 2, 2},
  508. &structSliceElem{3, 3, 3},
  509. &structSliceElem{5, 5, 5},
  510. &structSliceElem{6, 6, 6},
  511. &structSliceElem{7, 7, 7},
  512. }
  513. b.ReportAllocs()
  514. for i := 0; i < b.N; i++ {
  515. out.Reset()
  516. if err := Encode(&out, &value); err != nil {
  517. b.Fatal(err)
  518. }
  519. }
  520. }