Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- #include <type_traits>
- #include <cassert>
- #include <boost\filesystem.hpp>
- #include "PersistentMap.hpp"
- using namespace std;
- using namespace boost::filesystem;
- namespace fs = boost::filesystem;
- //void boost_example() {
- // path p("c:/temp");
- //
- // for (auto tmp : fs::directory_iterator(p)) {
- // if (fs::is_regular_file(tmp)) {
- // std::cout << tmp.path().filename().string() << std::endl;
- // }
- // }
- //}
- void modify(int& a) {
- a += 5;
- }
- /**
- * This function may fail during the run time, however it should not
- * prevent compiling of the correctly implemented task.
- */
- void compile_time_tests() {
- // const iterator conformance
- using mp = lib::PersistentMap<int, int>;
- using mp_it1 = mp::const_iterator;
- using mp_it2 = mp::iterator;
- {
- // const iterator
- static_assert(std::is_default_constructible<mp_it1>::value, "iterator is not default constructible");
- mp_it1 tmp1;
- *tmp1;
- static_assert(std::is_reference<decltype(*tmp1)>::value, "returned value by dereferencing iterator is not reference");
- //static_assert(std::is_const<decltype(*tmp1)>::value, "returned value by dereferencing iterator is not const");
- mp_it1 tmp2 = ++tmp1;
- mp_it1 tmp3 = tmp1++;
- mp_it1 tmp4(tmp1++);
- tmp1 == tmp2;
- tmp1 != tmp2;
- }
- {
- // non-const iterator
- static_assert(std::is_default_constructible<mp_it2>::value, "const iterator is not default constructible");
- mp_it2 tmp1;
- *tmp1;
- static_assert(std::is_reference<decltype(*tmp1)>::value, "returned value by dereferencing iterator is not reference");
- static_assert(!std::is_const<decltype(*tmp1)>::value, "returned value by dereferencing iterator is not const");
- mp_it2 tmp2 = ++tmp1;
- mp_it2 tmp3 = tmp1++;
- mp_it2 tmp4(tmp1++);
- tmp1 == tmp2;
- tmp1 != tmp2;
- }
- {
- mp_it1 tmp1;
- mp_it2 tmp2;
- static_assert(std::is_convertible<mp_it2, mp_it1>::value, "iterator is not convertible to const iterator");
- tmp1 == (mp_it1)tmp2;
- tmp1 != tmp2;
- }
- }
- int main(int argc, char* argv[]) {
- // boost_example();
- // compile_time_tests();
- //////////////////////////////////////////////////////////////////////////
- {
- // basic test
- using pm1_t = lib::PersistentMap<std::string, std::string>;
- pm1_t myMap("TestDirectory");
- myMap["file20.txt"] = "content 20";
- myMap["file10.txt"] = "content 10";
- myMap["file15.txt"] = "content 15";
- std::cout << "===============================" << std::endl;
- // iterate over all items I
- std::string file;
- std::string content;
- for (auto& i : myMap) {
- std::tie(file, content) = i;
- std::cout << file << ": " << content << std::endl;
- }
- std::cout << "===============================" << std::endl;
- // iterate over all items II
- for (pm1_t::iterator it = myMap.begin();
- it != myMap.end();
- ++it) {
- std::tie(file, content) = *it;
- std::cout << file << ": " << content << std::endl;
- }
- }
- //////////////////////////////////////////////////////////////////////////
- using pm2_t = lib::PersistentMap<int, int>;
- {
- pm2_t myMap("Test1");
- std::cout << "===============================" << std::endl;
- // iterate over all items III
- for (auto it : myMap) {
- std::cout << it.second << std::endl;
- }
- myMap[1] = 10;
- myMap[2] = 20;
- int c = 30;
- myMap[3] = c;
- modify(c);
- modify(myMap[2]);
- int a = myMap[1];
- int b = myMap[2];
- assert(a == 10);
- assert(b == 25);
- assert(c == 35);
- int ss= myMap[2] + 20;
- myMap[3] = myMap[2] + 20;
- assert(myMap[2] == 45);
- myMap[2] = myMap[2] + 20;
- assert(myMap[2] == 65);
- std::cout << "===============================" << std::endl;
- // iterate over all items IV
- for (pm2_t::value_type& it : myMap) {
- it.second = it.second + 1;
- }
- }
- {
- pm2_t newMyMap("Test1");
- newMyMap[3] = 33;
- assert(newMyMap[3] == 33);
- // insertion test I
- auto result = newMyMap.insert(std::make_pair(0, 123));
- assert(result.second == true);
- pm2_t::iterator it = result.first;
- int v = (*(it++)).second;
- assert(v == 123);
- v = (*(it++)).second;
- assert(v == 11);
- v = (*(it++)).second;
- assert(v == 66);
- v = (*(it++)).second;
- assert(v == 33);
- assert(it == newMyMap.end());
- // insertion test II
- result = newMyMap.insert(std::make_pair(0, 456));
- assert(result.second == false);
- it = result.first;
- assert((*it).second == 123);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement