root/trunk/whisperlib/common/base/test/free_list_test.cc

Revision 7, 3.6 kB (checked in by whispercastorg, 2 years ago)

version 0.2.0

Line 
1 // Copyright (c) 2009, Whispersoft s.r.l.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Whispersoft s.r.l. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Author: Catalin Popescu
31 #include <stdlib.h>
32 #include "common/base/types.h"
33
34 #include WHISPER_HASH_SET_HEADER
35
36 #include "common/base/log.h"
37 #include "common/base/free_list.h"
38 #include "common/base/system.h"
39
40 //////////////////////////////////////////////////////////////////////
41
42 DEFINE_int32(num_tests,
43              2000,
44              "We test w/ these many values");
45
46 //////////////////////////////////////////////////////////////////////
47
48 void Test(size_t max_size, size_t num_tests) {
49   LOG_INFO << " Testing:  max_size = " << max_size
50            << " num_tests = " << num_tests;
51   util::FreeList<string> strlist(max_size);
52   hash_set<string*> expected_content;  // what we expect in free list
53   hash_set<string*> allocs;            // keep track of allocations
54
55   for ( size_t i = 0; i < num_tests; i++ ) {
56     const int op = random() % 2;
57     if ( op || allocs.empty() ) {
58       // Alloc
59       string* p = strlist.New();
60       if ( !expected_content.empty() ) {
61         // We expect reuse if available
62         const size_t num_erased = expected_content.erase(p);
63         CHECK_EQ(num_erased, 1);
64       }
65       CHECK_EQ(allocs.insert(p).second, true);
66     } else {
67       // Free - find a random preallocated
68       const int num = random() % allocs.size();
69       hash_set<string*>::iterator it = allocs.begin();
70       for ( int i = 0; i < num; i++ ) {
71         ++it;
72       }
73       // We expect retention if free list not full..
74       CHECK_EQ(strlist.Dispose(*it), (expected_content.size() == max_size));
75       if ( expected_content.size() < max_size ) {
76         // and we track internally the retention
77         expected_content.insert(*it);
78       }
79       allocs.erase(it);
80     }
81   }
82   // In the end we free them all - simply - w / no test
83   while ( !allocs.empty() ) {
84     strlist.Dispose(*allocs.begin());
85     allocs.erase(allocs.begin());
86   }
87 }
88
89 int main(int argc, char* argv[]) {
90   common::Init(argc, argv);
91   Test(1, FLAGS_num_tests);
92   Test(5, FLAGS_num_tests);
93   Test(20, FLAGS_num_tests);
94   Test(100, FLAGS_num_tests);
95   LOG_INFO << "PASS";
96   common::Exit(0);
97 }
Note: See TracBrowser for help on using the browser.