Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/tests/cdt/trhbags.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
typedef struct _obj_s
23
{ Dtlink_t link;
24
long key;
25
long ord;
26
} Obj_t;
27
28
static int objcmp(Dt_t* dt, Void_t* arg1, Void_t* arg2, Dtdisc_t* disc)
29
{
30
Obj_t *o1 = (Obj_t*)arg1, *o2 = (Obj_t*)arg2;
31
32
return (int)(o1->key - o2->key);
33
}
34
35
static unsigned int objhash(Dt_t* dt, Void_t* arg, Dtdisc_t* disc)
36
{
37
Obj_t *o = (Obj_t*)arg;
38
return (unsigned int)(o->key);
39
}
40
41
Dtdisc_t Disc = { 0, 0, 0, 0, 0, objcmp, objhash, 0, 0 };
42
43
#define N_OBJ 100 /* total number of elements */
44
#define R_OBJ 5 /* number of times to repeat */
45
#define N_CHK 10
46
static Obj_t Obj[N_OBJ];
47
48
tmain()
49
{
50
Dt_t* dt;
51
Obj_t *o, proto;
52
long i, k, count, n;
53
54
for(i = 0; i < N_OBJ; i = k)
55
{ for(k = i; k < i+R_OBJ && k < N_OBJ; ++k)
56
{ Obj[k].key = i;
57
Obj[k].ord = k;
58
}
59
}
60
61
for(k = 0; k < 2; ++k)
62
{ if(!(dt = dtopen(&Disc, k == 0 ? Dtrhbag : Dtobag)) )
63
terror("Opening dictionary");
64
dtcustomize(dt, DT_SHARE, 1); /* turn on sharing */
65
66
for(i = 0; i < N_OBJ; ++i)
67
{ if(dtinsert(dt, Obj+i) != Obj+i)
68
terror("Insert %d,%d", Obj[i].key, Obj[i].ord);
69
70
if(i > 0 && (i%N_CHK) == 0)
71
if((count = dtsize(dt)) != i+1)
72
terror("Bad size %d (need %d)", count, i+1);
73
}
74
75
count = n = 0; /* count the group of elements with key == 0 */
76
for(o = (Obj_t*)dtflatten(dt); o; o = (Obj_t*)dtlink(dt,o), count += 1)
77
if(o->key == 0)
78
n += 1;
79
if(count != N_OBJ || n != R_OBJ)
80
terror("flatten %s: count=%d(need=%d) n=%d(need=%d)",
81
k == 0 ? "bag" : "obag", count, N_OBJ, n, R_OBJ);
82
83
/* delete a bunch of objects */
84
for(n = 0, i = 0; i < N_OBJ; i += R_OBJ, n += 1)
85
if(!dtdelete(dt, Obj+i))
86
terror("delete %s: i=%d",
87
k == 0 ? "bag" : "obag", i);
88
89
count = 0; /* count the left over */
90
for(o = (Obj_t*)dtflatten(dt); o; o = (Obj_t*)dtlink(dt,o))
91
count += 1;
92
if(count != N_OBJ-n)
93
terror("%s wrong count %d",
94
k == 0 ? "bag" : "obag", count);
95
96
dtclose(dt);
97
}
98
99
texit(0);
100
}
101
102