summaryrefslogtreecommitdiff
path: root/libs/canvas/test/optimizing_lookup_table.cc
blob: 6022ff3797aece2de7a3203ea726762d265e9e82 (plain)
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
#include "canvas/lookup_table.h"
#include "canvas/types.h"
#include "canvas/rectangle.h"
#include "canvas/group.h"
#include "canvas/canvas.h"
#include "optimizing_lookup_table.h"

using namespace std;
using namespace ArdourCanvas;

CPPUNIT_TEST_SUITE_REGISTRATION (OptimizingLookupTableTest);

void
OptimizingLookupTableTest::build_1 ()
{
	ImageCanvas canvas;
	Rectangle a (canvas.root(), Rect (0, 0, 32, 32));
	a.set_outline_width (0);
	Rectangle b (canvas.root(), Rect (0, 33, 32, 64));
	b.set_outline_width (0);
	Rectangle c (canvas.root(), Rect (33, 0, 64, 32));
	c.set_outline_width (0);
	Rectangle d (canvas.root(), Rect (33, 33, 64, 64));
	d.set_outline_width (0);
	OptimizingLookupTable table (*canvas.root(), 1);

	CPPUNIT_ASSERT (table._items_per_cell == 1);
	CPPUNIT_ASSERT (table._cell_size.x == 32);
	CPPUNIT_ASSERT (table._cell_size.y == 32);
	CPPUNIT_ASSERT (table._cells[0][0].front() == &a);
	CPPUNIT_ASSERT (table._cells[0][1].front() == &b);
	CPPUNIT_ASSERT (table._cells[1][0].front() == &c);
	CPPUNIT_ASSERT (table._cells[1][1].front() == &d);
}

void
OptimizingLookupTableTest::build_2 ()
{
	ImageCanvas canvas;
	Rectangle a (canvas.root(), Rect (0, 0, 713, 1024));
	a.set_outline_width (0);
	Rectangle b (canvas.root(), Rect (0, 0, 0, 1024));
	b.set_outline_width (0);
	OptimizingLookupTable table (*canvas.root(), 64);
}

void
OptimizingLookupTableTest::build_negative ()
{
	ImageCanvas canvas;
	Rectangle a (canvas.root(), Rect (-32, -32, 32, 32));
	OptimizingLookupTable table (*canvas.root(), 1);
}

void
OptimizingLookupTableTest::get_small ()
{
	ImageCanvas canvas;
	Rectangle a (canvas.root(), Rect (0, 0, 32, 32));
	a.set_outline_width (0);
	Rectangle b (canvas.root(), Rect (0, 33, 32, 64));
	b.set_outline_width (0);
	Rectangle c (canvas.root(), Rect (33, 0, 64, 32));
	c.set_outline_width (0);
	Rectangle d (canvas.root(), Rect (33, 33, 64, 64));
	d.set_outline_width (0);
	OptimizingLookupTable table (*canvas.root(), 1);

	vector<Item*> items = table.get (Rect (16, 16, 48, 48));
	CPPUNIT_ASSERT (items.size() == 4);

	items = table.get (Rect (32, 32, 33, 33));
	CPPUNIT_ASSERT (items.size() == 1);
}

void
OptimizingLookupTableTest::get_big ()
{
	ImageCanvas canvas;

	double const s = 8;
	int const N = 1024;

	for (int x = 0; x < N; ++x) {
		for (int y = 0; y < N; ++y) {
			Rectangle* r = new Rectangle (canvas.root());
			r->set_outline_width (0);
			r->set (Rect (x * s, y * s, (x + 1) * s, (y + 1) * s));
		}
	}

	OptimizingLookupTable table (*canvas.root(), 16);
	vector<Item*> items = table.get (Rect (0, 0, 15, 15));
	CPPUNIT_ASSERT (items.size() == 16);
}

/** Check that calling OptimizingLookupTable::get() returns things in the correct order.
 *  The order should be the same as it is in the owning group.
 */
void
OptimizingLookupTableTest::check_ordering ()
{
	ImageCanvas canvas;

	Rectangle a (canvas.root (), Rect (0, 0, 64, 64));
	Rectangle b (canvas.root (), Rect (0, 0, 64, 64));
	Rectangle c (canvas.root (), Rect (0, 0, 64, 64));

	/* since there have been bugs introduced due to sorting pointers,
	   get these rectangles in ascending order of their address
	*/

	list<Item*> items;
	items.push_back (&a);
	items.push_back (&b);
	items.push_back (&c);
	items.sort ();

	/* now arrange these items in the group in reverse order of address */

	for (list<Item*>::reverse_iterator i = items.rbegin(); i != items.rend(); ++i) {
		(*i)->raise_to_top ();
	}

	/* ask the LUT for the items */

	canvas.root()->ensure_lut ();
	vector<Item*> lut_items = canvas.root()->_lut->get (Rect (0, 0, 64, 64));
	CPPUNIT_ASSERT (lut_items.size() == 3);

	/* check that they are in the right order */

	vector<Item*>::iterator i = lut_items.begin ();
	list<Item*>::reverse_iterator j = items.rbegin ();

	while (i != lut_items.end ()) {
		CPPUNIT_ASSERT (*i == *j);
		++i;
		++j;
	}
}