File: dirty.cpp

package info (click to toggle)
libcaca 0.99.beta18-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 6,936 kB
  • sloc: ansic: 22,177; python: 2,316; cs: 1,213; cpp: 1,107; java: 916; objc: 836; makefile: 636; sh: 381; ruby: 193; asm: 65
file content (290 lines) | stat: -rw-r--r-- 9,497 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
 *  caca-test     testsuite program for libcaca
 *  Copyright (c) 2009 Sam Hocevar <sam@hocevar.net>
 *                All Rights Reserved
 *
 *  This program is free software. It comes without any warranty, to
 *  the extent permitted by applicable law. You can redistribute it
 *  and/or modify it under the terms of the Do What The Fuck You Want
 *  To Public License, Version 2, as published by Sam Hocevar. See
 *  http://sam.zoy.org/wtfpl/COPYING for more details.
 */

#include "config.h"

#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/TestCaller.h>
#include <cppunit/TestCase.h>
#include <cppunit/TestSuite.h>

#include "caca.h"

class DirtyTest : public CppUnit::TestCase
{
    CPPUNIT_TEST_SUITE(DirtyTest);
    CPPUNIT_TEST(test_create);
    CPPUNIT_TEST(test_put_char_dirty);
    CPPUNIT_TEST(test_put_char_not_dirty);
    CPPUNIT_TEST(test_simplify);
    CPPUNIT_TEST(test_box);
    CPPUNIT_TEST(test_blit);
    CPPUNIT_TEST_SUITE_END();

public:
    DirtyTest() : CppUnit::TestCase("Dirty Rectangles Test") {}

    void setUp() {}

    void tearDown() {}

    void test_create()
    {
        caca_canvas_t *cv;
        int dx, dy, dw, dh;

        /* Check that we only have one dirty rectangle upon creation. */
        cv = caca_create_canvas(WIDTH, HEIGHT);
        CPPUNIT_ASSERT_EQUAL(1, caca_get_dirty_rect_count(cv));

        /* Check that our only rectangle contains the whole canvas. */
        caca_get_dirty_rect(cv, 0, &dx, &dy, &dw, &dh);
        CPPUNIT_ASSERT_EQUAL(0, dx);
        CPPUNIT_ASSERT_EQUAL(0, dy);
        CPPUNIT_ASSERT_EQUAL(WIDTH, dw);
        CPPUNIT_ASSERT_EQUAL(HEIGHT, dh);

        /* Invalidate the dirty rectangle and check that it stays so. */
        caca_clear_dirty_rect_list(cv);
        CPPUNIT_ASSERT_EQUAL(0, caca_get_dirty_rect_count(cv));

        caca_free_canvas(cv);
    }

    void test_put_char_dirty()
    {
        caca_canvas_t *cv;
        int dx, dy, dw, dh;

        cv = caca_create_canvas(WIDTH, HEIGHT);

        /* Check that one character creates a 1x1 dirty rect. */
        caca_clear_dirty_rect_list(cv);
        caca_put_char(cv, 7, 3, 'x');

        CPPUNIT_ASSERT_EQUAL(1, caca_get_dirty_rect_count(cv));
        caca_get_dirty_rect(cv, 0, &dx, &dy, &dw, &dh);
        CPPUNIT_ASSERT_EQUAL(7, dx);
        CPPUNIT_ASSERT_EQUAL(3, dy);
        CPPUNIT_ASSERT_EQUAL(1, dw);
        CPPUNIT_ASSERT_EQUAL(1, dh);

        /* Check that a fullwidth character creates a 2x1 dirty rect. */
        caca_clear_canvas(cv);
        caca_clear_dirty_rect_list(cv);
        caca_put_char(cv, 7, 3, 0x2f06 /* ⼆ */);

        CPPUNIT_ASSERT_EQUAL(1, caca_get_dirty_rect_count(cv));
        caca_get_dirty_rect(cv, 0, &dx, &dy, &dw, &dh);
        CPPUNIT_ASSERT_EQUAL(7, dx);
        CPPUNIT_ASSERT_EQUAL(3, dy);
        CPPUNIT_ASSERT_EQUAL(2, dw);
        CPPUNIT_ASSERT_EQUAL(1, dh);

        /* Check that a character over a fullwidth character creates a
         * 2x1 dirty rect because of clobbering on the left side. */
        caca_clear_canvas(cv);
        caca_put_char(cv, 7, 3, 0x2f06 /* ⼆ */);
        caca_clear_dirty_rect_list(cv);
        caca_put_char(cv, 7, 3, 'x');

        CPPUNIT_ASSERT_EQUAL(1, caca_get_dirty_rect_count(cv));
        caca_get_dirty_rect(cv, 0, &dx, &dy, &dw, &dh);
        CPPUNIT_ASSERT_EQUAL(7, dx);
        CPPUNIT_ASSERT_EQUAL(3, dy);
        CPPUNIT_ASSERT_EQUAL(2, dw);
        CPPUNIT_ASSERT_EQUAL(1, dh);

        /* Check that a character over a fullwidth character creates a
         * 2x1 dirty rect because of clobbering on the right side. */
        caca_clear_canvas(cv);
        caca_put_char(cv, 7, 3, 0x2f06 /* ⼆ */);
        caca_clear_dirty_rect_list(cv);
        caca_put_char(cv, 8, 3, 'x');

        CPPUNIT_ASSERT_EQUAL(1, caca_get_dirty_rect_count(cv));
        caca_get_dirty_rect(cv, 0, &dx, &dy, &dw, &dh);
        CPPUNIT_ASSERT_EQUAL(7, dx);
        CPPUNIT_ASSERT_EQUAL(3, dy);
        CPPUNIT_ASSERT_EQUAL(2, dw);
        CPPUNIT_ASSERT_EQUAL(1, dh);

        /* Check that a fullwidth character over a fullwidth character creates
         * a 3x1 dirty rect because of clobbering on the left side. */
        caca_clear_canvas(cv);
        caca_put_char(cv, 7, 3, 0x2f06 /* ⼆ */);
        caca_clear_dirty_rect_list(cv);
        caca_put_char(cv, 6, 3, 0x2f06 /* ⼆ */);

        CPPUNIT_ASSERT_EQUAL(1, caca_get_dirty_rect_count(cv));
        caca_get_dirty_rect(cv, 0, &dx, &dy, &dw, &dh);
        CPPUNIT_ASSERT_EQUAL(6, dx);
        CPPUNIT_ASSERT_EQUAL(3, dy);
        CPPUNIT_ASSERT_EQUAL(3, dw);
        CPPUNIT_ASSERT_EQUAL(1, dh);

        /* Check that a fullwidth character over a fullwidth character creates
         * a 3x1 dirty rect because of clobbering on the right side. */
        caca_clear_canvas(cv);
        caca_put_char(cv, 7, 3, 0x2f06 /* ⼆ */);
        caca_clear_dirty_rect_list(cv);
        caca_put_char(cv, 8, 3, 0x2f06 /* ⼆ */);

        CPPUNIT_ASSERT_EQUAL(1, caca_get_dirty_rect_count(cv));
        caca_get_dirty_rect(cv, 0, &dx, &dy, &dw, &dh);
        CPPUNIT_ASSERT_EQUAL(7, dx);
        CPPUNIT_ASSERT_EQUAL(3, dy);
        CPPUNIT_ASSERT_EQUAL(3, dw);
        CPPUNIT_ASSERT_EQUAL(1, dh);
    }

    void test_put_char_not_dirty()
    {
        caca_canvas_t *cv;

        cv = caca_create_canvas(WIDTH, HEIGHT);

        /* Check that pasting the same character does not cause a dirty
         * rectangle to be created. */
        caca_put_char(cv, 7, 3, 'x');
        caca_clear_dirty_rect_list(cv);
        caca_put_char(cv, 7, 3, 'x');

        CPPUNIT_ASSERT_EQUAL(0, caca_get_dirty_rect_count(cv));

        /* Check that pasting the same fullwidth character does not cause
         * a dirty rectangle to be created. */
        caca_clear_canvas(cv);
        caca_put_char(cv, 7, 3, 0x2f06 /* ⼆ */);
        caca_clear_dirty_rect_list(cv);
        caca_put_char(cv, 7, 3, 0x2f06 /* ⼆ */);

        CPPUNIT_ASSERT_EQUAL(0, caca_get_dirty_rect_count(cv));
    }

    void test_simplify()
    {
        caca_canvas_t *cv;
        int dx, dy, dw, dh;

        cv = caca_create_canvas(WIDTH, HEIGHT);

        /* Check that N adjacent blits only create one dirty rectangle */
        caca_clear_dirty_rect_list(cv);
        for(int i = 0; i < 10; i++)
        {
            caca_put_char(cv, 7 + i, 3, '-');

            CPPUNIT_ASSERT_EQUAL(1, caca_get_dirty_rect_count(cv));
            caca_get_dirty_rect(cv, 0, &dx, &dy, &dw, &dh);
            CPPUNIT_ASSERT_EQUAL(7, dx);
            CPPUNIT_ASSERT_EQUAL(3, dy);
            CPPUNIT_ASSERT_EQUAL(1 + i, dw);
            CPPUNIT_ASSERT_EQUAL(1, dh);
        }

        /* Check that N adjacent blits only create one dirty rectangle */
        caca_clear_dirty_rect_list(cv);
        for(int j = 0; j < 10; j++)
        {
            caca_put_char(cv, 7, 3 + j, '|');

            CPPUNIT_ASSERT_EQUAL(1, caca_get_dirty_rect_count(cv));
            caca_get_dirty_rect(cv, 0, &dx, &dy, &dw, &dh);
            CPPUNIT_ASSERT_EQUAL(7, dx);
            CPPUNIT_ASSERT_EQUAL(3, dy);
            CPPUNIT_ASSERT_EQUAL(1, dw);
            CPPUNIT_ASSERT_EQUAL(1 + j, dh);
        }
    }

    void test_box()
    {
        caca_canvas_t *cv;
        int dx, dy, dw, dh;

        cv = caca_create_canvas(WIDTH, HEIGHT);
        caca_clear_dirty_rect_list(cv);

        /* Check that a filled box creates one dirty rectangle of the same
         * size. */
        caca_fill_box(cv, 7, 3, 14, 9, 'x');

        CPPUNIT_ASSERT_EQUAL(1, caca_get_dirty_rect_count(cv));
        caca_get_dirty_rect(cv, 0, &dx, &dy, &dw, &dh);
        CPPUNIT_ASSERT_EQUAL(7, dx);
        CPPUNIT_ASSERT_EQUAL(3, dy);
        CPPUNIT_ASSERT_EQUAL(14, dw);
        CPPUNIT_ASSERT_EQUAL(9, dh);

        /* Check that the same filled box does not create a new dirty
         * rectangle. */
        caca_clear_dirty_rect_list(cv);
        caca_fill_box(cv, 7, 3, 14, 9, 'x');

        CPPUNIT_ASSERT_EQUAL(0, caca_get_dirty_rect_count(cv));
    }

    void test_blit()
    {
        caca_canvas_t *cv, *cv2;
        int i, dx, dy, dw, dh;

        cv = caca_create_canvas(WIDTH, HEIGHT);
        caca_clear_dirty_rect_list(cv);
        cv2 = caca_create_canvas(2, 2);
        caca_fill_box(cv2, 0, 0, 2, 1, 'x');

        /* Check that blitting a canvas make a dirty rectangle
         * only for modified lines */
        /* FIXME: check this test's validity */

        caca_blit(cv, 1, 1, cv2, NULL);
        i = caca_get_dirty_rect_count(cv);
        CPPUNIT_ASSERT_EQUAL(1, i);
        caca_get_dirty_rect(cv, 0, &dx, &dy, &dw, &dh);

        CPPUNIT_ASSERT(1 == dx);
        CPPUNIT_ASSERT(1 == dy);
        CPPUNIT_ASSERT(2 <= dw);
        CPPUNIT_ASSERT(1 == dh);

        caca_clear_canvas(cv);
        caca_clear_dirty_rect_list(cv);

        /* Check that blitting a canvas make a dirty rectangle
         * only for modified chars when we have a mask */
        /* FIXME: check this test's validity */

        caca_blit(cv, 1, 1, cv2, cv2);
        i = caca_get_dirty_rect_count(cv);
        CPPUNIT_ASSERT_EQUAL(1, i);
        caca_get_dirty_rect(cv, 0, &dx, &dy, &dw, &dh);

        CPPUNIT_ASSERT(1 == dx);
        CPPUNIT_ASSERT(1 == dy);
        CPPUNIT_ASSERT(2 == dw);
        CPPUNIT_ASSERT(1 == dh);

        CPPUNIT_ASSERT(' ' == caca_get_char(cv, 0, 0));

    }

private:
    static int const WIDTH, HEIGHT;
};

int const DirtyTest::WIDTH = 80;
int const DirtyTest::HEIGHT = 50;

CPPUNIT_TEST_SUITE_REGISTRATION(DirtyTest);