1 // Copyright 2015, Tobias Hermann and the FunctionalPlus contributors.
2 // https://github.com/Dobiasd/FunctionalPlus
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt or copy at
5 //  http://www.boost.org/LICENSE_1_0.txt)
6 
7 #include <doctest/doctest.h>
8 #include <fplus/fplus.hpp>
9 
10 namespace {
11     typedef std::vector<int> IntVector;
12     IntVector xs = {1,2,2,3,2};
13     std::string xsShown("[1, 2, 2, 3, 2]");
14 
15     typedef std::vector<std::string> StringVector;
16     StringVector stringVec = {"foo", "bar"};
17     std::string stringVecShown("[foo, bar]");
18 
19     typedef std::set<int> IntSet;
20     IntSet xsSet = {1,2,3};
21     std::string xsSetShown("[1, 2, 3]");
22 
23     typedef std::set<std::string> StringSet;
24     StringSet stringSet = {"foo", "bar"};
25     std::string stringSetShown("[bar, foo]");
26 }
27 
28 TEST_CASE("show_test - show")
29 {
30     using namespace fplus;
31     std::map<int, std::string> mapToShow = {{1, "one"}, {2, "two"}};
32     REQUIRE_EQ(show_cont(mapToShow), "[(1, one), (2, two)]");
33 
34     REQUIRE_EQ(show_cont(xs), xsShown);
35     REQUIRE_EQ(show(xs), xsShown);
36 
37     REQUIRE_EQ(show_cont(xsSet), xsSetShown);
38     REQUIRE_EQ(show(xsSet), xsSetShown);
39 
40     REQUIRE_EQ(show_cont(stringVec), stringVecShown);
41     REQUIRE_EQ(show(stringVec), stringVecShown);
42 
43     REQUIRE_EQ(show_cont(stringSet), stringSetShown);
44     REQUIRE_EQ(show(stringSet), stringSetShown);
45 
46     REQUIRE_EQ(show_cont_with(", ", xs), xsShown);
47     std::string xsShownNLs = "(1,2,\n"
48                              " 2,3,\n"
49                              " 2)";
50     REQUIRE_EQ(show_cont_with_frame_and_newlines(",", "(", ")", xs, 2), xsShownNLs);
51     REQUIRE_EQ(show<int>(1), "1");
52 
53     REQUIRE_EQ(show(std::vector<std::vector<int>>({{1,2,3},{4,5,6}})), "[[1, 2, 3], [4, 5, 6]]");
54 }
55 
56 TEST_CASE("show_test - show_float")
57 {
58     using namespace fplus;
59     const double pi = 3.14159;
60     REQUIRE_EQ(show_float<double>(0, 3, pi), "3.142");
61     REQUIRE_EQ(show_float<double>(1, 3, pi), "3.142");
62     REQUIRE_EQ(show_float<double>(2, 3, pi), "03.142");
63     REQUIRE_EQ(show_float<double>(3, 3, pi), "003.142");
64     REQUIRE_EQ(show_float<double>(1, 2, pi), "3.14");
65     REQUIRE_EQ(show_float<double>(1, 4, pi), "3.1416");
66     REQUIRE_EQ(show_float<double>(1, 7, pi), "3.1415900");
67     REQUIRE_EQ(show_float<double>(0, 3, -pi), "-3.142");
68     REQUIRE_EQ(show_float<double>(1, 3, -pi), "-3.142");
69     REQUIRE_EQ(show_float<double>(2, 3, -pi), "-3.142");
70     REQUIRE_EQ(show_float<double>(3, 3, -pi), "-03.142");
71     REQUIRE_EQ(show_float<double>(4, 3, -pi), "-003.142");
72     REQUIRE_EQ(show_float<double>(0, 3, 0.142), "0.142");
73     REQUIRE_EQ(show_float<double>(1, 3, 0.142), "0.142");
74     REQUIRE_EQ(show_float<double>(2, 3, 0.142), "00.142");
75     REQUIRE_EQ(fill_left(' ', 8, show_float<double>(0, 3, -pi)), "  -3.142");
76 
77     REQUIRE_EQ(show_float_fill_left<double>(' ', 8, 3, pi), "   3.142");
78     REQUIRE_EQ(show_float_fill_left<double>(' ', 8, 6, pi), "3.141590");
79     REQUIRE_EQ(show_float_fill_left<double>(' ', 8, 3, -pi), "  -3.142");
80     REQUIRE_EQ(show_float_fill_left<double>(' ', 2, 3, -pi), "-3.142");
81 
82     REQUIRE_EQ(show_fill_left<int>(' ', 4, 3), "   3");
83     REQUIRE_EQ(show_fill_left<int>('0', 4, 3), "0003");
84     REQUIRE_EQ(show_fill_left<int>(' ', 4, 12345), "12345");
85 
86     REQUIRE_EQ(show_fill_right<int>(' ', 4, 3), "3   ");
87     REQUIRE_EQ(show_fill_right<int>(' ', 4, 12345), "12345");
88 }
89