Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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://sam.zoy.org/wtfpl/COPYING 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);