コード例 #1
0
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)
}
コード例 #2
0
ファイル: slice_test.go プロジェクト: gaowt1/slices
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)
	}
}
コード例 #3
0
ファイル: sexp_test.go プロジェクト: feyeleanor/sexp
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)
}