Disable all code related to libXp
[oweals/cde.git] / cde / programs / dtinfo / mmdb / dti_cc / main.C
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 // $XConsortium: main.cc /main/3 1996/06/11 16:57:24 cde-hal $
24 #include <iostream>
25 using namespace std;
26 #include "Exceptions.hh"
27 #include "cc_exceptions.h"
28 #include "CC_Slist.h"
29 #include "CC_Dlist.h"
30 #include "CC_String.h"
31 #include "CC_Tokenizer.h"
32 #include "CC_Stack.h"
33
34 CC_Boolean mytestfunc(int *a, void *b)
35 {
36   return(*a == 20);
37 }
38
39 #define TEST(tcond)                                    \
40    cout << "Testing " << "(" << #tcond << ")\t";       \
41    if (tcond) {                                        \
42       cout << "Test #" << TestNum << " passed\n";      \
43       TestPassed++;                                    \
44    }                                                   \
45    else {                                              \
46       cout << "Test #" << TestNum << " failed\n";      \
47       TestFailed++;                                    \
48    }                                                   \
49    TestNum++;                                         
50
51
52 main()
53 {
54
55   INIT_EXCEPTIONS();
56
57   try {
58     int TestNum = 1;
59     int TestPassed = 0;
60     int TestFailed = 0;
61     
62     CC_TPtrSlist<int> slist;
63     int a =10;
64     int b =20;
65     int c =30;
66
67     // inserting 10, 20 , 30 
68     slist.insert(&a);
69     slist.insert(&b);
70     slist.insert(&c);
71
72     // testing first
73     TEST(*slist.first() == 10);
74     TEST(*slist.at(0) == 10);
75     TEST(*slist.at(1) == 20);
76     TEST(*slist.at(2) == 30);
77     TEST(slist.entries() == 3);
78
79     int d =40;
80
81     slist.prepend(&d);
82     slist.append(&d);
83   
84     TEST(*slist.first() == 40);
85     TEST(*slist.at(4) == 40);
86   
87     TEST(*slist.removeAt(2) == 20);
88     TEST(*slist.at(2) == 30);
89     TEST(slist.find(&a) != NULL);
90     TEST(slist.find(&b) == NULL);
91     TEST(slist.contains(&a) == 1);
92     TEST(slist.contains(&b) == 0);
93     TEST(slist.remove(&b) == NULL);
94     TEST(*slist.remove(&a) == 10);
95     TEST(slist.remove(&a) == NULL);
96     TEST(*slist.remove(&d) == 40);
97     TEST(*slist.remove(&d) == 40);
98     TEST(slist.remove(&d) == NULL);
99     
100     // Test slist copy constructor
101     CC_TPtrSlistIterator<int> slist_iter( slist );
102     CC_TPtrSlist<int> new_slist( slist );
103     CC_TPtrSlistIterator<int> new_slist_iter(new_slist);
104
105     TEST(slist.entries() == new_slist.entries());
106     for ( int w = 0; w < slist.entries(); w++ ) {
107       slist_iter();
108       new_slist_iter();
109       TEST(*slist_iter.key() == *new_slist_iter.key());
110     }
111     
112     // empty list
113     CC_TPtrSlist<int> empty_list;
114     TEST(empty_list.remove(&d) == NULL) ;
115     TEST(empty_list.find(&d) == NULL);
116     TEST(empty_list.removeFirst() == NULL);
117     TEST(empty_list.removeLast() == NULL);
118     
119     empty_list.clearAndDestroy();
120     
121     CC_TPtrDlist<int> dlist;
122     dlist.append(&a);
123     dlist.append(&b);
124     dlist.append(&c);
125     
126     TEST(*dlist.first() == 10 );
127     TEST(*dlist.at(1) == 20 );
128     TEST(*dlist.at(2) == 30);
129     
130     TEST(*dlist.last() == 30);
131     
132     dlist.prepend(&c);
133     TEST(*dlist.first() == 30);
134     
135     dlist.insert(&b);
136     TEST(*dlist.last() == 20);
137     
138     TEST(*dlist.removeFirst() == 30);
139     TEST(*dlist.removeLast() == 20);
140     TEST(*dlist.remove(&b) == 20);
141     TEST(*dlist.remove(&c) == 30);
142     dlist.insert(&b);
143     dlist.insert(&c);
144
145     
146     CC_TPtrDlistIterator<int> dlist_iter(dlist);
147     CC_TPtrDlist<int> new_dlist(dlist);
148     CC_TPtrDlistIterator<int> new_dlist_iter(new_dlist);
149
150     // Compare the elements in the 2 lists 
151     for ( int i = 0; i < dlist.entries(); i++ ) {
152       dlist_iter();
153       new_dlist_iter();
154       TEST(*dlist_iter.key() == *new_dlist_iter.key());
155     }
156
157     /* Traverse backward */
158     for ( int j = 0; j < dlist.entries(); j++ ) {
159       cout << *dlist_iter.key() << ", ";
160       --dlist_iter;
161     }
162
163     cout << endl;
164     
165     dlist_iter += 2;
166     TEST(*dlist_iter.key() == 20);
167
168     TEST(*dlist.find(mytestfunc, NULL) == 20 );
169
170     // dlist.clearAndDestroy();
171
172     CC_String cstr("abc");
173     TEST(cstr.isNull() == FALSE);
174     TEST(cstr.length() == 3);
175     TEST(cstr.compareTo("abc") == 0);
176     TEST(cstr.compareTo("def") < 0 );
177   
178     TEST(cstr.compareTo("ABC") != 0);
179     TEST(cstr.compareTo("ABC", CC_String::ignoreCase) == 0);
180     TEST(cstr.compareTo("ABC", CC_String::exact) != 0);
181   
182     CC_String dstr("abc");
183     TEST(cstr.compareTo(dstr) == 0);
184     TEST(cstr.compareTo(dstr, CC_String::ignoreCase) == 0);
185     TEST(cstr.compareTo(dstr.data()) == 0);
186     TEST(cstr.compareTo("abcde") != 0 );
187     TEST(cstr.compareTo("abcde", CC_String::ignoreCase) != 0 );
188
189     CC_TValSlist<int> vlist;
190     vlist.append(a);
191     vlist.append(b);
192     vlist.append(c);
193     
194     CC_TValSlistIterator<int> vlist_iter(vlist);
195     CC_TValSlist<int> new_vlist(vlist);
196     TEST(vlist.entries() == new_vlist.entries());
197     CC_TValSlistIterator<int> new_vlist_iter(new_vlist);
198
199     for ( int k = 0; k < vlist.entries(); k++ ) {
200       ++vlist_iter;
201       ++new_vlist_iter;
202       TEST(vlist_iter.key() == new_vlist_iter.key());
203     }
204
205     vlist_iter.reset();
206     
207     cout << endl;
208     
209     
210     
211
212     CC_String cc_str1("This is a pretty long string for testing");
213     CC_Tokenizer next1( cc_str1 );
214     
215     while ( next1() ) {
216       cout << next1.data() << endl;
217     }
218
219
220     char str[128];
221     snprintf(str, sizeof(str), "\t\nthis is \t    another string\t\n");
222     CC_String cc_str2( (const char *)str);
223     
224     CC_Tokenizer next2( cc_str2 );
225     while ( next2() ) {
226       cout << next2.data() << endl;
227     }
228
229     int *empty;
230     CC_TPtrDlist<int> tdlist;
231     // tdlist.insert(empty);
232     TEST(tdlist.removeFirst() == NULL);
233     TEST(tdlist.removeLast() == NULL);
234     tdlist.prepend(empty);
235     tdlist.insert(empty);
236     tdlist.removeFirst();
237     tdlist.removeLast();
238     TEST(tdlist.removeFirst() == NULL);
239     // TEST(0);
240
241
242     Stack<int> stack;
243     stack.push(2);
244     stack.push(3);
245     stack.push(4);
246
247     TEST(stack.top() == 4 );
248     TEST(stack.pop() == 4 );
249     TEST(stack.pop() == 3 );
250     TEST(stack.pop() == 2 );
251     stack.push(20);
252     TEST(stack.top() == 20);
253     TEST(stack.pop() == 20);
254     TEST(stack.empty());
255
256     cout << "test passed = " << TestPassed << endl;
257     cout << "test failed = " << TestFailed << endl;
258     
259     try {
260       TEST(stack.top());  // This will throw exception
261     }
262     catch( Exception&, u)
263       {
264         cerr << "Exception generated by top() were caught\n";
265       }end_try;
266
267     try {
268       TEST(stack.pop());
269     }
270     catch( Exception&, u )
271       {
272         cerr << "Exception generated by pop() were caught\n";
273         exit(0);
274       }end_try;
275
276   }
277   catch(ccException&, u)
278     {
279       cerr << "ccException caught\n";
280     }
281   catch(Exception&, u)
282     {
283       cout << "Exception generated by pop() caught\n";
284       exit(1);
285     }end_try;
286 }
287   
288
289   
290
291