File: test-json-serialization.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 (28 lines) | stat: -rw-r--r-- 1,021 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
#include "tests.h"

void test_json_serialization(testing &t) {
    auto original = build_peg_parser([](common_peg_parser_builder & p) {
        return "<tool_call>" + p.json() + "</tool_call>";
    });

    auto json_serialized = original.to_json().dump();

    t.test("compare before/after", [&](testing &t) {
        auto deserialized = common_peg_arena::from_json(nlohmann::json::parse(json_serialized));

        // Test complex JSON
        std::string input = R"({"name": "test", "values": [1, 2, 3], "nested": {"a": true}})";
        common_peg_parse_context ctx1(input);
        common_peg_parse_context ctx2(input);

        auto result1 = original.parse(ctx1);
        auto result2 = deserialized.parse(ctx2);

        t.assert_equal("both_succeed", result1.success(), result2.success());
        t.assert_equal("same_end_pos", result1.end, result2.end);
    });

    t.bench("deserialize", [&]() {
        auto deserialized = common_peg_arena::from_json(nlohmann::json::parse(json_serialized));
    }, 100);
}