File: generics_test.go

package info (click to toggle)
golang-github-tinylib-msgp 1.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,344 kB
  • sloc: makefile: 48
file content (65 lines) | stat: -rw-r--r-- 1,513 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
package _generated

import (
	"bytes"
	"reflect"
	"testing"

	"github.com/tinylib/msgp/msgp"
)

func TestGenericsMarshal(t *testing.T) {
	x := GenericTest[Fixed, *Fixed]{}
	x.B = &Fixed{A: 1.5}
	x.C = append(x.C, Fixed{A: 2.5})
	x.D = map[string]Fixed{"hello": Fixed{A: 2.5}}
	x.E.A = Fixed{A: 2.5}
	x.F = append(x.F, GenericTest2[Fixed, *Fixed, string]{A: Fixed{A: 3.5}})
	x.G = map[string]GenericTest2[Fixed, *Fixed, string]{"hello": {A: Fixed{A: 3.5}}}

	bts, err := x.MarshalMsg(nil)
	if err != nil {
		t.Fatal(err)
	}
	got := GenericTest[Fixed, *Fixed]{}
	got.B = x.B // We must initialize this.
	*got.B = Fixed{}
	bts, err = got.UnmarshalMsg(bts)
	if err != nil {
		t.Fatal(err)
	}

	if !reflect.DeepEqual(x, got) {
		t.Errorf("\n got=%#v\nwant=%#v", got, x)
	}
}

func TestGenericsEncode(t *testing.T) {
	x := GenericTest[Fixed, *Fixed]{}
	x.B = &Fixed{A: 1.5}
	x.C = append(x.C, Fixed{A: 2.5})
	x.D = map[string]Fixed{"hello": Fixed{A: 2.5}}
	x.E.A = Fixed{A: 2.5}
	x.F = append(x.F, GenericTest2[Fixed, *Fixed, string]{A: Fixed{A: 3.5}})
	x.G = map[string]GenericTest2[Fixed, *Fixed, string]{"hello": {A: Fixed{A: 3.5}}}

	var buf bytes.Buffer
	w := msgp.NewWriter(&buf)
	err := x.EncodeMsg(w)
	if err != nil {
		t.Fatal(err)
	}
	w.Flush()
	got := GenericTest[Fixed, *Fixed]{}
	got.B = x.B // We must initialize this.
	*got.B = Fixed{}
	r := msgp.NewReader(&buf)
	err = got.DecodeMsg(r)
	if err != nil {
		t.Fatal(err)
	}

	if !reflect.DeepEqual(x, got) {
		t.Errorf("\n got=%#v\nwant=%#v", got, x)
	}
}