File: elsize_test.go

package info (click to toggle)
golang-github-tinylib-msgp 1.2.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 836 kB
  • sloc: makefile: 47
file content (90 lines) | stat: -rw-r--r-- 3,257 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package msgp

import "testing"

func TestBytespec(t *testing.T) {
	// verify that bytespec refactor for TinyGo behaves the same as the old code

	// previous sizes array setup verbatim:

	// size of every object on the wire,
	// plus type information. gives us
	// constant-time type information
	// for traversing composite objects.
	//
	sizes := [256]bytespec{
		mnil:      {size: 1, extra: constsize, typ: NilType},
		mfalse:    {size: 1, extra: constsize, typ: BoolType},
		mtrue:     {size: 1, extra: constsize, typ: BoolType},
		mbin8:     {size: 2, extra: extra8, typ: BinType},
		mbin16:    {size: 3, extra: extra16, typ: BinType},
		mbin32:    {size: 5, extra: extra32, typ: BinType},
		mext8:     {size: 3, extra: extra8, typ: ExtensionType},
		mext16:    {size: 4, extra: extra16, typ: ExtensionType},
		mext32:    {size: 6, extra: extra32, typ: ExtensionType},
		mfloat32:  {size: 5, extra: constsize, typ: Float32Type},
		mfloat64:  {size: 9, extra: constsize, typ: Float64Type},
		muint8:    {size: 2, extra: constsize, typ: UintType},
		muint16:   {size: 3, extra: constsize, typ: UintType},
		muint32:   {size: 5, extra: constsize, typ: UintType},
		muint64:   {size: 9, extra: constsize, typ: UintType},
		mint8:     {size: 2, extra: constsize, typ: IntType},
		mint16:    {size: 3, extra: constsize, typ: IntType},
		mint32:    {size: 5, extra: constsize, typ: IntType},
		mint64:    {size: 9, extra: constsize, typ: IntType},
		mfixext1:  {size: 3, extra: constsize, typ: ExtensionType},
		mfixext2:  {size: 4, extra: constsize, typ: ExtensionType},
		mfixext4:  {size: 6, extra: constsize, typ: ExtensionType},
		mfixext8:  {size: 10, extra: constsize, typ: ExtensionType},
		mfixext16: {size: 18, extra: constsize, typ: ExtensionType},
		mstr8:     {size: 2, extra: extra8, typ: StrType},
		mstr16:    {size: 3, extra: extra16, typ: StrType},
		mstr32:    {size: 5, extra: extra32, typ: StrType},
		marray16:  {size: 3, extra: array16v, typ: ArrayType},
		marray32:  {size: 5, extra: array32v, typ: ArrayType},
		mmap16:    {size: 3, extra: map16v, typ: MapType},
		mmap32:    {size: 5, extra: map32v, typ: MapType},
	}

	// set up fixed fields

	// fixint
	for i := mfixint; i < 0x80; i++ {
		sizes[i] = bytespec{size: 1, extra: constsize, typ: IntType}
	}

	// nfixint
	for i := uint16(mnfixint); i < 0x100; i++ {
		sizes[uint8(i)] = bytespec{size: 1, extra: constsize, typ: IntType}
	}

	// fixstr gets constsize,
	// since the prefix yields the size
	for i := mfixstr; i < 0xc0; i++ {
		sizes[i] = bytespec{size: 1 + rfixstr(i), extra: constsize, typ: StrType}
	}

	// fixmap
	for i := mfixmap; i < 0x90; i++ {
		sizes[i] = bytespec{size: 1, extra: varmode(2 * rfixmap(i)), typ: MapType}
	}

	// fixarray
	for i := mfixarray; i < 0xa0; i++ {
		sizes[i] = bytespec{size: 1, extra: varmode(rfixarray(i)), typ: ArrayType}
	}

	// compare all values to calcBytespec
	for i := 0; i < 256; i++ {
		sizeb := sizes[byte(i)]
		cb := calcBytespec(byte(i))
		if sizeb != cb {
			t.Errorf("at index 0x%x calculated sizes array %#v does not match calcBytespec %#v", i, sizeb, cb)
		}

		// all except the unused byte C1 should have non-zero size
		if i != 0xC1 && sizeb.size == 0 {
			t.Errorf("unexpected zero size for index 0x%x", i)
		}
	}
}