File: stresshashtest.cc

package info (click to toggle)
wvstreams 4.0.2-4
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 6,420 kB
  • ctags: 6,518
  • sloc: cpp: 52,544; sh: 5,770; ansic: 810; makefile: 461; tcl: 114; perl: 18
file content (111 lines) | stat: -rw-r--r-- 2,276 bytes parent folder | download | duplicates (3)
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
/*
 * Worldvisions Weaver Software:
 *   Copyright (C) 1997-2002 Net Integration Technologies, Inc.
 *
 * WvHashTable stress tester.  Adds a lot of elements to the table and 
 * looks them up, iterates through them, etc.  This requires lots of memory.
 *
 */

#include "wvhashtable.h"
#include "wvstring.h"
#include <stdio.h>
#include <assert.h>

#ifndef  ISDARWIN
# include <malloc.h>
#else
# include <stdlib.h>
#endif

struct Intstr
{
    int i;
    WvString s;
    
    Intstr(int _i, WvStringParm _s)
        { i = _i; s = _s; }
};

DeclareWvDict(Intstr, WvString, s);

const unsigned size = 1000, elems = 10000;

int main()
{
    //free(malloc(1)); // enable electric fence
    
    IntstrDict d(size);
    unsigned count, total;
    
    for (count = 0; count < elems; count++)
    {
	if (elems > 100 && !(count % (elems/20)))
	{
	    printf("\rAdding %d%%", count / (elems/100));
	    fflush(stdout);
	}
	d.add(new Intstr(count, count), true);
    }
    
    printf("\rAdded: total %d (should be %d)\n", d.count(), elems);
    
    total = 0;
    for (count = 0; count < d.numslots; count++)
    {
	WvLink *head = &d.wvslots[count].head;
	if (!head->next)
	    total++;
    }
    printf("%d of %d empty slots in the table.\n", total, d.numslots);
    printf("Avg chain length: %d (best %d)\n",
	   elems / (d.numslots - total), elems / d.numslots);
    

    printf("Removing...");
    fflush(stdout);
    
    for (count = 0; count < elems; count += 5)
    {
	assert(d[count]);
	d.remove(d[count]);
    }
    
    printf("\rRemoved.  New count: %d (should be %d)\n",
	   d.count(), elems - elems/5);
    
    IntstrDict::Iter i(d);
    total = d.count();
    count = 0;
    
    for (i.rewind(); i.next(); )
    {
	if (total > 20 && !(count % (total/20)))
	{
	    printf("\rIterate %d%%", count / (total/100));
	    fflush(stdout);
	}

	assert(i->s == WvString(i->i));
	count++;
    }
    
    printf("\rIterator okay.\n");

    
    for (count = 0; count < total; count++)
    {
	if (total > 100 && !(count % (total/20)))
	{
	    printf("\rSlow Iterate %d%%", count / (total/100));
	    fflush(stdout);
	}

	assert(!(count%5)
	       || (d[count] && d[count]->s == WvString(d[count]->i)));
    }
    
    printf("\rOne-by-one iterator okay.\n");
    
    return 0;
}