memgraph/tests/unit/skiplist.cpp

70 lines
1.9 KiB
C++
Raw Normal View History

2016-06-05 20:30:40 +08:00
#include <iostream>
#include "data_structures/skiplist/skiplist.hpp"
2016-06-15 06:06:21 +08:00
#include "utils/assert.hpp"
2016-06-05 20:30:40 +08:00
using std::cout;
using std::endl;
using skiplist_t = SkipList<int, int>;
2016-06-15 06:06:21 +08:00
void print_skiplist(const skiplist_t::Accessor &skiplist)
2016-06-05 20:30:40 +08:00
{
cout << "---- skiplist now has: ";
2016-06-15 06:06:21 +08:00
for (auto &kv : skiplist)
2016-06-05 20:30:40 +08:00
cout << "(" << kv.first << ", " << kv.second << ") ";
cout << "----" << endl;
}
int main(void)
{
skiplist_t skiplist;
auto accessor = skiplist.access();
// insert 10
2016-06-15 06:06:21 +08:00
permanent_assert(accessor.insert_unique(1, 10).second == true,
"add first element");
2016-06-05 20:30:40 +08:00
// try insert 10 again (should fail)
2016-06-15 06:06:21 +08:00
permanent_assert(accessor.insert_unique(1, 10).second == false,
"add the same element, should fail");
2016-06-05 20:30:40 +08:00
// insert 20
2016-06-15 06:06:21 +08:00
permanent_assert(accessor.insert_unique(2, 20).second == true,
"insert new unique element");
2016-06-05 20:30:40 +08:00
print_skiplist(accessor);
2016-06-15 06:06:21 +08:00
2016-06-05 20:30:40 +08:00
// value at key 3 shouldn't exist
2016-06-15 06:06:21 +08:00
permanent_assert((accessor.find(3) == accessor.end()) == true,
"try to find element which doesn't exist");
2016-06-05 20:30:40 +08:00
// value at key 2 should exist
2016-06-15 06:06:21 +08:00
permanent_assert((accessor.find(2) != accessor.end()) == true,
"find iterator");
2016-06-05 20:30:40 +08:00
// at key 2 is 20 (true)
2016-06-15 06:06:21 +08:00
permanent_assert(accessor.find(2)->second == 20, "find element");
2016-06-05 20:30:40 +08:00
// removed existing (1)
2016-06-15 06:06:21 +08:00
permanent_assert(accessor.remove(1) == true, "try to remove element");
2016-06-05 20:30:40 +08:00
// removed non-existing (3)
2016-06-15 06:06:21 +08:00
permanent_assert(accessor.remove(3) == false,
"try to remove element which doesn't exist");
2016-06-05 20:30:40 +08:00
// insert (1, 10)
2016-06-15 06:06:21 +08:00
permanent_assert(accessor.insert_unique(1, 10).second == true,
"insert unique element");
2016-06-05 20:30:40 +08:00
// insert (4, 40)
2016-06-15 06:06:21 +08:00
permanent_assert(accessor.insert_unique(4, 40).second == true,
"insert unique element");
2016-06-05 20:30:40 +08:00
print_skiplist(accessor);
return 0;
}