mirror of
https://github.com/zekexiao/pocketlang.git
synced 2025-02-06 12:46:53 +08:00
88c45cccac
- `func` keyword has change to `function`. - `elsif` changed to `else if`
109 lines
2.8 KiB
Plaintext
109 lines
2.8 KiB
Plaintext
## Core builtin functions and attribute tests.
|
|
|
|
## Math functions
|
|
from math import *
|
|
|
|
assert(hex(12648430) == '0xc0ffee')
|
|
assert(hex(255) == '0xff' and hex(10597059) == '0xa1b2c3')
|
|
assert(hex(-4294967295) == '-0xffffffff') ## the largest.
|
|
|
|
## string attributes.
|
|
assert(''.length == 0)
|
|
assert('test'.length == 4)
|
|
assert(''.lower == '' and ''.upper == '')
|
|
assert('already+lower '.lower == 'already+lower ')
|
|
assert('ALREADY+UPPER '.upper == 'ALREADY+UPPER ')
|
|
assert('tEST+InG'.lower == 'test+ing')
|
|
assert('tEST+InG'.upper == 'TEST+ING')
|
|
|
|
assert(' trim '.strip == 'trim')
|
|
assert(''.strip == '')
|
|
|
|
## List attribute
|
|
assert([].length == 0)
|
|
assert([1, 2, 3].length == 3)
|
|
|
|
## Function
|
|
assert(print.arity == -1)
|
|
assert(hex.arity == 1)
|
|
assert(function(a, b)end .arity == 2)
|
|
assert(print.name == "print")
|
|
def fn(p1, p2, p3) end
|
|
assert(fn.name == "fn")
|
|
assert(fn.arity == 3)
|
|
|
|
## String functions
|
|
assert(str_sub('c programming', 2, 11) == 'programming')
|
|
assert(str_sub('foobarbaz', 2, 3) == 'oba')
|
|
assert(str_sub('abcdef', 1, 2) == 'bc')
|
|
assert(str_sub('I am a boy', 0, 4) == 'I am')
|
|
assert(str_sub('programming', 2, 0) == '')
|
|
assert(str_sub('foobar', 5, 0) == '')
|
|
assert(str_sub('foobar', 0, 6) == 'foobar')
|
|
assert(str_sub('', 0, 0) == '')
|
|
|
|
## range
|
|
r = 1..5
|
|
assert(r.as_list == [1, 2, 3, 4])
|
|
assert(r.first == 1)
|
|
assert(r.last == 5)
|
|
|
|
assert(sin(0) == 0)
|
|
assert(sin(PI/2) == 1)
|
|
|
|
threshold = 0.0000000000001
|
|
|
|
assert(abs(cos(PI/3) - 0.5) < threshold )
|
|
assert(abs(tan(PI/4) - 1.0) < threshold )
|
|
for i in 0..1000
|
|
assert(abs(sin(i) / cos(i) - tan(i)) < threshold)
|
|
end
|
|
|
|
assert((cosh(.5) - 1.1276259652063807) < threshold)
|
|
assert((tanh(0.5) - 1.127625965206) < threshold)
|
|
for i in 0..100
|
|
assert(abs(sinh(i) / cosh(i) - tanh(i)) < threshold)
|
|
end
|
|
|
|
assert(abs(acos(PI/4) - 0.5) < 0.35)
|
|
assert(abs(atan(PI/4) - 0.5) < 0.2)
|
|
|
|
assert((acos(0.5) - 1.1276259652063807) < threshold)
|
|
assert((atan(0.3) - 1.1276259652063807) < threshold)
|
|
|
|
x = -1; interval = 1000
|
|
for i in 0..interval-1
|
|
x += 2/interval
|
|
assert(abs(sin(asin(x)) - x) < threshold)
|
|
assert(abs(cos(acos(x)) - x) < threshold)
|
|
assert(abs(tan(atan(x)) - x) < threshold)
|
|
end
|
|
|
|
assert(abs(log10(2) - 0.3010299956639812) < threshold)
|
|
assert(round(1.4) == 1)
|
|
assert(round(1.5) == 2)
|
|
assert(round(-1.5) == -2)
|
|
|
|
## Note that these mathe functions are removed temproarly from the core
|
|
## For more information see PR #201
|
|
##
|
|
##assert(abs(log2(2) - 1) < threshold)
|
|
##assert(abs(log2(1) - 0) < threshold)
|
|
##assert(abs(log2(5) - 2.321928094887362) < threshold)
|
|
##
|
|
##assert(abs(hypot(1,1) - 1.414213562373095) < threshold)
|
|
##assert(abs(hypot(3,5) - 5.830951894845301) < threshold)
|
|
##
|
|
##assert(abs(cbrt(27) - 3) < threshold)
|
|
##assert(abs(cbrt(9) - 2.080083823051904) < threshold)
|
|
##
|
|
##assert(abs(gamma(5) - 24) < threshold)
|
|
##assert(abs(gamma(2.2) - 1.101802490879713) < threshold)
|
|
|
|
|
|
|
|
|
|
# If we got here, that means all test were passed.
|
|
print('All TESTS PASSED')
|
|
|