Advertisement
Guest User

Main.cpp

a guest
May 4th, 2016
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.15 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <type_traits>
  5. #include <cassert>
  6. #include <boost\filesystem.hpp>
  7. #include "PersistentMap.hpp"
  8.  
  9.  
  10. using namespace std;
  11.  
  12. using namespace boost::filesystem;
  13. namespace fs = boost::filesystem;
  14.  
  15. //void boost_example() {
  16. //    path p("c:/temp");  
  17. //
  18. //    for (auto tmp : fs::directory_iterator(p)) {
  19. //        if (fs::is_regular_file(tmp)) {
  20. //            std::cout << tmp.path().filename().string() << std::endl;
  21. //        }
  22. //    }
  23. //}
  24.  
  25. void modify(int& a) {
  26.     a += 5;
  27. }
  28.  
  29. /**
  30.  * This function may fail during the run time, however it should not
  31.  * prevent compiling of the correctly implemented task.
  32.  */
  33. void compile_time_tests() {
  34.     // const iterator conformance
  35.  
  36.     using mp = lib::PersistentMap<int, int>;
  37.     using mp_it1 = mp::const_iterator;
  38.     using mp_it2 = mp::iterator;
  39.  
  40.     {
  41.         // const iterator
  42.         static_assert(std::is_default_constructible<mp_it1>::value, "iterator is not default constructible");
  43.         mp_it1 tmp1;
  44.         *tmp1;
  45.         static_assert(std::is_reference<decltype(*tmp1)>::value, "returned value by dereferencing iterator is not reference");
  46.         //static_assert(std::is_const<decltype(*tmp1)>::value, "returned value by dereferencing iterator is not const");
  47.  
  48.         mp_it1 tmp2 = ++tmp1;
  49.         mp_it1 tmp3 = tmp1++;
  50.         mp_it1 tmp4(tmp1++);
  51.  
  52.         tmp1 == tmp2;
  53.         tmp1 != tmp2;
  54.     }
  55.  
  56.     {
  57.         // non-const iterator
  58.         static_assert(std::is_default_constructible<mp_it2>::value, "const iterator is not default constructible");
  59.         mp_it2 tmp1;
  60.         *tmp1;
  61.         static_assert(std::is_reference<decltype(*tmp1)>::value, "returned value by dereferencing iterator is not reference");
  62.         static_assert(!std::is_const<decltype(*tmp1)>::value, "returned value by dereferencing iterator is not const");
  63.         mp_it2 tmp2 = ++tmp1;
  64.         mp_it2 tmp3 = tmp1++;
  65.         mp_it2 tmp4(tmp1++);
  66.  
  67.         tmp1 == tmp2;
  68.         tmp1 != tmp2;
  69.     }
  70.  
  71.     {
  72.         mp_it1 tmp1;
  73.         mp_it2 tmp2;
  74.         static_assert(std::is_convertible<mp_it2, mp_it1>::value, "iterator is not convertible to const iterator");
  75.         tmp1 == (mp_it1)tmp2;
  76.         tmp1 != tmp2;
  77.     }
  78. }
  79.  
  80. int main(int argc, char* argv[]) {
  81.  
  82.     // boost_example();
  83.     // compile_time_tests();
  84.  
  85.     //////////////////////////////////////////////////////////////////////////
  86.    
  87.     {
  88.         // basic test
  89.  
  90.         using pm1_t = lib::PersistentMap<std::string, std::string>;
  91.         pm1_t myMap("TestDirectory");
  92.         myMap["file20.txt"] = "content 20";
  93.         myMap["file10.txt"] = "content 10";
  94.         myMap["file15.txt"] = "content 15";
  95.  
  96.         std::cout << "===============================" << std::endl;
  97.  
  98.         // iterate over all items I
  99.  
  100.         std::string file;
  101.         std::string content;
  102.  
  103.         for (auto& i : myMap) {
  104.             std::tie(file, content) = i;
  105.             std::cout << file << ": " << content << std::endl;
  106.         }
  107.  
  108.         std::cout << "===============================" << std::endl;
  109.  
  110.         // iterate over all items II
  111.         for (pm1_t::iterator it = myMap.begin();
  112.              it != myMap.end();
  113.              ++it) {
  114.  
  115.             std::tie(file, content) = *it;
  116.             std::cout << file << ": " << content << std::endl;
  117.                
  118.         }
  119.     }
  120.  
  121.     //////////////////////////////////////////////////////////////////////////
  122.  
  123.     using pm2_t = lib::PersistentMap<int, int>;
  124.  
  125.     {
  126.         pm2_t myMap("Test1");
  127.  
  128.         std::cout << "===============================" << std::endl;
  129.  
  130.         // iterate over all items III
  131.         for (auto it : myMap) {
  132.             std::cout << it.second << std::endl;
  133.         }
  134.  
  135.         myMap[1] = 10;
  136.         myMap[2] = 20;
  137.  
  138.         int c = 30;
  139.         myMap[3] = c;
  140.  
  141.         modify(c);
  142.         modify(myMap[2]);
  143.  
  144.         int a = myMap[1];
  145.         int b = myMap[2];
  146.  
  147.  
  148.         assert(a == 10);
  149.         assert(b == 25);
  150.         assert(c == 35);
  151.  
  152.         int ss= myMap[2] + 20;
  153.         myMap[3] = myMap[2] + 20;
  154.         assert(myMap[2] == 45);
  155.  
  156.         myMap[2] = myMap[2] + 20;
  157.         assert(myMap[2] == 65);
  158.  
  159.         std::cout << "===============================" << std::endl;
  160.  
  161.         // iterate over all items IV
  162.         for (pm2_t::value_type& it : myMap) {
  163.             it.second = it.second + 1;
  164.         }
  165.     }
  166.  
  167.     {
  168.         pm2_t newMyMap("Test1");
  169.  
  170.         newMyMap[3] = 33;
  171.         assert(newMyMap[3] == 33);
  172.  
  173.         // insertion test I
  174.         auto result = newMyMap.insert(std::make_pair(0, 123));
  175.         assert(result.second == true);
  176.  
  177.         pm2_t::iterator it = result.first;
  178.         int v = (*(it++)).second;
  179.         assert(v == 123);
  180.         v = (*(it++)).second;
  181.         assert(v == 11);
  182.         v = (*(it++)).second;
  183.         assert(v == 66);
  184.         v = (*(it++)).second;
  185.         assert(v == 33);
  186.         assert(it == newMyMap.end());
  187.  
  188.         // insertion test II
  189.         result = newMyMap.insert(std::make_pair(0, 456));
  190.         assert(result.second == false);
  191.         it = result.first;
  192.         assert((*it).second == 123);
  193.     }
  194.  
  195.     return 0;
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement