blob: 473999addcf44b2f74b31de0b3bbd7b9fe550e0a [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 (
Colin Crossedc41762020-08-13 12:07:30 -070023 testModuleA = &moduleInfo{variant: variant{name: "testModuleA"}}
24 testModuleB = &moduleInfo{variant: variant{name: "testModuleB"}}
25 testModuleC = &moduleInfo{variant: variant{name: "testModuleC"}}
26 testModuleD = &moduleInfo{variant: variant{name: "testModuleD"}}
27 testModuleE = &moduleInfo{variant: variant{name: "testModuleE"}}
28 testModuleF = &moduleInfo{variant: variant{name: "testModuleF"}}
Colin Cross72bd1932015-03-16 00:13:59 -070029)
30
Colin Cross72bd1932015-03-16 00:13:59 -070031var spliceModulesTestCases = []struct {
Colin Cross5df74a82020-08-24 16:18:21 -070032 in modulesOrAliases
Colin Cross49c279a2016-08-05 22:30:44 -070033 at int
Colin Cross5df74a82020-08-24 16:18:21 -070034 with modulesOrAliases
35 out modulesOrAliases
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
Colin Cross5df74a82020-08-24 16:18:21 -070041 in: modulesOrAliases{testModuleA, testModuleB, testModuleC},
Colin Cross49c279a2016-08-05 22:30:44 -070042 at: 0,
Colin Cross5df74a82020-08-24 16:18:21 -070043 with: modulesOrAliases{testModuleD, testModuleE},
44 out: modulesOrAliases{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
Colin Cross5df74a82020-08-24 16:18:21 -070050 in: modulesOrAliases{testModuleA, testModuleB, testModuleC},
Colin Cross49c279a2016-08-05 22:30:44 -070051 at: 1,
Colin Cross5df74a82020-08-24 16:18:21 -070052 with: modulesOrAliases{testModuleD, testModuleE},
53 out: modulesOrAliases{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
Colin Cross5df74a82020-08-24 16:18:21 -070059 in: modulesOrAliases{testModuleA, testModuleB, testModuleC},
Colin Cross49c279a2016-08-05 22:30:44 -070060 at: 2,
Colin Cross5df74a82020-08-24 16:18:21 -070061 with: modulesOrAliases{testModuleD, testModuleE},
62 out: modulesOrAliases{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
Colin Cross5df74a82020-08-24 16:18:21 -070068 in: modulesOrAliases{testModuleA},
Colin Cross49c279a2016-08-05 22:30:44 -070069 at: 0,
Colin Cross5df74a82020-08-24 16:18:21 -070070 with: modulesOrAliases{testModuleD, testModuleE},
71 out: modulesOrAliases{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
Colin Cross5df74a82020-08-24 16:18:21 -070077 in: modulesOrAliases{testModuleA, testModuleB, testModuleC, nil}[0:3],
Colin Cross49c279a2016-08-05 22:30:44 -070078 at: 0,
Colin Cross5df74a82020-08-24 16:18:21 -070079 with: modulesOrAliases{testModuleD, testModuleE},
80 out: modulesOrAliases{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
Colin Cross5df74a82020-08-24 16:18:21 -070086 in: modulesOrAliases{testModuleA, testModuleB, testModuleC, nil}[0:3],
Colin Cross49c279a2016-08-05 22:30:44 -070087 at: 1,
Colin Cross5df74a82020-08-24 16:18:21 -070088 with: modulesOrAliases{testModuleD, testModuleE},
89 out: modulesOrAliases{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
Colin Cross5df74a82020-08-24 16:18:21 -070095 in: modulesOrAliases{testModuleA, testModuleB, testModuleC, nil}[0:3],
Colin Cross49c279a2016-08-05 22:30:44 -070096 at: 2,
Colin Cross5df74a82020-08-24 16:18:21 -070097 with: modulesOrAliases{testModuleD, testModuleE},
98 out: modulesOrAliases{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
Colin Cross5df74a82020-08-24 16:18:21 -0700104 in: modulesOrAliases{testModuleA, nil}[0:1],
Colin Cross49c279a2016-08-05 22:30:44 -0700105 at: 0,
Colin Cross5df74a82020-08-24 16:18:21 -0700106 with: modulesOrAliases{testModuleD, testModuleE},
107 out: modulesOrAliases{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 {
Colin Cross5df74a82020-08-24 16:18:21 -0700115 in := make(modulesOrAliases, len(testCase.in), cap(testCase.in))
Colin Cross72bd1932015-03-16 00:13:59 -0700116 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
Colin Cross5df74a82020-08-24 16:18:21 -0700142func sameArray(a, b modulesOrAliases) bool {
Colin Cross72bd1932015-03-16 00:13:59 -0700143 return &a[0:cap(a)][cap(a)-1] == &b[0:cap(b)][cap(b)-1]
Jamie Gennis6cafc2c2015-03-20 22:39:29 -0400144}