コード例 #1
0
func TestSingleAddLen(t *testing.T) {
	tests := []struct {
		input int
		want  int
	}{
		{0, 1},
		{1, 1},
		{64, 1},
		{256, 1},
	}

	// single add value case
	for _, test := range tests {
		var x intset.IntSet
		x.Add(test.input)
		if got := x.Len(); got != test.want {
			t.Errorf("Len() input %d = %d", test.input, got)
		}
	}
}
コード例 #2
0
func Example_ex6_1() {
	var x, z intset.IntSet
	x.Add(0)
	x.Add(128)
	x.Add(1)
	x.Add(2)
	x.Add(37)
	x.Add(64)

	fmt.Println(x.String())

	// Show the number of x elements
	fmt.Println(x.Len())

	x.Remove(0)
	x.Remove(64)
	x.Remove(128)
	x.Remove(2)
	fmt.Println(x.String())

	z = x.Copy()
	fmt.Println(z.String())

	// Confirm that z doesn't have x's reference.
	x.Add(5)
	fmt.Println(x.String())
	fmt.Println(z.String())

	// Clear x
	x.Clear()
	fmt.Println(x.String())

	// Output:
	// {0 1 2 37 64 128}
	// 6
	// {1 37}
	// {1 37}
	// {1 5 37}
	// {1 37}
	// {}
}
コード例 #3
0
func TestMultiAddLen(t *testing.T) {
	tests := []struct {
		input []int
		want  int
	}{
		{[]int{0, 1}, 2},
		{[]int{0, 0}, 1}, // duplicated input
		{[]int{0, 1, 2}, 3},
		{[]int{0, 64, 128}, 3},
	}

	for _, test := range tests {
		var x intset.IntSet
		for _, v := range test.input {
			x.Add(v)
		}
		if got := x.Len(); got != test.want {
			t.Errorf("Len() input %v = %d", test.input, got)
		}
	}
}