blob: 091f5bf7dbe46957cc954bb930d07df8bb5fdef2 [file] [log] [blame]
Colin Cross72bd1932015-03-16 00:13:59 -07001// Copyright 2015 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package blueprint
16
17import (
18 "reflect"
19 "testing"
20)
21
22var (
23 testModuleA = &moduleInfo{variantName: "testModuleA"}
24 testModuleB = &moduleInfo{variantName: "testModuleB"}
25 testModuleC = &moduleInfo{variantName: "testModuleC"}
26 testModuleD = &moduleInfo{variantName: "testModuleD"}
27 testModuleE = &moduleInfo{variantName: "testModuleE"}
28 testModuleF = &moduleInfo{variantName: "testModuleF"}
29)
30
31func (m *moduleInfo) String() string {
32 return m.variantName
33}
34
35var spliceModulesTestCases = []struct {
36 in []*moduleInfo
37 replace *moduleInfo
38 with []*moduleInfo
39 out []*moduleInfo
40 reallocate bool
41}{
42 {
43 // Insert at the beginning
44 in: []*moduleInfo{testModuleA, testModuleB, testModuleC},
45 replace: testModuleA,
46 with: []*moduleInfo{testModuleD, testModuleE},
47 out: []*moduleInfo{testModuleD, testModuleE, testModuleB, testModuleC},
48 reallocate: true,
49 },
50 {
51 // Insert in the middle
52 in: []*moduleInfo{testModuleA, testModuleB, testModuleC},
53 replace: testModuleB,
54 with: []*moduleInfo{testModuleD, testModuleE},
55 out: []*moduleInfo{testModuleA, testModuleD, testModuleE, testModuleC},
56 reallocate: true,
57 },
58 {
59 // Insert at the end
60 in: []*moduleInfo{testModuleA, testModuleB, testModuleC},
61 replace: testModuleC,
62 with: []*moduleInfo{testModuleD, testModuleE},
63 out: []*moduleInfo{testModuleA, testModuleB, testModuleD, testModuleE},
64 reallocate: true,
65 },
66 {
67 // Insert over a single element
68 in: []*moduleInfo{testModuleA},
69 replace: testModuleA,
70 with: []*moduleInfo{testModuleD, testModuleE},
71 out: []*moduleInfo{testModuleD, testModuleE},
72 reallocate: true,
73 },
74 {
75 // Insert at the beginning without reallocating
76 in: []*moduleInfo{testModuleA, testModuleB, testModuleC, nil}[0:3],
77 replace: testModuleA,
78 with: []*moduleInfo{testModuleD, testModuleE},
79 out: []*moduleInfo{testModuleD, testModuleE, testModuleB, testModuleC},
80 reallocate: false,
81 },
82 {
83 // Insert in the middle without reallocating
84 in: []*moduleInfo{testModuleA, testModuleB, testModuleC, nil}[0:3],
85 replace: testModuleB,
86 with: []*moduleInfo{testModuleD, testModuleE},
87 out: []*moduleInfo{testModuleA, testModuleD, testModuleE, testModuleC},
88 reallocate: false,
89 },
90 {
91 // Insert at the end without reallocating
92 in: []*moduleInfo{testModuleA, testModuleB, testModuleC, nil}[0:3],
93 replace: testModuleC,
94 with: []*moduleInfo{testModuleD, testModuleE},
95 out: []*moduleInfo{testModuleA, testModuleB, testModuleD, testModuleE},
96 reallocate: false,
97 },
98 {
99 // Insert over a single element without reallocating
100 in: []*moduleInfo{testModuleA, nil}[0:1],
101 replace: testModuleA,
102 with: []*moduleInfo{testModuleD, testModuleE},
103 out: []*moduleInfo{testModuleD, testModuleE},
104 reallocate: false,
105 },
106}
107
108func TestSpliceModules(t *testing.T) {
109 for _, testCase := range spliceModulesTestCases {
110 in := make([]*moduleInfo, len(testCase.in), cap(testCase.in))
111 copy(in, testCase.in)
112 origIn := in
113 got := spliceModules(in, testCase.replace, testCase.with)
114 if !reflect.DeepEqual(got, testCase.out) {
115 t.Errorf("test case: %v, %v -> %v", testCase.in, testCase.replace, testCase.with)
116 t.Errorf("incorrect output:")
117 t.Errorf(" expected: %v", testCase.out)
118 t.Errorf(" got: %v", got)
119 }
120 if sameArray(origIn, got) != !testCase.reallocate {
121 t.Errorf("test case: %v, %v -> %v", testCase.in, testCase.replace, testCase.with)
122 not := ""
123 if !testCase.reallocate {
124 not = " not"
125 }
126 t.Errorf(" expected to%s reallocate", not)
127 }
128 }
129}
130
131func sameArray(a, b []*moduleInfo) bool {
132 return &a[0:cap(a)][cap(a)-1] == &b[0:cap(b)][cap(b)-1]
Jamie Gennis6cafc2c2015-03-20 22:39:29 -0400133}