1 : // -*- mode: c++; fill-column: 79 -*-
2 : // $Id: test_pagecache.cc 2 2010-04-14 07:34:58Z tb $
3 :
4 : /*
5 : * STX Constant B-Tree Database Template Classes v0.7.0
6 : * Copyright (C) 2010 Timo Bingmann
7 : *
8 : * This library is free software; you can redistribute it and/or modify it
9 : * under the terms of the GNU Lesser General Public License as published by the
10 : * Free Software Foundation; either version 2.1 of the License, or (at your
11 : * option) any later version.
12 : *
13 : * This library is distributed in the hope that it will be useful, but WITHOUT
14 : * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 : * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16 : * for more details.
17 : *
18 : * You should have received a copy of the GNU Lesser General Public License
19 : * along with this library; if not, write to the Free Software Foundation,
20 : * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 : */
22 :
23 : /*
24 : * Check PageCache behaviour by adding fake pages step by step and
25 : * checking cache contents and LRU-order after each Store() or
26 : * Retrieve(). Also test the cache using its internal Verify()
27 : * function.
28 : */
29 :
30 : #define CBTREEDB_SELF_VERIFY
31 : #include "stx-cbtreedb.h"
32 :
33 : #include <assert.h>
34 :
35 : class CBTreeDBTest : public stx::CBTreeDB<>
36 : {
37 : public:
38 :
39 1 : void test1_pagecache()
40 : {
41 1 : PageCache pc(8);
42 :
43 1 : void* btreeid = reinterpret_cast<void*>(0x12345678);
44 :
45 1 : BTreePage p1;
46 :
47 1 : pc.Store(btreeid, 1, p1);
48 1 : pc.Store(btreeid, 2, p1);
49 1 : pc.Store(btreeid, 3, p1);
50 1 : pc.Store(btreeid, 4, p1);
51 1 : assert( pc.Verify() );
52 :
53 : {
54 1 : std::vector< std::pair<void*, uint32_t> > pagelist = pc.GetPagelist();
55 :
56 1 : assert( pagelist.size() == 4 );
57 1 : assert( pagelist[0].first == btreeid && pagelist[0].second == 4 );
58 1 : assert( pagelist[1].first == btreeid && pagelist[1].second == 3 );
59 1 : assert( pagelist[2].first == btreeid && pagelist[2].second == 2 );
60 1 : assert( pagelist[3].first == btreeid && pagelist[3].second == 1 );
61 : }
62 :
63 1 : pc.Retrieve(btreeid, 2, p1);
64 1 : assert( pc.Verify() );
65 :
66 : {
67 1 : std::vector< std::pair<void*, uint32_t> > pagelist = pc.GetPagelist();
68 :
69 1 : assert( pagelist.size() == 4 );
70 1 : assert( pagelist[0].first == btreeid && pagelist[0].second == 2 );
71 1 : assert( pagelist[1].first == btreeid && pagelist[1].second == 4 );
72 1 : assert( pagelist[2].first == btreeid && pagelist[2].second == 3 );
73 1 : assert( pagelist[3].first == btreeid && pagelist[3].second == 1 );
74 : }
75 :
76 1 : pc.Store(btreeid, 5, p1);
77 1 : pc.Store(btreeid, 6, p1);
78 1 : pc.Store(btreeid, 7, p1);
79 1 : pc.Store(btreeid, 8, p1);
80 1 : assert( pc.Verify() );
81 :
82 : {
83 1 : std::vector< std::pair<void*, uint32_t> > pagelist = pc.GetPagelist();
84 :
85 1 : assert( pagelist.size() == 8 );
86 1 : assert( pagelist[0].first == btreeid && pagelist[0].second == 8 );
87 1 : assert( pagelist[1].first == btreeid && pagelist[1].second == 7 );
88 1 : assert( pagelist[2].first == btreeid && pagelist[2].second == 6 );
89 1 : assert( pagelist[3].first == btreeid && pagelist[3].second == 5 );
90 1 : assert( pagelist[4].first == btreeid && pagelist[4].second == 2 );
91 1 : assert( pagelist[5].first == btreeid && pagelist[5].second == 4 );
92 1 : assert( pagelist[6].first == btreeid && pagelist[6].second == 3 );
93 1 : assert( pagelist[7].first == btreeid && pagelist[7].second == 1 );
94 : }
95 :
96 1 : pc.Store(btreeid, 9, p1);
97 1 : assert( pc.Verify() );
98 :
99 : {
100 1 : std::vector< std::pair<void*, uint32_t> > pagelist = pc.GetPagelist();
101 :
102 1 : assert( pagelist.size() == 8 );
103 1 : assert( pagelist[0].first == btreeid && pagelist[0].second == 9 );
104 1 : assert( pagelist[1].first == btreeid && pagelist[1].second == 8 );
105 1 : assert( pagelist[2].first == btreeid && pagelist[2].second == 7 );
106 1 : assert( pagelist[3].first == btreeid && pagelist[3].second == 6 );
107 1 : assert( pagelist[4].first == btreeid && pagelist[4].second == 5 );
108 1 : assert( pagelist[5].first == btreeid && pagelist[5].second == 2 );
109 1 : assert( pagelist[6].first == btreeid && pagelist[6].second == 4 );
110 1 : assert( pagelist[7].first == btreeid && pagelist[7].second == 3 );
111 : }
112 :
113 1 : pc.Store(btreeid, 10, p1);
114 1 : pc.Store(btreeid, 6, p1);
115 1 : assert( pc.Verify() );
116 :
117 : {
118 1 : std::vector< std::pair<void*, uint32_t> > pagelist = pc.GetPagelist();
119 :
120 1 : assert( pagelist.size() == 8 );
121 1 : assert( pagelist[0].first == btreeid && pagelist[0].second == 6 );
122 1 : assert( pagelist[1].first == btreeid && pagelist[1].second == 10 );
123 1 : assert( pagelist[2].first == btreeid && pagelist[2].second == 9 );
124 1 : assert( pagelist[3].first == btreeid && pagelist[3].second == 8 );
125 1 : assert( pagelist[4].first == btreeid && pagelist[4].second == 7 );
126 1 : assert( pagelist[5].first == btreeid && pagelist[5].second == 5 );
127 1 : assert( pagelist[6].first == btreeid && pagelist[6].second == 2 );
128 1 : assert( pagelist[7].first == btreeid && pagelist[7].second == 4 );
129 1 : }
130 1 : }
131 :
132 :
133 1 : void test2_pagecache()
134 : {
135 1 : PageCache pc(32);
136 :
137 1 : void* btreeid = reinterpret_cast<void*>(0x12345678);
138 :
139 1 : BTreePage p1;
140 :
141 1001 : for (unsigned int i = 0; i < 1000; ++i)
142 : {
143 1000 : if (i % 2 == 0)
144 : {
145 500 : pc.Store(btreeid, (i * 0x548A1B71) % 53, p1);
146 : }
147 : else
148 : {
149 500 : pc.Retrieve(btreeid, (i * 0x548A1B71) % 53, p1);
150 : }
151 :
152 1000 : assert( pc.Verify() );
153 1 : }
154 1 : }
155 : };
156 :
157 1 : int main()
158 : {
159 1 : CBTreeDBTest().test1_pagecache();
160 1 : CBTreeDBTest().test2_pagecache();
161 1 : return 0;
162 3 : }
|