You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

291 line
9.3 KiB

  1. /*
  2. * caca-test testsuite program for libcaca
  3. * Copyright (c) 2009 Sam Hocevar <sam@hocevar.net>
  4. * All Rights Reserved
  5. *
  6. * This program is free software. It comes without any warranty, to
  7. * the extent permitted by applicable law. You can redistribute it
  8. * and/or modify it under the terms of the Do What the Fuck You Want
  9. * to Public License, Version 2, as published by Sam Hocevar. See
  10. * http://www.wtfpl.net/ for more details.
  11. */
  12. #include "config.h"
  13. #include <cppunit/extensions/HelperMacros.h>
  14. #include <cppunit/TestCaller.h>
  15. #include <cppunit/TestCase.h>
  16. #include <cppunit/TestSuite.h>
  17. #include "caca.h"
  18. class DirtyTest : public CppUnit::TestCase
  19. {
  20. CPPUNIT_TEST_SUITE(DirtyTest);
  21. CPPUNIT_TEST(test_create);
  22. CPPUNIT_TEST(test_put_char_dirty);
  23. CPPUNIT_TEST(test_put_char_not_dirty);
  24. CPPUNIT_TEST(test_simplify);
  25. CPPUNIT_TEST(test_box);
  26. CPPUNIT_TEST(test_blit);
  27. CPPUNIT_TEST_SUITE_END();
  28. public:
  29. DirtyTest() : CppUnit::TestCase("Dirty Rectangles Test") {}
  30. void setUp() {}
  31. void tearDown() {}
  32. void test_create()
  33. {
  34. caca_canvas_t *cv;
  35. int dx, dy, dw, dh;
  36. /* Check that we only have one dirty rectangle upon creation. */
  37. cv = caca_create_canvas(WIDTH, HEIGHT);
  38. CPPUNIT_ASSERT_EQUAL(1, caca_get_dirty_rect_count(cv));
  39. /* Check that our only rectangle contains the whole canvas. */
  40. caca_get_dirty_rect(cv, 0, &dx, &dy, &dw, &dh);
  41. CPPUNIT_ASSERT_EQUAL(0, dx);
  42. CPPUNIT_ASSERT_EQUAL(0, dy);
  43. CPPUNIT_ASSERT_EQUAL(WIDTH, dw);
  44. CPPUNIT_ASSERT_EQUAL(HEIGHT, dh);
  45. /* Invalidate the dirty rectangle and check that it stays so. */
  46. caca_clear_dirty_rect_list(cv);
  47. CPPUNIT_ASSERT_EQUAL(0, caca_get_dirty_rect_count(cv));
  48. caca_free_canvas(cv);
  49. }
  50. void test_put_char_dirty()
  51. {
  52. caca_canvas_t *cv;
  53. int dx, dy, dw, dh;
  54. cv = caca_create_canvas(WIDTH, HEIGHT);
  55. /* Check that one character creates a 1x1 dirty rect. */
  56. caca_clear_dirty_rect_list(cv);
  57. caca_put_char(cv, 7, 3, 'x');
  58. CPPUNIT_ASSERT_EQUAL(1, caca_get_dirty_rect_count(cv));
  59. caca_get_dirty_rect(cv, 0, &dx, &dy, &dw, &dh);
  60. CPPUNIT_ASSERT_EQUAL(7, dx);
  61. CPPUNIT_ASSERT_EQUAL(3, dy);
  62. CPPUNIT_ASSERT_EQUAL(1, dw);
  63. CPPUNIT_ASSERT_EQUAL(1, dh);
  64. /* Check that a fullwidth character creates a 2x1 dirty rect. */
  65. caca_clear_canvas(cv);
  66. caca_clear_dirty_rect_list(cv);
  67. caca_put_char(cv, 7, 3, 0x2f06 /* ⼆ */);
  68. CPPUNIT_ASSERT_EQUAL(1, caca_get_dirty_rect_count(cv));
  69. caca_get_dirty_rect(cv, 0, &dx, &dy, &dw, &dh);
  70. CPPUNIT_ASSERT_EQUAL(7, dx);
  71. CPPUNIT_ASSERT_EQUAL(3, dy);
  72. CPPUNIT_ASSERT_EQUAL(2, dw);
  73. CPPUNIT_ASSERT_EQUAL(1, dh);
  74. /* Check that a character over a fullwidth character creates a
  75. * 2x1 dirty rect because of clobbering on the left side. */
  76. caca_clear_canvas(cv);
  77. caca_put_char(cv, 7, 3, 0x2f06 /* ⼆ */);
  78. caca_clear_dirty_rect_list(cv);
  79. caca_put_char(cv, 7, 3, 'x');
  80. CPPUNIT_ASSERT_EQUAL(1, caca_get_dirty_rect_count(cv));
  81. caca_get_dirty_rect(cv, 0, &dx, &dy, &dw, &dh);
  82. CPPUNIT_ASSERT_EQUAL(7, dx);
  83. CPPUNIT_ASSERT_EQUAL(3, dy);
  84. CPPUNIT_ASSERT_EQUAL(2, dw);
  85. CPPUNIT_ASSERT_EQUAL(1, dh);
  86. /* Check that a character over a fullwidth character creates a
  87. * 2x1 dirty rect because of clobbering on the right side. */
  88. caca_clear_canvas(cv);
  89. caca_put_char(cv, 7, 3, 0x2f06 /* ⼆ */);
  90. caca_clear_dirty_rect_list(cv);
  91. caca_put_char(cv, 8, 3, 'x');
  92. CPPUNIT_ASSERT_EQUAL(1, caca_get_dirty_rect_count(cv));
  93. caca_get_dirty_rect(cv, 0, &dx, &dy, &dw, &dh);
  94. CPPUNIT_ASSERT_EQUAL(7, dx);
  95. CPPUNIT_ASSERT_EQUAL(3, dy);
  96. CPPUNIT_ASSERT_EQUAL(2, dw);
  97. CPPUNIT_ASSERT_EQUAL(1, dh);
  98. /* Check that a fullwidth character over a fullwidth character creates
  99. * a 3x1 dirty rect because of clobbering on the left side. */
  100. caca_clear_canvas(cv);
  101. caca_put_char(cv, 7, 3, 0x2f06 /* ⼆ */);
  102. caca_clear_dirty_rect_list(cv);
  103. caca_put_char(cv, 6, 3, 0x2f06 /* ⼆ */);
  104. CPPUNIT_ASSERT_EQUAL(1, caca_get_dirty_rect_count(cv));
  105. caca_get_dirty_rect(cv, 0, &dx, &dy, &dw, &dh);
  106. CPPUNIT_ASSERT_EQUAL(6, dx);
  107. CPPUNIT_ASSERT_EQUAL(3, dy);
  108. CPPUNIT_ASSERT_EQUAL(3, dw);
  109. CPPUNIT_ASSERT_EQUAL(1, dh);
  110. /* Check that a fullwidth character over a fullwidth character creates
  111. * a 3x1 dirty rect because of clobbering on the right side. */
  112. caca_clear_canvas(cv);
  113. caca_put_char(cv, 7, 3, 0x2f06 /* ⼆ */);
  114. caca_clear_dirty_rect_list(cv);
  115. caca_put_char(cv, 8, 3, 0x2f06 /* ⼆ */);
  116. CPPUNIT_ASSERT_EQUAL(1, caca_get_dirty_rect_count(cv));
  117. caca_get_dirty_rect(cv, 0, &dx, &dy, &dw, &dh);
  118. CPPUNIT_ASSERT_EQUAL(7, dx);
  119. CPPUNIT_ASSERT_EQUAL(3, dy);
  120. CPPUNIT_ASSERT_EQUAL(3, dw);
  121. CPPUNIT_ASSERT_EQUAL(1, dh);
  122. }
  123. void test_put_char_not_dirty()
  124. {
  125. caca_canvas_t *cv;
  126. cv = caca_create_canvas(WIDTH, HEIGHT);
  127. /* Check that pasting the same character does not cause a dirty
  128. * rectangle to be created. */
  129. caca_put_char(cv, 7, 3, 'x');
  130. caca_clear_dirty_rect_list(cv);
  131. caca_put_char(cv, 7, 3, 'x');
  132. CPPUNIT_ASSERT_EQUAL(0, caca_get_dirty_rect_count(cv));
  133. /* Check that pasting the same fullwidth character does not cause
  134. * a dirty rectangle to be created. */
  135. caca_clear_canvas(cv);
  136. caca_put_char(cv, 7, 3, 0x2f06 /* ⼆ */);
  137. caca_clear_dirty_rect_list(cv);
  138. caca_put_char(cv, 7, 3, 0x2f06 /* ⼆ */);
  139. CPPUNIT_ASSERT_EQUAL(0, caca_get_dirty_rect_count(cv));
  140. }
  141. void test_simplify()
  142. {
  143. caca_canvas_t *cv;
  144. int dx, dy, dw, dh;
  145. cv = caca_create_canvas(WIDTH, HEIGHT);
  146. /* Check that N adjacent blits only create one dirty rectangle */
  147. caca_clear_dirty_rect_list(cv);
  148. for(int i = 0; i < 10; i++)
  149. {
  150. caca_put_char(cv, 7 + i, 3, '-');
  151. CPPUNIT_ASSERT_EQUAL(1, caca_get_dirty_rect_count(cv));
  152. caca_get_dirty_rect(cv, 0, &dx, &dy, &dw, &dh);
  153. CPPUNIT_ASSERT_EQUAL(7, dx);
  154. CPPUNIT_ASSERT_EQUAL(3, dy);
  155. CPPUNIT_ASSERT_EQUAL(1 + i, dw);
  156. CPPUNIT_ASSERT_EQUAL(1, dh);
  157. }
  158. /* Check that N adjacent blits only create one dirty rectangle */
  159. caca_clear_dirty_rect_list(cv);
  160. for(int j = 0; j < 10; j++)
  161. {
  162. caca_put_char(cv, 7, 3 + j, '|');
  163. CPPUNIT_ASSERT_EQUAL(1, caca_get_dirty_rect_count(cv));
  164. caca_get_dirty_rect(cv, 0, &dx, &dy, &dw, &dh);
  165. CPPUNIT_ASSERT_EQUAL(7, dx);
  166. CPPUNIT_ASSERT_EQUAL(3, dy);
  167. CPPUNIT_ASSERT_EQUAL(1, dw);
  168. CPPUNIT_ASSERT_EQUAL(1 + j, dh);
  169. }
  170. }
  171. void test_box()
  172. {
  173. caca_canvas_t *cv;
  174. int dx, dy, dw, dh;
  175. cv = caca_create_canvas(WIDTH, HEIGHT);
  176. caca_clear_dirty_rect_list(cv);
  177. /* Check that a filled box creates one dirty rectangle of the same
  178. * size. */
  179. caca_fill_box(cv, 7, 3, 14, 9, 'x');
  180. CPPUNIT_ASSERT_EQUAL(1, caca_get_dirty_rect_count(cv));
  181. caca_get_dirty_rect(cv, 0, &dx, &dy, &dw, &dh);
  182. CPPUNIT_ASSERT_EQUAL(7, dx);
  183. CPPUNIT_ASSERT_EQUAL(3, dy);
  184. CPPUNIT_ASSERT_EQUAL(14, dw);
  185. CPPUNIT_ASSERT_EQUAL(9, dh);
  186. /* Check that the same filled box does not create a new dirty
  187. * rectangle. */
  188. caca_clear_dirty_rect_list(cv);
  189. caca_fill_box(cv, 7, 3, 14, 9, 'x');
  190. CPPUNIT_ASSERT_EQUAL(0, caca_get_dirty_rect_count(cv));
  191. }
  192. void test_blit()
  193. {
  194. caca_canvas_t *cv, *cv2;
  195. int i, dx, dy, dw, dh;
  196. cv = caca_create_canvas(WIDTH, HEIGHT);
  197. caca_clear_dirty_rect_list(cv);
  198. cv2 = caca_create_canvas(2, 2);
  199. caca_fill_box(cv2, 0, 0, 2, 1, 'x');
  200. /* Check that blitting a canvas make a dirty rectangle
  201. * only for modified lines */
  202. /* FIXME: check this test's validity */
  203. caca_blit(cv, 1, 1, cv2, NULL);
  204. i = caca_get_dirty_rect_count(cv);
  205. CPPUNIT_ASSERT_EQUAL(1, i);
  206. caca_get_dirty_rect(cv, 0, &dx, &dy, &dw, &dh);
  207. CPPUNIT_ASSERT(1 == dx);
  208. CPPUNIT_ASSERT(1 == dy);
  209. CPPUNIT_ASSERT(2 <= dw);
  210. CPPUNIT_ASSERT(1 == dh);
  211. caca_clear_canvas(cv);
  212. caca_clear_dirty_rect_list(cv);
  213. /* Check that blitting a canvas make a dirty rectangle
  214. * only for modified chars when we have a mask */
  215. /* FIXME: check this test's validity */
  216. caca_blit(cv, 1, 1, cv2, cv2);
  217. i = caca_get_dirty_rect_count(cv);
  218. CPPUNIT_ASSERT_EQUAL(1, i);
  219. caca_get_dirty_rect(cv, 0, &dx, &dy, &dw, &dh);
  220. CPPUNIT_ASSERT(1 == dx);
  221. CPPUNIT_ASSERT(1 == dy);
  222. CPPUNIT_ASSERT(2 == dw);
  223. CPPUNIT_ASSERT(1 == dh);
  224. CPPUNIT_ASSERT(' ' == caca_get_char(cv, 0, 0));
  225. }
  226. private:
  227. static int const WIDTH, HEIGHT;
  228. };
  229. int const DirtyTest::WIDTH = 80;
  230. int const DirtyTest::HEIGHT = 50;
  231. CPPUNIT_TEST_SUITE_REGISTRATION(DirtyTest);