1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
|
# Tests of Starlark 'list'
load("assert.star", "assert", "freeze")
# literals
assert.eq([], [])
assert.eq([1], [1])
assert.eq([1], [1])
assert.eq([1, 2], [1, 2])
assert.ne([1, 2, 3], [1, 2, 4])
# truth
assert.true([0])
assert.true(not [])
# indexing, x[i]
abc = list("abc".elems())
assert.fails(lambda: abc[-4], "list index -4 out of range \\[-3:2]")
assert.eq(abc[-3], "a")
assert.eq(abc[-2], "b")
assert.eq(abc[-1], "c")
assert.eq(abc[0], "a")
assert.eq(abc[1], "b")
assert.eq(abc[2], "c")
assert.fails(lambda: abc[3], "list index 3 out of range \\[-3:2]")
# x[i] = ...
x3 = [0, 1, 2]
x3[1] = 2
x3[2] += 3
assert.eq(x3, [0, 2, 5])
def f2():
x3[3] = 4
assert.fails(f2, "out of range")
freeze(x3)
def f3():
x3[0] = 0
assert.fails(f3, "cannot assign to element of frozen list")
assert.fails(x3.clear, "cannot clear frozen list")
# list + list
assert.eq([1, 2, 3] + [3, 4, 5], [1, 2, 3, 3, 4, 5])
assert.fails(lambda: [1, 2] + (3, 4), "unknown.*list \\+ tuple")
assert.fails(lambda: (1, 2) + [3, 4], "unknown.*tuple \\+ list")
# list * int, int * list
assert.eq(abc * 0, [])
assert.eq(abc * -1, [])
assert.eq(abc * 1, abc)
assert.eq(abc * 3, ["a", "b", "c", "a", "b", "c", "a", "b", "c"])
assert.eq(0 * abc, [])
assert.eq(-1 * abc, [])
assert.eq(1 * abc, abc)
assert.eq(3 * abc, ["a", "b", "c", "a", "b", "c", "a", "b", "c"])
# list comprehensions
assert.eq([2 * x for x in [1, 2, 3]], [2, 4, 6])
assert.eq([2 * x for x in [1, 2, 3] if x > 1], [4, 6])
assert.eq(
[(x, y) for x in [1, 2] for y in [3, 4]],
[(1, 3), (1, 4), (2, 3), (2, 4)],
)
assert.eq([(x, y) for x in [1, 2] if x == 2 for y in [3, 4]], [(2, 3), (2, 4)])
assert.eq([2 * x for x in (1, 2, 3)], [2, 4, 6])
assert.eq([x for x in "abc".elems()], ["a", "b", "c"])
assert.eq([x for x in {"a": 1, "b": 2}], ["a", "b"])
assert.eq([(y, x) for x, y in {1: 2, 3: 4}.items()], [(2, 1), (4, 3)])
# corner cases of parsing:
assert.eq([x for x in range(12) if x % 2 == 0 if x % 3 == 0], [0, 6])
assert.eq([x for x in [1, 2] if lambda: None], [1, 2])
assert.eq([x for x in [1, 2] if (lambda: 3 if True else 4)], [1, 2])
# list function
assert.eq(list(), [])
assert.eq(list("ab".elems()), ["a", "b"])
# A list comprehension defines a separate lexical block,
# whether at top-level...
a = [1, 2]
b = [a for a in [3, 4]]
assert.eq(a, [1, 2])
assert.eq(b, [3, 4])
# ...or local to a function.
def listcompblock():
c = [1, 2]
d = [c for c in [3, 4]]
assert.eq(c, [1, 2])
assert.eq(d, [3, 4])
listcompblock()
# list.pop
x4 = [1, 2, 3, 4, 5]
assert.fails(lambda: x4.pop(-6), "index -6 out of range \\[-5:4]")
assert.fails(lambda: x4.pop(6), "index 6 out of range \\[-5:4]")
assert.eq(x4.pop(), 5)
assert.eq(x4, [1, 2, 3, 4])
assert.eq(x4.pop(1), 2)
assert.eq(x4, [1, 3, 4])
assert.eq(x4.pop(0), 1)
assert.eq(x4, [3, 4])
assert.eq(x4.pop(-2), 3)
assert.eq(x4, [4])
assert.eq(x4.pop(-1), 4)
assert.eq(x4, [])
# TODO(adonovan): test uses of list as sequence
# (for loop, comprehension, library functions).
# x += y for lists is equivalent to x.extend(y).
# y may be a sequence.
# TODO: Test that side-effects of 'x' occur only once.
def list_extend():
a = [1, 2, 3]
b = a
a = a + [4] # creates a new list
assert.eq(a, [1, 2, 3, 4])
assert.eq(b, [1, 2, 3]) # b is unchanged
a = [1, 2, 3]
b = a
a += [4] # updates a (and thus b) in place
assert.eq(a, [1, 2, 3, 4])
assert.eq(b, [1, 2, 3, 4]) # alias observes the change
a = [1, 2, 3]
b = a
a.extend([4]) # updates existing list
assert.eq(a, [1, 2, 3, 4])
assert.eq(b, [1, 2, 3, 4]) # alias observes the change
list_extend()
# Unlike list.extend(iterable), list += iterable makes its LHS name local.
a_list = []
def f4():
a_list += [1] # binding use => a_list is a local var
assert.fails(f4, "local variable a_list referenced before assignment")
# list += <not iterable>
def f5():
x = []
x += 1
assert.fails(f5, "unknown binary op: list \\+ int")
# frozen list += iterable
def f6():
x = []
freeze(x)
x += [1]
assert.fails(f6, "cannot apply \\+= to frozen list")
# list += hasfields (hasfields is not iterable but defines list+hasfields)
def f7():
x = []
x += hasfields()
return x
assert.eq(f7(), 42) # weird, but exercises a corner case in list+=x.
# append
x5 = [1, 2, 3]
x5.append(4)
x5.append("abc")
assert.eq(x5, [1, 2, 3, 4, "abc"])
# extend
x5a = [1, 2, 3]
x5a.extend("abc".elems()) # string
x5a.extend((True, False)) # tuple
assert.eq(x5a, [1, 2, 3, "a", "b", "c", True, False])
# list.insert
def insert_at(index):
x = list(range(3))
x.insert(index, 42)
return x
assert.eq(insert_at(-99), [42, 0, 1, 2])
assert.eq(insert_at(-2), [0, 42, 1, 2])
assert.eq(insert_at(-1), [0, 1, 42, 2])
assert.eq(insert_at(0), [42, 0, 1, 2])
assert.eq(insert_at(1), [0, 42, 1, 2])
assert.eq(insert_at(2), [0, 1, 42, 2])
assert.eq(insert_at(3), [0, 1, 2, 42])
assert.eq(insert_at(4), [0, 1, 2, 42])
# list.remove
def remove(v):
x = [3, 1, 4, 1]
x.remove(v)
return x
assert.eq(remove(3), [1, 4, 1])
assert.eq(remove(1), [3, 4, 1])
assert.eq(remove(4), [3, 1, 1])
assert.fails(lambda: [3, 1, 4, 1].remove(42), "remove: element not found")
# list.index
bananas = list("bananas".elems())
assert.eq(bananas.index("a"), 1) # bAnanas
assert.fails(lambda: bananas.index("d"), "value not in list")
# start
assert.eq(bananas.index("a", -1000), 1) # bAnanas
assert.eq(bananas.index("a", 0), 1) # bAnanas
assert.eq(bananas.index("a", 1), 1) # bAnanas
assert.eq(bananas.index("a", 2), 3) # banAnas
assert.eq(bananas.index("a", 3), 3) # banAnas
assert.eq(bananas.index("b", 0), 0) # Bananas
assert.eq(bananas.index("n", -3), 4) # banaNas
assert.fails(lambda: bananas.index("n", -2), "value not in list")
assert.eq(bananas.index("s", -2), 6) # bananaS
assert.fails(lambda: bananas.index("b", 1), "value not in list")
# start, end
assert.eq(bananas.index("s", -1000, 7), 6) # bananaS
assert.fails(lambda: bananas.index("s", -1000, 6), "value not in list")
assert.fails(lambda: bananas.index("d", -1000, 1000), "value not in list")
# slicing, x[i:j:k]
assert.eq(bananas[6::-2], list("snnb".elems()))
assert.eq(bananas[5::-2], list("aaa".elems()))
assert.eq(bananas[4::-2], list("nnb".elems()))
assert.eq(bananas[99::-2], list("snnb".elems()))
assert.eq(bananas[100::-2], list("snnb".elems()))
# TODO(adonovan): many more tests
# iterator invalidation
def iterator1():
list = [0, 1, 2]
for x in list:
list[x] = 2 * x
return list
assert.fails(iterator1, "assign to element.* during iteration")
def iterator2():
list = [0, 1, 2]
for x in list:
list.remove(x)
assert.fails(iterator2, "remove.*during iteration")
def iterator3():
list = [0, 1, 2]
for x in list:
list.append(3)
assert.fails(iterator3, "append.*during iteration")
def iterator4():
list = [0, 1, 2]
for x in list:
list.extend([3, 4])
assert.fails(iterator4, "extend.*during iteration")
def iterator5():
def f(x):
x.append(4)
list = [1, 2, 3]
_ = [f(list) for x in list]
assert.fails(iterator5, "append.*during iteration")
|