Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/tests/cdt/tobag.c
1810 views
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 <[email protected]> *
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
Dtdisc_t Rdisc =
29
{ 0, sizeof(long), -1,
30
newint, NIL(Dtfree_f), rcompare, hashint,
31
NIL(Dtmemory_f), NIL(Dtevent_f)
32
};
33
34
tmain()
35
{
36
Dt_t* dt;
37
Dtlink_t* link;
38
long x, g, i, k, count[10];
39
40
/* testing Dtobag */
41
dt = dtopen(&Disc,Dtobag);
42
x = 5;
43
if((g = (char*)dtinsert(dt,x) - (char*)0) != x)
44
terror("Insert -- expected %ld, got %ld", x, g);
45
x = 2;
46
if((g = (char*)dtinsert(dt,x) - (char*)0) != x)
47
terror("Insert -- expected %ld, got %ld", x, g);
48
x = 5;
49
if((g = (char*)dtinsert(dt,x) - (char*)0) != x)
50
terror("Insert -- expected %ld, got %ld", x, g);
51
for(k = 0, i = (long)dtfirst(dt); i; k = i, i = (long)dtnext(dt,i))
52
if(i < k)
53
terror("Disorder %ld >= %ld", k, i);
54
x = 3;
55
if((g = (char*)dtinsert(dt,x) - (char*)0) != x)
56
terror("Insert -- expected %ld, got %ld", x, g);
57
x = 5;
58
if((g = (char*)dtinsert(dt,x) - (char*)0) != x)
59
terror("Insert -- expected %ld, got %ld", x, g);
60
for(k = 0, i = (long)dtfirst(dt); i; k = i, i = (long)dtnext(dt,i))
61
if(i < k)
62
terror("Disorder %ld >= %ld", k, i);
63
x = 4;
64
if((g = (char*)dtinsert(dt,x) - (char*)0) != x)
65
terror("Insert -- expected %ld, got %ld", x, g);
66
x = 1;
67
if((g = (char*)dtinsert(dt,x) - (char*)0) != x)
68
terror("Insert -- expected %ld, got %ld", x, g);
69
for(k = 0, i = (long)dtfirst(dt); i; k = i, i = (long)dtnext(dt,i))
70
if(i < k)
71
terror("Disorder %ld >= %ld", k, i);
72
x = 2;
73
if((g = (char*)dtinsert(dt,x) - (char*)0) != x)
74
terror("Insert -- expected %ld, got %ld", x, g);
75
x = 5;
76
if((g = (char*)dtinsert(dt,x) - (char*)0) != x)
77
terror("Insert -- expected %ld, got %ld", x, g);
78
x = 4;
79
if((g = (char*)dtinsert(dt,x) - (char*)0) != x)
80
terror("Insert -- expected %ld, got %ld", x, g);
81
x = 3;
82
if((g = (char*)dtinsert(dt,x) - (char*)0) != x)
83
terror("Insert -- expected %ld, got %ld", x, g);
84
for(k = 0, i = (long)dtfirst(dt); i; k = i, i = (long)dtnext(dt,i))
85
if(i < k)
86
terror("Disorder %ld >= %ld", k, i);
87
x = 4;
88
if((g = (char*)dtinsert(dt,x) - (char*)0) != x)
89
terror("Insert -- expected %ld, got %ld", x, g);
90
x = 5;
91
if((g = (char*)dtinsert(dt,x) - (char*)0) != x)
92
terror("Insert -- expected %ld, got %ld", x, g);
93
for(k = 0, i = (long)dtfirst(dt); i; k = i, i = (long)dtnext(dt,i))
94
if(i < k)
95
terror("Disorder %ld >= %ld", k, i);
96
x = 3;
97
if((g = (char*)dtinsert(dt,x) - (char*)0) != x)
98
terror("Insert -- expected %ld, got %ld", x, g);
99
x = 4;
100
if((g = (char*)dtinsert(dt,x) - (char*)0) != x)
101
terror("Insert -- expected %ld, got %ld", x, g);
102
for(k = 0, i = (long)dtfirst(dt); i; k = i, i = (long)dtnext(dt,i))
103
if(i < k)
104
terror("Disorder %ld >= %ld", k, i);
105
for(i = 0; i <= 5; ++i)
106
count[i] = 0;
107
for(i = (long)dtfirst(dt); i; i = (long)dtnext(dt,i))
108
count[i] += 1;
109
for(i = 0; i <= 5; ++i)
110
if(count[i] != i)
111
terror("dtnext count failed -- expected %d, got %ld", i, count[i]);
112
for(i = 0; i <= 5; ++i)
113
count[i] = 0;
114
for(i = (long)dtlast(dt); i; i = (long)dtprev(dt,i))
115
count[i] += 1;
116
for(i = 0; i <= 5; ++i)
117
if(count[i] != i)
118
terror("dtprev count failed -- expected %d, got %ld", i, count[i]);
119
for(k = 0, i = (long)dtfirst(dt); i; k = i, i = (long)dtnext(dt,i))
120
if(i < k)
121
terror("Disorder %ld >= %ld", k, i);
122
123
for(link = dtflatten(dt), i = 1; link; ++i)
124
for(k = 1; k <= i; ++k, link = dtlink(dt,link))
125
if(i != (long)dtobj(dt,link))
126
terror("Bad element");
127
128
dtclear(dt);
129
if(dtsize(dt) > 0 )
130
terror("Non empty dictionary after clearing");
131
132
for(i = 1; i <= 10; ++i)
133
for(k = 1; k <= 10; ++k)
134
if((long)dtinsert(dt, k) != k)
135
terror("Can't insert k=%d at iteration %d", k, i);
136
137
for(k = 0, i = (long)dtatmost(dt,5L); i == 5; i = (long)dtnext(dt,i) )
138
k += 1;
139
if(k != 10)
140
terror("Did not see all 5's k=%d", k);
141
142
for(k = 0, i = (long)dtatleast(dt,3L); i == 3; i = (long)dtprev(dt,i) )
143
k += 1;
144
if(k != 10)
145
terror("Did not see all 3's k=%d", k);
146
147
texit(0);
148
}
149
150