• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1package cc
2
3import (
4	"android/soong/android"
5	"reflect"
6	"testing"
7)
8
9var firstUniqueElementsTestCases = []struct {
10	in  []string
11	out []string
12}{
13	{
14		in:  []string{"a"},
15		out: []string{"a"},
16	},
17	{
18		in:  []string{"a", "b"},
19		out: []string{"a", "b"},
20	},
21	{
22		in:  []string{"a", "a"},
23		out: []string{"a"},
24	},
25	{
26		in:  []string{"a", "b", "a"},
27		out: []string{"a", "b"},
28	},
29	{
30		in:  []string{"b", "a", "a"},
31		out: []string{"b", "a"},
32	},
33	{
34		in:  []string{"a", "a", "b"},
35		out: []string{"a", "b"},
36	},
37	{
38		in:  []string{"a", "b", "a", "b"},
39		out: []string{"a", "b"},
40	},
41	{
42		in:  []string{"liblog", "libdl", "libc++", "libdl", "libc", "libm"},
43		out: []string{"liblog", "libdl", "libc++", "libc", "libm"},
44	},
45}
46
47func TestFirstUniqueElements(t *testing.T) {
48	for _, testCase := range firstUniqueElementsTestCases {
49		out := firstUniqueElements(testCase.in)
50		if !reflect.DeepEqual(out, testCase.out) {
51			t.Errorf("incorrect output:")
52			t.Errorf("     input: %#v", testCase.in)
53			t.Errorf("  expected: %#v", testCase.out)
54			t.Errorf("       got: %#v", out)
55		}
56	}
57}
58
59var lastUniqueElementsTestCases = []struct {
60	in  []string
61	out []string
62}{
63	{
64		in:  []string{"a"},
65		out: []string{"a"},
66	},
67	{
68		in:  []string{"a", "b"},
69		out: []string{"a", "b"},
70	},
71	{
72		in:  []string{"a", "a"},
73		out: []string{"a"},
74	},
75	{
76		in:  []string{"a", "b", "a"},
77		out: []string{"b", "a"},
78	},
79	{
80		in:  []string{"b", "a", "a"},
81		out: []string{"b", "a"},
82	},
83	{
84		in:  []string{"a", "a", "b"},
85		out: []string{"a", "b"},
86	},
87	{
88		in:  []string{"a", "b", "a", "b"},
89		out: []string{"a", "b"},
90	},
91	{
92		in:  []string{"liblog", "libdl", "libc++", "libdl", "libc", "libm"},
93		out: []string{"liblog", "libc++", "libdl", "libc", "libm"},
94	},
95}
96
97func TestLastUniqueElements(t *testing.T) {
98	for _, testCase := range lastUniqueElementsTestCases {
99		out := lastUniqueElements(testCase.in)
100		if !reflect.DeepEqual(out, testCase.out) {
101			t.Errorf("incorrect output:")
102			t.Errorf("     input: %#v", testCase.in)
103			t.Errorf("  expected: %#v", testCase.out)
104			t.Errorf("       got: %#v", out)
105		}
106	}
107}
108
109var (
110	str11 = "01234567891"
111	str10 = str11[:10]
112	str9  = str11[:9]
113	str5  = str11[:5]
114	str4  = str11[:4]
115)
116
117var splitListForSizeTestCases = []struct {
118	in   []string
119	out  [][]string
120	size int
121}{
122	{
123		in:   []string{str10},
124		out:  [][]string{{str10}},
125		size: 10,
126	},
127	{
128		in:   []string{str9},
129		out:  [][]string{{str9}},
130		size: 10,
131	},
132	{
133		in:   []string{str5},
134		out:  [][]string{{str5}},
135		size: 10,
136	},
137	{
138		in:   []string{str11},
139		out:  nil,
140		size: 10,
141	},
142	{
143		in:   []string{str10, str10},
144		out:  [][]string{{str10}, {str10}},
145		size: 10,
146	},
147	{
148		in:   []string{str9, str10},
149		out:  [][]string{{str9}, {str10}},
150		size: 10,
151	},
152	{
153		in:   []string{str10, str9},
154		out:  [][]string{{str10}, {str9}},
155		size: 10,
156	},
157	{
158		in:   []string{str5, str4},
159		out:  [][]string{{str5, str4}},
160		size: 10,
161	},
162	{
163		in:   []string{str5, str4, str5},
164		out:  [][]string{{str5, str4}, {str5}},
165		size: 10,
166	},
167	{
168		in:   []string{str5, str4, str5, str4},
169		out:  [][]string{{str5, str4}, {str5, str4}},
170		size: 10,
171	},
172	{
173		in:   []string{str5, str4, str5, str5},
174		out:  [][]string{{str5, str4}, {str5}, {str5}},
175		size: 10,
176	},
177	{
178		in:   []string{str5, str5, str5, str4},
179		out:  [][]string{{str5}, {str5}, {str5, str4}},
180		size: 10,
181	},
182	{
183		in:   []string{str9, str11},
184		out:  nil,
185		size: 10,
186	},
187	{
188		in:   []string{str11, str9},
189		out:  nil,
190		size: 10,
191	},
192}
193
194func TestSplitListForSize(t *testing.T) {
195	for _, testCase := range splitListForSizeTestCases {
196		out, _ := splitListForSize(android.PathsForTesting(testCase.in), testCase.size)
197
198		var outStrings [][]string
199
200		if len(out) > 0 {
201			outStrings = make([][]string, len(out))
202			for i, o := range out {
203				outStrings[i] = o.Strings()
204			}
205		}
206
207		if !reflect.DeepEqual(outStrings, testCase.out) {
208			t.Errorf("incorrect output:")
209			t.Errorf("     input: %#v", testCase.in)
210			t.Errorf("      size: %d", testCase.size)
211			t.Errorf("  expected: %#v", testCase.out)
212			t.Errorf("       got: %#v", outStrings)
213		}
214	}
215}
216