test.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #include <stdbool.h>
  5. #include <string.h>
  6. #include <errno.h>
  7. #include "./assets/tsodinPog.c"
  8. #include "./assets/tsodinCup.c"
  9. #define NOB_IMPLEMENTATION
  10. #define NOB_STRIP_PREFIX
  11. #include "nob.h"
  12. #define PI 3.14159265359
  13. #define ARENA_IMPLEMENTATION
  14. #include "./arena.h"
  15. static Arena default_arena = {0};
  16. static Arena *context_arena = &default_arena;
  17. static void *context_alloc(size_t size)
  18. {
  19. assert(context_arena);
  20. return arena_alloc(context_arena, size);
  21. }
  22. static void *context_realloc(void *oldp, size_t oldsz, size_t newsz)
  23. {
  24. if (newsz <= oldsz) return oldp;
  25. return memcpy(context_alloc(newsz), oldp, oldsz);
  26. }
  27. #define STBI_MALLOC context_alloc
  28. #define STBI_FREE UNUSED
  29. #define STBI_REALLOC_SIZED context_realloc
  30. #define STB_IMAGE_IMPLEMENTATION
  31. #include "./stb_image.h"
  32. #define STBIW_MALLOC STBI_MALLOC
  33. #define STBIW_FREE STBI_FREE
  34. #define STBIW_REALLOC_SIZED STBI_REALLOC_SIZED
  35. #define STB_IMAGE_WRITE_IMPLEMENTATION
  36. #include "./stb_image_write.h"
  37. #define OLIVEC_IMPLEMENTATION
  38. #include "olive.c"
  39. #define BACKGROUND_COLOR 0xFF202020
  40. #define FOREGROUND_COLOR 0xFF2020FF
  41. #define WHITE_COLOR 0xFFAAAAAA
  42. #define RED_COLOR 0xFF2020AA
  43. #define GREEN_COLOR 0xFF20AA20
  44. #define BLUE_COLOR 0xFFAA2020
  45. #define ERROR_COLOR 0xFFFF00FF
  46. #define TEST_DIR_PATH "./test"
  47. bool canvas_stbi_load(const char *file_path, Olivec_Canvas *oc)
  48. {
  49. int width, height;
  50. uint32_t *pixels = (uint32_t*) stbi_load(file_path, &width, &height, NULL, 4);
  51. if (pixels == NULL) return false;
  52. *oc = olivec_canvas(pixels, width, height, width);
  53. return true;
  54. }
  55. bool canvas_stbi_save(Olivec_Canvas oc, const char *file_path)
  56. {
  57. return stbi_write_png(file_path, oc.width, oc.height, 4, oc.pixels, sizeof(uint32_t)*oc.stride);
  58. }
  59. typedef struct {
  60. Olivec_Canvas (*generate_actual_canvas)(void);
  61. const char *id;
  62. const char *expected_file_path;
  63. const char *actual_file_path;
  64. const char *diff_file_path;
  65. } Test_Case;
  66. #define DEFINE_TEST_CASE(name) \
  67. { \
  68. .generate_actual_canvas = test_##name, \
  69. .id = #name, \
  70. .expected_file_path = TEST_DIR_PATH "/" #name "_expected.png", \
  71. .actual_file_path = TEST_DIR_PATH "/" #name "_actual.png", \
  72. .diff_file_path = TEST_DIR_PATH "/" #name "_diff.png", \
  73. }
  74. bool update_test_case(const Test_Case *tc)
  75. {
  76. Olivec_Canvas actual_canvas = tc->generate_actual_canvas();
  77. const char *expected_file_path = tc->expected_file_path;
  78. if (!canvas_stbi_save(actual_canvas, expected_file_path)) {
  79. fprintf(stderr, "ERROR: could not write file %s: %s\n", expected_file_path, strerror(errno));
  80. return(false);
  81. }
  82. printf("%s: Generated %s\n", tc->id, expected_file_path);
  83. return(true);
  84. }
  85. Olivec_Canvas canvas_alloc(size_t width, size_t height)
  86. {
  87. uint32_t *pixels = context_alloc(sizeof(uint32_t)*width*height);
  88. return olivec_canvas(pixels, width, height, width);
  89. }
  90. typedef enum {
  91. REPLAY_PASSED,
  92. REPLAY_FAILED,
  93. REPLAY_ERRORED,
  94. } Replay_Result;
  95. static inline size_t min_size(size_t a, size_t b)
  96. {
  97. if (a < b) return a;
  98. return b;
  99. }
  100. static inline size_t max_size(size_t a, size_t b)
  101. {
  102. if (a > b) return a;
  103. return b;
  104. }
  105. Replay_Result run_test_case(const char *program_path, const Test_Case *tc)
  106. {
  107. printf("%s:", tc->id);
  108. fflush(stdout);
  109. const char *expected_file_path = tc->expected_file_path;
  110. const char *actual_file_path = tc->actual_file_path;
  111. const char *diff_file_path = tc->diff_file_path;
  112. Olivec_Canvas actual_canvas = tc->generate_actual_canvas();
  113. Olivec_Canvas expected_canvas;
  114. if (!canvas_stbi_load(expected_file_path, &expected_canvas)) {
  115. fprintf(stderr, "\n");
  116. fprintf(stderr, " ERROR: could not read %s: %s\n", expected_file_path, stbi_failure_reason());
  117. if (errno == ENOENT) {
  118. fprintf(stderr, " HINT: Consider running `$ %s update %s` to create it\n", program_path, tc->id);
  119. }
  120. return(REPLAY_ERRORED);
  121. }
  122. bool failed = false;
  123. if (expected_canvas.width != actual_canvas.width || expected_canvas.height != actual_canvas.height) {
  124. failed = true;
  125. }
  126. Olivec_Canvas diff_canvas =
  127. canvas_alloc(
  128. max_size(expected_canvas.width, actual_canvas.width),
  129. max_size(expected_canvas.height, actual_canvas.height));
  130. olivec_fill(diff_canvas, ERROR_COLOR);
  131. for (size_t y = 0; y < min_size(expected_canvas.height, actual_canvas.height); ++y) {
  132. for (size_t x = 0; x < min_size(expected_canvas.width, actual_canvas.width); ++x) {
  133. uint32_t expected_pixel = OLIVEC_PIXEL(expected_canvas, x, y);
  134. uint32_t actual_pixel = OLIVEC_PIXEL(actual_canvas, x, y);
  135. if (expected_pixel != actual_pixel) {
  136. OLIVEC_PIXEL(diff_canvas, x, y) = ERROR_COLOR;
  137. failed = true;
  138. } else {
  139. OLIVEC_PIXEL(diff_canvas, x, y) = expected_pixel;
  140. }
  141. }
  142. }
  143. if (failed) {
  144. fprintf(stderr, "\n");
  145. if (!canvas_stbi_save(actual_canvas, actual_file_path)) {
  146. fprintf(stderr, " ERROR: could not write image file with actual pixels %s: %s\n", actual_file_path, strerror(errno));
  147. return(REPLAY_ERRORED);
  148. }
  149. if (!canvas_stbi_save(diff_canvas, diff_file_path)) {
  150. fprintf(stderr, " ERROR: could not wrilte diff image file %s: %s\n", diff_file_path, strerror(errno));
  151. return(REPLAY_ERRORED);
  152. }
  153. fprintf(stderr, " TEST FAILURE: unexpected pixels in generated image\n");
  154. fprintf(stderr, " Expected: %s\n", expected_file_path);
  155. fprintf(stderr, " Actual: %s\n", actual_file_path);
  156. fprintf(stderr, " Diff: %s\n", diff_file_path);
  157. fprintf(stderr, " HINT: If this behaviour is intentional confirm that by updating the image with `$ %s update`\n", program_path);
  158. return(REPLAY_FAILED);
  159. }
  160. printf(" OK\n");
  161. return(REPLAY_PASSED);
  162. }
  163. Olivec_Canvas test_fill_rect(void)
  164. {
  165. size_t width = 128;
  166. size_t height = 128;
  167. Olivec_Canvas oc = canvas_alloc(width, height);
  168. olivec_fill(oc, BACKGROUND_COLOR);
  169. olivec_rect(oc, width/2 - width/8, height/2 - height/8, width/4, height/4, RED_COLOR);
  170. olivec_rect(oc, width - 1, height - 1, -width/2, -height/2, GREEN_COLOR);
  171. olivec_rect(oc, -width/4, -height/4, width/2, height/2, BLUE_COLOR);
  172. return oc;
  173. }
  174. Olivec_Canvas test_fill_circle(void)
  175. {
  176. size_t width = 128;
  177. size_t height = 128;
  178. Olivec_Canvas oc = canvas_alloc(width, height);
  179. olivec_fill(oc, BACKGROUND_COLOR);
  180. olivec_circle(oc, 0, 0, width/2, RED_COLOR);
  181. olivec_circle(oc, width/2, height/2, width/4, BLUE_COLOR);
  182. olivec_circle(oc, width*3/4, height*3/4, -width/4, GREEN_COLOR);
  183. return oc;
  184. }
  185. Olivec_Canvas test_draw_line(void)
  186. {
  187. size_t width = 128;
  188. size_t height = 128;
  189. Olivec_Canvas oc = canvas_alloc(width, height);
  190. olivec_fill(oc, BACKGROUND_COLOR);
  191. olivec_line(oc, 0, 0, width, height, RED_COLOR);
  192. olivec_line(oc, width, 0, 0, height, BLUE_COLOR);
  193. olivec_line(oc, width/2, 0, width/2, height, GREEN_COLOR);
  194. return oc;
  195. }
  196. Olivec_Canvas test_fill_triangle(void)
  197. {
  198. size_t width = 128;
  199. size_t height = 128;
  200. Olivec_Canvas oc = canvas_alloc(width, height);
  201. olivec_fill(oc, BACKGROUND_COLOR);
  202. {
  203. int x1 = width/2, y1 = height/8;
  204. int x2 = width/8, y2 = height/2;
  205. int x3 = width*7/8, y3 = height*7/8;
  206. olivec_triangle(oc, x1, y1, x2, y2, x3, y3, RED_COLOR);
  207. }
  208. {
  209. int x1 = width/2, y1 = height*2/8;
  210. int x2 = width*2/8, y2 = height/2;
  211. int x3 = width*6/8, y3 = height/2;
  212. olivec_triangle(oc, x1, y1, x2, y2, x3, y3, GREEN_COLOR);
  213. }
  214. {
  215. int x1 = width/8, y1 = height/8;
  216. int x2 = width/8, y2 = height*3/8;
  217. int x3 = width*3/8, y3 = height*3/8;
  218. olivec_triangle(oc, x1, y1, x2, y2, x3, y3, BLUE_COLOR);
  219. }
  220. return oc;
  221. }
  222. Olivec_Canvas test_alpha_blending(void)
  223. {
  224. size_t width = 128;
  225. size_t height = 128;
  226. Olivec_Canvas oc = canvas_alloc(width, height);
  227. olivec_fill(oc, BACKGROUND_COLOR);
  228. olivec_rect(oc, 0, 0, width*3/4, height*3/4, RED_COLOR);
  229. olivec_rect(oc, width-1, height-1, -width*3/4, -height*3/4, 0x5520AA20);
  230. olivec_circle(oc, width/2, height/2, width/4, 0xBBAA2020);
  231. olivec_triangle(oc, 0, height-1, width-1, height-1, width/2, 0, 0xBB20AAAA);
  232. olivec_triangle3c(oc, 0, 0, width-1, 0, width/2, height-1, 0xBB2020AA, 0xBB20AA20, 0xBBAA2020);
  233. return oc;
  234. }
  235. Olivec_Canvas test_checker_example(void)
  236. {
  237. int width = 800/2;
  238. int height = 600/2;
  239. int cols = (8*2);
  240. int rows = (6*2);
  241. int cell_width = (width/cols);
  242. int cell_height = (height/rows);
  243. Olivec_Canvas oc = canvas_alloc(width, height);
  244. olivec_fill(oc, BACKGROUND_COLOR);
  245. for (int y = 0; y < rows; ++y) {
  246. for (int x = 0; x < cols; ++x) {
  247. uint32_t color = BACKGROUND_COLOR;
  248. if ((x + y)%2 == 0) {
  249. color = 0xFF2020FF;
  250. }
  251. olivec_rect(oc, x*cell_width, y*cell_height, cell_width, cell_height, color);
  252. }
  253. }
  254. return oc;
  255. }
  256. Olivec_Canvas test_circle_example(void)
  257. {
  258. int width = 800/2;
  259. int height = 600/2;
  260. int cols = (8*2);
  261. int rows = (6*2);
  262. int cell_width = (width/cols);
  263. int cell_height = (height/rows);
  264. Olivec_Canvas oc = canvas_alloc(width, height);
  265. olivec_fill(oc, BACKGROUND_COLOR);
  266. for (int y = 0; y < rows; ++y) {
  267. for (int x = 0; x < cols; ++x) {
  268. float u = (float)x/cols;
  269. float v = (float)y/rows;
  270. float t = (u + v)/2;
  271. int radius = cell_width;
  272. if (cell_height < radius) radius = cell_height;
  273. olivec_circle(oc,
  274. x*cell_width + cell_width/2, y*cell_height + cell_height/2,
  275. (size_t) (radius/8*(1 - t) + radius/2*t),
  276. FOREGROUND_COLOR);
  277. }
  278. }
  279. return oc;
  280. }
  281. Olivec_Canvas test_lines_circle(void)
  282. {
  283. int width = 800/2;
  284. int height = 600/2;
  285. Olivec_Canvas oc = canvas_alloc(width, height);
  286. olivec_fill(oc, BACKGROUND_COLOR);
  287. size_t n = 20;
  288. float angle = 2*PI/n;
  289. float length = width;
  290. if (length > height) length = height;
  291. length /= 3;
  292. float x1 = width/2;
  293. float y1 = height/2;
  294. for (size_t i = 0; i < n; ++i) {
  295. float x2 = x1 + cosf(angle*i)*length;
  296. float y2 = y1 + sinf(angle*i)*length;
  297. olivec_line(oc, x1, y1, x2, y2, 0xFF1818FF);
  298. }
  299. return oc;
  300. }
  301. Olivec_Canvas test_lines_example(void)
  302. {
  303. int width = 800/2;
  304. int height = 600/2;
  305. Olivec_Canvas oc = canvas_alloc(width, height);
  306. olivec_fill(oc, BACKGROUND_COLOR);
  307. olivec_line(oc, 0, 0, width, height, FOREGROUND_COLOR);
  308. olivec_line(oc, width, 0, 0, height, FOREGROUND_COLOR);
  309. olivec_line(oc, 0, 0, width/4, height, 0xFF20FF20);
  310. olivec_line(oc, width/4, 0, 0, height, 0xFF20FF20);
  311. olivec_line(oc, width, 0, width/4*3, height, 0xFF20FF20);
  312. olivec_line(oc, width/4*3, 0, width, height, 0xFF20FF20);
  313. olivec_line(oc, 0, height/2, width, height/2, 0xFFFF3030);
  314. olivec_line(oc, width/2, 0, width/2, height, 0xFFFF3030);
  315. return oc;
  316. }
  317. Olivec_Canvas test_hello_world_text_rendering(void)
  318. {
  319. size_t size = 5;
  320. const char *text = "hello, world";
  321. size_t text_len = strlen(text);
  322. Olivec_Canvas oc = canvas_alloc(400, 150);
  323. olivec_fill(oc, BACKGROUND_COLOR);
  324. olivec_text(oc, text, oc.width/2 - OLIVEC_DEFAULT_FONT_WIDTH*size*text_len/2, oc.height/2 - OLIVEC_DEFAULT_FONT_HEIGHT*size/2, olivec_default_font, size, FOREGROUND_COLOR);
  325. return oc;
  326. }
  327. Olivec_Canvas test_line_edge_cases(void)
  328. {
  329. size_t width = 10;
  330. size_t height = 10;
  331. Olivec_Canvas oc = canvas_alloc(width, height);
  332. olivec_fill(oc, BACKGROUND_COLOR);
  333. // One pixel line
  334. olivec_line(oc, width/2, height/2, width/2, height/2, FOREGROUND_COLOR);
  335. // Out-of-bounds horizontally
  336. olivec_line(oc, width + 10, height/2, width + 20, height/2, FOREGROUND_COLOR);
  337. // Out-of-bounds vertically
  338. olivec_line(oc, width/2, height + 10, width/2, height + 20, FOREGROUND_COLOR);
  339. return oc;
  340. }
  341. Olivec_Canvas test_frame(void)
  342. {
  343. size_t width = 256;
  344. size_t height = 128;
  345. Olivec_Canvas oc = canvas_alloc(width, height);
  346. olivec_fill(oc, BACKGROUND_COLOR);
  347. {
  348. size_t w = width/2;
  349. size_t h = height/2;
  350. olivec_frame(oc, 0, 0, w, h, 1, RED_COLOR);
  351. }
  352. {
  353. olivec_frame(oc, width/2, height/2, width, height, 1, GREEN_COLOR);
  354. }
  355. // Odd thiccness
  356. {
  357. size_t w = width/2;
  358. size_t h = height/2;
  359. size_t t = 5;
  360. olivec_frame(oc, width/2 - w/2, height/2 - h/2, w, h, t, WHITE_COLOR);
  361. olivec_frame(oc, width/2 - w/2, height/2 - h/2, w, h, 1, RED_COLOR);
  362. }
  363. // Even thiccness
  364. {
  365. size_t w = width/4 + 1;
  366. size_t h = height/4;
  367. size_t t = 6;
  368. olivec_frame(oc, width/2 - w/2, height/2 - h/2, w, h, t, WHITE_COLOR);
  369. olivec_frame(oc, width/2 - w/2, height/2 - h/2, w, h, 1, RED_COLOR);
  370. }
  371. // Zero thiccness
  372. {
  373. size_t w = width/8;
  374. size_t h = height/8;
  375. size_t t = 0;
  376. olivec_frame(oc, width/2 - w/2, height/2 - h/2, w, h, t, WHITE_COLOR);
  377. }
  378. return oc;
  379. }
  380. Olivec_Canvas test_sprite_blend(void)
  381. {
  382. size_t width = 128;
  383. size_t height = 128;
  384. Olivec_Canvas dst = canvas_alloc(width, height);
  385. olivec_fill(dst, RED_COLOR);
  386. Olivec_Canvas src = canvas_alloc(width, height);
  387. for (size_t y = 0; y < src.height; ++y) {
  388. for (size_t x = 0; x < src.width; ++x) {
  389. if ((x + y)%2 == 0) {
  390. OLIVEC_PIXEL(src, x, y) = 0;
  391. } else {
  392. OLIVEC_PIXEL(src, x, y) = GREEN_COLOR;
  393. }
  394. }
  395. }
  396. olivec_sprite_blend(dst, 0, 0, width, height, src);
  397. return dst;
  398. }
  399. Olivec_Canvas test_sprite_blend_out_of_bounds_cut(void)
  400. {
  401. size_t width = 128;
  402. size_t height = 128;
  403. Olivec_Canvas dst = canvas_alloc(width, height);
  404. Olivec_Canvas src = olivec_canvas(tsodinPog_pixels, tsodinPog_width, tsodinPog_height, tsodinPog_width);
  405. olivec_fill(dst, RED_COLOR);
  406. olivec_sprite_blend(dst, -width/2, -height/2, width, height, src);
  407. olivec_sprite_blend(dst, width/2, -height/2, width, height, src);
  408. olivec_sprite_blend(dst, -width/2, height/2, width, height, src);
  409. olivec_sprite_blend(dst, width/2, height/2, width, height, src);
  410. return dst;
  411. }
  412. Olivec_Canvas test_sprite_blend_flip(void)
  413. {
  414. size_t width = 128;
  415. size_t height = 128;
  416. Olivec_Canvas dst = canvas_alloc(width, height);
  417. Olivec_Canvas src = olivec_canvas(tsodinPog_pixels, tsodinPog_width, tsodinPog_height, tsodinPog_width);
  418. olivec_fill(dst, RED_COLOR);
  419. olivec_sprite_blend(dst, 0, 0, width/2, height/2, src);
  420. olivec_sprite_blend(dst, width - 1, 0, -width/2, height/2, src);
  421. olivec_sprite_blend(dst, 0, height - 1, width/2, -height/2, src);
  422. olivec_sprite_blend(dst, width - 1, height - 1, -width/2, -height/2, src);
  423. return dst;
  424. }
  425. Olivec_Canvas test_sprite_blend_flip_cut(void)
  426. {
  427. size_t width = 128;
  428. size_t height = 128;
  429. Olivec_Canvas dst = canvas_alloc(width, height);
  430. Olivec_Canvas src = olivec_canvas(tsodinPog_pixels, tsodinPog_width, tsodinPog_height, tsodinPog_width);
  431. olivec_fill(dst, RED_COLOR);
  432. olivec_sprite_blend(dst, -width/2, -height/2, width, height, src);
  433. olivec_sprite_blend(dst, width - 1 + width/2, -height/2, -width, height, src);
  434. olivec_sprite_blend(dst, -width/2, height - 1 + height/2, width, -height, src);
  435. olivec_sprite_blend(dst, width - 1 + width/2, height - 1 + height/2, -width, -height, src);
  436. return dst;
  437. }
  438. Olivec_Canvas test_empty_rect(void)
  439. {
  440. size_t w = 8;
  441. size_t h = 8;
  442. Olivec_Canvas dst = canvas_alloc(w, h);
  443. olivec_fill(dst, BACKGROUND_COLOR);
  444. olivec_rect(dst, w/2, h/2, 0, 0, FOREGROUND_COLOR);
  445. return dst;
  446. }
  447. Olivec_Canvas test_sprite_blend_empty_rect(void)
  448. {
  449. size_t w = 8;
  450. size_t h = 8;
  451. Olivec_Canvas dst = canvas_alloc(w, h);
  452. Olivec_Canvas src = olivec_canvas(tsodinPog_pixels, tsodinPog_width, tsodinPog_height, tsodinPog_width);
  453. olivec_fill(dst, BACKGROUND_COLOR);
  454. olivec_sprite_blend(dst, 0, 0, 0, 0, src);
  455. return dst;
  456. }
  457. Olivec_Canvas test_sprite_blend_null(void)
  458. {
  459. size_t w = 8;
  460. size_t h = 8;
  461. Olivec_Canvas dst = canvas_alloc(w, h);
  462. olivec_fill(dst, BACKGROUND_COLOR);
  463. olivec_sprite_blend(dst, 0, 0, w, h, OLIVEC_CANVAS_NULL);
  464. return dst;
  465. }
  466. Olivec_Canvas test_sprite_blend_vs_copy(void)
  467. {
  468. Olivec_Canvas tsodinCup = olivec_canvas(tsodinCup_pixels, tsodinCup_width, tsodinCup_height, tsodinCup_width);
  469. size_t w = tsodinCup.width;
  470. size_t h = tsodinCup.height*2;
  471. Olivec_Canvas dst = canvas_alloc(w, h);
  472. olivec_fill(dst, RED_COLOR);
  473. olivec_sprite_blend(dst, 0, 0, tsodinCup.width, tsodinCup.height, tsodinCup);
  474. olivec_sprite_copy(dst, 0, tsodinCup.height, tsodinCup.width, tsodinCup.height, tsodinCup);
  475. return dst;
  476. }
  477. Olivec_Canvas test_barycentric_overflow(void)
  478. {
  479. size_t w = 256;
  480. size_t h = 256;
  481. Olivec_Canvas dst = canvas_alloc(w, h);
  482. olivec_fill(dst, 0xFF181818);
  483. olivec_triangle3c(dst, w/4, h/4, w/2, 0, 0, h, 0xFF0000FF, 0xFF00FF00, 0xFFFF0000);
  484. return dst;
  485. }
  486. Olivec_Canvas test_triangle_order_flip(void)
  487. {
  488. size_t w = 256;
  489. size_t h = 256;
  490. Olivec_Canvas dst = canvas_alloc(w, h);
  491. olivec_fill(dst, 0xFF181818);
  492. olivec_triangle3c(
  493. dst,
  494. w/4, h/4,
  495. 0, h,
  496. w, 0,
  497. 0xFF00FF00,
  498. 0xFFFF0000,
  499. 0xFF0000FF);
  500. return dst;
  501. }
  502. Olivec_Canvas test_bilinear_interpolation(void)
  503. {
  504. size_t factor = 2;
  505. Olivec_Canvas src = olivec_canvas(tsodinPog_pixels, tsodinPog_width, tsodinPog_height, tsodinPog_width);
  506. Olivec_Canvas dst = canvas_alloc(src.width*factor*2, src.height*factor);
  507. olivec_fill(dst, RED_COLOR);
  508. olivec_sprite_copy(dst, 0, 0, src.width*factor, src.height*factor, src);
  509. olivec_sprite_copy_bilinear(dst, src.width*factor, 0, src.width*factor, src.height*factor, src);
  510. return dst;
  511. }
  512. Olivec_Canvas test_fill_ellipse(void)
  513. {
  514. size_t factor = 3;
  515. Olivec_Canvas dst = canvas_alloc(100*factor, 50*factor);
  516. olivec_fill(dst, BACKGROUND_COLOR);
  517. olivec_ellipse(dst, dst.width/2, dst.height/2, dst.width/3, dst.height/3, RED_COLOR);
  518. return dst;
  519. }
  520. Olivec_Canvas test_line_bug_offset(void)
  521. {
  522. size_t factor = 3;
  523. size_t width = 100*factor;
  524. size_t height = 50*factor;
  525. Olivec_Canvas dst = canvas_alloc(width, height);
  526. olivec_fill(dst, BACKGROUND_COLOR);
  527. int x1 = 50;
  528. int y1 = 100;
  529. int x2 = 0;
  530. int y2 = -100;
  531. olivec_line(dst, x1, y1, x2, y2, GREEN_COLOR);
  532. olivec_circle(dst, x1, y1, 5, RED_COLOR);
  533. return dst;
  534. }
  535. Test_Case test_cases[] = {
  536. DEFINE_TEST_CASE(fill_rect),
  537. DEFINE_TEST_CASE(fill_circle),
  538. DEFINE_TEST_CASE(draw_line),
  539. DEFINE_TEST_CASE(fill_triangle),
  540. DEFINE_TEST_CASE(alpha_blending),
  541. DEFINE_TEST_CASE(checker_example),
  542. DEFINE_TEST_CASE(circle_example),
  543. DEFINE_TEST_CASE(lines_example),
  544. DEFINE_TEST_CASE(hello_world_text_rendering),
  545. DEFINE_TEST_CASE(lines_circle),
  546. DEFINE_TEST_CASE(line_edge_cases),
  547. DEFINE_TEST_CASE(frame),
  548. DEFINE_TEST_CASE(sprite_blend),
  549. DEFINE_TEST_CASE(sprite_blend_out_of_bounds_cut),
  550. DEFINE_TEST_CASE(sprite_blend_flip),
  551. DEFINE_TEST_CASE(sprite_blend_flip_cut),
  552. DEFINE_TEST_CASE(sprite_blend_empty_rect),
  553. DEFINE_TEST_CASE(empty_rect),
  554. DEFINE_TEST_CASE(sprite_blend_null),
  555. DEFINE_TEST_CASE(sprite_blend_vs_copy),
  556. DEFINE_TEST_CASE(triangle_order_flip),
  557. DEFINE_TEST_CASE(barycentric_overflow),
  558. DEFINE_TEST_CASE(bilinear_interpolation),
  559. DEFINE_TEST_CASE(fill_ellipse),
  560. DEFINE_TEST_CASE(line_bug_offset),
  561. };
  562. #define TEST_CASES_COUNT (sizeof(test_cases)/sizeof(test_cases[0]))
  563. void list_available_tests(void)
  564. {
  565. fprintf(stderr, "Available tests:\n");
  566. for (size_t i = 0; i < TEST_CASES_COUNT; ++i) {
  567. fprintf(stderr, " %s\n", test_cases[i].id);
  568. }
  569. }
  570. Test_Case *find_test_case_by_id(const char *id)
  571. {
  572. for (size_t i = 0; i < TEST_CASES_COUNT; ++i) {
  573. if (strcmp(test_cases[i].id, id) == 0) {
  574. return &test_cases[i];
  575. }
  576. }
  577. return NULL;
  578. }
  579. typedef struct {
  580. int (*run)(const char *program_path, int argc, char **argv);
  581. const char *id;
  582. const char *description;
  583. } Subcmd;
  584. void usage(const char *program_path);
  585. int subcmd_run(const char *program_path, int argc, char **argv)
  586. {
  587. if (argc <= 0) {
  588. for (size_t i = 0; i < TEST_CASES_COUNT; ++i) {
  589. if (run_test_case(program_path, &test_cases[i]) == REPLAY_ERRORED) return(1);
  590. arena_reset(&default_arena);
  591. }
  592. } else {
  593. const char *test_case_id = shift(argv, argc);
  594. Test_Case *tc = find_test_case_by_id(test_case_id);
  595. if (tc == NULL) {
  596. list_available_tests();
  597. fprintf(stderr, "ERROR: could not find test case `%s`\n", test_case_id);
  598. return(1);
  599. }
  600. if (run_test_case(program_path, tc) == REPLAY_ERRORED) return(1);
  601. }
  602. return 0;
  603. }
  604. int subcmd_update(const char *program_path, int argc, char **argv)
  605. {
  606. UNUSED(program_path);
  607. if (argc <= 0) {
  608. for (size_t i = 0; i < TEST_CASES_COUNT; ++i) {
  609. if (!update_test_case(&test_cases[i])) return(1);
  610. arena_reset(&default_arena);
  611. }
  612. } else {
  613. const char *test_case_id = shift(argv, argc);
  614. Test_Case *tc = find_test_case_by_id(test_case_id);
  615. if (tc == NULL) {
  616. list_available_tests();
  617. fprintf(stderr, "ERROR: could not find test case `%s`\n", test_case_id);
  618. return(1);
  619. }
  620. if (!update_test_case(tc)) return(1);
  621. }
  622. return 0;
  623. }
  624. int subcmd_list(const char *program_path, int argc, char **argv)
  625. {
  626. UNUSED(program_path);
  627. UNUSED(argc);
  628. UNUSED(argv);
  629. list_available_tests();
  630. return 0;
  631. }
  632. int subcmd_help(const char *program_path, int argc, char **argv)
  633. {
  634. UNUSED(argc);
  635. UNUSED(argv);
  636. usage(program_path);
  637. return 0;
  638. }
  639. #define DEFINE_SUBCMD(name, desc) \
  640. { \
  641. .run = subcmd_##name, \
  642. .id = #name, \
  643. .description = desc, \
  644. }
  645. Subcmd subcmds[] = {
  646. DEFINE_SUBCMD(run, "Run the tests"),
  647. DEFINE_SUBCMD(update, "Update the tests"),
  648. DEFINE_SUBCMD(list, "List all available tests"),
  649. DEFINE_SUBCMD(help, "Print this help message"),
  650. };
  651. #define SUBCMDS_COUNT (sizeof(subcmds)/sizeof(subcmds[0]))
  652. Subcmd *find_subcmd_by_id(const char *id)
  653. {
  654. for (size_t i = 0; i < SUBCMDS_COUNT; ++i) {
  655. if (strcmp(subcmds[i].id, id) == 0) {
  656. return &subcmds[i];
  657. }
  658. }
  659. return NULL;
  660. }
  661. void usage(const char *program_path)
  662. {
  663. fprintf(stderr, "Usage: %s [Subcommand]\n", program_path);
  664. fprintf(stderr, "Subcommands:\n");
  665. int width = 0;
  666. for (size_t i = 0; i < SUBCMDS_COUNT; ++i) {
  667. int len = strlen(subcmds[i].id);
  668. if (width < len) width = len;
  669. }
  670. for (size_t i = 0; i < SUBCMDS_COUNT; ++i) {
  671. fprintf(stderr, " %-*s - %s\n", width, subcmds[i].id, subcmds[i].description);
  672. }
  673. }
  674. int main(int argc, char **argv)
  675. {
  676. int result = 0;
  677. {
  678. const char *program_path = shift(argv, argc);
  679. if (argc <= 0) {
  680. usage(program_path);
  681. fprintf(stderr, "ERROR: no subcommand is provided\n");
  682. return_defer(1);
  683. }
  684. const char *subcmd_id = shift(argv, argc);
  685. Subcmd *subcmd = find_subcmd_by_id(subcmd_id);
  686. if (subcmd != NULL) {
  687. return_defer(subcmd->run(program_path, argc, argv));
  688. } else {
  689. usage(program_path);
  690. fprintf(stderr, "ERROR: unknown subcommand `%s`\n", subcmd_id);
  691. return_defer(1);
  692. }
  693. }
  694. defer:
  695. arena_free(&default_arena);
  696. return result;
  697. }