aboutsummaryrefslogtreecommitdiff
path: root/tests/test-suite/pm.js
blob: 214d498d5a5c84f4e28e67d8a70e38f38725d325 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
"use strict";

const test     = require('tape');

const lua     = require('../../src/lua.js');
const lauxlib = require('../../src/lauxlib.js');
const lualib  = require('../../src/lualib.js');
const {to_luastring} = require("../../src/fengaricore.js");

test("[test-suite] pm: pattern matching", function (t) {
    let luaCode = `
        function f(s, p)
          local i,e = string.find(s, p)
          if i then return string.sub(s, i, e) end
        end

        a,b = string.find('', '')    -- empty patterns are tricky
        assert(a == 1 and b == 0);
        a,b = string.find('alo', '')
        assert(a == 1 and b == 0)
        a,b = string.find('a\\0o a\\0o a\\0o', 'a', 1)   -- first position
        assert(a == 1 and b == 1)
        a,b = string.find('a\\0o a\\0o a\\0o', 'a\\0o', 2)   -- starts in the midle
        assert(a == 5 and b == 7)
        a,b = string.find('a\\0o a\\0o a\\0o', 'a\\0o', 9)   -- starts in the midle
        assert(a == 9 and b == 11)
        a,b = string.find('a\\0a\\0a\\0a\\0\\0ab', '\\0ab', 2);  -- finds at the end
        assert(a == 9 and b == 11);
        a,b = string.find('a\\0a\\0a\\0a\\0\\0ab', 'b')    -- last position
        assert(a == 11 and b == 11)
        assert(string.find('a\\0a\\0a\\0a\\0\\0ab', 'b\\0') == nil)   -- check ending
        assert(string.find('', '\\0') == nil)
        assert(string.find('alo123alo', '12') == 4)
        assert(string.find('alo123alo', '^12') == nil)

        assert(string.match("aaab", ".*b") == "aaab")
        assert(string.match("aaa", ".*a") == "aaa")
        assert(string.match("b", ".*b") == "b")

        assert(string.match("aaab", ".+b") == "aaab")
        assert(string.match("aaa", ".+a") == "aaa")
        assert(not string.match("b", ".+b"))

        assert(string.match("aaab", ".?b") == "ab")
        assert(string.match("aaa", ".?a") == "aa")
        assert(string.match("b", ".?b") == "b")

        assert(f('aloALO', '%l*') == 'alo')
        assert(f('aLo_ALO', '%a*') == 'aLo')

        assert(f("  \\n\\r*&\\n\\r   xuxu  \\n\\n", "%g%g%g+") == "xuxu")

        assert(f('aaab', 'a*') == 'aaa');
        assert(f('aaa', '^.*$') == 'aaa');
        assert(f('aaa', 'b*') == '');
        assert(f('aaa', 'ab*a') == 'aa')
        assert(f('aba', 'ab*a') == 'aba')
        assert(f('aaab', 'a+') == 'aaa')
        assert(f('aaa', '^.+$') == 'aaa')
        assert(f('aaa', 'b+') == nil)
        assert(f('aaa', 'ab+a') == nil)
        assert(f('aba', 'ab+a') == 'aba')
        assert(f('a$a', '.$') == 'a')
        assert(f('a$a', '.%$') == 'a$')
        assert(f('a$a', '.$.') == 'a$a')
        assert(f('a$a', '$$') == nil)
        assert(f('a$b', 'a$') == nil)
        assert(f('a$a', '$') == '')
        assert(f('', 'b*') == '')
        assert(f('aaa', 'bb*') == nil)
        assert(f('aaab', 'a-') == '')
        assert(f('aaa', '^.-$') == 'aaa')
        assert(f('aabaaabaaabaaaba', 'b.*b') == 'baaabaaabaaab')
        assert(f('aabaaabaaabaaaba', 'b.-b') == 'baaab')
        assert(f('alo xo', '.o$') == 'xo')
        assert(f(' \\n isto é assim', '%S%S*') == 'isto')
        assert(f(' \\n isto é assim', '%S*$') == 'assim')
        assert(f(' \\n isto é assim', '[a-z]*$') == 'assim')
        assert(f('um caracter ? extra', '[^%sa-z]') == '?')
        assert(f('', 'a?') == '')
        assert(f('á', 'á?') == 'á')
        assert(f('ábl', 'á?b?l?') == 'ábl')
        -- assert(f('  ábl', 'á?b?l?') == '')
        assert(f('aa', '^aa?a?a') == 'aa')
        -- assert(f(']]]áb', '[^]]') == 'á')
        assert(f("0alo alo", "%x*") == "0a")
        assert(f("alo alo", "%C+") == "alo alo")
    `, L;

    t.plan(2);

    t.doesNotThrow(function () {

        L = lauxlib.luaL_newstate();

        lualib.luaL_openlibs(L);

        lauxlib.luaL_loadstring(L, to_luastring(luaCode));

    }, "Lua program loaded without error");

    t.doesNotThrow(function () {

        lua.lua_call(L, 0, -1);

    }, "Lua program ran without error");

});


test("[test-suite] pm: tonumber", function (t) {
    let luaCode = `
        function f1(s, p)
          p = string.gsub(p, "%%([0-9])", function (s)
                return "%" .. (tonumber(s)+1)
               end)
          p = string.gsub(p, "^(^?)", "%1()", 1)
          p = string.gsub(p, "($?)$", "()%1", 1)
          local t = {string.match(s, p)}
          return string.sub(s, t[1], t[#t] - 1)
        end

        -- assert(f1('alo alx 123 b\\0o b\\0o', '(..*) %1') == "b\\0o b\\0o")
        -- assert(f1('axz123= 4= 4 34', '(.+)=(.*)=%2 %1') == '3= 4= 4 3')
        -- assert(f1('=======', '^(=*)=%1$') == '=======')
        assert(string.match('==========', '^([=]*)=%1$') == nil)
    `, L;

    t.plan(2);

    t.doesNotThrow(function () {

        L = lauxlib.luaL_newstate();

        lualib.luaL_openlibs(L);

        lauxlib.luaL_loadstring(L, to_luastring(luaCode));

    }, "Lua program loaded without error");

    t.doesNotThrow(function () {

        lua.lua_call(L, 0, -1);

    }, "Lua program ran without error");

});


test("[test-suite] pm: range", function (t) {
    let luaCode = `
        local function range (i, j)
          if i <= j then
            return i, range(i+1, j)
          end
        end

        local abc = string.char(range(0, 255));

        assert(string.len(abc) == 256)

        function strset (p)
          local res = {s=''}
          string.gsub(abc, p, function (c) res.s = res.s .. c end)
          return res.s
        end;

        assert(string.len(strset('[\\200-\\210]')) == 11)

        assert(strset('[a-z]') == "abcdefghijklmnopqrstuvwxyz")
        assert(strset('[a-z%d]') == strset('[%da-uu-z]'))
        assert(strset('[a-]') == "-a")
        assert(strset('[^%W]') == strset('[%w]'))
        assert(strset('[]%%]') == '%]')
        assert(strset('[a%-z]') == '-az')
        assert(strset('[%^%[%-a%]%-b]') == '-[]^ab')
        assert(strset('%Z') == strset('[\\1-\\255]'))
        assert(strset('.') == strset('[\\1-\\255%z]'))
    `, L;

    t.plan(2);

    t.doesNotThrow(function () {

        L = lauxlib.luaL_newstate();

        lualib.luaL_openlibs(L);

        lauxlib.luaL_loadstring(L, to_luastring(luaCode));

    }, "Lua program loaded without error");

    t.doesNotThrow(function () {

        lua.lua_call(L, 0, -1);

    }, "Lua program ran without error");

});

// Can't be represented by JS string, testing from actual lua file
test("[test-suite] pm: classes", function (t) {
    let L;

    t.plan(2);

    t.doesNotThrow(function () {

        L = lauxlib.luaL_newstate();

        lualib.luaL_openlibs(L);

        lauxlib.luaL_loadfile(L, to_luastring("tests/test-suite/pm-classes.lua"));

    }, "Lua program loaded without error");

    t.doesNotThrow(function () {

        lua.lua_call(L, 0, -1);

    }, "Lua program ran without error");

});

// Can't be represented by JS string, testing from actual lua file
test("[test-suite] pm: gsub", function (t) {
    let L;

    t.plan(2);

    t.doesNotThrow(function () {

        L = lauxlib.luaL_newstate();

        lualib.luaL_openlibs(L);

        lauxlib.luaL_loadfile(L, to_luastring("tests/test-suite/pm-gsub.lua"));

    }, "Lua program loaded without error");

    t.doesNotThrow(function () {

        lua.lua_call(L, 0, -1);

    }, "Lua program ran without error");

});


test("[test-suite] pm: empty matches", function (t) {
    let luaCode = `
        do   -- new (5.3.3) semantics for empty matches
          assert(string.gsub("a b cd", " *", "-") == "-a-b-c-d-")

          local res = ""
          local sub = "a  \\nbc\\t\\td"
          local i = 1
          for p, e in string.gmatch(sub, "()%s*()") do
            res = res .. string.sub(sub, i, p - 1) .. "-"
            i = e
          end
          assert(res == "-a-b-c-d-")
        end
    `, L;

    t.plan(2);

    t.doesNotThrow(function () {

        L = lauxlib.luaL_newstate();

        lualib.luaL_openlibs(L);

        lauxlib.luaL_loadstring(L, to_luastring(luaCode));

    }, "Lua program loaded without error");

    t.doesNotThrow(function () {

        lua.lua_call(L, 0, -1);

    }, "Lua program ran without error");

});


test("[test-suite] pm: gsub", function (t) {
    let luaCode = `
        assert(string.gsub("um (dois) tres (quatro)", "(%(%w+%))", string.upper) ==
                    "um (DOIS) tres (QUATRO)")

        do
          local function setglobal (n,v) rawset(_G, n, v) end
          string.gsub("a=roberto,roberto=a", "(%w+)=(%w%w*)", setglobal)
          assert(_G.a=="roberto" and _G.roberto=="a")
        end

        function f(a,b) return string.gsub(a,'.',b) end
        assert(string.gsub("trocar tudo em |teste|b| é |beleza|al|", "|([^|]*)|([^|]*)|", f) ==
                    "trocar tudo em bbbbb é alalalalalal")

        local function dostring (s) return load(s, "")() or "" end
        assert(string.gsub("alo $a='x'$ novamente $return a$",
                           "$([^$]*)%$",
                           dostring) == "alo  novamente x")

        x = string.gsub("$x=string.gsub('alo', '.', string.upper)$ assim vai para $return x$",
                 "$([^$]*)%$", dostring)
        assert(x == ' assim vai para ALO')

        t = {}
        s = 'a alo jose  joao'
        r = string.gsub(s, '()(%w+)()', function (a,w,b)
              assert(string.len(w) == b-a);
              t[a] = b-a;
            end)
        assert(s == r and t[1] == 1 and t[3] == 3 and t[7] == 4 and t[13] == 4)
    `, L;

    t.plan(2);

    t.doesNotThrow(function () {

        L = lauxlib.luaL_newstate();

        lualib.luaL_openlibs(L);

        lauxlib.luaL_loadstring(L, to_luastring(luaCode));

    }, "Lua program loaded without error");

    t.doesNotThrow(function () {

        lua.lua_call(L, 0, -1);

    }, "Lua program ran without error");

});


test("[test-suite] pm: gsub isbalanced", function (t) {
    let luaCode = `
        function isbalanced (s)
          return string.find(string.gsub(s, "%b()", ""), "[()]") == nil
        end

        assert(isbalanced("(9 ((8))(\\0) 7) \\0\\0 a b ()(c)() a"))
        assert(not isbalanced("(9 ((8) 7) a b (\\0 c) a"))
        assert(string.gsub("alo 'oi' alo", "%b''", '"') == 'alo " alo')
    `, L;

    t.plan(2);

    t.doesNotThrow(function () {

        L = lauxlib.luaL_newstate();

        lualib.luaL_openlibs(L);

        lauxlib.luaL_loadstring(L, to_luastring(luaCode));

    }, "Lua program loaded without error");

    t.doesNotThrow(function () {

        lua.lua_call(L, 0, -1);

    }, "Lua program ran without error");

});


test("[test-suite] pm: capture", function (t) {
    let luaCode = `
        local function checkerror (msg, f, ...)
          local s, err = pcall(f, ...)
          assert(not s and string.find(err, msg))
        end

        local t = {"apple", "orange", "lime"; n=0}
        assert(string.gsub("x and x and x", "x", function () t.n=t.n+1; return t[t.n] end)
                == "apple and orange and lime")

        t = {n=0}
        string.gsub("first second word", "%w%w*", function (w) t.n=t.n+1; t[t.n] = w end)
        assert(t[1] == "first" and t[2] == "second" and t[3] == "word" and t.n == 3)

        t = {n=0}
        assert(string.gsub("first second word", "%w+",
                 function (w) t.n=t.n+1; t[t.n] = w end, 2) == "first second word")
        assert(t[1] == "first" and t[2] == "second" and t[3] == nil)

        checkerror("invalid replacement value %(a table%)",
                    string.gsub, "alo", ".", {a = {}})
        checkerror("invalid capture index %%2", string.gsub, "alo", ".", "%2")
        checkerror("invalid capture index %%0", string.gsub, "alo", "(%0)", "a")
        checkerror("invalid capture index %%1", string.gsub, "alo", "(%1)", "a")
        checkerror("invalid use of '%%'", string.gsub, "alo", ".", "%x")
    `, L;

    t.plan(2);

    t.doesNotThrow(function () {

        L = lauxlib.luaL_newstate();

        lualib.luaL_openlibs(L);

        lauxlib.luaL_loadstring(L, to_luastring(luaCode));

    }, "Lua program loaded without error");

    t.doesNotThrow(function () {

        lua.lua_call(L, 0, -1);

    }, "Lua program ran without error");

});


test("[test-suite] pm: bug since 2.5 (C-stack overflow) (TODO: _soft)", function (t) {
    let luaCode = `
        _soft = true
        if not _soft then -- TODO
          local function f (size)
            local s = string.rep("a", size)
            local p = string.rep(".?", size)
            return pcall(string.match, s, p)
          end
          local r, m = f(80)
          assert(r and #m == 80)
          r, m = f(200000)
          assert(not r and string.find(m, "too complex"))
        end
    `, L;

    t.plan(2);

    t.doesNotThrow(function () {

        L = lauxlib.luaL_newstate();

        lualib.luaL_openlibs(L);

        lauxlib.luaL_loadstring(L, to_luastring(luaCode));

    }, "Lua program loaded without error");

    t.doesNotThrow(function () {

        lua.lua_call(L, 0, -1);

    }, "Lua program ran without error");

});


test("[test-suite] pm: big strings (TODO: _soft)", function (t) {
    let luaCode = `
        _soft = true -- TODO
        if not _soft then
          print("big strings")
          local a = string.rep('a', 300000)
          assert(string.find(a, '^a*.?$'))
          assert(not string.find(a, '^a*.?b$'))
          assert(string.find(a, '^a-.?$'))

          -- bug in 5.1.2
          a = string.rep('a', 10000) .. string.rep('b', 10000)
          assert(not pcall(string.gsub, a, 'b'))
        end
    `, L;

    t.plan(2);

    t.doesNotThrow(function () {

        L = lauxlib.luaL_newstate();

        lualib.luaL_openlibs(L);

        lauxlib.luaL_loadstring(L, to_luastring(luaCode));

    }, "Lua program loaded without error");

    t.doesNotThrow(function () {

        lua.lua_call(L, 0, -1);

    }, "Lua program ran without error");

});


test("[test-suite] pm: recursive nest of gsubs", function (t) {
    let luaCode = `
        function rev (s)
          return string.gsub(s, "(.)(.+)", function (c,s1) return rev(s1)..c end)
        end

        local x = "abcdef"
        assert(rev(rev(x)) == x)
    `, L;

    t.plan(2);

    t.doesNotThrow(function () {

        L = lauxlib.luaL_newstate();

        lualib.luaL_openlibs(L);

        lauxlib.luaL_loadstring(L, to_luastring(luaCode));

    }, "Lua program loaded without error");

    t.doesNotThrow(function () {

        lua.lua_call(L, 0, -1);

    }, "Lua program ran without error");

});


test("[test-suite] pm: gsub with tables", function (t) {
    let luaCode = `
        assert(string.gsub("alo alo", ".", {}) == "alo alo")
        assert(string.gsub("alo alo", "(.)", {a="AA", l=""}) == "AAo AAo")
        assert(string.gsub("alo alo", "(.).", {a="AA", l="K"}) == "AAo AAo")
        assert(string.gsub("alo alo", "((.)(.?))", {al="AA", o=false}) == "AAo AAo")

        assert(string.gsub("alo alo", "().", {'x','yy','zzz'}) == "xyyzzz alo")

        t = {}; setmetatable(t, {__index = function (t,s) return string.upper(s) end})
        assert(string.gsub("a alo b hi", "%w%w+", t) == "a ALO b HI")
    `, L;

    t.plan(2);

    t.doesNotThrow(function () {

        L = lauxlib.luaL_newstate();

        lualib.luaL_openlibs(L);

        lauxlib.luaL_loadstring(L, to_luastring(luaCode));

    }, "Lua program loaded without error");

    t.doesNotThrow(function () {

        lua.lua_call(L, 0, -1);

    }, "Lua program ran without error");

});


test("[test-suite] pm: gmatch", function (t) {
    let luaCode = `
        local a = 0
        for i in string.gmatch('abcde', '()') do assert(i == a+1); a=i end
        assert(a==6)

        t = {n=0}
        for w in string.gmatch("first second word", "%w+") do
              t.n=t.n+1; t[t.n] = w
        end
        assert(t[1] == "first" and t[2] == "second" and t[3] == "word")

        t = {3, 6, 9}
        for i in string.gmatch ("xuxx uu ppar r", "()(.)%2") do
          assert(i == table.remove(t, 1))
        end
        assert(#t == 0)

        t = {}
        for i,j in string.gmatch("13 14 10 = 11, 15= 16, 22=23", "(%d+)%s*=%s*(%d+)") do
          t[tonumber(i)] = tonumber(j)
        end
        a = 0
        for k,v in pairs(t) do assert(k+1 == v+0); a=a+1 end
        assert(a == 3)
    `, L;

    t.plan(2);

    t.doesNotThrow(function () {

        L = lauxlib.luaL_newstate();

        lualib.luaL_openlibs(L);

        lauxlib.luaL_loadstring(L, to_luastring(luaCode));

    }, "Lua program loaded without error");

    t.doesNotThrow(function () {

        lua.lua_call(L, 0, -1);

    }, "Lua program ran without error");

});


test("[test-suite] pm: tests for '%f' ('frontiers')", function (t) {
    let luaCode = `
        assert(string.gsub("aaa aa a aaa a", "%f[%w]a", "x") == "xaa xa x xaa x")
        assert(string.gsub("[[]] [][] [[[[", "%f[[].", "x") == "x[]] x]x] x[[[")
        assert(string.gsub("01abc45de3", "%f[%d]", ".") == ".01abc.45de.3")
        assert(string.gsub("01abc45 de3x", "%f[%D]%w", ".") == "01.bc45 de3.")
        assert(string.gsub("function", "%f[\\1-\\255]%w", ".") == ".unction")
        assert(string.gsub("function", "%f[^\\1-\\255]", ".") == "function.")

        assert(string.find("a", "%f[a]") == 1)
        assert(string.find("a", "%f[^%z]") == 1)
        assert(string.find("a", "%f[^%l]") == 2)
        assert(string.find("aba", "%f[a%z]") == 3)
        assert(string.find("aba", "%f[%z]") == 4)
        assert(not string.find("aba", "%f[%l%z]"))
        assert(not string.find("aba", "%f[^%l%z]"))

        local i, e = string.find(" alo aalo allo", "%f[%S].-%f[%s].-%f[%S]")
        assert(i == 2 and e == 5)
        local k = string.match(" alo aalo allo", "%f[%S](.-%f[%s].-%f[%S])")
        assert(k == 'alo ')

        local a = {1, 5, 9, 14, 17,}
        for k in string.gmatch("alo alo th02 is 1hat", "()%f[%w%d]") do
          assert(table.remove(a, 1) == k)
        end
        assert(#a == 0)
    `, L;

    t.plan(2);

    t.doesNotThrow(function () {

        L = lauxlib.luaL_newstate();

        lualib.luaL_openlibs(L);

        lauxlib.luaL_loadstring(L, to_luastring(luaCode));

    }, "Lua program loaded without error");

    t.doesNotThrow(function () {

        lua.lua_call(L, 0, -1);

    }, "Lua program ran without error");

});


test("[test-suite] pm: malformed patterns", function (t) {
    let luaCode = `
        local function malform (p, m)
          m = m or "malformed"
          local r, msg = pcall(string.find, "a", p)
          assert(not r and string.find(msg, m))
        end

        malform("(.", "unfinished capture")
        malform(".)", "invalid pattern capture")
        malform("[a")
        malform("[]")
        malform("[^]")
        malform("[a%]")
        malform("[a%")
        malform("%b")
        malform("%ba")
        malform("%")
        malform("%f", "missing")
    `, L;

    t.plan(2);

    t.doesNotThrow(function () {

        L = lauxlib.luaL_newstate();

        lualib.luaL_openlibs(L);

        lauxlib.luaL_loadstring(L, to_luastring(luaCode));

    }, "Lua program loaded without error");

    t.doesNotThrow(function () {

        lua.lua_call(L, 0, -1);

    }, "Lua program ran without error");

});


test("[test-suite] pm: \\0 in patterns", function (t) {
    let luaCode = `
        assert(string.match("ab\\0\\1\\2c", "[\\0-\\2]+") == "\\0\\1\\2")
        assert(string.match("ab\\0\\1\\2c", "[\\0-\\0]+") == "\\0")
        assert(string.find("b$a", "$\\0?") == 2)
        assert(string.find("abc\\0efg", "%\\0") == 4)
        assert(string.match("abc\\0efg\\0\\1e\\1g", "%b\\0\\1") == "\\0efg\\0\\1e\\1")
        assert(string.match("abc\\0\\0\\0", "%\\0+") == "\\0\\0\\0")
        assert(string.match("abc\\0\\0\\0", "%\\0%\\0?") == "\\0\\0")
    `, L;

    t.plan(2);

    t.doesNotThrow(function () {

        L = lauxlib.luaL_newstate();

        lualib.luaL_openlibs(L);

        lauxlib.luaL_loadstring(L, to_luastring(luaCode));

    }, "Lua program loaded without error");

    t.doesNotThrow(function () {

        lua.lua_call(L, 0, -1);

    }, "Lua program ran without error");

});


test("[test-suite] pm: magic char after \\0", function (t) {
    let luaCode = `
        assert(string.find("abc\\0\\0","\\0.") == 4)
        assert(string.find("abcx\\0\\0abc\\0abc","x\\0\\0abc\\0a.") == 4)
    `, L;

    t.plan(2);

    t.doesNotThrow(function () {

        L = lauxlib.luaL_newstate();

        lualib.luaL_openlibs(L);

        lauxlib.luaL_loadstring(L, to_luastring(luaCode));

    }, "Lua program loaded without error");

    t.doesNotThrow(function () {

        lua.lua_call(L, 0, -1);

    }, "Lua program ran without error");

});