forked from varoudis/depthmapX
-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathteststringutils.cpp
More file actions
230 lines (204 loc) · 6.92 KB
/
Copy pathteststringutils.cpp
File metadata and controls
230 lines (204 loc) · 6.92 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// Copyright (C) 2017-2018 Christian Sailer
// Copyright (C) 2017-2018 Petros Koutsolampros
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "catch.hpp"
#include "../genlib/stringutils.h"
#include "../cliTest/selfcleaningfile.h"
#include <fstream>
TEST_CASE("Tests for split function", "")
{
{
std::vector<std::string> stringParts = dXstring::split("foo,bar",',');
REQUIRE(stringParts.size() == 2);
REQUIRE(stringParts[0] == "foo");
REQUIRE(stringParts[1] == "bar");
}
{
std::vector<std::string> stringParts = dXstring::split("0.5,1.2",',');
REQUIRE(stringParts.size() == 2);
REQUIRE(stringParts[0] == "0.5");
REQUIRE(stringParts[1] == "1.2");
}
{
std::vector<std::string> stringParts = dXstring::split("0.5\t1.2",'\t');
REQUIRE(stringParts.size() == 2);
REQUIRE(stringParts[0] == "0.5");
REQUIRE(stringParts[1] == "1.2");
}
{
std::vector<std::string> stringParts = dXstring::split("0.5\t1.2\tfoo",'\t');
REQUIRE(stringParts.size() == 3);
REQUIRE(stringParts[0] == "0.5");
REQUIRE(stringParts[1] == "1.2");
REQUIRE(stringParts[2] == "foo");
}
{
// skip last blank element
std::vector<std::string> stringParts = dXstring::split("foo,bar,",',');
REQUIRE(stringParts.size() == 2);
}
{
// do not skip middle blank element
std::vector<std::string> stringParts = dXstring::split("foo,,bar",',');
REQUIRE(stringParts.size() == 3);
}
{
// do skip any empty elements when flag is set
// do not skip middle blank element
std::vector<std::string> stringParts = dXstring::split("foo,,bar",',', true);
REQUIRE(stringParts.size() == 2);
}
}
TEST_CASE("Read String")
{
{
// case empty string - just read 0 length and return new string object
SelfCleaningFile f("test.bin");
{
std::ofstream fs(f.Filename());
unsigned int length = 0;
fs.write(reinterpret_cast<char *>(&length), sizeof(unsigned int));
}
std::ifstream fs(f.Filename());
auto result = dXstring::readString(fs);
REQUIRE(result.empty());
}
// case non empty string - read length and then beam data into the string
{
SelfCleaningFile f("test.bin");
{
std::ofstream fs(f.Filename());
unsigned int length = 5;
const char *payload = "abcde";
fs.write(reinterpret_cast<char *>(&length), sizeof(unsigned int));
fs.write(payload, 5);
}
std::ifstream fs(f.Filename());
std::string result = dXstring::readString(fs);
REQUIRE(result == "abcde");
}
}
TEST_CASE("Write String")
{
{
// case empty string - just write 0 length
std::string testString;
SelfCleaningFile f("test.bin");
{
std::ofstream fs(f.Filename());
dXstring::writeString(fs, testString);
}
std::ifstream fs(f.Filename());
unsigned int length;
fs.read(reinterpret_cast<char *>(&length), sizeof(unsigned int));
REQUIRE(length == 0);
char dummy[1];
REQUIRE_FALSE(fs.read(dummy, 1));
REQUIRE(fs.eof());
}
{
// case non empty string - just write length plus content
std::string testString("cdfe");
SelfCleaningFile f("test.bin");
{
std::ofstream fs(f.Filename());
dXstring::writeString(fs, testString);
}
std::ifstream fs(f.Filename());
unsigned int length;
fs.read(reinterpret_cast<char *>(&length), sizeof(unsigned int));
REQUIRE(length == testString.length());
char buffer[5];
buffer[4] = '\0';
fs.read(buffer, length);
REQUIRE(testString == buffer);
char dummy[1];
REQUIRE_FALSE(fs.read(dummy, 1));
REQUIRE(fs.eof());
}
}
TEST_CASE("test string format")
{
REQUIRE(dXstring::formatString(1.0, "foo") == "foo");
REQUIRE(dXstring::formatString(1.0, "%+.16le") == "+1.0000000000000000e+00");
REQUIRE(dXstring::formatString(1.0) == "+1.0000000000000000e+00");
REQUIRE(dXstring::formatString(1.0, "%+.8le") == "+1.00000000e+00");
REQUIRE(dXstring::formatString(1 ) == " 1");
}
TEST_CASE("test tolower")
{
std::string tstr = "AbdUgs24*hHÜ";
auto result = dXstring::toLower(tstr);
REQUIRE(tstr == "abdugs24*hhÜ");
REQUIRE(result == "abdugs24*hhÜ");
}
TEST_CASE("test ltrim")
{
std::string normal = " fo o ";
dXstring::ltrim(normal);
REQUIRE( normal == "fo o ");
std::string empty = "";
dXstring::ltrim(empty);
REQUIRE( empty == "" );
std::string justBlanks = " ";
dXstring::ltrim(justBlanks);
REQUIRE( justBlanks == "");
std::string noBlanks = "foo ";
dXstring::ltrim(noBlanks);
REQUIRE(noBlanks == "foo ");
}
TEST_CASE("test rtrim")
{
std::string normal = " fo o ";
dXstring::rtrim(normal);
REQUIRE( normal == " fo o");
std::string empty = "";
dXstring::rtrim(empty);
REQUIRE( empty == "" );
std::string justBlanks = " ";
dXstring::rtrim(justBlanks);
REQUIRE( justBlanks == "");
std::string noBlanks = "foo ";
dXstring::rtrim(noBlanks);
REQUIRE(noBlanks == "foo");
}
TEST_CASE("test makeInitCaps")
{
std::string tstr = "abC DEf dEf \"fOO Bar\" blah bLuB";
dXstring::makeInitCaps(tstr);
REQUIRE(tstr == "Abc Def Def \"fOO Bar\" Blah Blub");
}
TEST_CASE("test isDouble")
{
REQUIRE(dXstring::isDouble("0"));
REQUIRE(dXstring::isDouble(" 1.345e23.1"));
REQUIRE_FALSE(dXstring::isDouble(""));
REQUIRE_FALSE(dXstring::isDouble("foo1234"));
}
TEST_CASE("test begins with")
{
REQUIRE(dXstring::beginsWith<std::string>("abcd", "abcd"));
REQUIRE(dXstring::beginsWith<std::string>("abcde", "abcd"));
REQUIRE_FALSE(dXstring::beginsWith<std::string>("abcd", "abcde"));
REQUIRE_FALSE(dXstring::beginsWith<std::string>("abcd", "ef"));
REQUIRE_FALSE(dXstring::beginsWith<std::string>("abcd", "aec"));
}
TEST_CASE("test safeGetline")
{
std::stringstream stream;
std::string out;
SECTION("Windows") { stream << "Test\r"; }
SECTION("Unix") { stream << "Test\n"; }
SECTION("Mixed") { stream << "Test\r\n"; }
dXstring::safeGetline(stream, out);
REQUIRE(out == "Test");
}