fix merge with master
[ardour.git] / libs / canvas / test / optimizing_lookup_table.cc
1 #include "canvas/lookup_table.h"
2 #include "canvas/types.h"
3 #include "canvas/rectangle.h"
4 #include "canvas/group.h"
5 #include "canvas/canvas.h"
6 #include "optimizing_lookup_table.h"
7
8 using namespace std;
9 using namespace ArdourCanvas;
10
11 CPPUNIT_TEST_SUITE_REGISTRATION (OptimizingLookupTableTest);
12
13 void
14 OptimizingLookupTableTest::build_1 ()
15 {
16         ImageCanvas canvas;
17         Rectangle a (canvas.root(), Rect (0, 0, 32, 32));
18         a.set_outline_width (0);
19         Rectangle b (canvas.root(), Rect (0, 33, 32, 64));
20         b.set_outline_width (0);
21         Rectangle c (canvas.root(), Rect (33, 0, 64, 32));
22         c.set_outline_width (0);
23         Rectangle d (canvas.root(), Rect (33, 33, 64, 64));
24         d.set_outline_width (0);
25         OptimizingLookupTable table (*canvas.root(), 1);
26         
27         CPPUNIT_ASSERT (table._items_per_cell == 1);
28         CPPUNIT_ASSERT (table._cell_size.x == 32);
29         CPPUNIT_ASSERT (table._cell_size.y == 32);
30         CPPUNIT_ASSERT (table._cells[0][0].front() == &a);
31         CPPUNIT_ASSERT (table._cells[0][1].front() == &b);
32         CPPUNIT_ASSERT (table._cells[1][0].front() == &c);
33         CPPUNIT_ASSERT (table._cells[1][1].front() == &d);
34 }
35
36 void
37 OptimizingLookupTableTest::build_2 ()
38 {
39         ImageCanvas canvas;
40         Rectangle a (canvas.root(), Rect (0, 0, 713, 1024));
41         a.set_outline_width (0);
42         Rectangle b (canvas.root(), Rect (0, 0, 0, 1024));
43         b.set_outline_width (0);
44         OptimizingLookupTable table (*canvas.root(), 64);
45 }
46
47 void
48 OptimizingLookupTableTest::build_negative ()
49 {
50         ImageCanvas canvas;
51         Rectangle a (canvas.root(), Rect (-32, -32, 32, 32));
52         OptimizingLookupTable table (*canvas.root(), 1);
53 }
54
55 void
56 OptimizingLookupTableTest::get_small ()
57 {
58         ImageCanvas canvas;
59         Rectangle a (canvas.root(), Rect (0, 0, 32, 32));
60         a.set_outline_width (0);
61         Rectangle b (canvas.root(), Rect (0, 33, 32, 64));
62         b.set_outline_width (0);
63         Rectangle c (canvas.root(), Rect (33, 0, 64, 32));
64         c.set_outline_width (0);
65         Rectangle d (canvas.root(), Rect (33, 33, 64, 64));
66         d.set_outline_width (0);
67         OptimizingLookupTable table (*canvas.root(), 1);
68         
69         vector<Item*> items = table.get (Rect (16, 16, 48, 48));
70         CPPUNIT_ASSERT (items.size() == 4);
71         
72         items = table.get (Rect (32, 32, 33, 33));
73         CPPUNIT_ASSERT (items.size() == 1);
74 }
75
76 void
77 OptimizingLookupTableTest::get_big ()
78 {
79         ImageCanvas canvas;
80
81         double const s = 8;
82         int const N = 1024;
83         
84         for (int x = 0; x < N; ++x) {
85                 for (int y = 0; y < N; ++y) {
86                         Rectangle* r = new Rectangle (canvas.root());
87                         r->set_outline_width (0);
88                         r->set (Rect (x * s, y * s, (x + 1) * s, (y + 1) * s));
89                 }
90         }
91
92         OptimizingLookupTable table (*canvas.root(), 16);
93         vector<Item*> items = table.get (Rect (0, 0, 15, 15));
94         CPPUNIT_ASSERT (items.size() == 16);
95 }
96
97 /** Check that calling OptimizingLookupTable::get() returns things in the correct order.
98  *  The order should be the same as it is in the owning group.
99  */
100 void
101 OptimizingLookupTableTest::check_ordering ()
102 {
103         ImageCanvas canvas;
104
105         Rectangle a (canvas.root (), Rect (0, 0, 64, 64));
106         Rectangle b (canvas.root (), Rect (0, 0, 64, 64));
107         Rectangle c (canvas.root (), Rect (0, 0, 64, 64));
108
109         /* since there have been bugs introduced due to sorting pointers,
110            get these rectangles in ascending order of their address
111         */
112
113         list<Item*> items;
114         items.push_back (&a);
115         items.push_back (&b);
116         items.push_back (&c);
117         items.sort ();
118
119         /* now arrange these items in the group in reverse order of address */
120
121         for (list<Item*>::reverse_iterator i = items.rbegin(); i != items.rend(); ++i) {
122                 (*i)->raise_to_top ();
123         }
124
125         /* ask the LUT for the items */
126
127         canvas.root()->ensure_lut ();
128         vector<Item*> lut_items = canvas.root()->_lut->get (Rect (0, 0, 64, 64));
129         CPPUNIT_ASSERT (lut_items.size() == 3);
130
131         /* check that they are in the right order */
132
133         vector<Item*>::iterator i = lut_items.begin ();
134         list<Item*>::reverse_iterator j = items.rbegin ();
135
136         while (i != lut_items.end ()) {
137                 CPPUNIT_ASSERT (*i == *j);
138                 ++i;
139                 ++j;
140         }
141 }