Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/tests/cdt/tstringset.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
#define strcmp(a,b) teststrcmp(a,b)
23
24
int N_cmp = 0; /* count number of compares */
25
int strcmp(const char* s1, const char* s2)
26
{ int d;
27
N_cmp++;
28
for(;; s1++, s2++)
29
{ if((d = s1[0] - s2[0]) )
30
return d;
31
if(s1[0] == 0)
32
return 0;
33
}
34
}
35
36
/* print statistics */
37
#define N_OBJ 100000
38
static int N_obj = N_OBJ, Count = 0, Brief = 0;
39
static int Perm[N_OBJ];
40
static char Key[N_OBJ][8]; /* max 7-digit numbers */
41
static char *Pat = "%07d";
42
43
tmain()
44
{
45
int i, k, t;
46
Dt_t* dt;
47
Dtdisc_t disc;
48
char *ks, tmp[16]; /* max 7-digit numbers */
49
50
/* create a permutation of size N_obj */
51
for(i = 0; i < N_obj; ++i)
52
Perm[i] = i;
53
#if RANDOM
54
for(i = N_obj; i > 1; --i)
55
{ k = ((unsigned int)rand())%i;
56
t = Perm[k]; Perm[k] = Perm[i-1]; Perm[i-1] = t;
57
}
58
#endif
59
60
/* make key */
61
for(i = 0; i < N_obj; ++i)
62
sprintf(Key[i], Pat, Perm[i]);
63
64
disc.key = disc.size = 0; disc.link = -1;
65
disc.makef = 0; disc.freef = 0;
66
disc.comparf = 0; disc.hashf = 0;
67
disc.memoryf = 0; disc.eventf = 0;
68
dt = dtopen(&disc, Dtset);
69
70
/* insert into table */
71
for(i = 0; i < N_obj; ++i)
72
{ dtinsert(dt, Key[i]);
73
if(((i+1)%1000) == 0 )
74
{ if(dtsize(dt) != i+1)
75
terror("Bad size=%d, should be %d", k, i+1);
76
for(k = 0; k < 1000; ++k)
77
{ sprintf(tmp, Pat, ((unsigned int)rand())%(i+1));
78
if(!(ks = (char*)dtsearch(dt,tmp)) )
79
terror("Not finding '%s'", tmp);
80
}
81
}
82
}
83
84
/* search in order of insertion */
85
for(Count = 0, i = 0; i < N_obj; ++i)
86
{ if(!(ks = (char*)dtsearch(dt,Key[i])) )
87
terror("Not finding '%s'", Key[i]);
88
if(strcmp(ks,Key[i]) == 0)
89
Count += 1;
90
}
91
if(Count != N_obj)
92
terror("Count=%d but should be %d", Count, N_obj);
93
94
/* search in a random order */
95
for(Count = 0, i = 0; i < N_obj; ++i)
96
{ sprintf(tmp, Pat, ((unsigned int)rand())%N_obj);
97
if(!(ks = (char*)dtsearch(dt,tmp)) )
98
terror("Not finding '%s'", Key[i]);
99
if(strcmp(ks,tmp) == 0)
100
Count += 1;
101
}
102
if(Count != N_obj)
103
terror("Count=%d but should be %d", Count, N_obj);
104
105
/* search in increasing order */
106
for(Count = 0, i = 0; i < N_obj; ++i)
107
{ sprintf(tmp, Pat, i);
108
if(!(ks = (char*)dtsearch(dt,tmp)) )
109
terror("Not finding '%s'", tmp);
110
if(strcmp(ks,tmp) == 0)
111
Count += 1;
112
}
113
if(Count != N_obj)
114
terror("Count=%d but should be %d", Count, N_obj);
115
116
texit(0);
117
}
118
119