-
Notifications
You must be signed in to change notification settings - Fork 1
/
intset_test.go
108 lines (97 loc) · 3.07 KB
/
intset_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
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
// Copyright (c) 2017 Opsidian Ltd.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package data
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestNewIntSetShouldCreateAnEmptySet(t *testing.T) {
i := NewIntSet()
assert.Equal(t, []int{}, i.data)
}
func TestNewIntSetShouldInsertValues(t *testing.T) {
i := NewIntSet(3, 2, 2, 1, 0)
assert.Equal(t, []int{0, 1, 2, 3}, i.data)
}
func TestEmptyIntSetReturnsWithAnEmptyIntSet(t *testing.T) {
assert.Equal(t, []int{}, EmptyIntSet.data)
}
func TestLenShouldReturnWithSetLength(t *testing.T) {
assert.Equal(t, 0, IntSet{[]int{}}.Len())
assert.Equal(t, 1, IntSet{[]int{1}}.Len())
assert.Equal(t, 2, IntSet{[]int{1, 2}}.Len())
}
func TestInsert(t *testing.T) {
type TC struct {
name string
i1 []int
val int
i2 []int
}
testCases := []TC{
{"Insert to empty list", []int{}, 1, []int{1}},
{"Insert zero", []int{}, 0, []int{0}},
{"Existing item should not be duplicated", []int{1}, 1, []int{1}},
{"Insert to the end of the list", []int{1}, 2, []int{1, 2}},
{"Insert to the beginning of the list", []int{2}, 1, []int{1, 2}},
{"Insert to the middle of the list", []int{1, 3}, 2, []int{1, 2, 3}},
}
for _, tc := range testCases {
i1c := make([]int, len(tc.i1))
copy(i1c, tc.i1)
i2 := IntSet{tc.i1}.Insert(tc.val)
assert.Equal(t, i1c, tc.i1, fmt.Sprintf("Set is not immutable: %s", tc.name))
assert.Equal(t, IntSet{tc.i2}, i2, fmt.Sprintf("Failed: %s", tc.name))
}
}
func TestUnion(t *testing.T) {
type TC struct {
name string
i1 []int
i2 []int
i3 []int
}
testCases := []TC{
{"Empty lists", []int{}, []int{}, []int{}},
{"First empty", []int{}, []int{1}, []int{1}},
{"Second empty", []int{1}, []int{}, []int{1}},
{"First longer", []int{1, 3}, []int{2}, []int{1, 2, 3}},
{"Second longer", []int{2}, []int{1, 3}, []int{1, 2, 3}},
{"First then second", []int{1, 2}, []int{3, 4}, []int{1, 2, 3, 4}},
{"Second then first", []int{3, 4}, []int{1, 2}, []int{1, 2, 3, 4}},
{"Mixed", []int{1, 3}, []int{2, 4}, []int{1, 2, 3, 4}},
{"Ignore duplicated", []int{1, 2, 3}, []int{2, 3, 4}, []int{1, 2, 3, 4}},
}
for _, tc := range testCases {
i1c := make([]int, len(tc.i1))
copy(i1c, tc.i1)
i2c := make([]int, len(tc.i2))
copy(i2c, tc.i2)
i3 := IntSet{tc.i1}.Union(IntSet{tc.i2})
assert.Equal(t, i1c, tc.i1, fmt.Sprintf("Set is not immutable: %s", tc.name))
assert.Equal(t, i2c, tc.i2, fmt.Sprintf("Set is not immutable: %s", tc.name))
assert.Equal(t, IntSet{tc.i3}, i3, fmt.Sprintf("Failed: %s", tc.name))
}
}
func TestEachShouldNotCallFunctionForEmptyList(t *testing.T) {
calledValue := -1
f := func(val int) {
calledValue = val
}
i := NewIntSet()
i.Each(f)
assert.Equal(t, -1, calledValue)
}
func TestEachShouldCallFunctionForAllItems(t *testing.T) {
calledValues := []int{}
f := func(val int) {
calledValues = append(calledValues, val)
}
i := IntSet{[]int{1, 2}}
i.Each(f)
assert.Equal(t, []int{1, 2}, calledValues)
}