1 /***********************************************************************
2 * *
3 * This software is part of the ast package *
4 * Copyright (c) 1999-2011 AT&T Intellectual Property *
5 * and is licensed under the *
6 * Eclipse Public License, Version 1.0 *
7 * by AT&T Intellectual Property *
8 * *
9 * A copy of the License is available at *
10 * http://www.eclipse.org/org/documents/epl-v10.html *
11 * (with md5 checksum b35adb5213ca9657e911e9befb180842) *
12 * *
13 * Information and Software Systems Research *
14 * AT&T Research *
15 * Florham Park NJ *
16 * *
17 * Glenn Fowler <glenn.s.fowler@gmail.com> *
18 * *
19 ***********************************************************************/
20 #include "dttest.h"
21
22 Dtdisc_t Disc =
23 { 0, sizeof(long), -1,
24 newint, NIL(Dtfree_f), compare, hashint,
25 NIL(Dtmemory_f), NIL(Dtevent_f)
26 };
27
28 static int Count, See[10];
29
30 #if __STD_C
visit(Dt_t * dt,Void_t * obj,Void_t * data)31 static int visit(Dt_t* dt, Void_t* obj, Void_t* data)
32 #else
33 static int visit(dt, obj, data)
34 Dt_t* dt;
35 Void_t* obj;
36 Void_t* data;
37 #endif
38 {
39 See[(long)obj] = 1;
40 Count += 1;
41 return 0;
42 }
43
tmain()44 tmain()
45 {
46 Dt_t *dt1, *dt2, *dt3;
47 long i, k;
48
49 if(!(dt1 = dtopen(&Disc,Dtoset)) )
50 terror("Opening Dtoset");
51 if(!(dt2 = dtopen(&Disc,Dtoset)) )
52 terror("Opening Dtoset");
53 if(!(dt3 = dtopen(&Disc,Dtoset)) )
54 terror("Opening Dtoset");
55
56 dtinsert(dt1,1L);
57 dtinsert(dt1,3L);
58 dtinsert(dt1,5L);
59 dtinsert(dt1,2L);
60
61 dtinsert(dt2,2L);
62 dtinsert(dt2,4L);
63 dtinsert(dt2,6L);
64 dtinsert(dt2,3L);
65
66 dtinsert(dt3,2L);
67 dtinsert(dt3,7L);
68 dtinsert(dt3,6L);
69 dtinsert(dt3,8L);
70
71 if((long)dtsearch(dt1,4L) != 0)
72 terror("Finding 4 here?");
73
74 dtview(dt1,dt2);
75 if((long)dtsearch(dt1,4L) != 4)
76 terror("Should find 4 here!");
77
78 dtwalk(dt1,visit,NIL(Void_t*));
79 if(Count != 6)
80 terror("Walk wrong length");
81 for(i = 1; i <= 6; ++i)
82 if(!See[i] )
83 terror("Bad walk");
84
85 dtinsert(dt1,2L);
86
87 Count = 0;
88 for(i = (long)dtfirst(dt1); i; i = (long)dtnext(dt1,i))
89 Count++;
90 if(Count != 6)
91 terror("Walk wrong length2");
92
93 Count = 0;
94 for(i = (long)dtlast(dt1); i; i = (long)dtprev(dt1,i))
95 Count++;
96 if(Count != 6)
97 terror("Walk wrong length3");
98
99 /* check union of elements in order across ordered sets */
100 dtview(dt2,dt3);
101 for(k = 1, i = (long)dtfirst(dt1); i; ++k, i = (long)dtnext(dt1,i) )
102 if(i != k)
103 terror("Elements not appearing in order");
104
105 dtinsert(dt3, 10L);
106 if((long)dtatmost(dt1,9L) != 8L)
107 terror("dtatmost failed on an order set");
108 if((long)dtatleast(dt1,9L) != 10L)
109 terror("dtatleast failed on an order set");
110
111 /* dt1: 1 3 5 2
112 dt2: 2 4 6 3
113 dt3: 2 7 6 8 10
114 */
115 Count = 0;
116 dtmethod(dt1,Dtset);
117 dtmethod(dt2,Dtset);
118 dtmethod(dt3,Dtset);
119 for(i = (long)dtfirst(dt1); i; i = (long)dtnext(dt1,i))
120 Count++;
121 if(Count != 9)
122 terror("Walk wrong length4");
123
124 texit(0);
125 }
126