pocketlang/tests/lang/basics.pk
2021-06-14 21:50:07 +05:30

71 lines
1.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")
## 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
# If we got here with no errors, then all test passed
print('All tests PASSED (100%)')