mirror of
https://github.com/zekexiao/pocketlang.git
synced 2025-02-06 12:46:53 +08:00
59333177cd
This commit implements list addition. A new list is allocated, and the contents of l1 and l2 are concatenated onto the list. This is done by extending the buffer template macro to support a `concat` operation, which given an `other` buffer, appends all elements of the `other` buffer to the end of the `self` buffer. Allocations are done as needed. We implement this additional operation, since the alternative would be to call `reserve()` for each `write()` operation, which doesn't seem ideal. Issue: #55
101 lines
2.5 KiB
Plaintext
101 lines
2.5 KiB
Plaintext
|
|
s = "foo"; s += "bar"
|
|
assert(s == "foobar")
|
|
assert( 1 + 2 * 3 == 7)
|
|
assert((1 + 2)* 3 == 9)
|
|
assert(42 % 40.0 == 2)
|
|
assert("'\"'" == '\'"\'')
|
|
assert("testing" == "test" + "ing")
|
|
|
|
assert(-0b10110010 == -178 and 0b11001010 == 202)
|
|
assert(0b1111111111111111 == 65535)
|
|
assert(
|
|
0b1111111111111111111111111111111111111111111111111111111111111111 ==
|
|
18446744073709551615)
|
|
|
|
assert(0xc0ffee == 12648430)
|
|
assert(0xa1b2c3 == 10597059 and 0xff == 255)
|
|
assert(0xffffffffffffffff == 18446744073709551615)
|
|
|
|
## Lists test.
|
|
l1 = [1, false, null, func print('hello') end]
|
|
assert(is_null(l1[2]))
|
|
l1[1] = true; assert(l1[1])
|
|
l1 = [] + [] ; assert(l1.length == 0)
|
|
l1 = [] + [1]; assert(l1.length == 1); assert(l1[0] == 1)
|
|
l1 = [1] + []; assert(l1.length == 1); assert(l1[0] == 1)
|
|
l2 = l1 + [1,2,3]; assert(l2.length == 4); assert(l2 == [1,1,2,3])
|
|
l3 = l2 + l1 + l2; assert(l3 == [1,1,2,3,1,1,1,2,3])
|
|
|
|
## Builtin functions tests.
|
|
assert(to_string(42) == '42')
|
|
|
|
## Core module test.
|
|
import math
|
|
h1 = math.hash("testing"); h2 = math.hash("test" + "ing")
|
|
assert(h1 == h2)
|
|
assert(math.ceil(1.1) == math.floor(2.9))
|
|
|
|
## Logical statement test
|
|
val = 0; a = false; b = true
|
|
get_true = func return true end
|
|
if a and b then assert(false) end
|
|
if a or b then val = 42 else assert(false) end assert(val == 42)
|
|
if get_true() or false then val = 12 end assert(val == 12)
|
|
|
|
## Recursive to_string list/map
|
|
l = [1]
|
|
list_append(l, l)
|
|
assert(to_string(l) == '[1, [...]]')
|
|
m = {}
|
|
m['m'] = m
|
|
assert(to_string(m) == '{"m":{...}}')
|
|
|
|
# Bitwise operation tests
|
|
assert(0b1010 | 0b0101 == 0b1111)
|
|
assert(0b1000 | 0b0001 == 0b1001)
|
|
assert(0b0011 | 0b1001 == 0b1011)
|
|
|
|
assert(0b1010 & 0b0101 == 0b0000)
|
|
assert(0b1100 & 0b1010 == 0b1000)
|
|
assert(0xabcd & 0xffff == 0xabcd)
|
|
|
|
for i in 0..100 do assert(i^i == 0) end
|
|
assert(0b0001 ^ 0b0011 == 0b0010)
|
|
assert(0b0010 ^ 0b1001 == 0b1011)
|
|
|
|
assert(0x1 << 0 == 0b1)
|
|
assert(0b1 << 2 == 0b100)
|
|
assert(0b1010 << 2 == 0b101000)
|
|
|
|
assert(0b1 >> 1 == 0)
|
|
assert(8 >> 1 == 0x4)
|
|
assert(8 >> 2 == 0b10)
|
|
assert(0xa >> 1 == 5)
|
|
|
|
x = 42 ; assert((x&=51) == 34)
|
|
x = 123 ; assert((x&=324) == 64)
|
|
for i in 0..1000
|
|
x = i; assert((x&=-1) == i)
|
|
end
|
|
|
|
x = 43690 ; assert((x|=21845) == 65535)
|
|
x = 204 ; assert((x|=119) == 255)
|
|
for i in 0..1000
|
|
x = i; assert((x|=0) == i)
|
|
x = i; assert((x|=-1) == -1)
|
|
x = i; assert((x|=i) == i)
|
|
end
|
|
|
|
x = 4321 ; assert((x^=1234) == 5171)
|
|
x = 77 ; assert((x^=88) == 21)
|
|
for i in 0..1000
|
|
x = i; assert((x^=i) == 0)
|
|
x = i; assert((x^=0) == i)
|
|
x = i; assert((x^=-1) == -i-1)
|
|
end
|
|
|
|
|
|
# If we got here, that means all test were passed.
|
|
print('All TESTS PASSED')
|