github.com/coyove/nj@v0.0.0-20221110084952-c7f8db1065c3/tests/test.nj.lua (about) 1 a={-10000:2} a[-10000]assert(G == "test") 2 3 assert(-1, -1) 4 assert(([1,0,2])[1]-1, -1) 5 assert(({a=0}).a-1, -1) 6 assert((0)-1, -1) 7 assert((2)+-1, 1) 8 assert(({a=-1}).a, -1) 9 a = 1 10 assert(a-2, -1) 11 12 do 13 local j = { a= 1, b= 2, array=[1, 2, {inner="inner"}]} 14 assert(j.b == 2) 15 j = json.parse("{\"a\":[[1]]}") 16 assert(j.a[0][0], 1) 17 end 18 19 20 print("s2") 21 22 assert(true and true or false) 23 assert(false and false or true) 24 25 function deepadd(v) 26 if v == 1e6 then 27 return v 28 end 29 return deepadd(v + 1) 30 end 31 assert(deepadd(0) == 1e6) 32 33 do 34 local sum = 0 35 for i = 1,10 do 36 for j=1,10,2 do 37 sum = sum * j 38 if j == 3 then break end 39 end 40 if i == 5 then break end 41 sum = sum + i 42 end 43 assert(sum == 174) 44 45 sum = 0 46 for _, v in [1,2,3,4,5,6] do 47 if v == 5 then continue end 48 sum = sum + v 49 end 50 assert(sum == 16) 51 52 sum = 0 53 lst = [1,2,3,4,5,6,7,8,9] 54 local k = 'not touched' 55 for k, v in lst do 56 if v%3 == 1 then continue end 57 sum = sum + v 58 end 59 assert(sum, 2 + 3 + 5 + 6 + 8 + 9) 60 assert(k, 'not touched') 61 end 62 63 do 64 local arr = [1,2,3] 65 for i =0,#(arr) do 66 assert( i+1 == arr[i]) 67 end 68 end 69 70 function f(a, b) assert(true); return [ b, a ] end 71 function g(a, b ,c) 72 b, c = f(b, c) 73 return [c, b, a] 74 end 75 76 do 77 a,b,c = g(1,2,3) 78 assert(a == 2 and b == 3 and c == 1) 79 end 80 81 function syntax_return_void() return end 82 function syntax_return_value(en) return en end 83 function syntax_return_void2(en) return 84 en end 85 assert(syntax_return_void2(1), nil) 86 87 ex = [ 1, 2 ] 88 ex[0], ex[1], ex3 = nil, ex[0], ex[1] 89 assert(ex3 == 2 and ex[0] == nil and ex[1] == 1) 90 91 assert(0x7fff_fffffffffffd < 0x7fffffffffffffff) 92 assert(0x7fff_ffff_ffff_fffd + 2 == 0x7fff_ffff_ffff_ffff) 93 assert(0x7fff_fffffffffffe + 1 == 0x7fffffffffffffff) 94 95 do 96 scope = 1 97 assert(scope == 1) 98 end 99 assert(scope == 1) 100 101 function callstr(a) 102 return a + "a" 103 end 104 105 assert(callstr("a") == "aa") 106 107 a = 0 108 assert(a == 0) 109 110 local a , b = 1, 2 111 assert(a == 1) 112 assert(b == 2) 113 114 if false then 115 assert(false) 116 elseif a == 1 then 117 local a = 3 118 a = a + 2 - b 119 assert(a == 3) 120 elseif true then 121 assert(false) 122 end 123 124 assert(a == 1) 125 126 function add(a,b) return a + b end 127 128 function fib2(a, b, n) 129 x = [] 130 while n do 131 c = add(a, b) 132 a = b 133 b = c 134 x.append(c) 135 n=n-1 136 end 137 return x 138 end 139 140 do 141 fib_seq = [1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578, 5702887, 9227465] 142 143 local s = fib2(0, 1, 33) 144 for i = 0,33 do 145 print(s[i]) 146 assert(fib_seq[i] == s[i]) 147 end 148 end 149 150 function deepadd2(a) 151 if (a <= 0) then return 0 end 152 return add(a, deepadd2(a - 1)) 153 end 154 155 e = 100000 156 assert( deepadd2(e) == (1 + e) * (e / 2 )) 157 158 a = 2 159 assert( 1 + 2 *3/4 == 2.5) 160 assert( 1 /2+ 2.5 * (a + 1) * 5 == 38) 161 assert((a + 1) % 2 == 1) 162 assert(math.mod(a + 1.25, 2) == 1.25) 163 assert(math.remainder(a + 1.25, 2) == -0.75) 164 assert(math.mod(-a - 1.25, 2) == -1.25) 165 166 do 167 local mark = 0 168 for i=0x7000000000000707,-1,-0x1000000000000101 do 169 mark=mark+0x1000000000000001 170 println(i, mark) 171 end 172 assert(mark == 0x8000000000000008) 173 end 174 175 assert(0==nativeVarargTest()) 176 assert(1==nativeVarargTest(1)) 177 assert(2==nativeVarargTest(1, 2)) 178 assert("10"==nativeVarargTest2("1")) 179 assert("11"==nativeVarargTest2("1", 2)) 180 181 assert(intAlias(1e9).Unix() == 1) 182 print(time.now().Format("2006-01-02")) 183 184 println("boolean test") 185 boolConvert(true) 186 187 function returnnil() 188 local _ = 1 + 1 189 end 190 assert(returnnil() == nil) 191 192 G_FLAG = 1 193 findGlobal() 194 assert(G_FLAG == 'ok') 195 196 flag = "G_FLA" + "G" 197 print(flag, {}, {a=2}, {"a": 2}) 198 199 function double(args...) 200 for i=0,#(args) do 201 args[i] = args[i] * 2 202 end 203 return args 204 end 205 206 x = double() 207 assert(#(x) == 0) 208 x = double(1) 209 assert(x[0] == 2) 210 x = double(1, 2, 3) 211 assert(x[0] + x[1] + x[2] == 12) 212 print(x) 213 214 function double2(k, args...) 215 for i=0,#(args) do 216 args[i] = args[i] * k 217 end 218 return args 219 end 220 221 x = double2(3, 1) 222 assert(x[0] == 3) 223 x = double2(4, 1, 2, 3) 224 assert(x[0] + x[1] + x[2] == 24) 225 226 function test_return(a) 227 end_ = 10+a 228 return end_ 229 end 230 assert(test_return((1)),11) 231 assert(test_return(((2))),12) 232 233 234 function bar() return 2 end 235 function foo() return bar end 236 assert(foo()(), 2) 237 238 -- test TLParen parsing 239 foo() 240 ({a=1}).a -- should run flawlessly 241 ({a=(1+bar())}).a 242 243 assert((function(x) return (x+1) end)(1), 2) 244 assert((function(x) end)(1), nil) 245 assert((function(x) return x.a end)({a=100}), 100) 246 247 a = 0 248 a += 1 249 assert(a, 1) 250 a -= 2 251 assert(a, -1) 252 a*=a 253 assert(a, 1) 254 a=[a] 255 a[0]/=2 256 assert(a[0], 0.5) 257 258 a[0]*=4 259 a[0]<<=3 260 assert(a[0], 16) 261 a[0] %= 7 262 assert(a[0], 2) 263 264 a=-a[0] 265 assert(a, -2) 266 a=a -1 267 assert(a, -2) 268 a=a- 1 269 assert(a, -3) 270 a=a - 1 271 assert(a, -4) 272 a=-[a,a - 1][1] 273 assert(a, 5) 274 a=-function() return -2 end() 275 assert(a, 2) 276 277 a=-(a-1)-a 278 assert(a, -3) 279 a=1-a-a 280 assert(a, 7) 281 a=1-a -a 282 assert(a, -6) 283 a=1-a - a 284 assert(a, 13) 285 a=1- 286 a 287 assert(a, -12) 288 a=a- 289 -1 290 assert(a, -11) 291 a=[a][0]- -1 292 assert(a, -10) 293 a=a- --comment 294 -1 295 assert(a, -9) 296 a=a - --comment 297 -1 298 assert(a, -8) 299 a=a - 300 -(a) 301 assert(a, -16) 302 a=a+ 303 -a 304 assert(a, 0) 305 306 assert(if(true, 10, panic("bad")), 10) 307 308 if (false)then end 309 if [0][0]then end 310 311 assert(if(true, "a", "")[0], "a".decodeutf8()[0]) 312 313 m = gomap({a=1,b=2}, "a", 10) 314 assert(m.a , 10) 315 316 cnt = 0 317 for k,v in m do 318 print(k, v) 319 cnt += 1 320 assert(v, if(k=='a', 10, if(k=='b', 2, nil))) 321 end 322 assert(cnt, 2) 323 324 m.c = 3 325 m.d = 4 326 for k,v in m do 327 if k == 'b' then m.delete(k) end 328 end 329 assert(#m, 3) 330 assert(not m.contains('b')) 331 332 function func1() end 333 function func2() end 334 function func1.a() end 335 function func2.a() end 336 337 a = 0 338 assert(a < 1) 339 assert(a < 1<<15-1) 340 assert(a < 1<<15) 341 assert(a < 1<<15+1) 342 343 a = 1e7 344 assert(1 < a) 345 assert(1<<15-1 < a) 346 assert(1<<15 < a) 347 assert(1<<15+1 < a) 348 349 assert(a-1, 9999999) 350 assert(1-a, -9999999) 351 assert(a+1, 10000001) 352 assert(1+a, 10000001) 353 assert(a+1<<15, 1e7+1<<15) 354 assert(1<<15-a, 1<<15-1e7) 355 356 tailcallobj = {} 357 function tailcallobj.foo(a) this.val += a.val; if this.val > 100 then return this.val end; return a.foo(this) end 358 359 tailcallobj1 = new(tailcallobj, {val=1}) 360 tailcallobj2 = new(tailcallobj, {val=1}) 361 362 -- 1+1+3+8+21+55... 363 -- 1+2+5+13+34+89... 364 assert(tailcallobj1.foo(tailcallobj2), 144) 365 366 function vargdefault(a... b0,b1) 367 if b1 then return (a + b0) * b1 end 368 if b0 then return a + b0 end 369 return a * 2 370 end 371 372 assert(vargdefault(1), 2) 373 assert(vargdefault(1, 2), 3) 374 assert(vargdefault(1, 2, 3), 9) 375 assert(vargdefault(1, 2, 3, 4), 9) 376 377 function vargdefault2(... b0,b1,b2) 378 if not b0 and not b1 and not b2 then return 777 end 379 return b0 or b1 or b2 380 end 381 assert(vargdefault2(), 777) 382 assert(vargdefault2(10), 10) 383 assert(vargdefault2(false, 11), 11) 384 385 a={-10000:2} 386 assert(a[-10000], 2)