source: libcaca/trunk/tests/dirty.cpp @ 3567

Last change on this file since 3567 was 3567, checked in by Sam Hocevar, 11 years ago

Add explanatory comments to the dirty rectangle unit tests.

  • Property svn:keywords set to Id
File size: 8.1 KB
Line 
1/*
2 *  caca-test     testsuite program for libcaca
3 *  Copyright (c) 2009 Sam Hocevar <sam@hocevar.net>
4 *                All Rights Reserved
5 *
6 *  $Id: dirty.cpp 3567 2009-07-24 06:48:27Z sam $
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
15#include "config.h"
16
17#include <cppunit/extensions/HelperMacros.h>
18#include <cppunit/TestCaller.h>
19#include <cppunit/TestCase.h>
20#include <cppunit/TestSuite.h>
21
22#include "caca.h"
23
24class DirtyTest : public CppUnit::TestCase
25{
26    CPPUNIT_TEST_SUITE(DirtyTest);
27    CPPUNIT_TEST(test_create);
28    CPPUNIT_TEST(test_put_char_dirty);
29    CPPUNIT_TEST(test_put_char_not_dirty);
30    CPPUNIT_TEST(test_box);
31    CPPUNIT_TEST(test_blit);
32    CPPUNIT_TEST_SUITE_END();
33
34public:
35    DirtyTest() : CppUnit::TestCase("Dirty Rectangles Test") {}
36
37    void setUp() {}
38
39    void tearDown() {}
40
41    void test_create()
42    {
43        caca_canvas_t *cv;
44        int dx, dy, dw, dh;
45
46        /* Check that we only have one dirty rectangle upon creation. */
47        cv = caca_create_canvas(WIDTH, HEIGHT);
48        CPPUNIT_ASSERT_EQUAL(1, caca_get_dirty_rect_count(cv));
49
50        /* Check that our only rectangle contains the whole canvas. */
51        caca_get_dirty_rect(cv, 0, &dx, &dy, &dw, &dh);
52        CPPUNIT_ASSERT_EQUAL(0, dx);
53        CPPUNIT_ASSERT_EQUAL(0, dy);
54        CPPUNIT_ASSERT_EQUAL(WIDTH, dw);
55        CPPUNIT_ASSERT_EQUAL(HEIGHT, dh);
56
57        /* Invalidate the dirty rectangle and check that it stays so. */
58        caca_clear_dirty_rect_list(cv);
59        CPPUNIT_ASSERT_EQUAL(0, caca_get_dirty_rect_count(cv));
60
61        caca_free_canvas(cv);
62    }
63
64    void test_put_char_dirty()
65    {
66        caca_canvas_t *cv;
67        int dx, dy, dw, dh;
68
69        cv = caca_create_canvas(WIDTH, HEIGHT);
70
71        /* Check that one character creates a 1x1 dirty rect. */
72        caca_clear_dirty_rect_list(cv);
73        caca_put_char(cv, 7, 3, 'x');
74
75        CPPUNIT_ASSERT_EQUAL(1, caca_get_dirty_rect_count(cv));
76        caca_get_dirty_rect(cv, 0, &dx, &dy, &dw, &dh);
77        CPPUNIT_ASSERT_EQUAL(7, dx);
78        CPPUNIT_ASSERT_EQUAL(3, dy);
79        CPPUNIT_ASSERT_EQUAL(1, dw);
80        CPPUNIT_ASSERT_EQUAL(1, dh);
81
82        /* Check that a fullwidth character creates a 2x1 dirty rect. */
83        caca_clear_canvas(cv);
84        caca_clear_dirty_rect_list(cv);
85        caca_put_char(cv, 7, 3, 0x2f06 /* ⼆ */);
86
87        CPPUNIT_ASSERT_EQUAL(1, caca_get_dirty_rect_count(cv));
88        caca_get_dirty_rect(cv, 0, &dx, &dy, &dw, &dh);
89        CPPUNIT_ASSERT_EQUAL(7, dx);
90        CPPUNIT_ASSERT_EQUAL(3, dy);
91        CPPUNIT_ASSERT_EQUAL(2, dw);
92        CPPUNIT_ASSERT_EQUAL(1, dh);
93
94        /* Check that a character over a fullwidth character creates a
95         * 2x1 dirty rect because of clobbering on the left side. */
96        caca_clear_canvas(cv);
97        caca_put_char(cv, 7, 3, 0x2f06 /* ⼆ */);
98        caca_clear_dirty_rect_list(cv);
99        caca_put_char(cv, 7, 3, 'x');
100
101        CPPUNIT_ASSERT_EQUAL(1, caca_get_dirty_rect_count(cv));
102        caca_get_dirty_rect(cv, 0, &dx, &dy, &dw, &dh);
103        CPPUNIT_ASSERT_EQUAL(7, dx);
104        CPPUNIT_ASSERT_EQUAL(3, dy);
105        CPPUNIT_ASSERT_EQUAL(2, dw);
106        CPPUNIT_ASSERT_EQUAL(1, dh);
107
108        /* Check that a character over a fullwidth character creates a
109         * 2x1 dirty rect because of clobbering on the right side. */
110        caca_clear_canvas(cv);
111        caca_put_char(cv, 7, 3, 0x2f06 /* ⼆ */);
112        caca_clear_dirty_rect_list(cv);
113        caca_put_char(cv, 8, 3, 'x');
114
115        CPPUNIT_ASSERT_EQUAL(1, caca_get_dirty_rect_count(cv));
116        caca_get_dirty_rect(cv, 0, &dx, &dy, &dw, &dh);
117        CPPUNIT_ASSERT_EQUAL(7, dx);
118        CPPUNIT_ASSERT_EQUAL(3, dy);
119        CPPUNIT_ASSERT_EQUAL(2, dw);
120        CPPUNIT_ASSERT_EQUAL(1, dh);
121
122        /* Check that a fullwidth character over a fullwidth character creates
123         * a 3x1 dirty rect because of clobbering on the left side. */
124        caca_clear_canvas(cv);
125        caca_put_char(cv, 7, 3, 0x2f06 /* ⼆ */);
126        caca_clear_dirty_rect_list(cv);
127        caca_put_char(cv, 6, 3, 0x2f06 /* ⼆ */);
128
129        CPPUNIT_ASSERT_EQUAL(1, caca_get_dirty_rect_count(cv));
130        caca_get_dirty_rect(cv, 0, &dx, &dy, &dw, &dh);
131        CPPUNIT_ASSERT_EQUAL(6, dx);
132        CPPUNIT_ASSERT_EQUAL(3, dy);
133        CPPUNIT_ASSERT_EQUAL(3, dw);
134        CPPUNIT_ASSERT_EQUAL(1, dh);
135
136        /* Check that a fullwidth character over a fullwidth character creates
137         * a 3x1 dirty rect because of clobbering on the right side. */
138        caca_clear_canvas(cv);
139        caca_put_char(cv, 7, 3, 0x2f06 /* ⼆ */);
140        caca_clear_dirty_rect_list(cv);
141        caca_put_char(cv, 8, 3, 0x2f06 /* ⼆ */);
142
143        CPPUNIT_ASSERT_EQUAL(1, caca_get_dirty_rect_count(cv));
144        caca_get_dirty_rect(cv, 0, &dx, &dy, &dw, &dh);
145        CPPUNIT_ASSERT_EQUAL(7, dx);
146        CPPUNIT_ASSERT_EQUAL(3, dy);
147        CPPUNIT_ASSERT_EQUAL(3, dw);
148        CPPUNIT_ASSERT_EQUAL(1, dh);
149    }
150
151    void test_put_char_not_dirty()
152    {
153        caca_canvas_t *cv;
154
155        cv = caca_create_canvas(WIDTH, HEIGHT);
156
157        /* Check that pasting the same character does not cause a dirty
158         * rectangle to be created. */
159        caca_put_char(cv, 7, 3, 'x');
160        caca_clear_dirty_rect_list(cv);
161        caca_put_char(cv, 7, 3, 'x');
162
163        CPPUNIT_ASSERT_EQUAL(0, caca_get_dirty_rect_count(cv));
164
165        /* Check that pasting the same fullwidth character does not cause
166         * a dirty rectangle to be created. */
167        caca_clear_canvas(cv);
168        caca_put_char(cv, 7, 3, 0x2f06 /* ⼆ */);
169        caca_clear_dirty_rect_list(cv);
170        caca_put_char(cv, 7, 3, 0x2f06 /* ⼆ */);
171
172        CPPUNIT_ASSERT_EQUAL(0, caca_get_dirty_rect_count(cv));
173    }
174
175    void test_box()
176    {
177        caca_canvas_t *cv;
178        int dx, dy, dw, dh;
179
180        cv = caca_create_canvas(WIDTH, HEIGHT);
181        caca_clear_dirty_rect_list(cv);
182
183        /* Check that a filled box creates one dirty rectangle of the same
184         * size. */
185        caca_fill_box(cv, 7, 3, 14, 9, 'x');
186
187        CPPUNIT_ASSERT_EQUAL(1, caca_get_dirty_rect_count(cv));
188        caca_get_dirty_rect(cv, 0, &dx, &dy, &dw, &dh);
189        CPPUNIT_ASSERT_EQUAL(7, dx);
190        CPPUNIT_ASSERT_EQUAL(3, dy);
191        CPPUNIT_ASSERT_EQUAL(14, dw);
192        CPPUNIT_ASSERT_EQUAL(9, dh);
193
194        /* Check that the same filled box does not create a new dirty
195         * rectangle. */
196        caca_clear_dirty_rect_list(cv);
197        caca_fill_box(cv, 7, 3, 14, 9, 'x');
198
199        CPPUNIT_ASSERT_EQUAL(0, caca_get_dirty_rect_count(cv));
200    }
201
202    void test_blit()
203    {
204        caca_canvas_t *cv, *cv2;
205        int i, dx, dy, dw, dh;
206
207        cv = caca_create_canvas(WIDTH, HEIGHT);
208        caca_clear_dirty_rect_list(cv);
209        cv2 = caca_create_canvas(2, 2);
210        caca_fill_box(cv2, 0, 0, 2, 1, 'x');
211
212        /* Check that blitting a canvas make a dirty rectangle
213         * only for modified lines */
214        /* FIXME: check this test's validity */
215
216        caca_blit(cv, 1, 1, cv2, NULL);
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
221        CPPUNIT_ASSERT(1 == dx);
222        CPPUNIT_ASSERT(1 == dy);
223        CPPUNIT_ASSERT(2 <= dw);
224        CPPUNIT_ASSERT(1 == dh);
225
226        caca_clear_canvas(cv);
227        caca_clear_dirty_rect_list(cv);
228
229        /* Check that blitting a canvas make a dirty rectangle
230         * only for modified chars when we have a mask */
231        /* FIXME: check this test's validity */
232
233        caca_blit(cv, 1, 1, cv2, cv2);
234        i = caca_get_dirty_rect_count(cv);
235        CPPUNIT_ASSERT_EQUAL(1, i);
236        caca_get_dirty_rect(cv, 0, &dx, &dy, &dw, &dh);
237
238        CPPUNIT_ASSERT(1 == dx);
239        CPPUNIT_ASSERT(1 == dy);
240        CPPUNIT_ASSERT(2 == dw);
241        CPPUNIT_ASSERT(1 == dh);
242
243        CPPUNIT_ASSERT(' ' == caca_get_char(cv, 0, 0));
244
245    }
246
247private:
248    static int const WIDTH, HEIGHT;
249};
250
251int const DirtyTest::WIDTH = 80;
252int const DirtyTest::HEIGHT = 50;
253
254CPPUNIT_TEST_SUITE_REGISTRATION(DirtyTest);
255
Note: See TracBrowser for help on using the repository browser.