File: bdbtest.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 (180 lines) | stat: -rw-r--r-- 5,055 bytes parent folder | download | duplicates (2)
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
/*
 * Note: this test program should be exactly the same as the one in
 * qdbmtest.cc, because the APIs should be identical.
 */
#include "wvautoconf.h"
#if defined(WITH_BDB)
#include "wvondiskhash.h"
#include "wvstringlist.h"

typedef WvOnDiskHash<WvString,WvString,WvBdbHash> StrStrMap;
typedef WvOnDiskHash<WvString,int,WvBdbHash> StrTimeMap;
typedef WvOnDiskHash<int,int,WvBdbHash> IntIntMap;

DeclareWvList(int);

// yes, I *am* crazy.
typedef WvOnDiskHash<WvList<WvString>,intList, WvBdbHash> ListMap;

int main()
{
    {
	StrStrMap ss("dbmfile");
	printf("\nList of strings:\n");
	{
	    StrStrMap::Iter i(ss);
            int guard = 0;
	    for (i.rewind(); i.next() && guard < 5; guard++)
		printf("'%s': '%s'\n", i.key().cstr(), i->cstr());
            if (guard == 5) printf("FAILED\n");
	}
	
	ss.add("hello", "world", true);
	ss.add("hello", "world2", true);
	ss.add("yellow", "5", true);
	if (ss.exists("appendable"))
	    ss.add("appendable",
		   ss["appendable"].append("-yak"), true);
	else
	    ss.add("appendable", "init");
	printf("appendable is now '%s'\n", ss["appendable"].cstr());

        printf("\nRestarting an iter:\n");
        {
            StrStrMap::Iter i(ss);
            int guard = 0;
            for (i.rewind(); i.next() && guard < 5; guard++)
            {
                printf("1 - '%s': '%s'\n", i.key().cstr(), i->cstr());
                if (i.key() == "hello")
                {
                    printf("Hit 'hello': modifying and starting over\n");
                    i().append("-yak");
                    i.save();
                    int guard2 = 0;
                    for (i.rewind(); i.next() && guard2 < 5; guard2++)
                        printf("1 - '%s': '%s'\n", i.key().cstr(), i->cstr());
                    if (guard == 5) guard2 = 5;
                    break;
                }
            }
            if (guard == 5) printf("FAILED\n");
        }

        printf("\nUnlinking 'hello' from an iter:\n");
        {
            StrStrMap::Iter i(ss);
            int guard = 0;
            for (i.rewind(); i.next() && guard < 5; guard++)
            {
		printf("1 - '%s': '%s'\n", i.key().cstr(), i->cstr());
                if (i.key() == "hello")
                    i.xunlink();
            }
            if (guard == 5) printf("FAILED\n");
            for (i.rewind(), guard = 0; i.next() && guard < 5; guard++)
		printf("2 - '%s': '%s'\n", i.key().cstr(), i->cstr());
            if (guard == 5) printf("FAILED\n");
        }
    }

    {
	StrTimeMap st;
        st.opendb("dbmfile2");
	printf("\nList of times:\n");
	{
	    StrTimeMap::Iter i(st);
            int guard = 0;
            for (i.rewind(); i.next() && guard < 5; guard++)
		printf("'%s': %d\n", i.key().cstr(), *i);
            if (guard == 5) printf("FAILED\n");
	}
	
	st.add("hello", 65, true);
	st.add("hello", 66, true);
	st.add("mellow", 97650, true);
	if (st.exists("addable"))
	    st.add("addable", st["addable"] + 5, true);
	else
	    st.add("addable", 7);
	printf("addable is now '%d'\n", st["addable"]);
    }
    
    {
	ListMap m("dbmfile3");
	
	// l1 is the same as l2; l3 is different.
	WvStringList l1; l1.split("big moose munchkin bunnies");
	WvStringList l2; l2.split("big   \nmoose\tmunchkin\n\nbunnies");
	WvStringList l3; l3.split("big   \nmoosemunchkin\n\nbunnies");
	printf("key elements: %d\n", l1.count());
	printf("key elements: %d\n", l2.count());
	printf("key elements: %d\n", l3.count());
	
	intList i1; i1.append(new int(7), true); i1.append(new int(8), true);
	intList i2; i2.append(new int(96), true);
	
	m.add(l1, i1, true);
	m.add(l2, i2, true);
	m.add(l3, i1, true);
	
	//for (int count = 0; count < 10000; count++)
	{
	    ListMap::Iter i(m);
            int guard = 0;
            for (i.rewind(); i.next() && guard < 5; guard++)
	    {
		WvList<WvString> &key = i.key();
		intList &data = i();
		
		printf("key(%d): ", key.count());
		{
		    WvList<WvString>::Iter ii(key);
		    for (ii.rewind(); ii.next(); )
			printf("'%s' ", ii->cstr());
		}
		printf("\n    ");
		{
		    intList::Iter ii(data);
		    for (ii.rewind(); ii.next(); )
			printf("%d ", ii());
		}
		printf("\n");
	    }
            if (guard == 5) printf("FAILED\n");
	}
    }

    {
        printf("\nTesting anonymous db:\n");
        bool ok = true;
        StrStrMap st;
        if (!st.isempty()) { ok = false; printf("Anon-db is NOT empty: FAILED\n"); }
        st.add("foo", "bar");
        if (st["foo"] != "bar") {
            ok = false;
            printf(WvString("Put in foo: bar; got out foo: %s: FAILED\n", st["foo"]));
        }
        if (ok) printf("Success!\n");
    }

    {
        printf("\nTesting iter:\n");
        IntIntMap m("ints");
        m.add(42,43,true);
        IntIntMap::Iter i(m);
        int guard = 0;
        for (i.rewind(); i.next() && guard < 5; guard++)
        {
            printf( "%d -> %d\n", i.key(), i() );
            i.save();
        }
        if (guard == 5) printf("FAILED\n");
    }
}
#else
int main(int argc, char *argv[])
{
  return 0;
}
#endif