github.com/ergo-services/ergo@v1.999.224/etf/decode_test.go (about) 1 package etf 2 3 import ( 4 "math/big" 5 "reflect" 6 "testing" 7 ) 8 9 func TestDecodeAtom(t *testing.T) { 10 expected := Atom("abc") 11 packet := []byte{ettAtomUTF8, 0, 3, 97, 98, 99} 12 term, _, err := Decode(packet, []Atom{}, DecodeOptions{}) 13 if err != nil || term != expected { 14 t.Fatal(err) 15 } 16 17 packet = []byte{ettSmallAtomUTF8, 3, 97, 98, 99} 18 term, _, err = Decode(packet, []Atom{}, DecodeOptions{}) 19 if err != nil || term != expected { 20 t.Fatal(err) 21 } 22 23 packet = []byte{ettSmallAtomUTF8, 4, 97, 98, 99} 24 term, _, err = Decode(packet, []Atom{}, DecodeOptions{}) 25 if err != errMalformedSmallAtomUTF8 { 26 t.Fatal(err) 27 } 28 29 packet = []byte{119} 30 term, _, err = Decode(packet, []Atom{}, DecodeOptions{}) 31 if err != errMalformedSmallAtomUTF8 { 32 t.Fatal(err) 33 } 34 35 } 36 37 func TestDecodeString(t *testing.T) { 38 expected := "abc" 39 packet := []byte{ettString, 0, 3, 97, 98, 99} 40 term, _, err := Decode(packet, []Atom{}, DecodeOptions{}) 41 if err != nil || term != expected { 42 t.Fatal(err) 43 } 44 45 packet = []byte{ettString, 3, 97, 98, 99} 46 term, _, err = Decode(packet, []Atom{}, DecodeOptions{}) 47 if err != errMalformedString { 48 t.Fatal(err) 49 } 50 } 51 52 func TestDecodeNewFloat(t *testing.T) { 53 expected := float64(2.1) 54 packet := []byte{ettNewFloat, 64, 0, 204, 204, 204, 204, 204, 205} 55 term, _, err := Decode(packet, []Atom{}, DecodeOptions{}) 56 if err != nil || term != expected { 57 t.Fatal(err) 58 } 59 60 packet = []byte{ettNewFloat, 64, 0, 204, 204, 204, 204, 204} 61 term, _, err = Decode(packet, []Atom{}, DecodeOptions{}) 62 if err != errMalformedNewFloat { 63 t.Fatal(err) 64 } 65 } 66 67 func TestDecodeInteger(t *testing.T) { 68 expected := int(88) 69 packet := []byte{ettSmallInteger, 88} 70 71 term, _, err := Decode(packet, []Atom{}, DecodeOptions{}) 72 if err != nil || term != expected { 73 t.Fatal(err) 74 } 75 76 packet = []byte{ettSmallInteger} 77 term, _, err = Decode(packet, []Atom{}, DecodeOptions{}) 78 if err != errMalformedSmallInteger { 79 t.Fatal(err) 80 } 81 82 expectedInteger := int64(-1234567890) 83 packet = []byte{ettInteger, 182, 105, 253, 46} 84 term, _, err = Decode(packet, []Atom{}, DecodeOptions{}) 85 if err != nil || term != expectedInteger { 86 t.Fatal(err, expectedInteger, term) 87 } 88 89 packet = []byte{ettInteger, 182, 105, 253} 90 term, _, err = Decode(packet, []Atom{}, DecodeOptions{}) 91 if err != errMalformedInteger { 92 t.Fatal(err) 93 } 94 95 //-9223372036854775808 96 expectedBigInt64 := int64(-9223372036854775808) 97 packet = []byte{ettSmallBig, 8, 1, 0, 0, 0, 0, 0, 0, 0, 128} 98 term, _, err = Decode(packet, []Atom{}, DecodeOptions{}) 99 if err != nil || term != expectedBigInt64 { 100 t.Fatal(err, term, expectedBigInt64) 101 } 102 103 largeBigString := "-12345678909876543210123456789098765432101234567890987654321012345678909876543210123456789098765432101234567890987654321012345678909876543210123456789098765432101234567890987654321012345678909876543210123456789098765432101234567890987654321012345678909876543210123456789098765432101234567890987654321012345678909876543210123456789098765432101234567890987654321012345678909876543210123456789098765432101234567890987654321012345678909876543210123456789098765432101234567890987654321012345678909876543210123456789098765432101234567890987654321012345678909876543210123456789098765432101234567890987654321012345678909876543210123456789098765432101234567890987654321012345678909876543210123456789098765432101234567890987654321012345678909876543210123456789098765432101234567890987654321012345678909876543210123456789098765432101234567890987654321012345678909876543210123456789098765432101234567890987654321012345678909876543210123456789098765432101234567890" 104 105 bigInt := new(big.Int) 106 bigInt.SetString(largeBigString, 10) 107 packet = []byte{ettLargeBig, 0, 0, 1, 139, 1, 210, 106, 44, 197, 54, 176, 151, 83, 243, 108 228, 193, 133, 194, 193, 21, 90, 196, 4, 252, 150, 226, 188, 79, 11, 8, 109 190, 106, 189, 21, 73, 176, 196, 54, 221, 118, 232, 212, 126, 141, 110 118, 154, 78, 238, 143, 34, 118, 245, 135, 17, 231, 224, 86, 71, 12, 111 175, 207, 224, 19, 206, 5, 241, 241, 207, 125, 243, 87, 18, 14, 162, 112 71, 3, 244, 85, 240, 211, 12, 141, 5, 38, 124, 232, 122, 104, 228, 36, 113 40, 124, 109, 196, 20, 94, 46, 167, 215, 107, 53, 51, 28, 45, 249, 146, 114 151, 18, 11, 246, 151, 220, 138, 139, 97, 63, 166, 255, 101, 12, 153, 115 247, 201, 62, 9, 131, 235, 67, 85, 13, 151, 200, 233, 239, 35, 224, 10, 116 101, 144, 107, 82, 206, 71, 226, 67, 212, 254, 15, 29, 122, 128, 38, 117 230, 60, 97, 146, 52, 241, 216, 220, 114, 82, 90, 166, 207, 31, 63, 118 112, 254, 19, 111, 225, 104, 159, 133, 186, 15, 5, 93, 220, 56, 6, 4, 119 197, 4, 196, 204, 94, 34, 144, 141, 31, 165, 188, 241, 105, 197, 82, 120 69, 77, 136, 207, 152, 76, 112, 79, 57, 159, 232, 165, 215, 0, 164, 121 231, 132, 124, 252, 90, 91, 71, 198, 254, 203, 83, 96, 42, 35, 240, 122 218, 174, 37, 112, 86, 218, 203, 135, 7, 88, 24, 245, 50, 173, 72, 133, 123 70, 2, 160, 235, 61, 151, 28, 124, 173, 254, 244, 37, 96, 19, 40, 192, 124 194, 51, 75, 51, 186, 229, 93, 142, 165, 50, 43, 129, 0, 78, 253, 159, 125 105, 151, 150, 253, 24, 109, 22, 123, 95, 55, 143, 126, 122, 109, 57, 126 73, 240, 191, 25, 140, 131, 27, 64, 252, 238, 174, 211, 89, 167, 38, 127 137, 32, 176, 174, 122, 64, 66, 171, 175, 113, 174, 247, 236, 67, 180, 128 179, 23, 58, 17, 117, 223, 18, 184, 223, 156, 151, 179, 18, 84, 145, 129 16, 18, 194, 121, 19, 186, 170, 49, 21, 7, 108, 174, 89, 59, 53, 62, 130 247, 232, 9, 184, 242, 60, 137, 96, 54, 183, 89, 206, 219, 81, 208, 131 214, 197, 254, 207, 3, 41, 224, 169, 181, 56, 132, 18, 116, 141, 89, 132 185, 133, 186, 46, 81, 244, 139, 188, 171, 206, 52, 225, 160, 232, 133 246, 254, 193, 1} 134 135 term, _, err = Decode(packet, []Atom{}, DecodeOptions{}) 136 if err != nil || bigInt.Cmp(term.(*big.Int)) != 0 { 137 t.Fatal(err, term, bigInt) 138 } 139 140 //-123456789098 should be treated as int64 141 expectedInt64 := int64(-123456789098) 142 packet = []byte{ettSmallBig, 5, 1, 106, 26, 153, 190, 28} 143 term, _, err = Decode(packet, []Atom{}, DecodeOptions{}) 144 if err != nil || term != expectedInt64 { 145 t.Fatal(err, term, expectedInt64) 146 } 147 148 // 18446744073709551615 149 expectedUint64 := uint64(18446744073709551615) 150 packet = []byte{ettSmallBig, 8, 0, 255, 255, 255, 255, 255, 255, 255, 255} 151 term, _, err = Decode(packet, []Atom{}, DecodeOptions{}) 152 if err != nil || term != expectedUint64 { 153 t.Fatal(err, term, expectedUint64) 154 } 155 } 156 157 func TestDecodeList(t *testing.T) { 158 expected := List{3.14, Atom("abc"), int64(987654321)} 159 packet := []byte{ettList, 0, 0, 0, 3, 70, 64, 9, 30, 184, 81, 235, 133, 31, 100, 0, 3, 97, 160 98, 99, 98, 58, 222, 104, 177, 106} 161 term, _, err := Decode(packet, []Atom{}, DecodeOptions{}) 162 if err != nil { 163 t.Fatal(err) 164 } 165 result := term.(List) 166 if !reflect.DeepEqual(expected, result) { 167 t.Fatal("result != expected") 168 } 169 170 } 171 172 func TestDecodeListNested(t *testing.T) { 173 // [1,[2,3,[4,5],6]] 174 expected := List{1, List{2, 3, List{4, 5}, 6}} 175 packet := []byte{108, 0, 0, 0, 2, 97, 1, 108, 0, 0, 0, 4, 97, 2, 97, 3, 108, 0, 0, 0, 2, 97, 4, 97, 5, 106, 176 97, 6, 106, 106} 177 178 term, _, err := Decode(packet, []Atom{}, DecodeOptions{}) 179 if err != nil { 180 t.Fatal(err) 181 } 182 183 result := term.(List) 184 if !reflect.DeepEqual(expected, result) { 185 t.Fatal("result != expected") 186 } 187 } 188 189 func TestDecodeTuple(t *testing.T) { 190 expected := Tuple{3.14, Atom("abc"), int64(987654321)} 191 packet := []byte{ettSmallTuple, 3, 70, 64, 9, 30, 184, 81, 235, 133, 31, 100, 0, 3, 97, 98, 99, 192 98, 58, 222, 104, 177} 193 194 term, _, err := Decode(packet, []Atom{}, DecodeOptions{}) 195 if err != nil { 196 t.Fatal(err) 197 } 198 result := term.(Tuple) 199 if !reflect.DeepEqual(expected, result) { 200 t.Fatal("result != expected") 201 } 202 } 203 204 func TestDecodeMap(t *testing.T) { 205 expected := Map{ 206 Atom("abc"): 123, 207 "abc": 4.56, 208 } 209 packet := []byte{116, 0, 0, 0, 2, 100, 0, 3, 97, 98, 99, 97, 123, 107, 0, 3, 97, 98, 210 99, 70, 64, 18, 61, 112, 163, 215, 10, 61} 211 212 term, _, err := Decode(packet, []Atom{}, DecodeOptions{}) 213 if err != nil { 214 t.Fatal(err) 215 } 216 217 result := term.(Map) 218 if !reflect.DeepEqual(expected, result) { 219 t.Fatal("result != expected") 220 } 221 222 } 223 224 func TestDecodeBinary(t *testing.T) { 225 expected := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0} 226 packet := []byte{ettBinary, 0, 0, 0, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0} 227 228 term, _, err := Decode(packet, []Atom{}, DecodeOptions{}) 229 if err != nil { 230 t.Fatal(err) 231 } 232 233 result := term.([]byte) 234 if !reflect.DeepEqual(expected, result) { 235 t.Fatal("result != expected") 236 } 237 } 238 239 func TestDecodeBitBinary(t *testing.T) { 240 expected := []byte{1, 2, 3, 4, 5} 241 packet := []byte{77, 0, 0, 0, 5, 3, 1, 2, 3, 4, 160} 242 243 term, _, err := Decode(packet, []Atom{}, DecodeOptions{}) 244 if err != nil { 245 t.Fatal(err) 246 } 247 248 result := term.([]byte) 249 if !reflect.DeepEqual(expected, result) { 250 t.Fatal("result != expected") 251 } 252 253 } 254 255 func TestDecodePid(t *testing.T) { 256 expected := Pid{ 257 Node: Atom("erl-demo@127.0.0.1"), 258 ID: 142, 259 Creation: 2, 260 } 261 packet := []byte{103, 100, 0, 18, 101, 114, 108, 45, 100, 101, 109, 111, 64, 49, 262 50, 55, 46, 48, 46, 48, 46, 49, 0, 0, 0, 142, 0, 0, 0, 0, 2} 263 term, _, err := Decode(packet, []Atom{}, DecodeOptions{}) 264 if err != nil { 265 t.Fatal(err) 266 } 267 268 result := term.(Pid) 269 if !reflect.DeepEqual(expected, result) { 270 t.Fatal("result != expected") 271 } 272 } 273 274 func TestDecodePidWithCacheAtom(t *testing.T) { 275 expected := Pid{ 276 Node: Atom("erl-demo@127.0.0.1"), 277 ID: 142, 278 Creation: 2, 279 } 280 packet := []byte{103, ettCacheRef, 0, 0, 0, 0, 142, 0, 0, 0, 0, 2} 281 cache := []Atom{Atom("erl-demo@127.0.0.1")} 282 term, _, err := Decode(packet, cache, DecodeOptions{}) 283 if err != nil { 284 t.Fatal(err) 285 } 286 287 result := term.(Pid) 288 if !reflect.DeepEqual(expected, result) { 289 t.Fatal("result != expected") 290 } 291 } 292 293 func TestDecodeRef(t *testing.T) { 294 expected := Ref{ 295 Node: Atom("erl-demo@127.0.0.1"), 296 Creation: 2, 297 ID: [5]uint32{73444, 3082813441, 2373634851}, 298 } 299 packet := []byte{114, 0, 3, 100, 0, 18, 101, 114, 108, 45, 100, 101, 109, 111, 64, 300 49, 50, 55, 46, 48, 46, 48, 46, 49, 2, 0, 1, 30, 228, 183, 192, 0, 1, 141, 301 122, 203, 35} 302 term, _, err := Decode(packet, []Atom{}, DecodeOptions{}) 303 if err != nil { 304 t.Fatal(err) 305 } 306 307 result := term.(Ref) 308 if !reflect.DeepEqual(expected, result) { 309 t.Fatal("result != expected") 310 } 311 } 312 313 func TestDecodeRefWithAtomCache(t *testing.T) { 314 expected := Ref{ 315 Node: Atom("erl-demo@127.0.0.1"), 316 Creation: 2, 317 ID: [5]uint32{73444, 3082813441, 2373634851}, 318 } 319 packet := []byte{114, 0, 3, ettCacheRef, 0, 2, 0, 1, 30, 228, 183, 192, 0, 1, 141, 320 122, 203, 35} 321 cache := []Atom{Atom("erl-demo@127.0.0.1")} 322 323 term, _, err := Decode(packet, cache, DecodeOptions{}) 324 if err != nil { 325 t.Fatal(err) 326 } 327 328 result := term.(Ref) 329 if !reflect.DeepEqual(expected, result) { 330 t.Fatal("result != expected") 331 } 332 } 333 func TestDecodeTupleRefPid(t *testing.T) { 334 expected := Tuple{ 335 Ref{ 336 Node: Atom("erl-demo@127.0.0.1"), 337 Creation: 2, 338 ID: [5]uint32{0x11f1c, 0xb7c00001, 0x8d7acb23}}, 339 Pid{ 340 Node: Atom("erl-demo@127.0.0.1"), 341 ID: 0x8e, 342 Creation: 0x2}} 343 packet := []byte{ettSmallTuple, 2, ettNewRef, 0, 3, ettAtom, 0, 18, 101, 114, 108, 45, 100, 101, 109, 344 111, 64, 49, 50, 55, 46, 48, 46, 48, 46, 49, 2, 0, 1, 31, 28, 183, 192, 0, 345 1, 141, 122, 203, 35, 103, 100, 0, 18, 101, 114, 108, 45, 100, 101, 346 109, 111, 64, 49, 50, 55, 46, 48, 46, 48, 46, 49, 0, 0, 0, 142, 0, 0, 0, 0, 347 2} 348 term, _, err := Decode(packet, []Atom{}, DecodeOptions{}) 349 if err != nil { 350 t.Fatal(err) 351 } 352 353 result := term.(Tuple) 354 if !reflect.DeepEqual(expected, result) { 355 t.Fatal("result != expected") 356 } 357 } 358 359 func TestDecodeTupleRefPidWithAtomCache(t *testing.T) { 360 expected := Tuple{ 361 Ref{ 362 Node: Atom("erl-demo@127.0.0.1"), 363 Creation: 2, 364 ID: [5]uint32{0x11f1c, 0xb7c00001, 0x8d7acb23}}, 365 Pid{ 366 Node: Atom("erl-demo@127.0.0.1"), 367 ID: 0x8e, 368 Creation: 0x2}} 369 packet := []byte{ettSmallTuple, 2, ettNewRef, 0, 3, ettCacheRef, 0, 370 2, 0, 1, 31, 28, 183, 192, 0, 1, 141, 122, 203, 35, 103, ettCacheRef, 0, 371 0, 0, 0, 142, 0, 0, 0, 0, 2} 372 cache := []Atom{Atom("erl-demo@127.0.0.1")} 373 term, _, err := Decode(packet, cache, DecodeOptions{}) 374 if err != nil { 375 t.Fatal(err) 376 } 377 378 result := term.(Tuple) 379 if !reflect.DeepEqual(expected, result) { 380 t.Fatal("result != expected") 381 } 382 } 383 func TestDecodePort(t *testing.T) { 384 expected := Port{ 385 Node: Atom("erl-demo@127.0.0.1"), 386 Creation: 2, 387 ID: 32, 388 } 389 packet := []byte{102, 100, 0, 18, 101, 114, 108, 45, 100, 101, 109, 111, 64, 49, 390 50, 55, 46, 48, 46, 48, 46, 49, 0, 0, 0, 32, 2} 391 392 term, _, err := Decode(packet, []Atom{}, DecodeOptions{}) 393 if err != nil { 394 t.Fatal(err) 395 } 396 397 result := term.(Port) 398 if !reflect.DeepEqual(expected, result) { 399 t.Fatal("result != expected") 400 } 401 } 402 403 func TestDecodeComplex(t *testing.T) { 404 //{"hello",[], #{v1 => [{3,13,3.13}, {abc, "abc"}], v2 => 12345}}. 405 expected := Tuple{"hello", List{}, 406 Map{Atom("v1"): List{Tuple{3, 13, 3.13}, Tuple{Atom("abc"), "abc"}}, 407 Atom("v2"): int64(12345)}} 408 packet := []byte{104, 3, 107, 0, 5, 104, 101, 108, 108, 111, 106, 116, 0, 0, 0, 2, 409 100, 0, 2, 118, 49, 108, 0, 0, 0, 2, 104, 3, 97, 3, 97, 13, 70, 64, 9, 10, 410 61, 112, 163, 215, 10, 104, 2, 100, 0, 3, 97, 98, 99, 107, 0, 3, 97, 98, 411 99, 106, 100, 0, 2, 118, 50, 98, 0, 0, 48, 57} 412 term, _, err := Decode(packet, []Atom{}, DecodeOptions{}) 413 if err != nil { 414 t.Fatal(err) 415 } 416 417 result := term.(Tuple) 418 if !reflect.DeepEqual(expected, result) { 419 t.Errorf("got %#v, want %#v", result, expected) 420 t.Fatal("result != expected") 421 } 422 } 423 424 var packetFunction []byte 425 426 func TestDecodeFunction(t *testing.T) { 427 // A = fun(X) -> X*2 end. 428 packet := []byte{112, 0, 0, 3, 76, 1, 245, 82, 198, 227, 120, 209, 152, 67, 80, 234, 429 138, 144, 123, 165, 151, 196, 0, 0, 0, 6, 0, 0, 0, 1, 100, 0, 8, 101, 114, 430 108, 95, 101, 118, 97, 108, 97, 6, 98, 7, 170, 150, 55, 103, 100, 0, 20, 431 101, 114, 108, 45, 100, 101, 109, 111, 50, 50, 64, 49, 50, 55, 46, 48, 432 46, 48, 46, 49, 0, 0, 0, 83, 0, 0, 0, 0, 1, 104, 4, 106, 104, 2, 100, 0, 4, 433 101, 118, 97, 108, 112, 0, 0, 2, 29, 3, 196, 150, 93, 173, 104, 167, 434 134, 253, 184, 200, 203, 147, 166, 63, 88, 201, 0, 0, 0, 21, 0, 0, 0, 4, 435 100, 0, 5, 115, 104, 101, 108, 108, 97, 21, 98, 6, 36, 178, 237, 103, 436 100, 0, 20, 101, 114, 108, 45, 100, 101, 109, 111, 50, 50, 64, 49, 50, 437 55, 46, 48, 46, 48, 46, 49, 0, 0, 0, 83, 0, 0, 0, 0, 1, 104, 2, 100, 0, 5, 438 118, 97, 108, 117, 101, 112, 0, 0, 0, 110, 2, 196, 150, 93, 173, 104, 439 167, 134, 253, 184, 200, 203, 147, 166, 63, 88, 201, 0, 0, 0, 5, 0, 0, 0, 440 1, 100, 0, 5, 115, 104, 101, 108, 108, 97, 5, 98, 6, 36, 178, 237, 103, 441 100, 0, 20, 101, 114, 108, 45, 100, 101, 109, 111, 50, 50, 64, 49, 50, 442 55, 46, 48, 46, 48, 46, 49, 0, 0, 0, 83, 0, 0, 0, 0, 1, 103, 100, 0, 20, 443 101, 114, 108, 45, 100, 101, 109, 111, 50, 50, 64, 49, 50, 55, 46, 48, 444 46, 48, 46, 49, 0, 0, 0, 77, 0, 0, 0, 0, 1, 114, 0, 3, 100, 0, 20, 101, 114, 445 108, 45, 100, 101, 109, 111, 50, 50, 64, 49, 50, 55, 46, 48, 46, 48, 46, 446 49, 1, 0, 3, 219, 136, 225, 146, 0, 9, 253, 168, 114, 208, 103, 100, 0, 447 20, 101, 114, 108, 45, 100, 101, 109, 111, 50, 50, 64, 49, 50, 55, 46, 448 48, 46, 48, 46, 49, 0, 0, 0, 77, 0, 0, 0, 0, 1, 112, 0, 0, 1, 14, 1, 196, 449 150, 93, 173, 104, 167, 134, 253, 184, 200, 203, 147, 166, 63, 88, 450 201, 0, 0, 0, 12, 0, 0, 0, 3, 100, 0, 5, 115, 104, 101, 108, 108, 97, 12, 451 98, 6, 36, 178, 237, 103, 100, 0, 20, 101, 114, 108, 45, 100, 101, 109, 452 111, 50, 50, 64, 49, 50, 55, 46, 48, 46, 48, 46, 49, 0, 0, 0, 83, 0, 0, 0, 453 0, 1, 104, 2, 100, 0, 5, 118, 97, 108, 117, 101, 112, 0, 0, 0, 110, 2, 454 196, 150, 93, 173, 104, 167, 134, 253, 184, 200, 203, 147, 166, 63, 455 88, 201, 0, 0, 0, 5, 0, 0, 0, 1, 100, 0, 5, 115, 104, 101, 108, 108, 97, 5, 456 98, 6, 36, 178, 237, 103, 100, 0, 20, 101, 114, 108, 45, 100, 101, 109, 457 111, 50, 50, 64, 49, 50, 55, 46, 48, 46, 48, 46, 49, 0, 0, 0, 83, 0, 0, 0, 458 0, 1, 103, 100, 0, 20, 101, 114, 108, 45, 100, 101, 109, 111, 50, 50, 459 64, 49, 50, 55, 46, 48, 46, 48, 46, 49, 0, 0, 0, 77, 0, 0, 0, 0, 1, 114, 0, 460 3, 100, 0, 20, 101, 114, 108, 45, 100, 101, 109, 111, 50, 50, 64, 49, 461 50, 55, 46, 48, 46, 48, 46, 49, 1, 0, 3, 219, 136, 225, 146, 0, 9, 253, 462 168, 114, 208, 103, 100, 0, 20, 101, 114, 108, 45, 100, 101, 109, 111, 463 50, 50, 64, 49, 50, 55, 46, 48, 46, 48, 46, 49, 0, 0, 0, 77, 0, 0, 0, 0, 1, 464 104, 2, 100, 0, 5, 118, 97, 108, 117, 101, 112, 0, 0, 0, 110, 2, 196, 465 150, 93, 173, 104, 167, 134, 253, 184, 200, 203, 147, 166, 63, 88, 466 201, 0, 0, 0, 5, 0, 0, 0, 1, 100, 0, 5, 115, 104, 101, 108, 108, 97, 5, 98, 467 6, 36, 178, 237, 103, 100, 0, 20, 101, 114, 108, 45, 100, 101, 109, 468 111, 50, 50, 64, 49, 50, 55, 46, 48, 46, 48, 46, 49, 0, 0, 0, 83, 0, 0, 0, 469 0, 1, 103, 100, 0, 20, 101, 114, 108, 45, 100, 101, 109, 111, 50, 50, 470 64, 49, 50, 55, 46, 48, 46, 48, 46, 49, 0, 0, 0, 77, 0, 0, 0, 0, 1, 108, 0, 471 0, 0, 1, 104, 5, 100, 0, 6, 99, 108, 97, 117, 115, 101, 97, 1, 108, 0, 0, 472 0, 1, 104, 3, 100, 0, 3, 118, 97, 114, 97, 1, 100, 0, 1, 78, 106, 106, 473 108, 0, 0, 0, 1, 104, 5, 100, 0, 2, 111, 112, 97, 1, 100, 0, 1, 42, 104, 3, 474 100, 0, 3, 118, 97, 114, 97, 1, 100, 0, 1, 78, 104, 3, 100, 0, 7, 105, 475 110, 116, 101, 103, 101, 114, 97, 1, 97, 2, 106, 106} 476 477 packetFunction = packet // save for benchmark 478 _, _, err := Decode(packet, []Atom{}, DecodeOptions{}) 479 if err != nil { 480 t.Fatal(err) 481 } 482 483 } 484 485 func TestDecodeRegisteredType(t *testing.T) { 486 type regTypeStruct3 struct { 487 C string 488 } 489 type regTypeStruct4 struct { 490 A uint8 491 B *regTypeStruct3 492 } 493 if a, err := RegisterType(regTypeStruct3{}, RegisterTypeOptions{}); err != nil { 494 t.Fatal(err) 495 } else { 496 defer UnregisterType(a) 497 } 498 499 if a, err := RegisterType(regTypeStruct4{}, RegisterTypeOptions{}); err != nil { 500 t.Fatal(err) 501 } else { 502 defer UnregisterType(a) 503 } 504 505 expected := regTypeStruct4{} 506 expected.A = 123 507 expected.B = ®TypeStruct3{ 508 C: "hello", 509 } 510 511 packet := []byte{ettSmallTuple, 3, ettSmallAtomUTF8, 49, 35, 103, 105, 116, 104, 117, 98, 46, 99, 111, 109, 47, 101, 114, 103, 111, 45, 115, 101, 114, 118, 105, 99, 101, 115, 47, 101, 114, 103, 111, 47, 101, 116, 102, 47, 114, 101, 103, 84, 121, 112, 101, 83, 116, 114, 117, 99, 116, 52, ettSmallInteger, 123, ettSmallTuple, 2, ettSmallAtomUTF8, 49, 35, 103, 105, 116, 104, 117, 98, 46, 99, 111, 109, 47, 101, 114, 103, 111, 45, 115, 101, 114, 118, 105, 99, 101, 115, 47, 101, 114, 103, 111, 47, 101, 116, 102, 47, 114, 101, 103, 84, 121, 112, 101, 83, 116, 114, 117, 99, 116, 51, ettString, 0, 5, 104, 101, 108, 108, 111} 512 513 term, _, err := Decode(packet, []Atom{}, DecodeOptions{}) 514 if err != nil { 515 t.Fatal(err) 516 } 517 switch tt := term.(type) { 518 case regTypeStruct4: 519 //fmt.Printf("TERM: %v %#v %#v\n", tt, tt, tt.B) 520 default: 521 t.Fatal("unknown type", tt) 522 } 523 524 } 525 526 // 527 // benchmarks 528 // 529 530 func BenchmarkDecodeAtom(b *testing.B) { 531 packet := []byte{ettAtomUTF8, 0, 3, 97, 98, 99} 532 for i := 0; i < b.N; i++ { 533 _, _, err := Decode(packet, []Atom{}, DecodeOptions{}) 534 if err != nil { 535 b.Fatal(err) 536 } 537 } 538 } 539 540 func BenchmarkDecodeString(b *testing.B) { 541 packet := []byte{ettString, 0, 3, 97, 98, 99} 542 for i := 0; i < b.N; i++ { 543 _, _, err := Decode(packet, []Atom{}, DecodeOptions{}) 544 if err != nil { 545 b.Fatal(err) 546 } 547 } 548 } 549 550 func BenchmarkDecodeNewFloat(b *testing.B) { 551 packet := []byte{ettNewFloat, 64, 0, 204, 204, 204, 204, 204, 205} 552 for i := 0; i < b.N; i++ { 553 _, _, err := Decode(packet, []Atom{}, DecodeOptions{}) 554 if err != nil { 555 b.Fatal(err) 556 } 557 } 558 } 559 560 func BenchmarkDecodeInteger(b *testing.B) { 561 packet := []byte{ettInteger, 182, 105, 253, 46} 562 for i := 0; i < b.N; i++ { 563 _, _, err := Decode(packet, []Atom{}, DecodeOptions{}) 564 if err != nil { 565 b.Fatal(err) 566 } 567 } 568 } 569 570 func BenchmarkDecodeSmallBigInteger(b *testing.B) { 571 packet := []byte{ettSmallBig, 8, 1, 177, 28, 108, 177, 244, 16, 34, 17} 572 for i := 0; i < b.N; i++ { 573 _, _, err := Decode(packet, []Atom{}, DecodeOptions{}) 574 if err != nil { 575 b.Fatal(err) 576 } 577 } 578 } 579 580 func BenchmarkDecodeSmallBigIntegerWithinInt64Range(b *testing.B) { 581 packet := []byte{ettSmallBig, 5, 1, 106, 26, 153, 190, 28} 582 for i := 0; i < b.N; i++ { 583 _, _, err := Decode(packet, []Atom{}, DecodeOptions{}) 584 if err != nil { 585 b.Fatal(err) 586 } 587 } 588 } 589 590 func BenchmarkDecodeList100Integer(b *testing.B) { 591 packet := []byte{} 592 packetInt := []byte{ettInteger, 182, 105, 253, 46} 593 packetList := []byte{ettList, 0, 0, 0, 100} 594 595 packet = append(packet, packetList...) 596 packet = append(packet, byte(106)) 597 598 for i := 0; i < 100; i++ { 599 packet = append(packet, packetInt...) 600 } 601 602 b.ResetTimer() 603 604 for i := 0; i < b.N; i++ { 605 _, _, err := Decode(packet, []Atom{}, DecodeOptions{}) 606 if err != nil { 607 b.Fatal(err) 608 } 609 } 610 611 } 612 613 func BenchmarkDecodeTuple(b *testing.B) { 614 packet := []byte{ettSmallTuple, 3, 70, 64, 9, 30, 184, 81, 235, 133, 31, 100, 0, 3, 97, 98, 99, 615 98, 58, 222, 104, 177} 616 for i := 0; i < b.N; i++ { 617 _, _, err := Decode(packet, []Atom{}, DecodeOptions{}) 618 if err != nil { 619 b.Fatal(err) 620 } 621 } 622 } 623 624 func BenchmarkDecodePid(b *testing.B) { 625 packet := []byte{103, 100, 0, 18, 101, 114, 108, 45, 100, 101, 109, 111, 64, 49, 626 50, 55, 46, 48, 46, 48, 46, 49, 0, 0, 0, 142, 0, 0, 0, 0, 2} 627 for i := 0; i < b.N; i++ { 628 _, _, err := Decode(packet, []Atom{}, DecodeOptions{}) 629 if err != nil { 630 b.Fatal(err) 631 } 632 } 633 } 634 635 func BenchmarkDecodePidWithAtomCache(b *testing.B) { 636 packet := []byte{103, ettCacheRef, 0, 0, 0, 0, 142, 0, 0, 0, 0, 2} 637 cache := []Atom{Atom("erl-demo@127.0.0.1")} 638 for i := 0; i < b.N; i++ { 639 _, _, err := Decode(packet, cache, DecodeOptions{}) 640 if err != nil { 641 b.Fatal(err) 642 } 643 } 644 } 645 646 func BenchmarkDecodeRef(b *testing.B) { 647 packet := []byte{114, 0, 3, 100, 0, 18, 101, 114, 108, 45, 100, 101, 109, 111, 64, 648 49, 50, 55, 46, 48, 46, 48, 46, 49, 2, 0, 1, 30, 228, 183, 192, 0, 1, 141, 649 122, 203, 35} 650 for i := 0; i < b.N; i++ { 651 _, _, err := Decode(packet, []Atom{}, DecodeOptions{}) 652 if err != nil { 653 b.Fatal(err) 654 } 655 } 656 } 657 658 func BenchmarkDecodeRefWithAtomCache(b *testing.B) { 659 packet := []byte{114, 0, 3, ettCacheRef, 0, 2, 0, 1, 30, 228, 183, 192, 0, 1, 141, 660 122, 203, 35} 661 cache := []Atom{Atom("erl-demo@127.0.0.1")} 662 for i := 0; i < b.N; i++ { 663 _, _, err := Decode(packet, cache, DecodeOptions{}) 664 if err != nil { 665 b.Fatal(err) 666 } 667 } 668 } 669 func BenchmarkDecodePort(b *testing.B) { 670 packet := []byte{102, 100, 0, 18, 101, 114, 108, 45, 100, 101, 109, 111, 64, 49, 671 50, 55, 46, 48, 46, 48, 46, 49, 0, 0, 0, 32, 2} 672 for i := 0; i < b.N; i++ { 673 _, _, err := Decode(packet, []Atom{}, DecodeOptions{}) 674 if err != nil { 675 b.Fatal(err) 676 } 677 } 678 } 679 680 func BenchmarkDecodeTupleRefPid(b *testing.B) { 681 682 packet := []byte{ettSmallTuple, 2, ettNewRef, 0, 3, ettAtom, 0, 18, 101, 114, 108, 45, 100, 101, 109, 683 111, 64, 49, 50, 55, 46, 48, 46, 48, 46, 49, 2, 0, 1, 31, 28, 183, 192, 0, 684 1, 141, 122, 203, 35, 103, 100, 0, 18, 101, 114, 108, 45, 100, 101, 685 109, 111, 64, 49, 50, 55, 46, 48, 46, 48, 46, 49, 0, 0, 0, 142, 0, 0, 0, 0, 686 2} 687 for i := 0; i < b.N; i++ { 688 _, _, err := Decode(packet, []Atom{}, DecodeOptions{}) 689 if err != nil { 690 b.Fatal(err) 691 } 692 } 693 } 694 695 func BenchmarkDecodeTupleRefPidWithAtomCache(b *testing.B) { 696 697 packet := []byte{ettSmallTuple, 2, ettNewRef, 0, 3, ettCacheRef, 0, 698 2, 0, 1, 31, 28, 183, 192, 0, 1, 141, 122, 203, 35, 103, ettCacheRef, 0, 699 0, 0, 0, 142, 0, 0, 0, 0, 2} 700 cache := []Atom{Atom("erl-demo@127.0.0.1")} 701 for i := 0; i < b.N; i++ { 702 _, _, err := Decode(packet, cache, DecodeOptions{}) 703 if err != nil { 704 b.Fatal(err) 705 } 706 } 707 }