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
|
/*
* Dillo Widget
*
* Copyright 2005-2007 Sebastian Geerken <sgeerken@dillo.org>
* Copyright 2025 Rodrigo Arias Mallo <rodarima@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "lout/object.hh"
#include "lout/container.hh"
using namespace lout::object;
using namespace lout::container::typed;
class ReverseComparator: public Comparator
{
private:
Comparator *reversed;
public:
ReverseComparator (Comparator *reversed) { this->reversed = reversed; }
int compare(Object *o1, Object *o2) { return - reversed->compare (o1, o2); }
};
void testHashSet ()
{
puts ("--- testHashSet ---");
HashSet<String> h(true);
h.put (new String ("one"));
h.put (new String ("two"));
h.put (new String ("three"));
char *p;
puts (p = h.toString());
dFree(p);
}
void testHashTable ()
{
puts ("--- testHashTable ---");
HashTable<String, Integer> h(true, true);
h.put (new String ("one"), new Integer (1));
h.put (new String ("two"), new Integer (2));
h.put (new String ("three"), new Integer (3));
char *p;
puts (p = h.toString());
dFree(p);
h.put (new String ("one"), new Integer (4));
h.put (new String ("two"), new Integer (5));
h.put (new String ("three"), new Integer (6));
puts (p = h.toString());
dFree(p);
}
void testVector1 ()
{
ReverseComparator reverse (&standardComparator);
puts ("--- testVector (1) ---");
Vector<String> v (true, 1);
char *p;
v.put (new String ("one"));
v.put (new String ("two"));
v.put (new String ("three"));
puts (p = v.toString());
dFree(p);
v.sort (&reverse);
puts (p = v.toString());
dFree(p);
v.sort ();
puts (p = v.toString());
dFree(p);
}
void testVector2 ()
{
puts ("--- testVector (2) ---");
Vector<String> v (1, true);
char *p;
v.insertSorted (new String ("one"));
puts (p = v.toString());
dFree(p);
v.insertSorted (new String ("two"));
puts (p = v.toString());
dFree(p);
v.insertSorted (new String ("three"));
puts (p = v.toString());
dFree(p);
v.insertSorted (new String ("five"));
puts (p = v.toString());
dFree(p);
v.insertSorted (new String ("six"));
puts (p = v.toString());
dFree(p);
v.insertSorted (new String ("four"));
puts (p = v.toString());
dFree(p);
for (int b = 0; b < 2; b++) {
bool mustExist = b;
printf ("mustExist = %s\n", mustExist ? "true" : "false");
String k ("alpha");
printf (" '%s' -> %d\n", k.chars(), v.bsearch (&k, mustExist));
for (Iterator<String> it = v.iterator(); it.hasNext(); ) {
String *k1 = it.getNext();
printf (" '%s' -> %d\n", k1->chars(), v.bsearch (k1, mustExist));
char buf[64];
strcpy (buf, k1->chars());
strcat (buf, "-var");
String k2 (buf);
printf (" '%s' -> %d\n", k2.chars(), v.bsearch (&k2, mustExist));
}
}
}
void testVector3 ()
{
// Regression test: resulted once incorrectly (0, 2, 3), should
// result in (1, 2, 3).
puts ("--- testVector (3) ---");
Vector<String> v (true, 1);
String k ("omega");
v.put (new String ("alpha"));
printf (" -> %d\n", v.bsearch (&k, false));
v.put (new String ("beta"));
printf (" -> %d\n", v.bsearch (&k, false));
v.put (new String ("gamma"));
printf (" -> %d\n", v.bsearch (&k, false));
}
void testStackAsQueue ()
{
puts ("--- testStackAsQueue ---");
Stack<Integer> s (true);
for (int i = 1; i <= 10; i++)
s.pushUnder (new Integer (i));
while (s.size () > 0) {
Integer *i = s.getTop ();
printf ("%d\n", i->getValue ());
s.pop ();
}
}
int main (int argc, char *argv[])
{
testHashSet ();
testHashTable ();
testVector1 ();
testVector2 ();
testVector3 ();
testStackAsQueue ();
return 0;
}
|