blob: a67aeb1264629d637aca8ceb79cd428ec2b52e19 [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
Colin Cross72bd1932015-03-16 00:13:59 -070031var spliceModulesTestCases = []struct {
32 in []*moduleInfo
Colin Cross49c279a2016-08-05 22:30:44 -070033 at int
Colin Cross72bd1932015-03-16 00:13:59 -070034 with []*moduleInfo
35 out []*moduleInfo
Colin Cross49c279a2016-08-05 22:30:44 -070036 outAt int
Colin Cross72bd1932015-03-16 00:13:59 -070037 reallocate bool
38}{
39 {
40 // Insert at the beginning
41 in: []*moduleInfo{testModuleA, testModuleB, testModuleC},
Colin Cross49c279a2016-08-05 22:30:44 -070042 at: 0,
Colin Cross72bd1932015-03-16 00:13:59 -070043 with: []*moduleInfo{testModuleD, testModuleE},
44 out: []*moduleInfo{testModuleD, testModuleE, testModuleB, testModuleC},
Colin Cross49c279a2016-08-05 22:30:44 -070045 outAt: 1,
Colin Cross72bd1932015-03-16 00:13:59 -070046 reallocate: true,
47 },
48 {
49 // Insert in the middle
50 in: []*moduleInfo{testModuleA, testModuleB, testModuleC},
Colin Cross49c279a2016-08-05 22:30:44 -070051 at: 1,
Colin Cross72bd1932015-03-16 00:13:59 -070052 with: []*moduleInfo{testModuleD, testModuleE},
53 out: []*moduleInfo{testModuleA, testModuleD, testModuleE, testModuleC},
Colin Cross49c279a2016-08-05 22:30:44 -070054 outAt: 2,
Colin Cross72bd1932015-03-16 00:13:59 -070055 reallocate: true,
56 },
57 {
58 // Insert at the end
59 in: []*moduleInfo{testModuleA, testModuleB, testModuleC},
Colin Cross49c279a2016-08-05 22:30:44 -070060 at: 2,
Colin Cross72bd1932015-03-16 00:13:59 -070061 with: []*moduleInfo{testModuleD, testModuleE},
62 out: []*moduleInfo{testModuleA, testModuleB, testModuleD, testModuleE},
Colin Cross49c279a2016-08-05 22:30:44 -070063 outAt: 3,
Colin Cross72bd1932015-03-16 00:13:59 -070064 reallocate: true,
65 },
66 {
67 // Insert over a single element
68 in: []*moduleInfo{testModuleA},
Colin Cross49c279a2016-08-05 22:30:44 -070069 at: 0,
Colin Cross72bd1932015-03-16 00:13:59 -070070 with: []*moduleInfo{testModuleD, testModuleE},
71 out: []*moduleInfo{testModuleD, testModuleE},
Colin Cross49c279a2016-08-05 22:30:44 -070072 outAt: 1,
Colin Cross72bd1932015-03-16 00:13:59 -070073 reallocate: true,
74 },
75 {
76 // Insert at the beginning without reallocating
77 in: []*moduleInfo{testModuleA, testModuleB, testModuleC, nil}[0:3],
Colin Cross49c279a2016-08-05 22:30:44 -070078 at: 0,
Colin Cross72bd1932015-03-16 00:13:59 -070079 with: []*moduleInfo{testModuleD, testModuleE},
80 out: []*moduleInfo{testModuleD, testModuleE, testModuleB, testModuleC},
Colin Cross49c279a2016-08-05 22:30:44 -070081 outAt: 1,
Colin Cross72bd1932015-03-16 00:13:59 -070082 reallocate: false,
83 },
84 {
85 // Insert in the middle without reallocating
86 in: []*moduleInfo{testModuleA, testModuleB, testModuleC, nil}[0:3],
Colin Cross49c279a2016-08-05 22:30:44 -070087 at: 1,
Colin Cross72bd1932015-03-16 00:13:59 -070088 with: []*moduleInfo{testModuleD, testModuleE},
89 out: []*moduleInfo{testModuleA, testModuleD, testModuleE, testModuleC},
Colin Cross49c279a2016-08-05 22:30:44 -070090 outAt: 2,
Colin Cross72bd1932015-03-16 00:13:59 -070091 reallocate: false,
92 },
93 {
94 // Insert at the end without reallocating
95 in: []*moduleInfo{testModuleA, testModuleB, testModuleC, nil}[0:3],
Colin Cross49c279a2016-08-05 22:30:44 -070096 at: 2,
Colin Cross72bd1932015-03-16 00:13:59 -070097 with: []*moduleInfo{testModuleD, testModuleE},
98 out: []*moduleInfo{testModuleA, testModuleB, testModuleD, testModuleE},
Colin Cross49c279a2016-08-05 22:30:44 -070099 outAt: 3,
Colin Cross72bd1932015-03-16 00:13:59 -0700100 reallocate: false,
101 },
102 {
103 // Insert over a single element without reallocating
104 in: []*moduleInfo{testModuleA, nil}[0:1],
Colin Cross49c279a2016-08-05 22:30:44 -0700105 at: 0,
Colin Cross72bd1932015-03-16 00:13:59 -0700106 with: []*moduleInfo{testModuleD, testModuleE},
107 out: []*moduleInfo{testModuleD, testModuleE},
Colin Cross49c279a2016-08-05 22:30:44 -0700108 outAt: 1,
Colin Cross72bd1932015-03-16 00:13:59 -0700109 reallocate: false,
110 },
111}
112
113func TestSpliceModules(t *testing.T) {
114 for _, testCase := range spliceModulesTestCases {
115 in := make([]*moduleInfo, len(testCase.in), cap(testCase.in))
116 copy(in, testCase.in)
117 origIn := in
Colin Cross49c279a2016-08-05 22:30:44 -0700118 got, gotAt := spliceModules(in, testCase.at, testCase.with)
Colin Cross72bd1932015-03-16 00:13:59 -0700119 if !reflect.DeepEqual(got, testCase.out) {
Colin Cross49c279a2016-08-05 22:30:44 -0700120 t.Errorf("test case: %v, %v -> %v", testCase.in, testCase.at, testCase.with)
Colin Cross72bd1932015-03-16 00:13:59 -0700121 t.Errorf("incorrect output:")
122 t.Errorf(" expected: %v", testCase.out)
123 t.Errorf(" got: %v", got)
124 }
Colin Cross49c279a2016-08-05 22:30:44 -0700125 if gotAt != testCase.outAt {
126 t.Errorf("test case: %v, %v -> %v", testCase.in, testCase.at, testCase.with)
127 t.Errorf("incorrect index:")
128 t.Errorf(" expected: %d", testCase.outAt)
129 t.Errorf(" got: %d", gotAt)
130 }
Colin Cross72bd1932015-03-16 00:13:59 -0700131 if sameArray(origIn, got) != !testCase.reallocate {
Colin Cross49c279a2016-08-05 22:30:44 -0700132 t.Errorf("test case: %v, %v -> %v", testCase.in, testCase.at, testCase.with)
Colin Cross72bd1932015-03-16 00:13:59 -0700133 not := ""
134 if !testCase.reallocate {
135 not = " not"
136 }
137 t.Errorf(" expected to%s reallocate", not)
138 }
139 }
140}
141
142func sameArray(a, b []*moduleInfo) bool {
143 return &a[0:cap(a)][cap(a)-1] == &b[0:cap(b)][cap(b)-1]
Jamie Gennis6cafc2c2015-03-20 22:39:29 -0400144}