#define CBTREEDB_SELF_VERIFY
#include "stx-cbtreedb.h"
#include <assert.h>
class CBTreeDBTest : public stx::CBTreeDB<>
{
public:
void test1_pagecache()
{
PageCache pc(8);
void* btreeid = reinterpret_cast<void*>(0x12345678);
BTreePage p1;
pc.Store(btreeid, 1, p1);
pc.Store(btreeid, 2, p1);
pc.Store(btreeid, 3, p1);
pc.Store(btreeid, 4, p1);
assert( pc.Verify() );
{
std::vector< std::pair<void*, uint32_t> > pagelist = pc.GetPagelist();
assert( pagelist.size() == 4 );
assert( pagelist[0].first == btreeid && pagelist[0].second == 4 );
assert( pagelist[1].first == btreeid && pagelist[1].second == 3 );
assert( pagelist[2].first == btreeid && pagelist[2].second == 2 );
assert( pagelist[3].first == btreeid && pagelist[3].second == 1 );
}
pc.Retrieve(btreeid, 2, p1);
assert( pc.Verify() );
{
std::vector< std::pair<void*, uint32_t> > pagelist = pc.GetPagelist();
assert( pagelist.size() == 4 );
assert( pagelist[0].first == btreeid && pagelist[0].second == 2 );
assert( pagelist[1].first == btreeid && pagelist[1].second == 4 );
assert( pagelist[2].first == btreeid && pagelist[2].second == 3 );
assert( pagelist[3].first == btreeid && pagelist[3].second == 1 );
}
pc.Store(btreeid, 5, p1);
pc.Store(btreeid, 6, p1);
pc.Store(btreeid, 7, p1);
pc.Store(btreeid, 8, p1);
assert( pc.Verify() );
{
std::vector< std::pair<void*, uint32_t> > pagelist = pc.GetPagelist();
assert( pagelist.size() == 8 );
assert( pagelist[0].first == btreeid && pagelist[0].second == 8 );
assert( pagelist[1].first == btreeid && pagelist[1].second == 7 );
assert( pagelist[2].first == btreeid && pagelist[2].second == 6 );
assert( pagelist[3].first == btreeid && pagelist[3].second == 5 );
assert( pagelist[4].first == btreeid && pagelist[4].second == 2 );
assert( pagelist[5].first == btreeid && pagelist[5].second == 4 );
assert( pagelist[6].first == btreeid && pagelist[6].second == 3 );
assert( pagelist[7].first == btreeid && pagelist[7].second == 1 );
}
pc.Store(btreeid, 9, p1);
assert( pc.Verify() );
{
std::vector< std::pair<void*, uint32_t> > pagelist = pc.GetPagelist();
assert( pagelist.size() == 8 );
assert( pagelist[0].first == btreeid && pagelist[0].second == 9 );
assert( pagelist[1].first == btreeid && pagelist[1].second == 8 );
assert( pagelist[2].first == btreeid && pagelist[2].second == 7 );
assert( pagelist[3].first == btreeid && pagelist[3].second == 6 );
assert( pagelist[4].first == btreeid && pagelist[4].second == 5 );
assert( pagelist[5].first == btreeid && pagelist[5].second == 2 );
assert( pagelist[6].first == btreeid && pagelist[6].second == 4 );
assert( pagelist[7].first == btreeid && pagelist[7].second == 3 );
}
pc.Store(btreeid, 10, p1);
pc.Store(btreeid, 6, p1);
assert( pc.Verify() );
{
std::vector< std::pair<void*, uint32_t> > pagelist = pc.GetPagelist();
assert( pagelist.size() == 8 );
assert( pagelist[0].first == btreeid && pagelist[0].second == 6 );
assert( pagelist[1].first == btreeid && pagelist[1].second == 10 );
assert( pagelist[2].first == btreeid && pagelist[2].second == 9 );
assert( pagelist[3].first == btreeid && pagelist[3].second == 8 );
assert( pagelist[4].first == btreeid && pagelist[4].second == 7 );
assert( pagelist[5].first == btreeid && pagelist[5].second == 5 );
assert( pagelist[6].first == btreeid && pagelist[6].second == 2 );
assert( pagelist[7].first == btreeid && pagelist[7].second == 4 );
}
}
void test2_pagecache()
{
PageCache pc(32);
void* btreeid = reinterpret_cast<void*>(0x12345678);
BTreePage p1;
for (unsigned int i = 0; i < 1000; ++i)
{
if (i % 2 == 0)
{
pc.Store(btreeid, (i * 0x548A1B71) % 53, p1);
}
else
{
pc.Retrieve(btreeid, (i * 0x548A1B71) % 53, p1);
}
assert( pc.Verify() );
}
}
};
int main()
{
CBTreeDBTest().test1_pagecache();
CBTreeDBTest().test2_pagecache();
return 0;
}