cursor_test.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* Host test for the NBCUR1 cursor bundle built by mkcursors
  2. * (make test-cursors): structure, a few Adwaita hotspots, the subsampled
  3. * spinner, and that every frame has visible pixels. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include "cursor_abi.h"
  8. static int fails;
  9. #define CHECK(c, ...) do { if (!(c)) { fails++; \
  10. fprintf(stderr, "FAIL %s:%d: ", __FILE__, __LINE__); \
  11. fprintf(stderr, __VA_ARGS__); fputc('\n', stderr); } } while (0)
  12. int main(int argc, char **argv)
  13. {
  14. const char *path = argc > 1 ? argv[1] : "cursors/adwaita24.cur";
  15. FILE *f = fopen(path, "rb");
  16. unsigned char *buf;
  17. long size;
  18. const struct cursor_file_hdr *h;
  19. const struct cursor_file_shape *s;
  20. unsigned int i, fr;
  21. if (!f) {
  22. perror(path);
  23. return 1;
  24. }
  25. fseek(f, 0, SEEK_END);
  26. size = ftell(f);
  27. fseek(f, 0, SEEK_SET);
  28. buf = malloc((size_t)size);
  29. if (!buf || fread(buf, 1, (size_t)size, f) != (size_t)size)
  30. return 1;
  31. fclose(f);
  32. h = (const struct cursor_file_hdr *)buf;
  33. s = (const struct cursor_file_shape *)(buf + sizeof(*h));
  34. CHECK(memcmp(h->magic, CURSOR_FILE_MAGIC, 8) == 0, "magic");
  35. CHECK(h->count == CURSOR_COUNT, "count %u", h->count);
  36. CHECK(h->nominal == 24, "nominal %u", h->nominal);
  37. if (fails)
  38. return 1;
  39. for (i = 0; i < h->count; i++) {
  40. unsigned int px = (unsigned int)s[i].w * s[i].h;
  41. CHECK(s[i].w == 24 && s[i].h == 24, "shape %u size %ux%u",
  42. i, s[i].w, s[i].h);
  43. CHECK(s[i].hot_x < s[i].w && s[i].hot_y < s[i].h,
  44. "shape %u hotspot", i);
  45. CHECK(s[i].frames >= 1, "shape %u frames", i);
  46. CHECK((s[i].offset & 3) == 0 &&
  47. s[i].offset + px * 4u * s[i].frames <= (unsigned long)size,
  48. "shape %u range", i);
  49. for (fr = 0; fr < s[i].frames; fr++) {
  50. const unsigned int *p = (const unsigned int *)
  51. (buf + s[i].offset) + fr * px;
  52. unsigned int k, opaque = 0, bad = 0;
  53. for (k = 0; k < px; k++) {
  54. unsigned int a = p[k] >> 24;
  55. if (a)
  56. opaque++;
  57. /* Premultiplied: no channel above alpha. */
  58. if (((p[k] >> 16) & 0xFF) > a ||
  59. ((p[k] >> 8) & 0xFF) > a || (p[k] & 0xFF) > a)
  60. bad++;
  61. }
  62. CHECK(opaque > 10, "shape %u frame %u is empty", i, fr);
  63. CHECK(bad == 0, "shape %u frame %u not premultiplied", i,
  64. fr);
  65. }
  66. }
  67. /* Hotspots from the Adwaita 51 Xcursor files. */
  68. CHECK(s[CURSOR_DEFAULT].hot_x == 3 && s[CURSOR_DEFAULT].hot_y == 1,
  69. "default hotspot");
  70. CHECK(s[CURSOR_TEXT].hot_x == 11 && s[CURSOR_TEXT].hot_y == 12,
  71. "text hotspot");
  72. CHECK(s[CURSOR_POINTER].hot_x == 7 && s[CURSOR_POINTER].hot_y == 5,
  73. "pointer hotspot");
  74. /* move is Adwaita's all-resize (its own `move` is a symlink to the
  75. * arrow). */
  76. CHECK(s[CURSOR_MOVE].hot_x == 12 && s[CURSOR_MOVE].hot_y == 11,
  77. "move hotspot");
  78. /* 60 frames x 16 ms, every 4th kept: 15 frames x 64 ms. */
  79. CHECK(s[CURSOR_WAIT].frames == 15 && s[CURSOR_WAIT].delay_ms == 64,
  80. "wait %u frames x %u ms", s[CURSOR_WAIT].frames,
  81. s[CURSOR_WAIT].delay_ms);
  82. CHECK(s[CURSOR_PROGRESS].frames == 15, "progress frames");
  83. CHECK(s[CURSOR_DEFAULT].frames == 1 && s[CURSOR_DEFAULT].delay_ms == 0,
  84. "default is static");
  85. i = h->count;
  86. free(buf);
  87. if (fails) {
  88. fprintf(stderr, "cursor_test: %d failure(s)\n", fails);
  89. return 1;
  90. }
  91. printf("cursor_test: %u shapes OK (%ld bytes)\n", i, size);
  92. return 0;
  93. }