forked from hashicorp/go-set
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserialization_test.go
58 lines (49 loc) · 1.48 KB
/
serialization_test.go
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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package set
import (
"encoding/json"
"testing"
"github.com/shoenig/test/must"
)
func TestSerialization(t *testing.T) {
t.Run("Set", func(t *testing.T) {
set := New[int](3)
set.InsertSlice([]int{1, 2, 3})
bs, err := json.Marshal(set)
must.NoError(t, err)
must.StrContains(t, string(bs), "1")
must.StrContains(t, string(bs), "2")
must.StrContains(t, string(bs), "3")
dstSet := New[int](3)
err = json.Unmarshal(bs, dstSet)
must.NoError(t, err)
must.MapEq(t, dstSet.items, set.items)
})
t.Run("HashSet", func(t *testing.T) {
set := NewHashSet[*company, string](10)
set.InsertSlice([]*company{c1, c2, c3})
bs, err := json.Marshal(set)
must.NoError(t, err)
must.StrContains(t, string(bs), `"street":1`)
must.StrContains(t, string(bs), `"street":2`)
must.StrContains(t, string(bs), `"street":3`)
dstSet := NewHashSet[*company, string](10)
err = json.Unmarshal(bs, dstSet)
must.NoError(t, err)
must.MapEqual(t, dstSet.items, set.items)
})
t.Run("TreeSet", func(t *testing.T) {
set := NewTreeSet[int, Compare[int]](Cmp[int])
set.InsertSlice([]int{10, 3, 13})
bs, err := json.Marshal(set)
must.NoError(t, err)
must.StrContains(t, string(bs), "10")
must.StrContains(t, string(bs), "3")
must.StrContains(t, string(bs), "13")
dstSet := NewTreeSet[int, Compare[int]](Cmp[int])
err = json.Unmarshal(bs, dstSet)
must.NoError(t, err)
must.Eq(t, set.Slice(), dstSet.Slice())
})
}