| 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: 21 янв. 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_format 1 |
| 27 |
#define XFACE_width 23 |
| 28 |
#define XFACE_height 22 |
| 29 |
#define XFACE_ncolors 4 |
| 30 |
#define XFACE_chars_per_pixel 2 |
| 31 |
|
| 32 |
static const char *XFACE_colors[] = { |
| 33 |
" ", "#ff0000", |
| 34 |
"Y ", "#00ff00", |
| 35 |
"+ ", "#ffff00", |
| 36 |
"x ", "#ffffff" |
| 37 |
}; |
| 38 |
|
| 39 |
static const char *XFACE_pixels[] = { |
| 40 |
"x x x x x x x x x x x x + x x x x x x ", |
| 41 |
" x x x x x x x x x x x x x x x x Y ", |
| 42 |
"x x x x x x x x x x x x + x x x x x x ", |
| 43 |
" x x x x x x x x x x x x x x x x Y ", |
| 44 |
"x x x x x x x x x x x x + x x x x x x ", |
| 45 |
"Y Y Y Y Y x Y Y Y Y Y + x + x + x + x + x + Y ", |
| 46 |
"x x x x x x x x x x x x + x x x x x x ", |
| 47 |
" x x x x x x x x x x x x x x x x Y ", |
| 48 |
"x x x x x x x x x x x x + x x x x x x ", |
| 49 |
" x x x x x x x x x x x x x x x x Y ", |
| 50 |
"x x x x x x x x x x x x + x x x x x x ", |
| 51 |
" x x x x Y x x x Y ", |
| 52 |
" x x x Y x x x ", |
| 53 |
" x x x x Y x x x Y ", |
| 54 |
" x x x Y x x x ", |
| 55 |
" x x x x Y x x x Y ", |
| 56 |
"x x x x x x x x x x x x x x x x x x x x x x x ", |
| 57 |
" x x x x Y x x x Y ", |
| 58 |
" x x x Y x x x ", |
| 59 |
" x x x x Y x x x Y ", |
| 60 |
" x x x Y x x x ", |
| 61 |
" x x x x Y x x x Y " |
| 62 |
}; |
| 63 |
|
| 64 |
|
| 65 |
UTEST_BEGIN("runtime.fmt.xpm", xpm1) |
| 66 |
|
| 67 |
bool validate_color(const lsp::xpm::Color & c, const char *code, uint32_t rgba) |
| 68 |
{ |
| 69 |
if (!c.has_code(code)) |
| 70 |
return false; |
| 71 |
|
| 72 |
if (c.mono_visual().is_set()) |
| 73 |
return false; |
| 74 |
if (c.symbolic_visual().is_set()) |
| 75 |
return false; |
| 76 |
if (c.gray4_visual().is_set()) |
| 77 |
return false; |
| 78 |
if (c.gray_visual().is_set()) |
| 79 |
return false; |
| 80 |
|
| 81 |
if (!c.color_visual().is_rgb24()) |
| 82 |
return false; |
| 83 |
if (c.color_visual().rgb24() != rgba) |
| 84 |
return false; |
| 85 |
if (c.color_visual().name() != NULL) |
| 86 |
return false; |
| 87 |
|
| 88 |
return true; |
| 89 |
} |
| 90 |
|
| 91 |
void validate_icon(lsp::xpm::Parser *parser) |
| 92 |
{ |
| 93 |
// Read and validate header |
| 94 |
UTEST_ASSERT(parser != NULL); |
| 95 |
xpm::header_t hdr; |
| 96 |
UTEST_ASSERT(parser->read_header(&hdr) == STATUS_OK); |
| 97 |
|
| 98 |
UTEST_ASSERT(hdr.version == xpm::VERSION_XPM1); |
| 99 |
UTEST_ASSERT(hdr.width == XFACE_width); |
| 100 |
UTEST_ASSERT(hdr.height == XFACE_height); |
| 101 |
UTEST_ASSERT(hdr.num_colors == XFACE_ncolors); |
| 102 |
UTEST_ASSERT(hdr.x_hotspot == 0); |
| 103 |
UTEST_ASSERT(hdr.y_hotspot == 0); |
| 104 |
UTEST_ASSERT(!hdr.has_extensions); |
| 105 |
|
| 106 |
// Read colors |
| 107 |
lsp::xpm::Color c; |
| 108 |
UTEST_ASSERT(parser->read_color(&c) == STATUS_OK); |
| 109 |
UTEST_ASSERT(validate_color(c, " ", 0xff0000)); |
| 110 |
UTEST_ASSERT(parser->read_color(&c) == STATUS_OK); |
| 111 |
UTEST_ASSERT(validate_color(c, "Y ", 0x00ff00)); |
| 112 |
UTEST_ASSERT(parser->read_color(&c) == STATUS_OK); |
| 113 |
UTEST_ASSERT(validate_color(c, "+ ", 0xffff00)); |
| 114 |
UTEST_ASSERT(parser->read_color(&c) == STATUS_OK); |
| 115 |
UTEST_ASSERT(validate_color(c, "x ", 0xffffff)); |
| 116 |
UTEST_ASSERT(parser->read_color(&c) == STATUS_NOT_FOUND); |
| 117 |
|
| 118 |
// Read rows |
| 119 |
char *buf = new char[XFACE_width * XFACE_chars_per_pixel]; |
| 120 |
UTEST_ASSERT(buf != NULL); |
| 121 |
lsp_finally { delete[] buf; }; |
| 122 |
|
| 123 |
for (size_t i=0; i<XFACE_height; ++i) |
| 124 |
{ |
| 125 |
UTEST_ASSERT(parser->read_line(buf) == STATUS_OK); |
| 126 |
UTEST_ASSERT(memcmp(buf, XFACE_pixels[i], XFACE_width * XFACE_chars_per_pixel) == 0); |
| 127 |
} |
| 128 |
|
| 129 |
UTEST_ASSERT(parser->read_line(buf) == STATUS_NOT_FOUND); |
| 130 |
|
| 131 |
// Read extensions |
| 132 |
xpm::Extension ext; |
| 133 |
UTEST_ASSERT(parser->read_ext(&ext) == STATUS_NOT_FOUND); |
| 134 |
} |
| 135 |
|
| 136 |
void test_read_builtin() |
| 137 |
{ |
| 138 |
lsp::xpm::Parser *parser = NULL; |
| 139 |
UTEST_ASSERT( |
| 140 |
lsp::xpm::make_xpm1( |
| 141 |
&parser, |
| 142 |
XFACE_width, XFACE_height, |
| 143 |
XFACE_ncolors, XFACE_chars_per_pixel, |
| 144 |
XFACE_colors, XFACE_pixels) == STATUS_OK); |
| 145 |
|
| 146 |
validate_icon(parser); |
| 147 |
|
| 148 |
// Close and delete parser |
| 149 |
UTEST_ASSERT(parser->close() == STATUS_OK); |
| 150 |
delete parser; |
| 151 |
} |
| 152 |
|
| 153 |
void test_read_file() |
| 154 |
{ |
| 155 |
io::Path path; |
| 156 |
UTEST_ASSERT(path.fmt("%s/fmt/xpm/xpm1.xpm", resources()) > 0); |
| 157 |
|
| 158 |
printf("Reading file %s\n", path.as_native()); |
| 159 |
|
| 160 |
lsp::xpm::Parser *parser = NULL; |
| 161 |
UTEST_ASSERT(lsp::xpm::open(&parser, &path) == STATUS_OK); |
| 162 |
|
| 163 |
validate_icon(parser); |
| 164 |
|
| 165 |
// Close and delete parser |
| 166 |
UTEST_ASSERT(parser->close() == STATUS_OK); |
| 167 |
delete parser; |
| 168 |
} |
| 169 |
|
| 170 |
UTEST_MAIN |
| 171 |
{ |
| 172 |
printf("Testing read from built-in structure ...\n"); |
| 173 |
test_read_builtin(); |
| 174 |
printf("Testing read from file...\n"); |
| 175 |
test_read_file(); |
| 176 |
} |
| 177 |
|
| 178 |
UTEST_END |
| 179 |
|
| 180 |
|
| 181 |
|