Debian Codegraph @

modules/lsp-runtime-lib/src/test/utest/fmt/xpm/xpm2.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
23 #include <lsp-plug.in/test-fw/utest.h>
24 #include <lsp-plug.in/fmt/xpm/xpm.h>
25 #include <lsp-plug.in/stdlib/string.h>
26
27 #define XFACE_width 23
28 #define XFACE_height 22
29 #define XFACE_ncolors 6
30 #define XFACE_chars_per_pixel 2
31
32 static const char *XFACE_pixels[] = {
33 "x x x x x x x x x x x x + x x x x x x ",
34 " x x x x x x x x x x x x x x x x Y ",
35 "x x x x x x x x x x x x + x x x x x x ",
36 " x x x x x x x x x x x x x x x x Y ",
37 "x x x x x x x x x x x x + x x x x x x ",
38 "Y Y Y Y Y x Y Y Y Y Y + x + x + x + x + x + Y ",
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 " x x x x Y x x x Y ",
45 " x x x Y x x x ",
46 " x x x x Y x x x Y ",
47 " x x x Y x x x ",
48 " x x x x Y x x x Y ",
49 "x x x x x 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 };
56
57
58 UTEST_BEGIN("runtime.fmt.xpm", xpm2)
59
60 void validate_icon(lsp::xpm::Parser *parser)
61 {
62 // Read and validate header
63 UTEST_ASSERT(parser != NULL);
64 xpm::header_t hdr;
65 UTEST_ASSERT(parser->read_header(&hdr) == STATUS_OK);
66
67 UTEST_ASSERT(hdr.version == xpm::VERSION_XPM2);
68 UTEST_ASSERT(hdr.width == XFACE_width);
69 UTEST_ASSERT(hdr.height == XFACE_height);
70 UTEST_ASSERT(hdr.num_colors == XFACE_ncolors);
71 UTEST_ASSERT(hdr.x_hotspot == 11);
72 UTEST_ASSERT(hdr.y_hotspot == 12);
73 UTEST_ASSERT(hdr.has_extensions);
74
75 // Read colors
76 lsp::xpm::Color c;
77
78 UTEST_ASSERT(parser->read_color(&c) == STATUS_OK);
79 UTEST_ASSERT(c.has_code(" "));
80 UTEST_ASSERT(c.mono_visual().has_name("white"));
81 UTEST_ASSERT(c.symbolic_visual().has_name("light_color"));
82 UTEST_ASSERT(!c.gray4_visual().is_set());
83 UTEST_ASSERT(!c.gray_visual().is_set());
84 UTEST_ASSERT(c.color_visual().has_name("red"));
85
86 UTEST_ASSERT(parser->read_color(&c) == STATUS_OK);
87 UTEST_ASSERT(c.has_code("Y "));
88 UTEST_ASSERT(c.mono_visual().has_name("black"));
89 UTEST_ASSERT(c.symbolic_visual().has_name("lines_in_mix"));
90 UTEST_ASSERT(!c.gray4_visual().is_set());
91 UTEST_ASSERT(!c.gray_visual().is_set());
92 UTEST_ASSERT(c.color_visual().has_name("green"));
93
94 UTEST_ASSERT(parser->read_color(&c) == STATUS_OK);
95 UTEST_ASSERT(c.has_code("+ "));
96 UTEST_ASSERT(c.mono_visual().has_name("white"));
97 UTEST_ASSERT(c.symbolic_visual().has_name("lines_in_dark"));
98 UTEST_ASSERT(!c.gray4_visual().is_set());
99 UTEST_ASSERT(c.gray_visual().has_name("grey"));
100 UTEST_ASSERT(c.color_visual().has_name("yellow"));
101
102 UTEST_ASSERT(parser->read_color(&c) == STATUS_OK);
103 UTEST_ASSERT(c.has_code("x "));
104 UTEST_ASSERT(c.mono_visual().has_name("black"));
105 UTEST_ASSERT(c.symbolic_visual().has_name("dark_color"));
106 UTEST_ASSERT(c.gray4_visual().has_name("grey"));
107 UTEST_ASSERT(!c.gray_visual().is_set());
108 UTEST_ASSERT(!c.color_visual().is_set());
109
110 UTEST_ASSERT(parser->read_color(&c) == STATUS_OK);
111 UTEST_ASSERT(c.has_code(".*"));
112 UTEST_ASSERT(!c.mono_visual().is_set());
113 UTEST_ASSERT(!c.symbolic_visual().is_set());
114 UTEST_ASSERT(!c.gray4_visual().is_set());
115 UTEST_ASSERT(!c.gray_visual().is_set());
116 UTEST_ASSERT(c.color_visual().rgb48() == 0xbeefcafebabeull);
117 UTEST_ASSERT(c.color_visual().rgb24() == 0x00becaba);
118
119 UTEST_ASSERT(parser->read_color(&c) == STATUS_OK);
120 UTEST_ASSERT(c.has_code("./"));
121 UTEST_ASSERT(!c.mono_visual().is_set());
122 UTEST_ASSERT(!c.symbolic_visual().is_set());
123 UTEST_ASSERT(!c.gray4_visual().is_set());
124 UTEST_ASSERT(!c.gray_visual().is_set());
125 UTEST_ASSERT(c.color_visual().rgb24() == 0x00abcdef);
126 UTEST_ASSERT(c.color_visual().rgb48() == 0xababcdcdefefull);
127
128 UTEST_ASSERT(parser->read_color(&c) == STATUS_NOT_FOUND);
129
130 // Read rows
131 char *buf = new char[XFACE_width * XFACE_chars_per_pixel];
132 UTEST_ASSERT(buf != NULL);
133 lsp_finally { delete[] buf; };
134
135 for (size_t i=0; i<XFACE_height; ++i)
136 {
137 UTEST_ASSERT(parser->read_line(buf) == STATUS_OK);
138 UTEST_ASSERT(memcmp(buf, XFACE_pixels[i], XFACE_width * XFACE_chars_per_pixel) == 0);
139 }
140
141 UTEST_ASSERT(parser->read_line(buf) == STATUS_NOT_FOUND);
142
143 // Read extensions
144 xpm::Extension ext;
145
146 UTEST_ASSERT(parser->read_ext(&ext) == STATUS_OK);
147 UTEST_ASSERT(ext.has_name("ext1"));
148 UTEST_ASSERT(ext.rows() == 1);
149 UTEST_ASSERT(ext.row(0) != NULL);
150 UTEST_ASSERT(strcmp(ext.row(0), "data1") == 0);
151
152 UTEST_ASSERT(parser->read_ext(&ext) == STATUS_OK);
153 UTEST_ASSERT(ext.has_name("ext2"));
154 UTEST_ASSERT(ext.rows() == 2);
155 UTEST_ASSERT(ext.row(0) != NULL);
156 UTEST_ASSERT(strcmp(ext.row(0), "data2_1") == 0);
157 UTEST_ASSERT(ext.row(1) != NULL);
158 UTEST_ASSERT(strcmp(ext.row(1), "data2_2") == 0);
159
160 UTEST_ASSERT(parser->read_ext(&ext) == STATUS_NOT_FOUND);
161 }
162
163 void test_read_file()
164 {
165 io::Path path;
166 UTEST_ASSERT(path.fmt("%s/fmt/xpm/xpm2.xpm", resources()) > 0);
167
168 printf("Reading file %s\n", path.as_native());
169
170 lsp::xpm::Parser *parser = NULL;
171 UTEST_ASSERT(lsp::xpm::open(&parser, &path) == STATUS_OK);
172
173 validate_icon(parser);
174
175 // Close and delete parser
176 UTEST_ASSERT(parser->close() == STATUS_OK);
177 delete parser;
178 }
179
180 UTEST_MAIN
181 {
182 printf("Testing read from file...\n");
183 test_read_file();
184 }
185
186 UTEST_END
187
188