Debian Codegraph @

modules/lsp-runtime-lib/src/test/utest/fmt/config/serializer.cpp

raw
1 /*
2 * Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2020 Vladimir Sadovnikov <[email protected]>
4 *
5 * This file is part of lsp-runtime-lib
6 * Created on: 2 мая 2020 г.
7 *
8 * lsp-runtime-lib is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * any later version.
12 *
13 * lsp-runtime-lib is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22 #include <lsp-plug.in/test-fw/utest.h>
23 #include <lsp-plug.in/test-fw/helpers.h>
24 #include <lsp-plug.in/fmt/config/PullParser.h>
25 #include <lsp-plug.in/fmt/config/Serializer.h>
26 #include <stdarg.h>
27 #include <math.h>
28
29 namespace lsp
30 {
31 using namespace lsp::config;
32 }
33
34 UTEST_BEGIN("runtime.fmt.config", serializer)
35
36 void test_serialize(LSPString & out)
37 {
38 Serializer s;
39
40 static const char *output =
41 "# Single-line comment\n"
42 "\n"
43 "# Multi\n"
44 "# line\n"
45 "# comment\n"
46 "\n"
47 "key1 = -123\n"
48 "key2 = u32:123\n"
49 "key3 = i64:\"16\"\n"
50 "key4 = \"440\"\n"
51 "key5 = 3.14\n"
52 "key6 = f32:3.14159\n"
53 "key7 = -inf db\n"
54 "key8 = \"0.00000\"\n"
55 "key9 = текст\n"
56 "key10 = \\ \\ \\ text\\ with\\ \\#\\ sign\\ and\\ \\\"quotes\\\"\\ \\ \\ \n"
57 "key11 = str:\\ \\ \\ text\\ with\\ \\#\\ sign\\ and\\ \\\"quotes\\\"\\ \\ \\ \n"
58 "key12 = str:\" text with # sign and \\\"quotes\\\" \"\n"
59 "/the/valid/key = value\n"
60 "key13 = blob:\"text/plain:10:1234567890\"\n"
61 "key14 = blob:\":5:abcde\"\n"
62 "key15 = true\n"
63 "key16 = \"false\"\n"
64 "key17 = bool:true\n"
65 "key18 = bool:\"false\"\n"
66 ;
67
68 UTEST_ASSERT(s.wrap(&out) == STATUS_OK);
69
70 UTEST_ASSERT(s.write_comment("Single-line comment") == STATUS_OK);
71 UTEST_ASSERT(s.writeln() == STATUS_OK);
72 UTEST_ASSERT(s.write_comment("Multi\nline\ncomment") == STATUS_OK);
73 UTEST_ASSERT(s.writeln() == STATUS_OK);
74
75 value_t v;
76
77 v.i32 = -123;
78 UTEST_ASSERT(s.write("key1", &v, SF_TYPE_I32) == STATUS_OK);
79 v.u32 = 123;
80 UTEST_ASSERT(s.write("key2", &v, SF_TYPE_U32 | SF_TYPE_SET) == STATUS_OK);
81 v.i64 = 16;
82 UTEST_ASSERT(s.write("key3", &v, SF_TYPE_I64 | SF_TYPE_SET | SF_QUOTED) == STATUS_OK);
83 v.u64 = 440;
84 UTEST_ASSERT(s.write("key4", &v, SF_TYPE_U64 | SF_QUOTED) == STATUS_OK);
85 v.f32 = 3.141591111111;
86 UTEST_ASSERT(s.write("key5", &v, SF_TYPE_F32 | SF_PREC_SHORT) == STATUS_OK);
87 UTEST_ASSERT(s.write("key6", &v, SF_TYPE_F32 | SF_TYPE_SET | SF_PREC_NORMAL) == STATUS_OK);
88 v.f64 = -INFINITY;
89 UTEST_ASSERT(s.write("key7", &v, SF_TYPE_F64 | SF_PREC_SHORT | SF_DECIBELS) == STATUS_OK);
90 v.f64 = 0.0f;
91 UTEST_ASSERT(s.write("key8", &v, SF_TYPE_F64 | SF_PREC_NORMAL | SF_QUOTED) == STATUS_OK);
92
93 v.str = const_cast<char *>("текст");
94 UTEST_ASSERT(s.write("key9", &v, SF_TYPE_STR) == STATUS_OK);
95 v.str = const_cast<char *>(" text with # sign and \"quotes\" ");
96 UTEST_ASSERT(s.write("key10", &v, SF_TYPE_STR) == STATUS_OK);
97 UTEST_ASSERT(s.write("key11", &v, SF_TYPE_STR | SF_TYPE_SET) == STATUS_OK);
98 UTEST_ASSERT(s.write("key12", &v, SF_TYPE_STR | SF_TYPE_SET | SF_QUOTED) == STATUS_OK);
99
100 v.str = const_cast<char *>("value");
101 UTEST_ASSERT(s.write("wrong key", &v, SF_TYPE_STR) == STATUS_INVALID_VALUE);
102 UTEST_ASSERT(s.write("wrong/key", &v, SF_TYPE_STR) == STATUS_INVALID_VALUE);
103
104 UTEST_ASSERT(s.write("/the/valid/key", &v, SF_TYPE_STR) == STATUS_OK);
105
106 v.blob.length = 10;
107 v.blob.ctype = const_cast<char *>("text/plain");
108 v.blob.data = const_cast<char *>("1234567890");
109 UTEST_ASSERT(s.write("key13", &v, SF_TYPE_BLOB) == STATUS_OK);
110
111 v.blob.length = 5;
112 v.blob.ctype = NULL;
113 v.blob.data = const_cast<char *>("abcde");
114 UTEST_ASSERT(s.write("key14", &v, SF_TYPE_BLOB) == STATUS_OK);
115
116 v.bval = true;
117 UTEST_ASSERT(s.write("key15", &v, SF_TYPE_BOOL) == STATUS_OK);
118 v.bval = false;
119 UTEST_ASSERT(s.write("key16", &v, SF_TYPE_BOOL | SF_QUOTED) == STATUS_OK);
120 v.bval = true;
121 UTEST_ASSERT(s.write("key17", &v, SF_TYPE_BOOL | SF_TYPE_SET) == STATUS_OK);
122 v.bval = false;
123 UTEST_ASSERT(s.write("key18", &v, SF_TYPE_BOOL | SF_TYPE_SET | SF_QUOTED) == STATUS_OK);
124
125 UTEST_ASSERT(s.close() == STATUS_OK);
126
127 UTEST_ASSERT(out.equals_utf8(output),
128 printf("Got output:\n%s\n", out.get_utf8());
129 printf("Expected output:\n%s\n", output);
130 );
131 }
132
133 void test_deserialize(LSPString &out)
134 {
135 PullParser p;
136
137 UTEST_ASSERT(p.wrap(&out) == STATUS_OK);
138 status_t res;
139 while ((res = p.next()) != STATUS_EOF)
140 printf(" got parameter: %s\n", p.current()->name.get_native());
141
142 UTEST_ASSERT(res == STATUS_EOF);
143 UTEST_ASSERT(p.close() == STATUS_OK);
144 }
145
146
147 UTEST_MAIN
148 {
149 LSPString out;
150
151 printf("Testing serialization...\n");
152 test_serialize(out);
153
154 printf("Testing deserialization...\n");
155 test_deserialize(out);
156 }
157
158 UTEST_END;
159
160