daily pastebin goal
55%
SHARE
TWEET

Untitled

a guest Dec 7th, 2017 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. TEST_CASE("basicMine") {
  3.     std::stringstream stream;
  4.  
  5.     SECTION("void") {
  6.         //REQUIRE(PrettyPrint::typeString() == "void");
  7.     }
  8.  
  9.     SECTION("bool") {
  10.         REQUIRE(PrettyPrint::typeString(true) == "bool");
  11.         REQUIRE(PrettyPrint::dataString(true) == "true");
  12.     }
  13.  
  14.     SECTION("float") {
  15.         float val = 0.1f;
  16.         stream << "This is a " << PrettyPrint::type(val) << " that contains " << PrettyPrint::data(val);
  17.         REQUIRE(stream.str() == "This is a float that contains 0.1f");
  18.     }
  19.  
  20.     SECTION("pair") {
  21.         auto pair = std::make_pair(0.1, '\n');
  22.         REQUIRE(PrettyPrint::typeString(pair) == "pair<double, char>");
  23.     }
  24.  
  25.     SECTION("char") {
  26.         REQUIRE(PrettyPrint::typeString('a') == "char");
  27.         stream << PrettyPrint::data('\x1') << 100;
  28.         REQUIRE(stream.str() == "'\\x1'100");
  29.     }
  30.  
  31.     SECTION("string") {
  32.         std::string hello = "Hello";
  33.         //REQUIRE(PrettyPrint::typeString("ahoj") == "char[5]");
  34.         REQUIRE(PrettyPrint::typeString(hello) == "string");
  35.     }
  36.  
  37.     SECTION("map") {
  38.         const std::map<int, unsigned> m = { { 3, 14 }, { 7, 42 }, { 0, 0 } };
  39.         REQUIRE(PrettyPrint::dataString(m) == "map<int, unsigned>{ 0: 0u, 3: 14u, 7: 42u }");
  40.     }
  41.  
  42.     SECTION("vector") {
  43.         const std::vector<int> v = { };
  44.         REQUIRE(PrettyPrint::dataString(v) == "vector<int>{ }");
  45.         const std::vector<int> vi = {1, 2, 3};
  46.         REQUIRE(PrettyPrint::dataString(vi) == "vector<int>{ 1, 2, 3 }");
  47.         const std::vector<char> vc = {'a', 'b', 'c'};
  48.         REQUIRE(PrettyPrint::dataString(vc) == "vector<char>{ 'a', 'b', 'c' }");
  49.     }
  50.  
  51.     SECTION("set") {
  52.         const std::set<int> v = { };
  53.         REQUIRE(PrettyPrint::dataString(v) == "set<int>{ }");
  54.         const std::set<int> vi = {1, 2, 3};
  55.         REQUIRE(PrettyPrint::dataString(vi) == "set<int>{ 1, 2, 3 }");
  56.         const std::set<char> vc = {'a', 'b', 'c'};
  57.         REQUIRE(PrettyPrint::dataString(vc) == "set<char>{ 'a', 'b', 'c' }");
  58.     }
  59.  
  60.     SECTION("C_ptr") {
  61.         int i = -1;
  62.         int *ptr = &i;
  63.         std::stringstream s;
  64.         s << "int*{ ";
  65.         s << ptr;
  66.         s << " }";
  67.         std::cout << s.str() << std::endl;
  68.         REQUIRE(PrettyPrint::dataString(ptr) == s.str());
  69.  
  70.         int *ptrnull;
  71.         REQUIRE(PrettyPrint::dataString(ptrnull) == "int*{ nullptr }");
  72.     }
  73.  
  74.     SECTION("unique_ptr") {
  75.         const auto ptr = std::make_unique<int>(-1);
  76.         std::unique_ptr<int> ptrnull = nullptr;
  77.         std::stringstream s;
  78.         s << "int*{ ";
  79.         s << ptr.get();
  80.         s << " }";
  81.         std::cout << s.str() << std::endl;
  82.         std::cout << PrettyPrint::dataString(ptr) << std::endl;
  83.         REQUIRE_THAT(PrettyPrint::dataString(ptrnull), Catch::StartsWith("unique_ptr<int>{ "));
  84.         REQUIRE_THAT(PrettyPrint::dataString(ptr), Catch::StartsWith("unique_ptr<int>{ "));
  85.         REQUIRE_THAT(PrettyPrint::dataString(ptr), Catch::EndsWith(" }"));
  86.     }
  87.  
  88.     SECTION("array") {
  89.         int array[3] = { 0, 1, 2};
  90.         REQUIRE(PrettyPrint::typeString(array) == "int[3]");
  91.         REQUIRE(PrettyPrint::dataString(array) == "int[3]{ 0, 1, 2 }");
  92.     }
  93. }
  94.  
  95. struct Person {
  96.     std::string name;
  97.     std::string surname;
  98. };
  99.  
  100. namespace PrettyPrint {
  101.     template <>
  102.     struct Type<Person> {
  103.         std::ostream& operator()(std::ostream& os) const {
  104.             return os << "Person";
  105.         }
  106.     };
  107.  
  108.     template <>
  109.     struct Data<Person> {
  110.         Data(const Person& val) : val(val) {}
  111.         std::ostream& operator()(std::ostream& os) const {
  112.             return os << PrettyPrint::type(val) << ": " << val.name << " " << val.surname;
  113.         }
  114.         const Person& val;
  115.     };
  116. }
  117.  
  118.  
  119. TEST_CASE("basicR") {
  120.     std::stringstream stream;
  121.  
  122.     SECTION("bool") {
  123.         REQUIRE(PrettyPrint::typeString(true) == "bool");
  124.         REQUIRE(PrettyPrint::dataString(true) == "true");
  125.     }
  126.  
  127.     SECTION("float") {
  128.         float val = 0.1f;
  129.         stream << "This is a " << PrettyPrint::type(val) << " that contains " << PrettyPrint::data(val);
  130.         REQUIRE(stream.str() == "This is a float that contains 0.1f");
  131.     }
  132.  
  133.     SECTION("pair") {
  134.         auto pair = std::make_pair(0.1, '\n');
  135.         REQUIRE(PrettyPrint::typeString(pair) == "pair<double, char>");
  136.         REQUIRE(PrettyPrint::dataString(pair) == "pair<double, char>{ 0.1, '\\n' }");
  137.  
  138.     }
  139.  
  140.     SECTION("My nullptr"){
  141.         std::nullptr_t aNullptr;
  142.         REQUIRE(PrettyPrint::typeString(aNullptr) == "nullptr_t");
  143.         REQUIRE(PrettyPrint::dataString(nullptr) == "nullptr_t{ nullptr }");
  144.         //REQUIRE(PrettyPrint::dataString(aNullptr) == "nullptr_t{ nullptr }");
  145.  
  146.     }
  147.  
  148.  
  149.     SECTION("char") {
  150.         REQUIRE(PrettyPrint::typeString('a') == "char");
  151.         stream << PrettyPrint::data('\x1') << 100;
  152.         REQUIRE(stream.str() == "'\\x1'100");
  153.         REQUIRE(PrettyPrint::dataString('\f')  == "\'\\f\'");
  154.         int i = 5;
  155.         int *ptrInt = &i;
  156.  
  157.         /*char ch = 'c';
  158.         char *ptr = &ch;
  159.         REQUIRE(PrettyPrint::dataString(ptr) == "");*/
  160.     }
  161.  
  162.  
  163.     SECTION("string") {
  164.         std::string hello = "Hello";
  165.         REQUIRE(PrettyPrint::typeString("ahoj") == "char[5]");
  166.         //std::cout << PrettyPrint::dataString(hello);
  167.  
  168.         REQUIRE(PrettyPrint::dataString(hello) == "\"Hello\"");
  169.  
  170.         REQUIRE(PrettyPrint::dataString("abc") == "char[4]{ 'a', 'b', 'c', '\\0' }");
  171.         REQUIRE(PrettyPrint::typeString(hello) == "string");
  172.         REQUIRE(PrettyPrint::typeString("4") == "char[2]");
  173.     }
  174.  
  175.     SECTION("Vector"){
  176.         std::vector<int> vector = {5,6,3,1,05,123,456,78999};
  177.         std::vector<int> empty_vector = { };
  178.         REQUIRE(PrettyPrint::typeString(vector) == "vector<int>");
  179.         REQUIRE(PrettyPrint::dataString(vector) == "vector<int>{ 5, 6, 3, 1, 5, 123, 456, 78999 }");
  180.         REQUIRE(PrettyPrint::dataString(empty_vector) == "vector<int>{ }");
  181.         //std::cout << PrettyPrint::dataString(vector);
  182.     }
  183.  
  184.     SECTION("array") {
  185.         int mArray[5] = {0,1,2,3,4};
  186.         REQUIRE(PrettyPrint::typeString(mArray) == "int[5]");
  187.         REQUIRE(PrettyPrint::dataString(mArray) == "int[5]{ 0, 1, 2, 3, 4 }");
  188.     }
  189.  
  190.     SECTION("Set") {
  191.         std::set<int> set = {5, 6, 3, 1, 05};
  192.         std::set<int> empty_set = {};
  193.         REQUIRE(PrettyPrint::typeString(set) == "set<int>");
  194.         REQUIRE(PrettyPrint::dataString(set) == "set<int>{ 1, 3, 5, 6 }");
  195.         REQUIRE(PrettyPrint::dataString(empty_set) == "set<int>{ }");
  196.     }
  197.  
  198.     SECTION("map") {
  199.         const std::map<int, unsigned> m = { { 3, 14 }, { 7, 42 }, { 0, 0 } };
  200.         const std::map<std::string, float> m1 = {{"a", 0.1}, {"b", 0.2}};
  201.         const std::map<float, bool> empty_map = { };
  202.         REQUIRE(PrettyPrint::dataString(m) == "map<int, unsigned>{ 0: 0u, 3: 14u, 7: 42u }");
  203.         REQUIRE(PrettyPrint::dataString(m1) == "map<string, float>{ \"a\": 0.1f, \"b\": 0.2f }");
  204.         REQUIRE(PrettyPrint::dataString(empty_map) == "map<float, bool>{ }");
  205.     }
  206.  
  207.     SECTION("raw pointer"){
  208.         void* p;
  209.         int my_int = 30;
  210.         int* a = &my_int;
  211.         REQUIRE(PrettyPrint::typeString(p) == "void*");
  212.         REQUIRE(PrettyPrint::typeString(a) == "int*");
  213.         //std::cout << PrettyPrint::dataString(a) << "\n";
  214.         REQUIRE_THAT(PrettyPrint::dataString(a), Catch::StartsWith("int*{ "));
  215.         REQUIRE_THAT(PrettyPrint::dataString(a), Catch::EndsWith(" }"));
  216.  
  217.     }
  218.  
  219.  
  220.     SECTION("unique_ptr") {
  221.         const auto ptr = std::make_unique<int>(-1);
  222.         REQUIRE(PrettyPrint::typeString(ptr) == "unique_ptr<int>");
  223.         REQUIRE_THAT(PrettyPrint::dataString(ptr), Catch::StartsWith("unique_ptr<int>{ "));
  224.         REQUIRE_THAT(PrettyPrint::dataString(ptr), Catch::EndsWith(" }"));
  225.     }
  226.  
  227.  
  228. }
RAW Paste Data
Top