/* splinter -- CDE theme derivation tests. */ #include #include #include "theme.h" #include "palettes.h" static int failures; #define CHECK(cond) do { \ if (!(cond)) { \ fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \ failures++; \ } \ } while (0) static int luma(const unsigned char *c) { return 299 * c[0] + 587 * c[1] + 114 * c[2]; } int main(void) { struct sp_theme t, t2; struct sp_buf err; const char **names; size_t count, i; int seen_delphinium = 0; if (sp_buf_init(&err) != 0) return 1; /* every palette in the CDE tree is present, and sorted by name */ CHECK(sp_theme_list(&names, &count) == count); CHECK(count == sp_palette_count); CHECK(count >= 40); for (i = 1; i < count; i++) CHECK(strcmp(names[i - 1], names[i]) < 0); /* the default is Delphinium, and NULL means the default */ CHECK(sp_theme_load(NULL, &t, &err) == 0); CHECK(strcmp(t.name, SP_THEME_DEFAULT) == 0); CHECK(strcmp(t.name, "Delphinium") == 0); CHECK(sp_theme_load("Delphinium", &t2, &err) == 0); CHECK(memcmp(&t, &t2, sizeof(t)) == 0); /* roles are derived by luminance, so bg is darker than fg in every one */ for (i = 0; i < count; i++) { CHECK(sp_theme_load(names[i], &t, &err) == 0); CHECK(luma(t.bg) <= luma(t.fg)); CHECK(luma(t.border) >= luma(t.bg)); CHECK(luma(t.status) <= luma(t.accent)); } (void)seen_delphinium; /* an unknown palette is an error, never a silent fallback */ sp_buf_clear(&err); CHECK(sp_theme_load("NoSuchPalette", &t, &err) == -1); CHECK(strstr(err.p, "unknown palette") != NULL); /* colour mapping stays inside the terminal's palette */ CHECK(sp_color_rgb(0, 0, 0, 256) >= 16); CHECK(sp_color_rgb(0, 0, 0, 256) <= 255); CHECK(sp_color_rgb(255, 255, 255, 256) >= 16); CHECK(sp_color_rgb(255, 255, 255, 256) <= 255); CHECK(sp_color_rgb(0, 0, 0, 8) >= 0 && sp_color_rgb(0, 0, 0, 8) < 8); CHECK(sp_color_rgb(255, 255, 255, 16) >= 0); CHECK(sp_color_rgb(255, 255, 255, 16) < 16); sp_buf_free(&err); if (failures == 0) printf("test_theme: ok (%zu palettes)\n", count); return failures == 0 ? 0 : 1; }