text_canvas_test.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /* Host test for the 8x16 terminal-cell renderer (make test-textcanvas).
  2. * Exercises the renderer directly; no kernel headers are involved. */
  3. #include <stdio.h>
  4. #include "text_canvas.h"
  5. #include "font_8x16.h"
  6. #define COLS 64u
  7. #define ROWS 32u
  8. #define W (COLS * TEXT_CELL_W)
  9. #define H (ROWS * TEXT_CELL_H)
  10. /* Hash of the fully rendered reference grid. Regenerate only when the font,
  11. * palette or cell metrics intentionally change. */
  12. #define GOLDEN 0x8598C6C9u
  13. static unsigned short cells[COLS * ROWS];
  14. static unsigned short fb[W * H];
  15. static int failures;
  16. /* Same standard VGA palette as text_canvas.c, in RGB565. */
  17. static const unsigned short pal565[16] = {
  18. 0x0000, 0x0015, 0x0540, 0x0555,
  19. 0xA800, 0xA815, 0xAAA0, 0xAD55,
  20. 0x52AA, 0x52BF, 0x57EA, 0x57FF,
  21. 0xFAAA, 0xFABF, 0xFFEA, 0xFFFF,
  22. };
  23. static void expect(unsigned int x, unsigned int y, unsigned short want,
  24. const char *what)
  25. {
  26. unsigned short got;
  27. if (x >= W || y >= H) {
  28. printf("FAIL %s: (%u,%u) out of range\n", what, x, y);
  29. failures++;
  30. return;
  31. }
  32. got = fb[y * W + x];
  33. if (got != want) {
  34. printf("FAIL %s at (%u,%u): got 0x%04X want 0x%04X\n",
  35. what, x, y, got, want);
  36. failures++;
  37. }
  38. }
  39. static int glyph_bit(char ch, unsigned int row, unsigned int col)
  40. {
  41. return (font_8x16[(unsigned int)(unsigned char)ch * FONT_8X16_ROWS +
  42. row] >> (7 - col)) & 1;
  43. }
  44. static void fill_cells(unsigned short value)
  45. {
  46. unsigned int i;
  47. for (i = 0; i < COLS * ROWS; i++)
  48. cells[i] = value;
  49. }
  50. static void put_text(unsigned int col, unsigned int row, const char *s,
  51. unsigned char attr)
  52. {
  53. unsigned int i;
  54. for (i = 0; s[i] && col + i < COLS; i++)
  55. cells[row * COLS + col + i] =
  56. (unsigned short)(((unsigned short)attr << 8) |
  57. (unsigned char)s[i]);
  58. }
  59. static unsigned int hash565(const unsigned short *px, unsigned int n)
  60. {
  61. unsigned int h = 2166136261u;
  62. unsigned int i;
  63. for (i = 0; i < n; i++) {
  64. h = (h ^ (px[i] & 0xFF)) * 16777619u;
  65. h = (h ^ (px[i] >> 8)) * 16777619u;
  66. }
  67. return h;
  68. }
  69. int main(void)
  70. {
  71. struct text_canvas c = { fb, W, H };
  72. unsigned int cursor = 3 * COLS + 5;
  73. unsigned int i, hash;
  74. fill_cells(0x0720); /* space, light gray on black */
  75. put_text(0, 0, "Nebs OS 8x16", 0x0F);
  76. put_text(0, 1, "0123456789", 0x1E);
  77. put_text(2, 2, "ABCDEFGH", 0x4A);
  78. cells[1 * COLS + 12] = 0x1E20; /* blue background space */
  79. cells[3 * COLS + 4] = 0x4A20; /* red background space sentinel */
  80. cells[3 * COLS + 5] = 0x0020; /* black space under the cursor */
  81. /* The full render must cover every pixel. */
  82. for (i = 0; i < W * H; i++)
  83. fb[i] = 0xDEAD;
  84. text_canvas_render(&c, cells, COLS, ROWS, 0, 0, COLS, ROWS,
  85. cursor, 1, 1);
  86. for (i = 0; i < W * H; i++) {
  87. if (fb[i] == 0xDEAD) {
  88. printf("FAIL full render left sentinel at pixel %u\n", i);
  89. failures++;
  90. break;
  91. }
  92. }
  93. hash = hash565(fb, W * H);
  94. if (hash != GOLDEN) {
  95. printf("FAIL full render hash 0x%08X, expected 0x%08X\n",
  96. hash, GOLDEN);
  97. failures++;
  98. }
  99. /* 8x16 glyph mapping: 'A' at (2,2), attr 0x4A = light green on red. */
  100. {
  101. unsigned int row, bit;
  102. int set_seen = 0, clear_seen = 0;
  103. for (row = 0; row < FONT_8X16_ROWS; row++) {
  104. for (bit = 0; bit < 8; bit++) {
  105. unsigned int x = 2 * TEXT_CELL_W + bit;
  106. unsigned int y = 2 * TEXT_CELL_H + row;
  107. int on = glyph_bit('A', row, bit);
  108. if (on && !set_seen) {
  109. expect(x, y, pal565[10], "glyph set");
  110. set_seen = 1;
  111. }
  112. if (!on && !clear_seen) {
  113. expect(x, y, pal565[4], "glyph clear");
  114. clear_seen = 1;
  115. }
  116. }
  117. }
  118. if (!set_seen || !clear_seen) {
  119. printf("FAIL glyph test could not find set/clear bits\n");
  120. failures++;
  121. }
  122. }
  123. /* Cell boundary: row 0 background at y=15, row 1 at y=16. */
  124. expect(12 * TEXT_CELL_W, 15, pal565[0], "row 0 bottom");
  125. expect(12 * TEXT_CELL_W, 16, pal565[1], "row 1 top");
  126. /* Cursor: black space at (5,3), white 2x16 bar. */
  127. expect(5 * TEXT_CELL_W + 0, 3 * TEXT_CELL_H + 0, pal565[15],
  128. "cursor x0");
  129. expect(5 * TEXT_CELL_W + 1, 3 * TEXT_CELL_H + 0, pal565[15],
  130. "cursor x1");
  131. expect(5 * TEXT_CELL_W + 2, 3 * TEXT_CELL_H + 0, pal565[0],
  132. "cursor x2");
  133. expect(5 * TEXT_CELL_W + 0, 3 * TEXT_CELL_H + 15, pal565[15],
  134. "cursor bottom");
  135. expect(5 * TEXT_CELL_W + 0, 4 * TEXT_CELL_H, pal565[0],
  136. "cursor next row");
  137. /* Unfocused windows must not show the cursor. */
  138. text_canvas_render(&c, cells, COLS, ROWS, 0, 0, COLS, ROWS,
  139. cursor, 1, 0);
  140. expect(5 * TEXT_CELL_W + 0, 3 * TEXT_CELL_H + 0, pal565[0],
  141. "cursor unfocused");
  142. /* Block update: only cell (5,3) changes. */
  143. expect(4 * TEXT_CELL_W, 3 * TEXT_CELL_H, pal565[4],
  144. "sentinel before block");
  145. cells[3 * COLS + 5] = (0x0F << 8) | 'X';
  146. text_canvas_render(&c, cells, COLS, ROWS, 5, 3, 1, 1,
  147. 0xFFFFFFFFu, 0, 0);
  148. expect(4 * TEXT_CELL_W, 3 * TEXT_CELL_H, pal565[4],
  149. "sentinel after block");
  150. expect(5 * TEXT_CELL_W, 4 * TEXT_CELL_H, pal565[0],
  151. "row 4 after block");
  152. {
  153. unsigned int row, bit;
  154. int set_seen = 0, clear_seen = 0;
  155. for (row = 0; row < FONT_8X16_ROWS; row++) {
  156. for (bit = 0; bit < 8; bit++) {
  157. unsigned int x = 5 * TEXT_CELL_W + bit;
  158. unsigned int y = 3 * TEXT_CELL_H + row;
  159. int on = glyph_bit('X', row, bit);
  160. if (on && !set_seen) {
  161. expect(x, y, pal565[15], "X set");
  162. set_seen = 1;
  163. }
  164. if (!on && !clear_seen) {
  165. expect(x, y, pal565[0], "X clear");
  166. clear_seen = 1;
  167. }
  168. }
  169. }
  170. if (!set_seen || !clear_seen) {
  171. printf("FAIL X glyph test could not find bits\n");
  172. failures++;
  173. }
  174. }
  175. /* A block at the last cell must clamp without touching the canvas
  176. * bounds, and still render the cell. */
  177. cells[(ROWS - 1) * COLS + (COLS - 1)] = (0x0F << 8) | 'X';
  178. text_canvas_render(&c, cells, COLS, ROWS, COLS - 1, ROWS - 1, 4, 4,
  179. 0xFFFFFFFFu, 0, 0);
  180. {
  181. unsigned int row, bit;
  182. int set_seen = 0;
  183. for (row = 0; row < FONT_8X16_ROWS && !set_seen; row++)
  184. for (bit = 0; bit < 8; bit++)
  185. if (glyph_bit('X', row, bit)) {
  186. expect((COLS - 1) * TEXT_CELL_W + bit,
  187. (ROWS - 1) * TEXT_CELL_H + row,
  188. pal565[15], "edge block");
  189. set_seen = 1;
  190. break;
  191. }
  192. if (!set_seen) {
  193. printf("FAIL edge block glyph bits not found\n");
  194. failures++;
  195. }
  196. }
  197. if (failures) {
  198. printf("%d text canvas test(s) failed\n", failures);
  199. return 1;
  200. }
  201. printf("all text canvas tests passed\n");
  202. return 0;
  203. }