Colin Cross | 72bd193 | 2015-03-16 00:13:59 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package blueprint |
| 16 | |
| 17 | import ( |
| 18 | "reflect" |
| 19 | "testing" |
| 20 | ) |
| 21 | |
| 22 | var ( |
Colin Cross | edc4176 | 2020-08-13 12:07:30 -0700 | [diff] [blame] | 23 | 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 Cross | 72bd193 | 2015-03-16 00:13:59 -0700 | [diff] [blame] | 29 | ) |
| 30 | |
Colin Cross | 72bd193 | 2015-03-16 00:13:59 -0700 | [diff] [blame] | 31 | var spliceModulesTestCases = []struct { |
Colin Cross | 5df74a8 | 2020-08-24 16:18:21 -0700 | [diff] [blame] | 32 | in modulesOrAliases |
Colin Cross | 49c279a | 2016-08-05 22:30:44 -0700 | [diff] [blame] | 33 | at int |
Colin Cross | 5df74a8 | 2020-08-24 16:18:21 -0700 | [diff] [blame] | 34 | with modulesOrAliases |
| 35 | out modulesOrAliases |
Colin Cross | 49c279a | 2016-08-05 22:30:44 -0700 | [diff] [blame] | 36 | outAt int |
Colin Cross | 72bd193 | 2015-03-16 00:13:59 -0700 | [diff] [blame] | 37 | reallocate bool |
| 38 | }{ |
| 39 | { |
| 40 | // Insert at the beginning |
Colin Cross | 5df74a8 | 2020-08-24 16:18:21 -0700 | [diff] [blame] | 41 | in: modulesOrAliases{testModuleA, testModuleB, testModuleC}, |
Colin Cross | 49c279a | 2016-08-05 22:30:44 -0700 | [diff] [blame] | 42 | at: 0, |
Colin Cross | 5df74a8 | 2020-08-24 16:18:21 -0700 | [diff] [blame] | 43 | with: modulesOrAliases{testModuleD, testModuleE}, |
| 44 | out: modulesOrAliases{testModuleD, testModuleE, testModuleB, testModuleC}, |
Colin Cross | 49c279a | 2016-08-05 22:30:44 -0700 | [diff] [blame] | 45 | outAt: 1, |
Colin Cross | 72bd193 | 2015-03-16 00:13:59 -0700 | [diff] [blame] | 46 | reallocate: true, |
| 47 | }, |
| 48 | { |
| 49 | // Insert in the middle |
Colin Cross | 5df74a8 | 2020-08-24 16:18:21 -0700 | [diff] [blame] | 50 | in: modulesOrAliases{testModuleA, testModuleB, testModuleC}, |
Colin Cross | 49c279a | 2016-08-05 22:30:44 -0700 | [diff] [blame] | 51 | at: 1, |
Colin Cross | 5df74a8 | 2020-08-24 16:18:21 -0700 | [diff] [blame] | 52 | with: modulesOrAliases{testModuleD, testModuleE}, |
| 53 | out: modulesOrAliases{testModuleA, testModuleD, testModuleE, testModuleC}, |
Colin Cross | 49c279a | 2016-08-05 22:30:44 -0700 | [diff] [blame] | 54 | outAt: 2, |
Colin Cross | 72bd193 | 2015-03-16 00:13:59 -0700 | [diff] [blame] | 55 | reallocate: true, |
| 56 | }, |
| 57 | { |
| 58 | // Insert at the end |
Colin Cross | 5df74a8 | 2020-08-24 16:18:21 -0700 | [diff] [blame] | 59 | in: modulesOrAliases{testModuleA, testModuleB, testModuleC}, |
Colin Cross | 49c279a | 2016-08-05 22:30:44 -0700 | [diff] [blame] | 60 | at: 2, |
Colin Cross | 5df74a8 | 2020-08-24 16:18:21 -0700 | [diff] [blame] | 61 | with: modulesOrAliases{testModuleD, testModuleE}, |
| 62 | out: modulesOrAliases{testModuleA, testModuleB, testModuleD, testModuleE}, |
Colin Cross | 49c279a | 2016-08-05 22:30:44 -0700 | [diff] [blame] | 63 | outAt: 3, |
Colin Cross | 72bd193 | 2015-03-16 00:13:59 -0700 | [diff] [blame] | 64 | reallocate: true, |
| 65 | }, |
| 66 | { |
| 67 | // Insert over a single element |
Colin Cross | 5df74a8 | 2020-08-24 16:18:21 -0700 | [diff] [blame] | 68 | in: modulesOrAliases{testModuleA}, |
Colin Cross | 49c279a | 2016-08-05 22:30:44 -0700 | [diff] [blame] | 69 | at: 0, |
Colin Cross | 5df74a8 | 2020-08-24 16:18:21 -0700 | [diff] [blame] | 70 | with: modulesOrAliases{testModuleD, testModuleE}, |
| 71 | out: modulesOrAliases{testModuleD, testModuleE}, |
Colin Cross | 49c279a | 2016-08-05 22:30:44 -0700 | [diff] [blame] | 72 | outAt: 1, |
Colin Cross | 72bd193 | 2015-03-16 00:13:59 -0700 | [diff] [blame] | 73 | reallocate: true, |
| 74 | }, |
| 75 | { |
| 76 | // Insert at the beginning without reallocating |
Colin Cross | 5df74a8 | 2020-08-24 16:18:21 -0700 | [diff] [blame] | 77 | in: modulesOrAliases{testModuleA, testModuleB, testModuleC, nil}[0:3], |
Colin Cross | 49c279a | 2016-08-05 22:30:44 -0700 | [diff] [blame] | 78 | at: 0, |
Colin Cross | 5df74a8 | 2020-08-24 16:18:21 -0700 | [diff] [blame] | 79 | with: modulesOrAliases{testModuleD, testModuleE}, |
| 80 | out: modulesOrAliases{testModuleD, testModuleE, testModuleB, testModuleC}, |
Colin Cross | 49c279a | 2016-08-05 22:30:44 -0700 | [diff] [blame] | 81 | outAt: 1, |
Colin Cross | 72bd193 | 2015-03-16 00:13:59 -0700 | [diff] [blame] | 82 | reallocate: false, |
| 83 | }, |
| 84 | { |
| 85 | // Insert in the middle without reallocating |
Colin Cross | 5df74a8 | 2020-08-24 16:18:21 -0700 | [diff] [blame] | 86 | in: modulesOrAliases{testModuleA, testModuleB, testModuleC, nil}[0:3], |
Colin Cross | 49c279a | 2016-08-05 22:30:44 -0700 | [diff] [blame] | 87 | at: 1, |
Colin Cross | 5df74a8 | 2020-08-24 16:18:21 -0700 | [diff] [blame] | 88 | with: modulesOrAliases{testModuleD, testModuleE}, |
| 89 | out: modulesOrAliases{testModuleA, testModuleD, testModuleE, testModuleC}, |
Colin Cross | 49c279a | 2016-08-05 22:30:44 -0700 | [diff] [blame] | 90 | outAt: 2, |
Colin Cross | 72bd193 | 2015-03-16 00:13:59 -0700 | [diff] [blame] | 91 | reallocate: false, |
| 92 | }, |
| 93 | { |
| 94 | // Insert at the end without reallocating |
Colin Cross | 5df74a8 | 2020-08-24 16:18:21 -0700 | [diff] [blame] | 95 | in: modulesOrAliases{testModuleA, testModuleB, testModuleC, nil}[0:3], |
Colin Cross | 49c279a | 2016-08-05 22:30:44 -0700 | [diff] [blame] | 96 | at: 2, |
Colin Cross | 5df74a8 | 2020-08-24 16:18:21 -0700 | [diff] [blame] | 97 | with: modulesOrAliases{testModuleD, testModuleE}, |
| 98 | out: modulesOrAliases{testModuleA, testModuleB, testModuleD, testModuleE}, |
Colin Cross | 49c279a | 2016-08-05 22:30:44 -0700 | [diff] [blame] | 99 | outAt: 3, |
Colin Cross | 72bd193 | 2015-03-16 00:13:59 -0700 | [diff] [blame] | 100 | reallocate: false, |
| 101 | }, |
| 102 | { |
| 103 | // Insert over a single element without reallocating |
Colin Cross | 5df74a8 | 2020-08-24 16:18:21 -0700 | [diff] [blame] | 104 | in: modulesOrAliases{testModuleA, nil}[0:1], |
Colin Cross | 49c279a | 2016-08-05 22:30:44 -0700 | [diff] [blame] | 105 | at: 0, |
Colin Cross | 5df74a8 | 2020-08-24 16:18:21 -0700 | [diff] [blame] | 106 | with: modulesOrAliases{testModuleD, testModuleE}, |
| 107 | out: modulesOrAliases{testModuleD, testModuleE}, |
Colin Cross | 49c279a | 2016-08-05 22:30:44 -0700 | [diff] [blame] | 108 | outAt: 1, |
Colin Cross | 72bd193 | 2015-03-16 00:13:59 -0700 | [diff] [blame] | 109 | reallocate: false, |
| 110 | }, |
| 111 | } |
| 112 | |
| 113 | func TestSpliceModules(t *testing.T) { |
| 114 | for _, testCase := range spliceModulesTestCases { |
Colin Cross | 5df74a8 | 2020-08-24 16:18:21 -0700 | [diff] [blame] | 115 | in := make(modulesOrAliases, len(testCase.in), cap(testCase.in)) |
Colin Cross | 72bd193 | 2015-03-16 00:13:59 -0700 | [diff] [blame] | 116 | copy(in, testCase.in) |
| 117 | origIn := in |
Colin Cross | 49c279a | 2016-08-05 22:30:44 -0700 | [diff] [blame] | 118 | got, gotAt := spliceModules(in, testCase.at, testCase.with) |
Colin Cross | 72bd193 | 2015-03-16 00:13:59 -0700 | [diff] [blame] | 119 | if !reflect.DeepEqual(got, testCase.out) { |
Colin Cross | 49c279a | 2016-08-05 22:30:44 -0700 | [diff] [blame] | 120 | t.Errorf("test case: %v, %v -> %v", testCase.in, testCase.at, testCase.with) |
Colin Cross | 72bd193 | 2015-03-16 00:13:59 -0700 | [diff] [blame] | 121 | t.Errorf("incorrect output:") |
| 122 | t.Errorf(" expected: %v", testCase.out) |
| 123 | t.Errorf(" got: %v", got) |
| 124 | } |
Colin Cross | 49c279a | 2016-08-05 22:30:44 -0700 | [diff] [blame] | 125 | 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 Cross | 72bd193 | 2015-03-16 00:13:59 -0700 | [diff] [blame] | 131 | if sameArray(origIn, got) != !testCase.reallocate { |
Colin Cross | 49c279a | 2016-08-05 22:30:44 -0700 | [diff] [blame] | 132 | t.Errorf("test case: %v, %v -> %v", testCase.in, testCase.at, testCase.with) |
Colin Cross | 72bd193 | 2015-03-16 00:13:59 -0700 | [diff] [blame] | 133 | not := "" |
| 134 | if !testCase.reallocate { |
| 135 | not = " not" |
| 136 | } |
| 137 | t.Errorf(" expected to%s reallocate", not) |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
Colin Cross | 5df74a8 | 2020-08-24 16:18:21 -0700 | [diff] [blame] | 142 | func sameArray(a, b modulesOrAliases) bool { |
Colin Cross | 72bd193 | 2015-03-16 00:13:59 -0700 | [diff] [blame] | 143 | return &a[0:cap(a)][cap(a)-1] == &b[0:cap(b)][cap(b)-1] |
Jamie Gennis | 6cafc2c | 2015-03-20 22:39:29 -0400 | [diff] [blame] | 144 | } |