mirror of
https://github.com/zekexiao/pocketlang.git
synced 2025-02-06 12:46:53 +08:00
87 lines
1.8 KiB
Plaintext
87 lines
1.8 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")
|
|
|
|
## Lists test.
|
|
l1 = [1, false, null, func print('hello') end]
|
|
assert(is_null(l1[2]))
|
|
l1[1] = true; assert(l1[1])
|
|
|
|
## 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((1 | 1) == 1)
|
|
assert((1 | 4) == 5)
|
|
assert((3 | 5) == 7)
|
|
|
|
assert((1 & 1) == 1)
|
|
assert((1 & 2) == 0)
|
|
assert((4 & 7) == 4)
|
|
|
|
assert((1 ^ 1) == 0)
|
|
assert((1 ^ 3) == 2)
|
|
assert((2 ^ 9) == 11)
|
|
assert((0 ^ 0) == 0)
|
|
|
|
assert((1 << 0) == 1)
|
|
assert((1 << 2) == 4)
|
|
assert((3 << 2) == 12)
|
|
|
|
assert((1 >> 1) == 0)
|
|
assert((8 >> 1) == 4)
|
|
assert((8 >> 2) == 2)
|
|
assert((4 >> 2) == 1)
|
|
|
|
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 with no errors, then all test passed
|
|
print('All tests PASSED (100%)')
|
|
|