File: StdContainers.cpp

package info (click to toggle)
plog 1.1.11-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,404 kB
  • sloc: cpp: 13,637; ansic: 473; sh: 24; makefile: 4
file content (159 lines) | stat: -rw-r--r-- 4,627 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include "Common.h"
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <string>

#ifdef __cpp_lib_span
#   include <span>
#endif

SCENARIO("std containers")
{
    GIVEN("logger is initialised")
    {
        plog::TestAppender testAppender;
        plog::Logger<PLOG_DEFAULT_INSTANCE_ID> logger(plog::verbose);
        logger.addAppender(&testAppender);

        WHEN("empty collection")
        {
            std::vector<int> vectorOfInts;
            PLOGI << vectorOfInts;

            THEN("the result is as expected")
            {
                CHECK_EQ(testAppender.getMessage(), PLOG_NSTR("[]"));
            }
        }

        WHEN("std::vector")
        {
            std::vector<int> vectorOfInts;
            vectorOfInts.push_back(1);
            vectorOfInts.push_back(2);
            vectorOfInts.push_back(3);
            PLOGI << vectorOfInts;

            THEN("the result is as expected")
            {
                CHECK_EQ(testAppender.getMessage(), PLOG_NSTR("[1, 2, 3]"));
            }
        }

        WHEN("std::deque")
        {
            std::deque<std::string> dequeOfStrings;
            dequeOfStrings.push_back("one");
            dequeOfStrings.push_back("two");
            dequeOfStrings.push_back("three");
            PLOGI << dequeOfStrings;

            THEN("the result is as expected")
            {
                CHECK_EQ(testAppender.getMessage(), PLOG_NSTR("[one, two, three]"));
            }
        }

        WHEN("std::list")
        {
            std::list<const char*> listOfCharPointers;
            listOfCharPointers.push_back("one");
            listOfCharPointers.push_back("two");
            listOfCharPointers.push_back(NULL);
            PLOGI << listOfCharPointers;

            THEN("the result is as expected")
            {
                CHECK_EQ(testAppender.getMessage(), PLOG_NSTR("[one, two, (null)]"));
            }
        }

        WHEN("std::set")
        {
            std::set<int> setOfInts;
            setOfInts.insert(10);
            setOfInts.insert(20);
            setOfInts.insert(30);
            PLOGI << setOfInts;

            THEN("the result is as expected")
            {
                CHECK_EQ(testAppender.getMessage(), PLOG_NSTR("[10, 20, 30]"));
            }
        }

        WHEN("std::map")
        {
            std::map<std::string, int> mapStringToInt;
            mapStringToInt["red"] = 1;
            mapStringToInt["green"] = 2;
            mapStringToInt["blue"] = 4;
            PLOGI << mapStringToInt;

            THEN("the result is as expected")
            {
                CHECK_EQ(testAppender.getMessage(), PLOG_NSTR("[blue:4, green:2, red:1]"));
            }
        }

        WHEN("std::multimap")
        {
            std::multimap<int, std::string> multimapIntToString;
            multimapIntToString.insert(std::make_pair(1, "one"));
            multimapIntToString.insert(std::make_pair(1, "uno"));
            multimapIntToString.insert(std::make_pair(2, "two"));
            multimapIntToString.insert(std::make_pair(2, "due"));
            PLOGI << multimapIntToString;

            THEN("the result is as expected")
            {
                CHECK_EQ(testAppender.getMessage(), PLOG_NSTR("[1:one, 1:uno, 2:two, 2:due]"));
            }
        }

        WHEN("std::vector of std::vector")
        {
            std::vector<std::vector<int> > vectorOfVectorsOfInts(3);
            vectorOfVectorsOfInts[0].push_back(1);
            vectorOfVectorsOfInts[0].push_back(2);
            vectorOfVectorsOfInts[1].push_back(-1);
            vectorOfVectorsOfInts[1].push_back(-2);
            PLOGI << vectorOfVectorsOfInts;

            THEN("the result is as expected")
            {
                CHECK_EQ(testAppender.getMessage(), PLOG_NSTR("[[1, 2], [-1, -2], []]"));
            }
        }

        WHEN("std::pair")
        {
            std::pair<int, int> pairOfInts(5, 10);
            PLOGI << pairOfInts;

            THEN("the result is as expected")
            {
                CHECK_EQ(testAppender.getMessage(), PLOG_NSTR("5:10"));
            }
        }

#if 0 // std::span is not supported yet as it has no const_iterator till c++23
#ifdef __cpp_lib_span
        WHEN("std::span")
        {
            int arr[] = {1, 2, 3};
            std::span<int> spanOfInts(std::begin(arr), std::end(arr));
            PLOGI << spanOfInts;

            THEN("the result is as expected")
            {
                CHECK_EQ(testAppender.getMessage(), PLOG_NSTR("[1, 2, 3]"));
            }
        }
#endif
#endif
    }
}