| 1 |
project('plusassign') |
| 2 |
|
| 3 |
x = [] |
| 4 |
|
| 5 |
x += 'a' |
| 6 |
|
| 7 |
if x.length() != 1 |
| 8 |
error('Incorrect append') |
| 9 |
endif |
| 10 |
|
| 11 |
if x[0] != 'a' |
| 12 |
error('Incorrect append 2.') |
| 13 |
endif |
| 14 |
|
| 15 |
y = x |
| 16 |
|
| 17 |
x += 'b' |
| 18 |
|
| 19 |
if y.length() != 1 |
| 20 |
error('Immutability broken.') |
| 21 |
endif |
| 22 |
|
| 23 |
if y[0] != 'a' |
| 24 |
error('Immutability broken 2.') |
| 25 |
endif |
| 26 |
|
| 27 |
if x.length() != 2 |
| 28 |
error('Incorrect append 3') |
| 29 |
endif |
| 30 |
|
| 31 |
if x[0] != 'a' |
| 32 |
error('Incorrect append 4.') |
| 33 |
endif |
| 34 |
|
| 35 |
if x[1] != 'b' |
| 36 |
error('Incorrect append 5.') |
| 37 |
endif |
| 38 |
|
| 39 |
# Now with evil added: append yourself. |
| 40 |
|
| 41 |
x += x |
| 42 |
|
| 43 |
if x.length() != 4 |
| 44 |
error('Incorrect selfappend.') |
| 45 |
endif |
| 46 |
|
| 47 |
# += on strings |
| 48 |
|
| 49 |
bra = 'bra' |
| 50 |
foo = 'A' |
| 51 |
foo += bra |
| 52 |
foo += 'cada' |
| 53 |
foo += bra |
| 54 |
assert (foo == 'Abracadabra', 'string += failure [@0@]'.format(foo)) |
| 55 |
assert (bra == 'bra', 'string += modified right argument!') |
| 56 |
foo += ' ' + foo |
| 57 |
assert (foo == 'Abracadabra Abracadabra', 'string += failure [@0@]'.format(foo)) |
| 58 |
|
| 59 |
# += on ints |
| 60 |
|
| 61 |
foo = 5 |
| 62 |
foo += 6 |
| 63 |
assert (foo == 11, 'int += failure [@0@]'.format(foo)) |
| 64 |
bar = 99 |
| 65 |
foo += bar |
| 66 |
assert (foo == 110, 'int += failure [@0@]'.format(foo)) |
| 67 |
assert (bar == 99, 'int += modified right argument"') |
| 68 |
bar += foo + 1 |
| 69 |
assert (bar == 210, 'int += failure [@0@]'.format(bar)) |
| 70 |
assert (foo == 110, 'int += modified right argument"') |