File: test-basic.cpp

package info (click to toggle)
llama.cpp 7593%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 71,012 kB
  • sloc: cpp: 329,391; ansic: 48,249; python: 32,103; lisp: 10,053; sh: 6,070; objc: 1,349; javascript: 924; xml: 384; makefile: 233
file content (454 lines) | stat: -rw-r--r-- 20,253 bytes parent folder | download
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
#include "tests.h"

void test_basic(testing & t) {
    t.test("chars", [](testing & t) {
        // Test common escape sequences - newline
        t.test("escape_sequence_newline", [](testing &t) {
            auto common_chat_combinator_parser = build_peg_parser([](common_peg_parser_builder & p) { return p.chars("[\\n\\t\\\\]"); });

            common_peg_parse_context ctx;
            common_peg_parse_result  result;

            ctx    = common_peg_parse_context("\n");
            result = common_chat_combinator_parser.parse(ctx);
            t.assert_equal("escape_sequence_newline", true, result.success());
        });

        // Test common escape sequences - tab
        t.test("escape_sequence_tab", [](testing &t) {
            auto common_chat_combinator_parser = build_peg_parser([](common_peg_parser_builder & p) { return p.chars("[\\n\\t\\\\]"); });

            common_peg_parse_context ctx;
            common_peg_parse_result  result;

            ctx    = common_peg_parse_context("\t");
            result = common_chat_combinator_parser.parse(ctx);
            t.assert_equal("escape_sequence_tab", true, result.success());
        });

        // Test common escape sequences - backslash
        t.test("escape_sequence_backslash", [](testing &t) {
            auto common_chat_combinator_parser = build_peg_parser([](common_peg_parser_builder & p) { return p.chars("[\\n\\t\\\\]"); });

            common_peg_parse_context ctx;
            common_peg_parse_result  result;

            ctx    = common_peg_parse_context("\\");
            result = common_chat_combinator_parser.parse(ctx);
            t.assert_equal("escape_sequence_backslash", true, result.success());
        });

        // Test common escape sequences - space (should ())
        t.test("escape_sequence_space_fail", [](testing &t) {
            auto common_chat_combinator_parser = build_peg_parser([](common_peg_parser_builder & p) { return p.chars("[\\n\\t\\\\]"); });

            common_peg_parse_context ctx;
            common_peg_parse_result  result;

            ctx    = common_peg_parse_context(" ");
            result = common_chat_combinator_parser.parse(ctx);
            t.assert_equal("escape_sequence_space_fail", true, result.fail());
        });

        // Test escaped dash - 'a' should succeed
        t.test("escaped_dash_a", [](testing &t) {
            auto common_chat_combinator_parser = build_peg_parser([](common_peg_parser_builder & p) { return p.chars("[a\\-z]"); });

            common_peg_parse_context ctx;
            common_peg_parse_result  result;

            ctx    = common_peg_parse_context("a");
            result = common_chat_combinator_parser.parse(ctx);
            t.assert_equal("escaped_dash_a", true, result.success());
        });

        // Test escaped dash - '-' should succeed (literal dash)
        t.test("escaped_dash_literal", [](testing &t) {
            auto common_chat_combinator_parser = build_peg_parser([](common_peg_parser_builder & p) { return p.chars("[a\\-z]"); });

            common_peg_parse_context ctx;
            common_peg_parse_result  result;

            ctx    = common_peg_parse_context("-");
            result = common_chat_combinator_parser.parse(ctx);
            t.assert_equal("escaped_dash_literal", true, result.success());
        });

        // Test escaped dash - 'z' should succeed
        t.test("escaped_dash_z", [](testing &t) {
            auto common_chat_combinator_parser = build_peg_parser([](common_peg_parser_builder & p) { return p.chars("[a\\-z]"); });

            common_peg_parse_context ctx;
            common_peg_parse_result  result;

            ctx    = common_peg_parse_context("z");
            result = common_chat_combinator_parser.parse(ctx);
            t.assert_equal("escaped_dash_z", true, result.success());
        });

        // Test escaped dash - 'b' should NOT match (since \- is literal dash, not range)
        t.test("escaped_dash_b_fail", [](testing &t) {
            auto common_chat_combinator_parser = build_peg_parser([](common_peg_parser_builder & p) { return p.chars("[a\\-z]"); });

            common_peg_parse_context ctx;
            common_peg_parse_result  result;

            ctx    = common_peg_parse_context("b");
            result = common_chat_combinator_parser.parse(ctx);
            t.assert_equal("escaped_dash_b_fail", true, result.fail());
        });
    });


    t.test("optional", [](testing & t) {
        // Full match with optional part present
        t.test("optional_present", [](testing &t) {
            auto parser = build_peg_parser([](common_peg_parser_builder & p) {
                return p.literal("hello") + p.optional(p.literal(" world"));
            });

            auto ctx    = common_peg_parse_context("hello world");
            auto result = parser.parse(ctx);
            t.assert_equal("optional_present", true, result.success());
            t.assert_equal("optional_present_end", 11u, result.end);
        });

        // Full match with optional part absent
        t.test("optional_absent", [](testing &t) {
            auto parser = build_peg_parser([](common_peg_parser_builder & p) {
                return p.literal("hello") + p.optional(p.literal(" world"));
            });

            auto ctx    = common_peg_parse_context("hello", false);
            auto result = parser.parse(ctx);
            t.assert_equal("optional_absent", true, result.success());
            t.assert_equal("optional_absent_end", 5u, result.end);
        });

        // Partial match - waiting for more input to determine if optional matches
        t.test("partial_match_need_more", [](testing &t) {
            auto parser = build_peg_parser([](common_peg_parser_builder & p) {
                return p.literal("hello") + p.optional(p.literal(" world"));
            });

            auto ctx    = common_peg_parse_context("hello ", true);
            auto result = parser.parse(ctx);
            t.assert_equal("partial_match_need_more", true, result.need_more_input());
        });
    });

    t.test("partial parsing", [](testing & t) {
        // Literals - Basic Success
        t.test("literal_success", [&](testing & t) {
            auto parser = build_peg_parser([](common_peg_parser_builder & p) { return p.literal("hello"); });

            common_peg_parse_context ctx;
            common_peg_parse_result  result;

            ctx    = common_peg_parse_context("hello");
            result = parser.parse(ctx);
            t.assert_equal("literal_success", true, result.success());
        });

        // Char Classes - Basic Lowercase Success
        t.test("char_class_lowercase_success", [&](testing & t) {
            auto parser = build_peg_parser([](common_peg_parser_builder & p) { return p.chars("a-z"); });

            common_peg_parse_context ctx;
            common_peg_parse_result  result;

            ctx    = common_peg_parse_context("a");
            result = parser.parse(ctx);
            t.assert_equal("char_class_lowercase_success", true, result.success());
        });

        // Char Classes - Uppercase Fail
        t.test("char_class_uppercase_fail", [&](testing & t) {
            auto parser = build_peg_parser([](common_peg_parser_builder & p) { return p.chars("a-z"); });

            common_peg_parse_context ctx;
            common_peg_parse_result  result;

            ctx    = common_peg_parse_context("A");
            result = parser.parse(ctx);
            t.assert_equal("char_class_uppercase_fail", true, result.fail());
        });

        // Char Classes with Dash - Lowercase Success
        t.test("char_class_with_dash_lowercase", [&](testing & t) {
            auto parser = build_peg_parser([](common_peg_parser_builder & p) { return p.chars("a-z-"); });

            common_peg_parse_context ctx;
            common_peg_parse_result  result;

            ctx    = common_peg_parse_context("f");
            result = parser.parse(ctx);
            t.assert_equal("char_class_with_dash_lowercase", true, result.success());
        });

        // Char Classes with Dash - Literal Dash Success
        t.test("char_class_with_dash_literal_dash", [&](testing & t) {
            auto parser = build_peg_parser([](common_peg_parser_builder & p) { return p.chars("a-z-"); });

            common_peg_parse_context ctx;
            common_peg_parse_result  result;

            ctx    = common_peg_parse_context("-");
            result = parser.parse(ctx);
            t.assert_equal("char_class_with_dash_literal_dash", true, result.success());
        });

        // Char Classes with Dash - Uppercase Fail
        t.test("char_class_with_dash_uppercase_fail", [&](testing & t) {
            auto parser = build_peg_parser([](common_peg_parser_builder & p) { return p.chars("a-z-"); });

            common_peg_parse_context ctx;
            common_peg_parse_result  result;

            ctx    = common_peg_parse_context("A");
            result = parser.parse(ctx);
            t.assert_equal("char_class_with_dash_uppercase_fail", true, result.fail());
        });

        // Sequences - Partial Match 1
        t.test("sequence_partial_match_1", [&](testing & t) {
            auto parser = build_peg_parser([](common_peg_parser_builder & p) { return p.literal("<think>") + p.literal("</think>"); });

            auto ctx    = common_peg_parse_context("<thi", true);
            auto result = parser.parse(ctx);
            t.assert_equal("sequence_partial_match_1", true, result.need_more_input());
        });

        // Sequences - Partial Match 2
        t.test("sequence_partial_match_2", [&](testing & t) {
            auto parser = build_peg_parser([](common_peg_parser_builder & p) { return p.literal("begin") + p.literal("end"); });

            auto ctx    = common_peg_parse_context("begin", true);
            auto result = parser.parse(ctx);
            t.assert_equal("sequence_partial_match_2", true, result.need_more_input());
        });

        // Sequences - Partial Match 3
        t.test("sequence_partial_match_3", [&](testing & t) {
            auto parser = build_peg_parser([](common_peg_parser_builder & p) { return p.literal("<think>") + p.literal("</think>"); });

            auto ctx    = common_peg_parse_context("<think></", true);
            auto result = parser.parse(ctx);
            t.assert_equal("sequence_partial_match_3", true, result.need_more_input());
        });

        // Sequences - Full Match
        t.test("sequence_full_match", [&](testing & t) {
            auto common_chat_combinator_parser = build_peg_parser([](common_peg_parser_builder & p) { return p.literal("hello") + p.literal("world"); });

            auto ctx    = common_peg_parse_context("helloworld", false);
            auto result = common_chat_combinator_parser.parse(ctx);
            t.assert_equal("sequence_full_match", true, result.success());
        });

        // Sequences - No Match
        t.test("sequence_no_match", [&](testing & t) {
            auto parser = build_peg_parser([](common_peg_parser_builder & p) { return p.literal("<think>") + p.literal("</think>"); });

            auto ctx    = common_peg_parse_context("<think>I am common_chat_combinator_parser", true);
            auto result = parser.parse(ctx);
            t.assert_equal("sequence_no_match", true, result.fail());
        });

        // Choices - Partial Match 1
        t.test("choices_partial_match_1", [&](testing & t) {
            auto parser = build_peg_parser([](common_peg_parser_builder & p) { return p.literal("option1") | p.literal("option2"); });

            auto ctx    = common_peg_parse_context("opt", true);
            auto result = parser.parse(ctx);
            t.assert_equal("choices_partial_match_1", true, result.need_more_input());
        });

        // Choices - Partial Match 2
        t.test("choices_partial_match_2", [&](testing & t) {
            auto parser =
                build_peg_parser([](common_peg_parser_builder & p) { return p.literal("choice_a") | p.literal("choice_b"); });

            auto ctx    = common_peg_parse_context("choice", true);
            auto result = parser.parse(ctx);
            t.assert_equal("choices_partial_match_2", true, result.need_more_input());
        });

        // Choices - Full Match 1
        t.test("choices_full_match_1", [&](testing & t) {
            auto parser = build_peg_parser([](common_peg_parser_builder & p) { return p.literal("first") | p.literal("second"); });

            auto ctx    = common_peg_parse_context("first", false);
            auto result = parser.parse(ctx);
            t.assert_equal("choices_full_match_1", true, result.success());
        });

        // Choices - Full Match 2
        t.test("choices_full_match_2", [&](testing & t) {
            auto parser = build_peg_parser([](common_peg_parser_builder & p) { return p.literal("alpha") | p.literal("beta"); });

            auto ctx    = common_peg_parse_context("beta", false);
            auto result = parser.parse(ctx);
            t.assert_equal("choices_full_match_2", true, result.success());
        });

        // Choices - No Match
        t.test("choices_no_match", [&](testing & t) {
            auto parser = build_peg_parser([](common_peg_parser_builder & p) { return p.literal("good") | p.literal("better"); });

            auto ctx    = common_peg_parse_context("best", false);
            auto result = parser.parse(ctx);
            t.assert_equal("choices_no_match", true, result.fail());
        });

        // Zero or More - Partial Match 1
        t.test("zero_or_more_partial_match_1", [&](testing & t) {
            auto parser = build_peg_parser([](common_peg_parser_builder & p) { return p.zero_or_more(p.literal("ab")); });

            auto ctx    = common_peg_parse_context("a", true);
            auto result = parser.parse(ctx);
            t.assert_equal("zero_or_more_partial_match_1", true, result.need_more_input());
        });

        // Zero or More - Partial Match 2
        t.test("zero_or_more_partial_match_2", [&](testing & t) {
            auto parser = build_peg_parser([](common_peg_parser_builder & p) { return p.zero_or_more(p.literal("xy")); });

            auto ctx    = common_peg_parse_context("xyx", true);
            auto result = parser.parse(ctx);
            t.assert_equal("zero_or_more_partial_match_2", true, result.need_more_input());
        });

        // Zero or More - Full Match
        t.test("zero_or_more_full_match", [&](testing & t) {
            auto parser = build_peg_parser([](common_peg_parser_builder & p) { return p.zero_or_more(p.literal("test")); });

            auto ctx    = common_peg_parse_context("test", false);
            auto result = parser.parse(ctx);
            t.assert_equal("zero_or_more_full_match", true, result.success());
        });

        // One or More - Partial Match 1
        t.test("one_or_more_partial_match_1", [&](testing & t) {
            auto parser = build_peg_parser([](common_peg_parser_builder & p) { return p.one_or_more(p.literal("repeat")); });

            auto ctx    = common_peg_parse_context("rep", true);
            auto result = parser.parse(ctx);
            t.assert_equal("one_or_more_partial_match_1", true, result.need_more_input());
        });

        // One or More - Partial Match 2
        t.test("one_or_more_partial_match_2", [&](testing & t) {
            auto parser = build_peg_parser([](common_peg_parser_builder & p) { return p.one_or_more(p.literal("ab")); });

            auto ctx    = common_peg_parse_context("aba", true);
            auto result = parser.parse(ctx);
            t.assert_equal("one_or_more_partial_match_2", true, result.need_more_input());
        });

        // One or More - Full Match
        t.test("one_or_more_full_match", [&](testing & t) {
            auto parser = build_peg_parser([](common_peg_parser_builder & p) { return p.one_or_more(p.literal("single")); });

            auto ctx    = common_peg_parse_context("single", false);
            auto result = parser.parse(ctx);
            t.assert_equal("one_or_more_full_match", true, result.success());
        });

        // One or More - No Match
        t.test("one_or_more_no_match", [&](testing & t) {
            auto parser = build_peg_parser([](common_peg_parser_builder & p) { return p.one_or_more(p.literal("()")); });

            auto ctx    = common_peg_parse_context("success", false);
            auto result = parser.parse(ctx);
            t.assert_equal("one_or_more_no_match", true, result.fail());
        });
    });


    t.test("recursive rules", [](testing &t) {
        // Test simple number
        t.test("simple_number", [](testing &t) {
            auto value_parser = build_peg_parser([](common_peg_parser_builder & p) {
                p.rule("number", p.chars("0-9"));
                p.rule("list", p.literal("[") + p.ref("value") + p.literal("]"));
                return p.rule("value", p.ref("number") | p.ref("list"));
            });

            common_peg_parse_context ctx("1", false);
            auto           result = value_parser.parse(ctx);

            t.assert_equal("result_is_success", true, result.success());
        });

        // Test simple list
        t.test("simple_list", [](testing &t) {
            auto value_parser = build_peg_parser([](common_peg_parser_builder & p) {
                p.rule("number", p.chars("0-9"));
                p.rule("list", p.literal("[") + p.ref("value") + p.literal("]"));
                return p.rule("value", p.ref("number") | p.ref("list"));
            });

            common_peg_parse_context ctx("[1]", false);
            auto           result = value_parser.parse(ctx);

            t.assert_equal("result_is_success", true, result.success());
        });

        // Test nested list
        t.test("nested_list", [](testing &t) {
            auto value_parser = build_peg_parser([](common_peg_parser_builder & p) {
                p.rule("number", p.chars("0-9"));
                p.rule("list", p.literal("[") + p.ref("value") + p.literal("]"));
                return p.rule("value", p.ref("number") | p.ref("list"));
            });

            common_peg_parse_context ctx("[[2]]", false);
            auto           result = value_parser.parse(ctx);

            t.assert_equal("result_is_success", true, result.success());
        });

        // Test deeply nested list
        t.test("deeply_nested_list", [](testing &t) {
            auto value_parser = build_peg_parser([](common_peg_parser_builder & p) {
                p.rule("number", p.chars("0-9"));
                p.rule("list", p.literal("[") + p.ref("value") + p.literal("]"));
                return p.rule("value", p.ref("number") | p.ref("list"));
            });

            common_peg_parse_context ctx("[[[3]]]", false);
            auto           result = value_parser.parse(ctx);

            t.assert_equal("result_is_success", true, result.success());
        });

        // Test need_more_input match
        t.test("need_more_input_match", [](testing &t) {
            auto value_parser = build_peg_parser([](common_peg_parser_builder & p) {
                p.rule("number", p.chars("0-9"));
                p.rule("list", p.literal("[") + p.ref("value") + p.literal("]"));
                return p.rule("value", p.ref("number") | p.ref("list"));
            });

            common_peg_parse_context ctx("[[", true);
            auto           result = value_parser.parse(ctx);

            t.assert_equal("result_is_need_more_input", true, result.need_more_input());
        });

        // Test no match
        t.test("no_match", [](testing &t) {
            auto value_parser = build_peg_parser([](common_peg_parser_builder & p) {
                p.rule("number", p.chars("0-9"));
                p.rule("list", p.literal("[") + p.ref("value") + p.literal("]"));
                return p.rule("value", p.ref("number") | p.ref("list"));
            });

            common_peg_parse_context ctx("[a]", false);
            auto           result = value_parser.parse(ctx);

            t.assert_equal("result_is_fail", true, result.fail());
        });
    });
}