File: encode_test.go

package info (click to toggle)
golang-github-aws-smithy-go 1.20.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,116 kB
  • sloc: java: 19,678; xml: 166; sh: 131; makefile: 70
file content (115 lines) | stat: -rw-r--r-- 2,287 bytes parent folder | download
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package cbor

import (
	"math"
	"math/big"
	"reflect"
	"testing"

	"github.com/aws/smithy-go/encoding/cbor"
	"github.com/aws/smithy-go/ptr"
)

func TestEncode_KitchenSink(t *testing.T) {
	type subtarget struct {
		Int8  int8
		Int16 int16
	}
	type target struct {
		Int8          int8
		Int16         int16
		Int32         int32
		Int64         int64
		Uint8         uint8
		Uint16        uint16
		Uint32        uint32
		Uint64        uint64
		String        string
		List          []subtarget
		Map           map[string]subtarget
		UintptrNil    *uint
		UintptrNonnil *uint
		Bool          bool
		Float         float64
		BigInt        *big.Int
		BigNegInt     *big.Int
	}

	in := target{
		Int8:   -8,
		Int16:  -16,
		Int32:  -32,
		Int64:  -64,
		Uint8:  8,
		Uint16: 16,
		Uint32: 32,
		Uint64: 64,
		String: "foo",
		List: []subtarget{
			{Int8: -8},
		},
		Map: map[string]subtarget{
			"k0": {Int8: -8},
		},
		UintptrNil:    nil,
		UintptrNonnil: ptr.Uint(4),
		Bool:          true,
		Float:         math.Inf(1),
		BigInt:        new(big.Int).SetBytes([]byte{1, 0, 0, 0, 0, 0, 0, 0, 0}),
		BigNegInt: new(big.Int).Sub(
			big.NewInt(-1),
			new(big.Int).SetBytes([]byte{1, 0, 0, 0, 0, 0, 0, 0, 0}),
		),
	}

	expect := cbor.Map{
		"Int8":   cbor.NegInt(8),
		"Int16":  cbor.NegInt(16),
		"Int32":  cbor.NegInt(32),
		"Int64":  cbor.NegInt(64),
		"Uint8":  cbor.Uint(8),
		"Uint16": cbor.Uint(16),
		"Uint32": cbor.Uint(32),
		"Uint64": cbor.Uint(64),
		"String": cbor.String("foo"),
		"List": cbor.List{
			cbor.Map{
				"Int8":  cbor.NegInt(8),
				"Int16": cbor.Uint(0), // implicit
			},
		},
		"Map": cbor.Map{
			"k0": cbor.Map{
				"Int8":  cbor.NegInt(8),
				"Int16": cbor.Uint(0),
			},
		},
		"UintptrNil":    &cbor.Nil{},
		"UintptrNonnil": cbor.Uint(4),
		"Bool":          cbor.Bool(true),
		"Float":         cbor.Float64(math.Inf(1)),
		"BigInt": &cbor.Tag{
			ID:    2,
			Value: cbor.Slice{1, 0, 0, 0, 0, 0, 0, 0, 0},
		},
		"BigNegInt": &cbor.Tag{
			ID:    3,
			Value: cbor.Slice{1, 0, 0, 0, 0, 0, 0, 0, 0},
		},
	}

	enc := &encoder{}
	encoded, err := enc.Encode(in)
	if err != nil {
		t.Fatal(err)
	}

	actual, err := cbor.Decode(encoded)
	if err != nil {
		t.Fatal(err)
	}

	if !reflect.DeepEqual(expect, actual) {
		t.Errorf("%v != %v", expect, actual)
	}
}