Ben Clayton | 52c37f3 | 2021-05-31 19:49:50 +0000 | [diff] [blame] | 1 | // Copyright 2021 The Tint Authors. |
| 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 exprel or implied. |
| 12 | // See the License for the specific language governing permilions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package list_test |
| 16 | |
| 17 | import ( |
| 18 | "reflect" |
| 19 | "testing" |
| 20 | |
Ben Clayton | 59e96fe | 2022-04-07 17:50:24 +0000 | [diff] [blame] | 21 | "dawn.googlesource.com/dawn/tools/src/list" |
Ben Clayton | 52c37f3 | 2021-05-31 19:49:50 +0000 | [diff] [blame] | 22 | ) |
| 23 | |
| 24 | // A simple implementation of list.List. Many methods are just stubs |
| 25 | type customList struct{} |
| 26 | |
| 27 | func (customList) Count() int { return 3 } |
| 28 | func (customList) Get(i int) interface{} { return 10 + i*10 } |
| 29 | func (customList) Set(i int, v interface{}) {} |
| 30 | func (customList) Append(v interface{}) {} |
| 31 | func (customList) Copy(dst, src, count int) {} |
| 32 | func (customList) CopyFrom(l list.List, dst, src, count int) {} |
| 33 | func (customList) Resize(count int) {} |
| 34 | func (customList) ElementType() reflect.Type { return nil } |
| 35 | |
| 36 | var _ list.List = customList{} // Interface compliance check |
| 37 | |
| 38 | func TestNew(t *testing.T) { |
| 39 | l := list.New(reflect.TypeOf(0), 3) |
| 40 | |
| 41 | if n := l.Count(); n != 3 { |
| 42 | t.Errorf("Count(0): %v", n) |
| 43 | } |
| 44 | if n := l.Get(0); n != 0 { |
| 45 | t.Errorf("Get(0): %v", n) |
| 46 | } |
| 47 | if n := l.Get(1); n != 0 { |
| 48 | t.Errorf("Get(1): %v", n) |
| 49 | } |
| 50 | if n := l.Get(2); n != 0 { |
| 51 | t.Errorf("Get(2): %v", n) |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | func TestCopy(t *testing.T) { |
| 56 | slice := []int{1, 2, 3} |
| 57 | l := list.Wrap(&slice) |
| 58 | |
| 59 | c := list.Copy(l) |
| 60 | |
| 61 | if n := c.Count(); n != 3 { |
| 62 | t.Errorf("Count(0): %v", n) |
| 63 | } |
| 64 | if n := c.Get(0); n != 1 { |
| 65 | t.Errorf("Get(0): %v", n) |
| 66 | } |
| 67 | if n := c.Get(1); n != 2 { |
| 68 | t.Errorf("Get(1): %v", n) |
| 69 | } |
| 70 | if n := c.Get(2); n != 3 { |
| 71 | t.Errorf("Get(2): %v", n) |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | func TestListCount(t *testing.T) { |
| 76 | slice := make([]int, 5) |
| 77 | l := list.Wrap(&slice) |
| 78 | |
| 79 | if c := l.Count(); c != 5 { |
| 80 | t.Errorf("Count() is %v", c) |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | func TestListGrow(t *testing.T) { |
| 85 | slice := []int{} |
| 86 | l := list.Wrap(&slice) |
| 87 | |
| 88 | l.Resize(10) |
| 89 | |
| 90 | if len(slice) != 10 { |
| 91 | t.Errorf("len(slice) after Resize(10) is %v", len(slice)) |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | func TestListShrink(t *testing.T) { |
| 96 | slice := make([]int, 10) |
| 97 | l := list.Wrap(&slice) |
| 98 | |
| 99 | l.Resize(5) |
| 100 | |
| 101 | if len(slice) != 5 { |
| 102 | t.Errorf("len(slice) after Resize(5) is %v", len(slice)) |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | func TestListCopy(t *testing.T) { |
| 107 | slice := []int{0, 10, 20, 0, 0, 0} |
| 108 | l := list.Wrap(&slice) |
| 109 | |
| 110 | l.Copy(3, 1, 2) |
| 111 | |
| 112 | if !reflect.DeepEqual(slice, []int{0, 10, 20, 10, 20, 0}) { |
| 113 | t.Errorf("after Copy(), slice: %v", slice) |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | func TestListCopyFromList(t *testing.T) { |
| 118 | sliceA := []int{10, 20, 30, 40, 50, 60} |
| 119 | lA := list.Wrap(&sliceA) |
| 120 | |
| 121 | sliceB := []int{1, 2, 3, 4, 5, 6} |
| 122 | lB := list.Wrap(&sliceB) |
| 123 | |
| 124 | lA.CopyFrom(lB, 1, 2, 3) |
| 125 | |
| 126 | if !reflect.DeepEqual(sliceA, []int{10, 3, 4, 5, 50, 60}) { |
| 127 | t.Errorf("after CopyFrom(), slice: %v", sliceA) |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | func TestListCopyFromCustomList(t *testing.T) { |
| 132 | sliceA := []int{10, 20, 30, 40, 50, 60} |
| 133 | lA := list.Wrap(&sliceA) |
| 134 | |
| 135 | lA.CopyFrom(customList{}, 1, 2, 3) |
| 136 | |
| 137 | if !reflect.DeepEqual(sliceA, []int{10, 30, 40, 50, 50, 60}) { |
| 138 | t.Errorf("after CopyFrom(), slice: %v", sliceA) |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | func TestListGet(t *testing.T) { |
| 143 | slice := []int{0, 10, 20, 10, 20} |
| 144 | l := list.Wrap(&slice) |
| 145 | |
| 146 | if n := l.Get(0); n != 0 { |
| 147 | t.Errorf("Get(0): %v", n) |
| 148 | } |
| 149 | if n := l.Get(1); n != 10 { |
| 150 | t.Errorf("Get(1): %v", n) |
| 151 | } |
| 152 | if n := l.Get(2); n != 20 { |
| 153 | t.Errorf("Get(2): %v", n) |
| 154 | } |
| 155 | if n := l.Get(3); n != 10 { |
| 156 | t.Errorf("Get(3): %v", n) |
| 157 | } |
| 158 | if n := l.Get(4); n != 20 { |
| 159 | t.Errorf("Get(4): %v", n) |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | func TestListSet(t *testing.T) { |
| 164 | slice := []int{0, 10, 20, 10, 20} |
| 165 | l := list.Wrap(&slice) |
| 166 | |
| 167 | l.Set(0, 50) |
| 168 | l.Set(2, 90) |
| 169 | l.Set(4, 60) |
| 170 | |
| 171 | if !reflect.DeepEqual(slice, []int{50, 10, 90, 10, 60}) { |
| 172 | t.Errorf("after Set(), slice: %v", slice) |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | func TestListAppendItem(t *testing.T) { |
| 177 | slice := []int{1, 2, 3} |
| 178 | l := list.Wrap(&slice) |
| 179 | |
| 180 | l.Append(9) |
| 181 | |
| 182 | if c := len(slice); c != 4 { |
| 183 | t.Errorf("len(slice): %v", 4) |
| 184 | } |
| 185 | if n := slice[3]; n != 9 { |
| 186 | t.Errorf("slice[3]: %v", n) |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | func TestListAppendItems(t *testing.T) { |
| 191 | slice := []int{1, 2, 3} |
| 192 | l := list.Wrap(&slice) |
| 193 | |
| 194 | l.Append([]int{9, 8, 7}) |
| 195 | |
| 196 | if !reflect.DeepEqual(slice, []int{1, 2, 3, 9, 8, 7}) { |
| 197 | t.Errorf("after Append(), slice: %v", slice) |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | func TestListAppendList(t *testing.T) { |
| 202 | sliceA := []int{1, 2, 3} |
| 203 | lA := list.Wrap(&sliceA) |
| 204 | |
| 205 | sliceB := []int{9, 8, 7} |
| 206 | lB := list.Wrap(&sliceB) |
| 207 | |
| 208 | lA.Append(lB) |
| 209 | |
| 210 | if !reflect.DeepEqual(sliceA, []int{1, 2, 3, 9, 8, 7}) { |
| 211 | t.Errorf("after Append(), sliceA: %v", sliceA) |
| 212 | } |
| 213 | if !reflect.DeepEqual(sliceB, []int{9, 8, 7}) { |
| 214 | t.Errorf("after Append(), sliceB: %v", sliceB) |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | func TestListAppendCustomList(t *testing.T) { |
| 219 | sliceA := []int{1, 2, 3} |
| 220 | lA := list.Wrap(&sliceA) |
| 221 | |
| 222 | lA.Append(customList{}) |
| 223 | |
| 224 | if !reflect.DeepEqual(sliceA, []int{1, 2, 3, 10, 20, 30}) { |
| 225 | t.Errorf("after Append(), sliceA: %v", sliceA) |
| 226 | } |
| 227 | } |