當前位置: 首頁>>代碼示例>>Golang>>正文


Golang IntSet.Add方法代碼示例

本文整理匯總了Golang中github.com/mttrs/gopl/io/ch6/intset.IntSet.Add方法的典型用法代碼示例。如果您正苦於以下問題:Golang IntSet.Add方法的具體用法?Golang IntSet.Add怎麽用?Golang IntSet.Add使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/mttrs/gopl/io/ch6/intset.IntSet的用法示例。


在下文中一共展示了IntSet.Add方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: TestAdd

// May be bad. It looks like String() method test...
func TestAdd(t *testing.T) {
	tests := []struct {
		input int
		want  string
	}{
		{0, "{0}"},
		{1, "{1}"},
		{64, "{64}"},
	}
	for _, test := range tests {
		var x intset.IntSet
		x.Add(test.input)
		if got := x.String(); got != test.want {
			t.Errorf("Add(%d) = %s", test.input, got)
		}
	}
}
開發者ID:bailijiang,項目名稱:gopl.exercise.io,代碼行數:18,代碼來源:intset_test.go

示例2: TestHas

func TestHas(t *testing.T) {
	var x intset.IntSet
	x.Add(0)
	x.Add(64)
	tests := []struct {
		input int
		want  bool
	}{
		{0, true},
		{64, true},
		{3, false},
		{128, false},
	}
	for _, test := range tests {
		if got := x.Has(test.input); got != test.want {
			t.Errorf("Has(%d) = %t", test.input, got)
		}
	}
}
開發者ID:bailijiang,項目名稱:gopl.exercise.io,代碼行數:19,代碼來源:intset_test.go

示例3: TestSingleAddLen

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)
		}
	}
}
開發者ID:bailijiang,項目名稱:gopl.exercise.io,代碼行數:20,代碼來源:intset_test.go

示例4: Example_ex6_1

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}
	// {}
}
開發者ID:bailijiang,項目名稱:gopl.exercise.io,代碼行數:41,代碼來源:intset_test.go

示例5: TestMultiAddLen

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)
		}
	}
}
開發者ID:bailijiang,項目名稱:gopl.exercise.io,代碼行數:21,代碼來源:intset_test.go

示例6: Example_one

func Example_one() {
	var x, y intset.IntSet
	x.Add(1)
	x.Add(144)
	x.Add(9)
	fmt.Println(x.String()) // "{1 9 144}"

	y.Add(9)
	y.Add(42)
	fmt.Println(y.String()) // "{9 42}"

	x.UnionWith(&y)
	fmt.Println(x.String()) // "{1 9 42 144}"

	fmt.Println(x.Has(9), x.Has(123)) // "true false"

	// Output:
	// {1 9 144}
	// {9 42}
	// {1 9 42 144}
	// true false
}
開發者ID:bailijiang,項目名稱:gopl.exercise.io,代碼行數:22,代碼來源:intset_test.go

示例7: Example_two

func Example_two() {
	var x intset.IntSet
	x.Add(1)
	x.Add(144)
	x.Add(9)
	x.Add(42)

	fmt.Println(&x)         // "{1 9 42 144}"
	fmt.Println(x.String()) // "{1 9 42 144}"
	fmt.Println(x)          // "{[4398046511618 0 65536]}"

	// Output:
	// {1 9 42 144}
	// {1 9 42 144}
	// {[4398046511618 0 65536]}
}
開發者ID:bailijiang,項目名稱:gopl.exercise.io,代碼行數:16,代碼來源:intset_test.go


注:本文中的github.com/mttrs/gopl/io/ch6/intset.IntSet.Add方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。