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.

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