File: LargeTest.cc

package info (click to toggle)
stx-btree 0.8.3-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 4,948 kB
  • ctags: 6,441
  • sloc: cpp: 6,251; sh: 3,674; makefile: 161; awk: 16; xml: 9
file content (221 lines) | stat: -rw-r--r-- 5,913 bytes parent folder | download
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// $Id: LargeTest.cc 113 2008-09-07 15:25:51Z tb $

/*
 * STX B+ Tree Template Classes v0.8.3
 * Copyright (C) 2008 Timo Bingmann
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by the
 * Free Software Foundation; either version 2.1 of the License, or (at your
 * option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include <cppunit/extensions/HelperMacros.h>

#include <stdlib.h>
#include <time.h>

#include <stx/btree_multiset.h>
#include <set>

class LargeTest : public CPPUNIT_NS::TestFixture
{
    CPPUNIT_TEST_SUITE( LargeTest );
    CPPUNIT_TEST(test_3200_10);
    CPPUNIT_TEST(test_320_1000);
    CPPUNIT_TEST(test_320_10000);
    CPPUNIT_TEST(test_sequence);
    CPPUNIT_TEST_SUITE_END();

protected:

    struct traits_nodebug
    {
        static const bool       selfverify = true;
        static const bool       debug = false;

        static const int        leafslots = 8;
        static const int        innerslots = 8;
    };

    void test_multi(const unsigned int insnum, const unsigned int modulo)
    {
        typedef stx::btree_multiset<unsigned int,
            std::less<unsigned int>, struct traits_nodebug> btree_type;

        btree_type bt;

        typedef std::multiset<unsigned int> multiset_type;
        multiset_type set;

        // *** insert
        srand(34234235);
        for(unsigned int i = 0; i < insnum; i++)
        {
            unsigned int k = rand() % modulo;

            CPPUNIT_ASSERT( bt.size() == set.size() );
            bt.insert(k);
            set.insert(k);
            CPPUNIT_ASSERT( bt.count(k) == set.count(k) );

            CPPUNIT_ASSERT( bt.size() == set.size() );
        }

        CPPUNIT_ASSERT( bt.size() == insnum );

        // *** iterate
        btree_type::iterator bi = bt.begin();
        multiset_type::const_iterator si = set.begin();
        for(; bi != bt.end() && si != set.end(); ++bi, ++si)
        {
            CPPUNIT_ASSERT( *si == bi.key() );
        }
        CPPUNIT_ASSERT( bi == bt.end() );
        CPPUNIT_ASSERT( si == set.end() );

        // *** existance
        srand(34234235);
        for(unsigned int i = 0; i < insnum; i++)
        {
            unsigned int k = rand() % modulo;

            CPPUNIT_ASSERT( bt.exists(k) );
        }

        // *** counting
        srand(34234235);
        for(unsigned int i = 0; i < insnum; i++)
        {
            unsigned int k = rand() % modulo;

            CPPUNIT_ASSERT( bt.count(k) == set.count(k) );
        }

        // *** deletion
        srand(34234235);
        for(unsigned int i = 0; i < insnum; i++)
        {
            unsigned int k = rand() % modulo;

            if (set.find(k) != set.end())
            {
                CPPUNIT_ASSERT( bt.size() == set.size() );

                CPPUNIT_ASSERT( bt.exists(k) );
                CPPUNIT_ASSERT( bt.erase_one(k) );
                set.erase( set.find(k) );

                CPPUNIT_ASSERT( bt.size() == set.size() );
            }
        }

        CPPUNIT_ASSERT( bt.empty() );
        CPPUNIT_ASSERT( set.empty() );
    }

    void test_3200_10()
    {
        test_multi(3200, 10);
    }

    void test_320_1000()
    {
        test_multi(320, 1000);
    }

    void test_320_10000()
    {
        test_multi(320, 10000);
    }

    void test_sequence()
    {
        typedef stx::btree_multiset<unsigned int,
            std::less<unsigned int>, struct traits_nodebug> btree_type;

        btree_type bt;

        const unsigned int insnum = 10000;

        typedef std::multiset<unsigned int> multiset_type;
        multiset_type set;

        // *** insert
        srand(34234235);
        for(unsigned int i = 0; i < insnum; i++)
        {
            unsigned int k = i;

            CPPUNIT_ASSERT( bt.size() == set.size() );
            bt.insert(k);
            set.insert(k);
            CPPUNIT_ASSERT( bt.count(k) == set.count(k) );

            CPPUNIT_ASSERT( bt.size() == set.size() );
        }

        CPPUNIT_ASSERT( bt.size() == insnum );

        // *** iterate
        btree_type::iterator bi = bt.begin();
        multiset_type::const_iterator si = set.begin();
        for(; bi != bt.end() && si != set.end(); ++bi, ++si)
        {
            CPPUNIT_ASSERT( *si == bi.key() );
        }
        CPPUNIT_ASSERT( bi == bt.end() );
        CPPUNIT_ASSERT( si == set.end() );

        // *** existance
        srand(34234235);
        for(unsigned int i = 0; i < insnum; i++)
        {
            unsigned int k = i;

            CPPUNIT_ASSERT( bt.exists(k) );
        }

        // *** counting
        srand(34234235);
        for(unsigned int i = 0; i < insnum; i++)
        {
            unsigned int k = i;

            CPPUNIT_ASSERT( bt.count(k) == set.count(k) );
        }

        // *** deletion
        srand(34234235);
        for(unsigned int i = 0; i < insnum; i++)
        {
            unsigned int k = i;

            if (set.find(k) != set.end())
            {
                CPPUNIT_ASSERT( bt.size() == set.size() );

                CPPUNIT_ASSERT( bt.exists(k) );
                CPPUNIT_ASSERT( bt.erase_one(k) );
                set.erase( set.find(k) );

                CPPUNIT_ASSERT( bt.size() == set.size() );
            }
        }

        CPPUNIT_ASSERT( bt.empty() );
        CPPUNIT_ASSERT( set.empty() );
    }

};

CPPUNIT_TEST_SUITE_REGISTRATION( LargeTest );