Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dtinfo / DtMmdb / oliasdb / mark_test.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: mark_test.cc /main/5 1996/07/18 16:03:09 drk $ */
24
25 #ifdef REGRESSION_TEST
26
27 #include "mark_test.h"
28
29 void print_mark_list(mark_base* bptr, char* locator)
30 {
31    oid_list_handler* x = bptr -> get_mark_list(locator);
32
33    if ( x == 0 ) {
34      MESSAGE(cerr, "empty oid_list");
35      return ;
36    }
37
38    int ind = (*x) -> first();
39
40    while (ind) {
41
42       oid_t id = (*x) -> operator()(ind);
43
44       if ( id.eq(ground) == false ) {
45          mark_smart_ptr y(bptr, id);
46          debug(cerr, y);
47       }
48
49       (*x) -> next(ind);
50    }
51 }
52
53 mark_base* get_mark_base(char* path, char* name)
54 {
55    mark_base* ub = 0;
56
57    try {
58      ub = new mark_base(
59                              path,
60                              name,
61                              "this marks",
62                              user_base::WRITE
63                         );
64    }
65
66    catch (mmdbException &,e)
67    {
68        return 0;
69    } end_try;
70
71             
72    if ( ub -> open_status() != user_base::SUCC ) {
73        return 0;
74    }
75
76    return ub;
77 }
78
79             
80 void _get_id(istream& in, oid_t& x)
81 {
82    static char buf[LBUFSIZ];
83    in.getline(buf, LBUFSIZ);
84
85    if ( buf[0] != 'k' || buf[1] != '\t' ) {
86        debug(cerr, buf);
87        throw(stringException("_get_id(): missing k\t"));
88    }
89
90     char* key = buf+2;
91
92     oid_t id(key, true, false);
93
94     x = id;
95 }
96
97 void _get_key(istream& in, char*& key)
98 {
99    static char buf[LBUFSIZ];
100    in.getline(buf, LBUFSIZ);
101
102    if ( buf[0] != 'k' || buf[1] != '\t' ) {
103        debug(cerr, buf);
104      throw(stringException("_get_key(): missing k\t"));
105    }
106
107    key = buf + 2;
108 }
109
110 void _get_data(istream& in, char*& data)
111 {
112    static char buf[LBUFSIZ];
113    in.getline(buf, LBUFSIZ);
114
115    if ( buf[0] != 'd' || buf[1] != '\t' ) {
116        debug(cerr, buf);
117      throw(stringException("_get_data(): missing d\t"));
118    }
119
120    data = buf + 2;
121 }
122
123 void _get_key_pos(istream& in, char*& key, int& n)
124 {
125    static char buf[LBUFSIZ];
126    in.getline(buf, LBUFSIZ);
127
128    if ( buf[0] != 'k' || buf[1] != '\t' ) {
129        debug(cerr, buf);
130      throw(stringException("_get_key_pos(): missing k\t"));
131    }
132
133    key = buf + 2;
134
135    char* nextToken = strchr(key, '\t');
136
137    if ( nextToken == 0 )
138      throw(stringException("can't find position (integer)."));
139
140    *nextToken = 0;
141
142    n = atoi(nextToken+1);
143 }
144
145
146 oid_t get_nth_mark_id(mark_base* ub, char* key, int n)
147 {
148    oid_list_handler* handle = ub -> get_mark_list(key);
149    
150    if ( handle == 0 ) {
151      throw(stringException("empty mark list"));
152    }
153
154    int ind = (*handle) -> first();
155
156    int i = 0;
157
158    while (ind) {
159
160       if ( n == i ) {
161         oid_t id = (*handle) -> operator()(ind);
162         delete handle;
163         return id;
164       }
165
166       (*handle) -> next(ind);
167       i++;
168    }
169    
170    int count = (*handle)->count();
171    delete handle;
172    throw(boundaryException(0, count, i));
173 }
174
175
176 //////////////
177 // create
178 // k \t locator
179 // d \t data
180 //////////////
181 int _create_subtest(char* buf, istream& in, mark_base* ub) 
182 {
183     char* key, *data;
184     mark_smart_ptr* x = 0;
185
186     if ( strcmp(buf, "create") == 0 ) {
187                    
188        cout << ">>> create:" << endl;
189
190        _get_key(in, key);
191        _get_data(in, data);
192
193        cout << "---- new mark smtart_ptr ..." << endl;
194        x = new mark_smart_ptr(ub, key);
195
196        cout << "---- updating ..." << endl;
197        x -> update_usermark(data, strlen(data));
198        delete x;
199        cout << "<<< create:" << endl;
200    } else
201       return 1;
202 }
203       
204   
205 //////////////
206 // get_by_key 
207 // k \t locator \t n
208 //////////////
209 _get_by_key_subtest(char* buf, istream& in, mark_base* ub)
210 {
211    char* key;
212    int n;
213    mark_smart_ptr* x = 0;
214
215    if ( strcmp(buf, "get_by_key") == 0 ) {
216       cout << ">>> get_by_key:" << endl;
217                    
218       _get_key_pos(in, key, n);
219
220       oid_t id = get_nth_mark_id(ub, key, n);
221
222       x = new mark_smart_ptr(ub, id);
223                       
224       cout << x->node_locator();
225       cout << "\n";
226       cout << *(x->mark_value());
227       cout << "\n";
228                       
229       delete x;
230                
231       cout << "<<< get_by_key:" << endl;
232    } else return 1;
233 }
234
235 //////////////
236 // get_by_oid
237 // k \t oidInAscii
238 //////////////
239 _get_by_oid_subtest(char* buf, istream& in, mark_base* ub)
240 {
241    char* key;
242    mark_smart_ptr* x = 0;
243    if ( strcmp(buf, "get_by_oid") == 0 ) {
244                       
245       _get_key(in, key);
246
247       oid_t id(key, true, false);
248                       
249       x = new mark_smart_ptr(ub, id);
250                       
251       cout << x->node_locator();
252       cout << "\n";
253       cout << *(x->mark_value());
254       cout << "\n";
255                       
256       delete x;
257                
258    } else 
259       return 1;
260 }
261
262 //////////////
263 // show_for_a_locator
264 // k \t locator
265 //////////////
266 _show_for_a_locator_subtest(char* buf, istream& in, mark_base* ub)
267 {
268    char* key;
269    if ( strcmp(buf, "show_for_a_locator") == 0 ) {
270                       
271       cout << ">>> show_for_a_locator:" << endl;
272
273       _get_key(in, key);
274
275       oid_list_handler* z = ub-> get_mark_list(key);
276
277       if ( z == 0 ) 
278          throw(stringException("empty oid_list"));
279                    
280                       
281       int ind = (*z) -> first();
282                    
283       while (ind) {
284                    
285          oid_t id = (*z) -> operator()(ind);
286                    
287                          
288          if ( id.eq(ground) == false ) {
289                             
290             mark_smart_ptr y(ub, id);
291             cout << y.its_oid();
292             cout << "\n";
293             cout << y.node_locator();
294             cout << "\n";
295             cout << *(y.mark_value());
296             cout << "\n";
297                          
298          }
299                    
300          (*z) -> next(ind);
301       }
302
303       delete z;
304
305       cout << "<<< show_for_a_locator" << endl;
306                 
307    } else return 1;
308 }
309
310 //////////////
311 //show_all_marks_subtest 
312 //////////////
313 _show_all_marks_subtest(char* buf, istream& in, mark_base* ub)
314 {
315    if ( strcmp(buf, "show_all_marks") == 0 ) {
316       cout << ">>> show_all_marks" << endl;
317
318       mmdb_pos_t ind = ub -> first();
319       while (ind) {
320          oid_t id = ub -> get_mark_oid(ind);
321
322          if ( id.eq(ground) == false ) {
323                             
324             mark_smart_ptr y(ub, id);
325             cout << y.its_oid();
326             cout << "\n";
327             cout << y.node_locator();
328             cout << "\n";
329             cout << *(y.mark_value());
330             cout << "\n";
331                          
332          }
333
334          ub -> next(ind);
335       }
336       cout << "<<< show_all_marks." << endl;
337    } else
338       return 1;
339 }
340
341 //////////////////////////////////////
342 //////////////////////////////////////
343 int _update_by_id_subtest(istream& in, oid_t id, mark_base* ub)
344 {
345    char* data;
346    mark_smart_ptr* x = 0;
347    _get_data(in, data);
348
349     x = new mark_smart_ptr(ub, id);
350                       
351     cout << x -> its_oid() << "\n";
352
353     x -> update_usermark(data, strlen(data));
354     delete x;
355
356     x = new mark_smart_ptr(ub, id);
357                       
358     pstring* y = x->mark_value();
359                       
360     if ( y -> size() != strlen(data) || strcmp(y -> get(), data) != 0 ) 
361     {
362       debug(cerr, y->size());
363       debug(cerr, (long)strlen(data));
364       debug(cerr, y -> get());
365       debug(cerr, data);
366       throw(stringException("improperly updated mark"));
367                       
368     }
369                       
370     delete x;
371
372     return 0;
373 }
374
375 //////////////
376 // update_by_key 
377 // k \t locator \t n
378 // d \t data
379 //////////////
380 _update_by_key_subtest(char* buf, istream& in, mark_base* ub)
381 {
382    char* key;
383    int n;
384
385    if ( strcmp(buf, "update_by_key") == 0 ) {
386
387       cout << ">>> update_by_key" << endl;
388
389       _get_key_pos(in, key, n);
390
391       oid_t id = get_nth_mark_id(ub, key, n);
392
393       _update_by_id_subtest(in, id, ub);
394
395       cout << "<<< update_by_key" << endl;
396       return 0;
397
398    } else 
399       return 1;
400 }
401   
402 ////////////////////
403 // update_by_id 
404 // k \t oid_tInAscii
405 // d \t data
406 ////////////////////
407
408 _update_by_id_subtest(char* buf, istream& in, mark_base* ub)
409 {
410     if ( strcmp(buf, "update_by_id") == 0 ) {
411
412        cout << ">>> _update_by_id_subtest" << endl;
413
414        oid_t id;
415
416        _get_id(in, id);
417
418        _update_by_id_subtest(in, id, ub);
419
420        cout << "<<< _update_by_id_subtest" << endl;
421        return 0;
422
423      } else
424        return 1; 
425 }
426
427
428 //////////////
429 // delete_by_key
430 // k \t locator \t n
431 //////////////
432 _delete_by_key_subtest(char* buf, istream& in, mark_base* ub)
433 {
434    mark_smart_ptr* x = 0;
435    char* key; int n;
436    if ( strcmp(buf, "delete_by_key") == 0 ) {
437
438       cout << ">>> _delete_by_key_subtest" << endl;
439
440       _get_key_pos(in, key, n);
441
442       oid_t id = get_nth_mark_id(ub, key, n);
443
444       x = new mark_smart_ptr(ub, id);
445
446       x -> remove_from_db();
447                    
448       delete x;
449
450       cout << "<<< _delete_by_key_subtest" << endl;
451         
452     } else return 1;
453 }
454
455 //////////////
456 // delete_by_oid
457 // k \t oidInAscii
458 //////////////
459 _delete_by_oid_subtest(char* buf, istream& in, mark_base* ub)
460 {
461    mark_smart_ptr* x = 0;
462    if ( strcmp(buf, "delete_by_oid") == 0 ) {
463         oid_t id;
464         _get_id(in, id);
465
466         x = new mark_smart_ptr(ub, id);
467                       
468         x -> remove_from_db();
469                       
470         delete x;
471                
472     } else return 1;
473 }
474
475 int mark_test(int argc, char** argv)
476 {
477     int ok = 2;
478
479     if ( strcmp(argv[1], "create_mark_base") == 0 ) {
480       if ( argc != 4 ) {
481          MESSAGE(cerr, "args: create_mark_base path name");
482          ok = -1;
483       } else {
484
485          mark_base* ub = get_mark_base(argv[2], argv[3]);
486
487          if ( ub == 0 || ub -> open_status() != user_base::SUCC ) {
488             ok = -1;
489          } else {
490             delete ub;
491             ok = 0;
492          }
493       }
494    } else
495
496    if ( strcmp(argv[1], "mark_comprehensive") == 0 ) {
497       if ( argc != 5 ) {
498          MESSAGE(cerr,
499            "mark_comprehensive args: mark_comprehensive test_file path name");
500          ok = -1;
501       } else {
502
503          mark_base* ub = get_mark_base(argv[3], argv[4]);
504
505          if ( ub == 0 || ub -> open_status() != user_base::SUCC ) {
506             ok = -1;
507          } else {
508
509
510             fstream in(argv[2], ios::in);
511
512             int i=1;
513             char buf[LBUFSIZ];
514
515             while ( in.getline(buf, LBUFSIZ) ) {
516
517                if ( buf[0] == '#' )
518                    continue;
519
520                if ( _create_subtest(buf, in, ub) != 1 )
521                    continue;
522
523                if ( _get_by_key_subtest(buf, in, ub) != 1 )
524                    continue;
525
526                if ( _get_by_oid_subtest(buf, in, ub) != 1 )
527                    continue;
528
529                if ( _show_for_a_locator_subtest(buf, in, ub) != 1 )
530                    continue;
531
532                if ( _show_all_marks_subtest(buf, in, ub) != 1 )
533                    continue;
534
535                if ( _update_by_key_subtest(buf, in, ub) != 1 )
536                    continue;
537
538                if ( _update_by_id_subtest(buf, in, ub) != 1 )
539                    continue;
540
541                if ( _delete_by_key_subtest(buf, in, ub) != 1 )
542                    continue;
543
544                if ( _delete_by_oid_subtest(buf, in, ub) != 1 ) {
545                    debug(cerr, buf);
546                    throw(stringException("bad subtest command"));
547                }
548
549                if ( i % 50 == 0 )
550                   MESSAGE(cerr, form("%d processed", i));
551  
552                i++;
553             }
554          }
555
556          delete ub;
557          ok = 0;
558       }
559    }
560
561    return ok;
562 }
563 #endif