Debian Codegraph @

modules/lsp-runtime-lib/src/test/utest/fmt/xpm/xpm3.cpp

raw
1 /*
2 * Copyright (C) 2026 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2026 Vladimir Sadovnikov <[email protected]>
4 *
5 * This file is part of lsp-runtime-lib
6 * Created on: 23 янв. 2026 г.
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/fmt/xpm/xpm.h>
24 #include <lsp-plug.in/stdlib/string.h>
25
26 #define XFACE_width 23
27 #define XFACE_height 22
28 #define XFACE_ncolors 6
29 #define XFACE_chars_per_pixel 2
30
31 static const char * const XFACE_data[] = {
32 "23 22 6 2 11 12 XPMEXT",
33 " c red m white s light color g4 grey ",
34 "Y c green m black s lines_in_mix",
35 "+ c yellow m white s lines_in_dark g dark slate grey ",
36 "x m black g4 dark slate grey s dark_color ",
37 ".* c #beefcafebabe",
38 "./ c #abcdef",
39 "x x x x x x x x x x x x + x x x x x x ",
40 " x x x x x x x x x x x x x x x x Y ",
41 "x x x x x x x x x x x x + x x x x x x ",
42 " x x x x x x x x x x x x x x x x Y ",
43 "x x x x x x x x x x x x + x x x x x x ",
44 "Y Y Y Y Y x Y Y Y Y Y + x + x + x + x + x + Y ",
45 "x x x x x x x x x x x x + x x x x x x ",
46 " x x x x x x x x x x x x x x x x Y ",
47 "x x x x x x x x x x x x + x x x x x x ",
48 " x x x x x x x x x x x x x x x x Y ",
49 "x x x x x x x x x x x x + x x x x x x ",
50 " x x x x Y x x x Y ",
51 " x x x Y x x x ",
52 " x x x x Y x x x Y ",
53 " x x x Y x x x ",
54 " x x x x Y x x x Y ",
55 "x x x x x x x x x x x x x x x x x x x x x x x ",
56 " x x x x Y x x x Y ",
57 " x x x Y x x x ",
58 " x x x x Y x x x Y ",
59 " x x x Y x x x ",
60 " x x x x Y x x x Y ",
61 "XPMEXT ext1 data1",
62 "XPMEXT ext2",
63 "data2_1",
64 "data2_2",
65 "XPMENDEXT"
66 };
67
68 UTEST_BEGIN("runtime.fmt.xpm", xpm3)
69
70 void validate_icon(lsp::xpm::Parser *parser)
71 {
72 // Read and validate header
73 UTEST_ASSERT(parser != NULL);
74 xpm::header_t hdr;
75 UTEST_ASSERT(parser->read_header(&hdr) == STATUS_OK);
76
77 UTEST_ASSERT(hdr.version == xpm::VERSION_XPM3);
78 UTEST_ASSERT(hdr.width == XFACE_width);
79 UTEST_ASSERT(hdr.height == XFACE_height);
80 UTEST_ASSERT(hdr.num_colors == XFACE_ncolors);
81 UTEST_ASSERT(hdr.x_hotspot == 11);
82 UTEST_ASSERT(hdr.y_hotspot == 12);
83 UTEST_ASSERT(hdr.has_extensions);
84
85 // Read colors
86 lsp::xpm::Color c;
87
88 UTEST_ASSERT(parser->read_color(&c) == STATUS_OK);
89 UTEST_ASSERT(c.has_code(" "));
90 UTEST_ASSERT(c.mono_visual().has_name("white"));
91 UTEST_ASSERT(c.symbolic_visual().has_name("light color"));
92 UTEST_ASSERT(c.gray4_visual().has_name("grey"));
93 UTEST_ASSERT(!c.gray_visual().is_set());
94 UTEST_ASSERT(c.color_visual().has_name("red"));
95
96 UTEST_ASSERT(parser->read_color(&c) == STATUS_OK);
97 UTEST_ASSERT(c.has_code("Y "));
98 UTEST_ASSERT(c.mono_visual().has_name("black"));
99 UTEST_ASSERT(c.symbolic_visual().has_name("lines_in_mix"));
100 UTEST_ASSERT(!c.gray4_visual().is_set());
101 UTEST_ASSERT(!c.gray_visual().is_set());
102 UTEST_ASSERT(c.color_visual().has_name("green"));
103
104 UTEST_ASSERT(parser->read_color(&c) == STATUS_OK);
105 UTEST_ASSERT(c.has_code("+ "));
106 UTEST_ASSERT(c.mono_visual().has_name("white"));
107 UTEST_ASSERT(c.symbolic_visual().has_name("lines_in_dark"));
108 UTEST_ASSERT(!c.gray4_visual().is_set());
109 UTEST_ASSERT(c.gray_visual().has_name("dark slate grey"));
110 UTEST_ASSERT(c.color_visual().has_name("yellow"));
111
112 UTEST_ASSERT(parser->read_color(&c) == STATUS_OK);
113 UTEST_ASSERT(c.has_code("x "));
114 UTEST_ASSERT(c.mono_visual().has_name("black"));
115 UTEST_ASSERT(c.symbolic_visual().has_name("dark_color"));
116 UTEST_ASSERT(c.gray4_visual().has_name("dark slate grey"));
117 UTEST_ASSERT(!c.gray_visual().is_set());
118 UTEST_ASSERT(!c.color_visual().is_set());
119
120 UTEST_ASSERT(parser->read_color(&c) == STATUS_OK);
121 UTEST_ASSERT(c.has_code(".*"));
122 UTEST_ASSERT(!c.mono_visual().is_set());
123 UTEST_ASSERT(!c.symbolic_visual().is_set());
124 UTEST_ASSERT(!c.gray4_visual().is_set());
125 UTEST_ASSERT(!c.gray_visual().is_set());
126 UTEST_ASSERT(c.color_visual().rgb48() == 0xbeefcafebabeull);
127 UTEST_ASSERT(c.color_visual().rgb24() == 0x00becaba);
128
129 UTEST_ASSERT(parser->read_color(&c) == STATUS_OK);
130 UTEST_ASSERT(c.has_code("./"));
131 UTEST_ASSERT(!c.mono_visual().is_set());
132 UTEST_ASSERT(!c.symbolic_visual().is_set());
133 UTEST_ASSERT(!c.gray4_visual().is_set());
134 UTEST_ASSERT(!c.gray_visual().is_set());
135 UTEST_ASSERT(c.color_visual().rgb24() == 0x00abcdef);
136 UTEST_ASSERT(c.color_visual().rgb48() == 0xababcdcdefefull);
137
138 UTEST_ASSERT(parser->read_color(&c) == STATUS_NOT_FOUND);
139
140 // Read rows
141 char *buf = new char[XFACE_width * XFACE_chars_per_pixel];
142 UTEST_ASSERT(buf != NULL);
143 lsp_finally { delete[] buf; };
144
145 for (size_t i=0; i<XFACE_height; ++i)
146 {
147 UTEST_ASSERT(parser->read_line(buf) == STATUS_OK);
148 UTEST_ASSERT(memcmp(buf, XFACE_data[i + XFACE_ncolors + 1], XFACE_width * XFACE_chars_per_pixel) == 0);
149 }
150
151 UTEST_ASSERT(parser->read_line(buf) == STATUS_NOT_FOUND);
152
153 // Read extensions
154 xpm::Extension ext;
155
156 UTEST_ASSERT(parser->read_ext(&ext) == STATUS_OK);
157 UTEST_ASSERT(ext.has_name("ext1"));
158 UTEST_ASSERT(ext.rows() == 1);
159 UTEST_ASSERT(ext.row(0) != NULL);
160 UTEST_ASSERT(strcmp(ext.row(0), "data1") == 0);
161
162 UTEST_ASSERT(parser->read_ext(&ext) == STATUS_OK);
163 UTEST_ASSERT(ext.has_name("ext2"));
164 UTEST_ASSERT(ext.rows() == 2);
165 UTEST_ASSERT(ext.row(0) != NULL);
166 UTEST_ASSERT(strcmp(ext.row(0), "data2_1") == 0);
167 UTEST_ASSERT(ext.row(1) != NULL);
168 UTEST_ASSERT(strcmp(ext.row(1), "data2_2") == 0);
169
170 UTEST_ASSERT(parser->read_ext(&ext) == STATUS_NOT_FOUND);
171 }
172
173 void test_read_builtin()
174 {
175 lsp::xpm::Parser *parser = NULL;
176 UTEST_ASSERT(
177 lsp::xpm::make_xpm3(
178 &parser,
179 XFACE_data, sizeof(XFACE_data)/ sizeof(XFACE_data[0])) == STATUS_OK);
180
181 validate_icon(parser);
182
183 // Close and delete parser
184 UTEST_ASSERT(parser->close() == STATUS_OK);
185 delete parser;
186 }
187
188 void test_read_file()
189 {
190 io::Path path;
191 UTEST_ASSERT(path.fmt("%s/fmt/xpm/xpm3.xpm", resources()) > 0);
192
193 printf("Reading file %s\n", path.as_native());
194
195 lsp::xpm::Parser *parser = NULL;
196 UTEST_ASSERT(lsp::xpm::open(&parser, &path) == STATUS_OK);
197
198 validate_icon(parser);
199
200 // Close and delete parser
201 UTEST_ASSERT(parser->close() == STATUS_OK);
202 delete parser;
203 }
204
205 UTEST_MAIN
206 {
207 printf("Testing read from built-in structure ...\n");
208 test_read_builtin();
209 printf("Testing read from file...\n");
210 test_read_file();
211 }
212
213 UTEST_END
214
215