func TestVSliceFlatten(t *testing.T) {
	ConfirmFlatten := func(s, r VSlice) {
		o := s.String()
		if s.Flatten(); !r.Equal(s) {
			t.Fatalf("Flatten(%v) should be %v but is %v", o, r, s)
		}
	}
	ConfirmFlatten(VList(), VList())
	ConfirmFlatten(VList(1), VList(1))
	ConfirmFlatten(VList(1, VList(2)), VList(1, 2))
	ConfirmFlatten(VList(1, VList(2, VList(3))), VList(1, 2, 3))
	ConfirmFlatten(VList(1, 2, VList(3, VList(4, 5), VList(6, VList(7, 8, 9), VList(10, 11)))), VList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11))

	ConfirmFlatten(VList(0, lists.List(1, 2, VList(3, 4))), VList(0, lists.List(1, 2, VList(3, 4))))
	ConfirmFlatten(VList(0, lists.List(1, 2, lists.List(3, 4))), VList(0, lists.List(1, 2, 3, 4)))

	ConfirmFlatten(VList(0, lists.Loop(1, 2)), VList(0, lists.Loop(1, 2)))
	ConfirmFlatten(VList(0, lists.List(1, lists.Loop(2, 3))), VList(0, lists.List(1, 2, 3)))

	ConfirmFlatten(VList(0, lists.List(1, 2, lists.Loop(3, 4))), VList(0, lists.List(1, 2, 3, 4)))
	ConfirmFlatten(VList(3, 4, VList(5, 6, 7)), VList(3, 4, 5, 6, 7))
	ConfirmFlatten(VList(0, lists.Loop(1, 2, VList(3, 4, VList(5, 6, 7)))), VList(0, lists.Loop(1, 2, VList(3, 4, 5, 6, 7))))

	sxp := VList(1, 2, VList(3, VList(4, 5), VList(6, VList(7, 8, 9), VList(10, 11))))
	rxp := VList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)
	ConfirmFlatten(sxp, rxp)

	rxp = VList(1, 2, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 3, 4, 5, 6, 7, 8, 9, 10, 11, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)
	sxp = VList(1, 2, sxp, VList(3, VList(4, 5), VList(6, VList(7, 8, 9), VList(10, 11), sxp)))
	ConfirmFlatten(sxp, rxp)
}
Esempio n. 2
0
func TestReverse(t *testing.T) {
	ConfirmReverse := func(o, r interface{}) {
		Reverse(o)
		if !Equal(o, r) {
			t.Fatalf("Reverse(o) should be %v but is %v", r, o)
		}
	}
	ConfirmReverse(slices.Slice{0, 1, 2, 3, 4, 5}, slices.Slice{5, 4, 3, 2, 1, 0})
	ConfirmReverse(lists.List(0, 1, 2, 3, 4, 5), lists.List(5, 4, 3, 2, 1, 0))
	ConfirmReverse([]int{0, 1, 2, 3, 4, 5}, []int{5, 4, 3, 2, 1, 0})
}
Esempio n. 3
0
func TestEqual(t *testing.T) {
	ConfirmEqual := func(x, y interface{}) {
		if !Equal(x, y) {
			t.Fatalf("Equal(%v, %v) should be true", x, y)
		}
	}
	ConfirmEqual(0, 0)
	ConfirmEqual("a", "a")
	ConfirmEqual(lists.List(0, 1, 2), lists.List(0, 1, 2))
	t.Logf("Write more tests")
}
Esempio n. 4
0
func TestBlockClear(t *testing.T) {
	ConfirmBlockClear := func(i interface{}, d, n int, r interface{}) {
		BlockClear(i, d, n)
		if !Equal(i, r) {
			t.Fatalf("BlockClear(i, %v, %v, %v) should be %v but is %v", d, n, r, i)
		}
	}

	//	slices.Slice{} is a type which supports the Blitter interface
	ConfirmBlockClear(slices.Slice{0, 1, 2, 3, 4, 5}, 3, 3, slices.Slice{0, 1, 2, nil, nil, nil})
	ConfirmBlockClear(slices.Slice{0, 1, 2, 3, 4, 5}, 0, 3, slices.Slice{nil, nil, nil, 3, 4, 5})

	//	List() returns a LinearList which supports the Indexable interface
	ConfirmBlockClear(lists.List(0, 1, 2, 3, 4, 5), 3, 3, lists.List(0, 1, 2, nil, nil, nil))
	ConfirmBlockClear(lists.List(0, 1, 2, 3, 4, 5), 0, 3, lists.List(nil, nil, nil, 3, 4, 5))

	//	[]int{} slices are handled using reflection
	ConfirmBlockClear([]int{0, 1, 2, 3, 4, 5}, 3, 3, []int{0, 1, 2, 0, 0, 0})
	ConfirmBlockClear([]int{0, 1, 2, 3, 4, 5}, 0, 3, []int{0, 0, 0, 3, 4, 5})
}
Esempio n. 5
0
func TestCap(t *testing.T) {
	ConfirmCap := func(o interface{}, r int) {
		if x := Cap(o); x != r {
			t.Fatalf("Len(%v) should be %v but is %v", o, r, x)
		}
	}
	ConfirmCap(0, 0)
	ConfirmCap(([]int)(nil), 0)
	ConfirmCap([]int{}, 0)
	ConfirmCap([]int{0}, 1)
	ConfirmCap(lists.List(0, 1, 2), 0)
	ConfirmCap([]int{0, 1, 2}, 3)
	ConfirmCap(slices.Slice{0, 1, 2}, 3)
}
Esempio n. 6
0
func TestSliceFlatten(t *testing.T) {
	ConfirmFlatten := func(s, r Slice) {
		if s.Flatten(); !s.Equal(r) {
			t.Fatalf("%v should be %v", s, r)
		}
	}
	ConfirmFlatten(Slice{}, Slice{})
	ConfirmFlatten(Slice{1}, Slice{1})
	ConfirmFlatten(Slice{1, Slice{2}}, Slice{1, 2})
	ConfirmFlatten(Slice{1, Slice{2, Slice{3}}}, Slice{1, 2, 3})
	ConfirmFlatten(Slice{1, 2, Slice{3, Slice{4, 5}, Slice{6, Slice{7, 8, 9}, Slice{10, 11}}}}, Slice{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11})

	ConfirmFlatten(Slice{0, lists.List(1, 2, Slice{3, 4})}, Slice{0, lists.List(1, 2, Slice{3, 4})})
	ConfirmFlatten(Slice{0, lists.List(1, 2, lists.List(3, 4))}, Slice{0, lists.List(1, 2, 3, 4)})

	ConfirmFlatten(Slice{0, lists.Loop(1, 2)}, Slice{0, lists.Loop(1, 2)})
	ConfirmFlatten(Slice{0, lists.List(1, lists.Loop(2, 3))}, Slice{0, lists.List(1, 2, 3)})

	ConfirmFlatten(Slice{0, lists.List(1, 2, lists.Loop(3, 4))}, Slice{0, lists.List(1, 2, 3, 4)})
	ConfirmFlatten(Slice{3, 4, Slice{5, 6, 7}}, Slice{3, 4, 5, 6, 7})
	ConfirmFlatten(Slice{0, lists.Loop(1, 2, Slice{3, 4, Slice{5, 6, 7}})}, Slice{0, lists.Loop(1, 2, Slice{3, 4, 5, 6, 7})})

	sxp := Slice{1, 2, Slice{3, Slice{4, 5}, Slice{6, Slice{7, 8, 9}, Slice{10, 11}}}}
	rxp := Slice{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}
	sxp.Flatten()
	if !rxp.Equal(sxp) {
		t.Fatalf("Flatten failed: %v", sxp)
	}

	rxp = Slice{1, 2, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 3, 4, 5, 6, 7, 8, 9, 10, 11, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}
	sxp = Slice{1, 2, sxp, Slice{3, Slice{4, 5}, Slice{6, Slice{7, 8, 9}, Slice{10, 11}, sxp}}}
	sxp.Flatten()
	if !rxp.Equal(sxp) {
		t.Fatalf("Flatten failed with explicit expansions: %v", sxp)
	}
}
Esempio n. 7
0
func TestBlockCopy(t *testing.T) {
	ConfirmBlockCopy := func(i interface{}, d, s, n int, r interface{}) {
		BlockCopy(i, d, s, n)
		if !Equal(i, r) {
			t.Fatalf("BlockCopy(i, %v, %v, %v) should be %v but is %v", d, s, n, r, i)
		}
	}

	//	slices.Slice is a type which supports the Blitter interface
	ConfirmBlockCopy(slices.Slice{0, 1, 2, 3, 4, 5}, 0, 1, -1, slices.Slice{0, 1, 2, 3, 4, 5})
	ConfirmBlockCopy(slices.Slice{0, 1, 2, 3, 4, 5}, 0, 1, 0, slices.Slice{0, 1, 2, 3, 4, 5})
	ConfirmBlockCopy(slices.Slice{0, 1, 2, 3, 4, 5}, 1, 0, 0, slices.Slice{0, 1, 2, 3, 4, 5})

	ConfirmBlockCopy(slices.Slice{0, 1, 2, 3, 4, 5}, -1, 0, 3, slices.Slice{0, 1, 2, 3, 4, 5})
	ConfirmBlockCopy(slices.Slice{0, 1, 2, 3, 4, 5}, 0, -1, 3, slices.Slice{0, 1, 2, 3, 4, 5})

	ConfirmBlockCopy(slices.Slice{0, 1, 2, 3, 4, 5}, 3, 0, 3, slices.Slice{0, 1, 2, 0, 1, 2})
	ConfirmBlockCopy(slices.Slice{0, 1, 2, 3, 4, 5}, 0, 3, 3, slices.Slice{3, 4, 5, 3, 4, 5})
	ConfirmBlockCopy(slices.Slice{0, 1, 2, 3, 4, 5}, 0, 0, 3, slices.Slice{0, 1, 2, 3, 4, 5})

	ConfirmBlockCopy(slices.Slice{0, 1, 2, 3, 4, 5}, 3, 0, 4, slices.Slice{0, 1, 2, 0, 1, 2})
	ConfirmBlockCopy(slices.Slice{0, 1, 2, 3, 4, 5}, 0, 3, 4, slices.Slice{3, 4, 5, 3, 4, 5})

	//	List() returns a LinearList which supports the Indexable interface
	ConfirmBlockCopy(lists.List(0, 1, 2, 3, 4, 5), 0, 1, -1, lists.List(0, 1, 2, 3, 4, 5))
	ConfirmBlockCopy(lists.List(0, 1, 2, 3, 4, 5), 0, 1, 0, lists.List(0, 1, 2, 3, 4, 5))
	ConfirmBlockCopy(lists.List(0, 1, 2, 3, 4, 5), 1, 0, 0, lists.List(0, 1, 2, 3, 4, 5))

	ConfirmBlockCopy(lists.List(0, 1, 2, 3, 4, 5), -1, 0, 3, lists.List(0, 1, 2, 3, 4, 5))
	ConfirmBlockCopy(lists.List(0, 1, 2, 3, 4, 5), 0, -1, 3, lists.List(0, 1, 2, 3, 4, 5))

	ConfirmBlockCopy(lists.List(0, 1, 2, 3, 4, 5), 3, 0, 3, lists.List(0, 1, 2, 0, 1, 2))
	ConfirmBlockCopy(lists.List(0, 1, 2, 3, 4, 5), 0, 3, 3, lists.List(3, 4, 5, 3, 4, 5))
	ConfirmBlockCopy(lists.List(0, 1, 2, 3, 4, 5), 0, 0, 3, lists.List(0, 1, 2, 3, 4, 5))

	ConfirmBlockCopy(lists.List(0, 1, 2, 3, 4, 5), 3, 0, 4, lists.List(0, 1, 2, 0, 1, 2))
	ConfirmBlockCopy(lists.List(0, 1, 2, 3, 4, 5), 0, 3, 4, lists.List(3, 4, 5, 3, 4, 5))

	//	[]int{} slices are handled using reflection
	ConfirmBlockCopy([]int{0, 1, 2, 3, 4, 5}, 0, 1, -1, []int{0, 1, 2, 3, 4, 5})
	ConfirmBlockCopy([]int{0, 1, 2, 3, 4, 5}, 0, 1, 0, []int{0, 1, 2, 3, 4, 5})
	ConfirmBlockCopy([]int{0, 1, 2, 3, 4, 5}, 1, 0, 0, []int{0, 1, 2, 3, 4, 5})

	ConfirmBlockCopy([]int{0, 1, 2, 3, 4, 5}, -1, 0, 3, []int{0, 1, 2, 3, 4, 5})
	ConfirmBlockCopy([]int{0, 1, 2, 3, 4, 5}, 0, -1, 3, []int{0, 1, 2, 3, 4, 5})

	ConfirmBlockCopy([]int{0, 1, 2, 3, 4, 5}, 3, 0, 3, []int{0, 1, 2, 0, 1, 2})
	ConfirmBlockCopy([]int{0, 1, 2, 3, 4, 5}, 0, 3, 3, []int{3, 4, 5, 3, 4, 5})
	ConfirmBlockCopy([]int{0, 1, 2, 3, 4, 5}, 0, 0, 3, []int{0, 1, 2, 3, 4, 5})

	ConfirmBlockCopy([]int{0, 1, 2, 3, 4, 5}, 3, 0, 4, []int{0, 1, 2, 0, 1, 2})
	ConfirmBlockCopy([]int{0, 1, 2, 3, 4, 5}, 0, 3, 4, []int{3, 4, 5, 3, 4, 5})
}
Esempio n. 8
0
func TestDepth(t *testing.T) {
	ConfirmDepth := func(o interface{}, d int) {
		if x := Depth(o); d != x {
			t.Fatalf("Depth(%v) should be %v but is %v", o, d, x)
		}
	}

	ConfirmDepth(nil, 0)
	ConfirmDepth(1, 0)
	ConfirmDepth([]int{}, 0)
	ConfirmDepth([]int{0, 1, 2}, 1)

	ConfirmDepth(lists.List(4, 3, 2, 1), 1)
	ConfirmDepth(lists.List(5,
		lists.List(4, 3),
		2), 2)
	ConfirmDepth(lists.List(6,
		lists.List(5,
			lists.List(4, 3, 2)),
		1), 3)
	ConfirmDepth(lists.List(7,
		lists.List(6,
			lists.List(5,
				4,
				lists.List(3, 2),
				1)),
		0), 4)
	ConfirmDepth(lists.List(8,
		lists.List(7,
			lists.List(6,
				5,
				lists.List(4, 3),
				2)),
		lists.List(1,
			lists.List(0, -1))), 4)
	ConfirmDepth(lists.List(9,
		lists.List(8,
			lists.List(7,
				lists.List(6, 5)),
			lists.List(4,
				3,
				lists.List(2, 1),
				0))), 4)
	ConfirmDepth(lists.List('A',
		lists.List(9,
			slices.Slice{8,
				slices.Slice{7, 6}},
			lists.List(5,
				4,
				lists.List(3, 2),
				1))), 4)
	ConfirmDepth(lists.List('B',
		lists.List('A',
			slices.Slice{9,
				slices.Slice{8,
					slices.Slice{7, 6}}},
			lists.List(5,
				4,
				lists.List(3, 2),
				1))), 5)

	ConfirmDepth(lists.Loop(4, 3, 2, 1), 1)
	ConfirmDepth(lists.Loop(5,
		lists.Loop(4, 3),
		2), 2)
	ConfirmDepth(lists.Loop(6,
		lists.Loop(5,
			lists.Loop(4, 3, 2)),
		1), 3)
	ConfirmDepth(lists.Loop(7,
		lists.Loop(6,
			lists.Loop(5,
				4,
				lists.Loop(3, 2),
				1)),
		0), 4)
	ConfirmDepth(lists.Loop(8,
		lists.Loop(7,
			lists.Loop(6,
				5,
				lists.Loop(4, 3),
				2)),
		lists.Loop(1,
			lists.Loop(0, -1))), 4)
	ConfirmDepth(lists.Loop(9,
		lists.Loop(8,
			lists.Loop(7,
				lists.Loop(6, 5)),
			lists.Loop(4,
				3,
				lists.Loop(2, 1),
				0))), 4)
	ConfirmDepth(lists.Loop('A',
		lists.Loop(9,
			slices.Slice{8,
				slices.Slice{7, 6}},
			lists.Loop(5,
				4,
				lists.Loop(3, 2),
				1))), 4)
	ConfirmDepth(lists.Loop('B',
		lists.Loop('A',
			slices.Slice{9,
				slices.Slice{8,
					slices.Slice{7, 6}}},
			lists.Loop(5,
				4,
				lists.Loop(3, 2),
				1))), 5)
}
Esempio n. 9
0
func BenchmarkBlockClearIndexable(b *testing.B) {
	l := lists.List(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
	for i := 0; i < b.N; i++ {
		BlockClear(l, 0, 5)
	}
}