File: examples.c

package info (click to toggle)
libwn6 6.0-17
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, sarge
  • size: 6,012 kB
  • ctags: 3,903
  • sloc: ansic: 45,078; makefile: 960; csh: 274; sh: 17
file content (200 lines) | stat: -rw-r--r-- 3,499 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/****************************************************************************

COPYRIGHT NOTICE:

  The source code in this file is provided free of charge
  to the author's consulting clients.  It is in the
  public domain and therefore may be used by anybody for
  any purpose.

AUTHOR:

  Will Naylor

****************************************************************************/
#include <stdio.h>

#include "wnlib.h"
#include "wnasrt.h"

#include "wnhtbl.h"



#if 0
main()
{
  extern bool wn_streqnc();
  char s1[100],s2[100],s3[100],*s3_copy;
  wn_htab string_table;   /* hash table */

  wn_gpmake("no_free");   /* memory group for all operations below */
  wn_gplabel("main group");

  wn_mkstrhtab(&string_table);  /* make string hash table, using current
                                   memory group */

  while(TRUE)
  {
    printf("command > ");
    scanf("%s",s1);

    if(wn_streqnc(s1,"enter"))   /* enter value */
    {
      printf("enter key: ");
      scanf("%s",s2);
      printf("enter value: ");
      scanf("%s",s3);

      wn_stracpy(&s3_copy,s3);  /* make allocated memory copy of s3 */

      wn_hfins((ptr)s3_copy,string_table,(ptr)s2); /* insert into 
                                                      hash table */ 
    }
    else if(wn_streqnc(s1,"print"))  /* print value */
    {
      printf("enter key: ");
      scanf("%s",s2);

      if(wn_hget((ptr *)&s3_copy,string_table,(ptr)s2))
      {
        printf("value = %s\n",s3_copy);
      }
      else
      {
        printf("key <%s> not found.\n",s2);
      }
    }
    else if(wn_streqnc(s1,"exit"))  /* exit */
    {
      printf("exiting.\n");

      break;
    }
    else
    {
      printf("illegal command.\n");
    }
  }

  wn_gpfree();            /* release all memory */
}
#endif



#if 1
main()
{
  wn_htab table;
  int i,j,num;
  bool success;

  /*
  for(i=0;i<2000000;++i)
  {
    num = wn_inthash(i);
  }

  return;
  */

  wn_gpmake("no_free");

  wn_mkinthtab(&table);

  for(i=0;i<10000;++i)
  {
    success = wn_hins((ptr)(i+1),table,(ptr)i);
    wn_assert(success);
  }

  wn_hverify(table);

  /*
  for(j=0;j<50;++j)
  */
  for(i=0;i<10000;++i)
  {
    success = wn_hget((ptr *)&num,table,(ptr)i);
    wn_assert(success);
  }

  wn_hverify(table);

  for(i=0;i<5000;++i)
  {
    success = wn_hdel(table,(ptr)i);
    wn_assert(success);
  }

  wn_hverify(table);

  for(i=0;i<5000;++i)
  {
    success = wn_hdel(table,(ptr)i);
    wn_assert(!(success));
  }

  wn_hverify(table);

  for(i=0;i<5000;++i)
  {
    success = wn_hget((ptr *)&num,table,(ptr)i);
    wn_assert(!(success));
  }

  for(i=5000;i<10000;++i)
  {
    success = wn_hget((ptr *)&num,table,(ptr)i);
    wn_assert(success);
    wn_assert(num == i+1);
  }

  for(i=2500;i<7500;++i)
  {
    success = wn_hfins((ptr)i,table,(ptr)i);
    wn_assert(success);
  }

  for(i=2500;i<7500;++i)
  {
    success = wn_hget((ptr *)&num,table,(ptr)i);
    wn_assert(success);
    wn_assert(num == i);
  }

  for(i=0;i<2500;++i)
  {
    success = wn_hget((ptr *)&num,table,(ptr)i);
    wn_assert(!(success));
  }

  for(i=7500;i<10000;++i)
  {
    success = wn_hget((ptr *)&num,table,(ptr)i);
    wn_assert(success);
    wn_assert(num == i+1);
  }

  wn_hverify(table);

  for(i=2500;i<10000;++i)
  {
    success = wn_hdel(table,(ptr)i);
    wn_assert(success);
  }

  wn_hverify(table);

  for(i=0;i<10000;++i)
  {
    success = wn_hget((ptr *)&num,table,(ptr)i);
    wn_assert(!(success));
  }

  printf("success!!!\n");
}
#endif