-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTest-stringtoolbox.cpp
More file actions
152 lines (136 loc) · 5.55 KB
/
Test-stringtoolbox.cpp
File metadata and controls
152 lines (136 loc) · 5.55 KB
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
/*
* MIT License
*
* Copyright (c) 2018 Christian Berger
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file
#include "catch.hpp"
#include "stringtoolbox.hpp"
TEST_CASE("Test ltrim.") {
std::string test1l{"ABC"};
std::string test2l{" ABC"};
std::string test3l{"ABC "};
std::string test4l{" ABC "};
std::string test5l;
std::string test6l{"A BC"};
std::string test7l{" A BC"};
std::string test8l{"A BC "};
std::string test9l{" A BC "};
REQUIRE(3 == stringtoolbox::ltrim(test1l).size());
REQUIRE(3 == stringtoolbox::ltrim(test2l).size());
REQUIRE(4 == stringtoolbox::ltrim(test3l).size());
REQUIRE(4 == stringtoolbox::ltrim(test4l).size());
REQUIRE(stringtoolbox::ltrim(test5l).empty());
REQUIRE(4 == stringtoolbox::ltrim(test6l).size());
REQUIRE(4 == stringtoolbox::ltrim(test7l).size());
REQUIRE(5 == stringtoolbox::ltrim(test8l).size());
REQUIRE(5 == stringtoolbox::ltrim(test9l).size());
}
TEST_CASE("Test rtrim.") {
std::string test1r{"ABC"};
std::string test2r{" ABC"};
std::string test3r{"ABC "};
std::string test4r{" ABC "};
std::string test5r;
std::string test6r{"A BC"};
std::string test7r{" A BC"};
std::string test8r{"A BC "};
std::string test9r{" A BC "};
std::string test10r{" \t\t "};
REQUIRE(3 == stringtoolbox::rtrim(test1r).size());
REQUIRE(4 == stringtoolbox::rtrim(test2r).size());
REQUIRE(3 == stringtoolbox::rtrim(test3r).size());
REQUIRE(4 == stringtoolbox::rtrim(test4r).size());
REQUIRE(stringtoolbox::rtrim(test5r).empty());
REQUIRE(4 == stringtoolbox::rtrim(test6r).size());
REQUIRE(5 == stringtoolbox::rtrim(test7r).size());
REQUIRE(4 == stringtoolbox::rtrim(test8r).size());
REQUIRE(5 == stringtoolbox::rtrim(test9r).size());
REQUIRE(stringtoolbox::rtrim(test10r).empty());
}
TEST_CASE("Test trim.") {
std::string test1t{"ABC"};
std::string test2t{" ABC"};
std::string test3t{"ABC "};
std::string test4t{" ABC "};
std::string test5t;
std::string test6t{"A BC"};
std::string test7t{" A BC"};
std::string test8t{"A BC "};
std::string test9t{" A BC "};
REQUIRE(3 == stringtoolbox::trim(test1t).size());
REQUIRE(3 == stringtoolbox::trim(test2t).size());
REQUIRE(3 == stringtoolbox::trim(test3t).size());
REQUIRE(3 == stringtoolbox::trim(test4t).size());
REQUIRE(stringtoolbox::trim(test5t).empty());
REQUIRE(4 == stringtoolbox::trim(test6t).size());
REQUIRE(4 == stringtoolbox::trim(test7t).size());
REQUIRE(4 == stringtoolbox::trim(test8t).size());
REQUIRE(4 == stringtoolbox::trim(test9t).size());
}
TEST_CASE("Test replace all") {
REQUIRE("_ABC" == stringtoolbox::replaceAll(" ABC", ' ', '_'));
REQUIRE("_ABC_" == stringtoolbox::replaceAll(" ABC ", ' ', '_'));
REQUIRE("_A_BC_" == stringtoolbox::replaceAll(" A BC ", ' ', '_'));
REQUIRE("_A_B__C__" == stringtoolbox::replaceAll(" A B C ", ' ', '_'));
}
TEST_CASE("Test split") {
std::string s1 = "abc";
std::vector<std::string> vs1 = stringtoolbox::split(s1, ',');
REQUIRE(vs1.size() == 1);
std::string s2 = "abc;def";
std::vector<std::string> vs2_no = stringtoolbox::split(s2, ',');
REQUIRE(vs2_no.size() == 1);
std::vector<std::string> vs2 = stringtoolbox::split(s2, ';');
REQUIRE(vs2.size() == 2);
REQUIRE(vs2.at(0) == "abc");
REQUIRE(vs2.at(1) == "def");
std::string s3 = ";abc;def";
std::vector<std::string> vs3 = stringtoolbox::split(s3, ';');
REQUIRE(vs3.size() == 3);
REQUIRE(vs3.at(1) == "abc");
REQUIRE(vs3.at(2) == "def");
std::string s4 = "abc;def;";
std::vector<std::string> vs4 = stringtoolbox::split(s4, ';');
REQUIRE(vs4.size() == 3);
REQUIRE(vs4.at(0) == "abc");
REQUIRE(vs4.at(1) == "def");
std::string s5 = ";abc;def;";
std::vector<std::string> vs5 = stringtoolbox::split(s5, ';');
REQUIRE(vs5.size() == 4);
REQUIRE(vs5.at(1) == "abc");
REQUIRE(vs5.at(2) == "def");
std::string s6 = ";abc;;def;";
std::vector<std::string> vs6 = stringtoolbox::split(s6, ';');
REQUIRE(vs6.size() == 5);
REQUIRE(vs6.at(1) == "abc");
REQUIRE(vs6.at(3) == "def");
}
TEST_CASE("Empty field") {
std::string s1 = "1;0;;57.71941;11.95701";
std::vector<std::string> vs1 = stringtoolbox::split(s1, ';');
REQUIRE(vs1.size() == 5);
REQUIRE(vs1.at(0) == "1");
REQUIRE(vs1.at(1) == "0");
REQUIRE(vs1.at(2) == "");
REQUIRE(vs1.at(3) == "57.71941");
REQUIRE(vs1.at(4) == "11.95701");
}