2305 |
02 Aug 10 |
peter |
// $Id$ |
2305 |
02 Aug 10 |
peter |
2 |
|
2305 |
02 Aug 10 |
peter |
3 |
/* |
4207 |
26 Aug 22 |
peter |
Copyright (C) 2010, 2012, 2022 Peter Johansson |
2305 |
02 Aug 10 |
peter |
5 |
|
2305 |
02 Aug 10 |
peter |
This file is part of the yat library, http://dev.thep.lu.se/yat |
2305 |
02 Aug 10 |
peter |
7 |
|
2305 |
02 Aug 10 |
peter |
The yat library is free software; you can redistribute it and/or |
2305 |
02 Aug 10 |
peter |
modify it under the terms of the GNU General Public License as |
2305 |
02 Aug 10 |
peter |
published by the Free Software Foundation; either version 3 of the |
2305 |
02 Aug 10 |
peter |
License, or (at your option) any later version. |
2305 |
02 Aug 10 |
peter |
12 |
|
2305 |
02 Aug 10 |
peter |
The yat library is distributed in the hope that it will be useful, |
2305 |
02 Aug 10 |
peter |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
2305 |
02 Aug 10 |
peter |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
2305 |
02 Aug 10 |
peter |
General Public License for more details. |
2305 |
02 Aug 10 |
peter |
17 |
|
2305 |
02 Aug 10 |
peter |
You should have received a copy of the GNU General Public License |
2305 |
02 Aug 10 |
peter |
along with yat. If not, see <http://www.gnu.org/licenses/>. |
2305 |
02 Aug 10 |
peter |
20 |
*/ |
2305 |
02 Aug 10 |
peter |
21 |
|
2881 |
18 Nov 12 |
peter |
22 |
#include <config.h> |
2881 |
18 Nov 12 |
peter |
23 |
|
2305 |
02 Aug 10 |
peter |
24 |
#include "Suite.h" |
2305 |
02 Aug 10 |
peter |
25 |
|
2305 |
02 Aug 10 |
peter |
26 |
#include "yat/utility/split.h" |
2305 |
02 Aug 10 |
peter |
27 |
|
2305 |
02 Aug 10 |
peter |
28 |
#include <string> |
2305 |
02 Aug 10 |
peter |
29 |
#include <vector> |
2305 |
02 Aug 10 |
peter |
30 |
|
2305 |
02 Aug 10 |
peter |
31 |
using namespace theplu::yat; |
2305 |
02 Aug 10 |
peter |
32 |
|
2879 |
16 Nov 12 |
peter |
33 |
template<typename T> |
2305 |
02 Aug 10 |
peter |
34 |
void test_split(const std::vector<std::string>& correct, const std::string& str, |
2879 |
16 Nov 12 |
peter |
35 |
T delim, test::Suite& suite); |
2305 |
02 Aug 10 |
peter |
36 |
|
2305 |
02 Aug 10 |
peter |
37 |
int main(int argc, char* argv[]) |
4200 |
19 Aug 22 |
peter |
38 |
{ |
2305 |
02 Aug 10 |
peter |
39 |
test::Suite suite(argc, argv); |
2305 |
02 Aug 10 |
peter |
40 |
|
2305 |
02 Aug 10 |
peter |
41 |
std::vector<std::string> correct; |
2305 |
02 Aug 10 |
peter |
42 |
correct.resize(1); |
2305 |
02 Aug 10 |
peter |
43 |
correct[0]="banan"; |
2305 |
02 Aug 10 |
peter |
44 |
test_split(correct, "banan", 'x', suite); |
2305 |
02 Aug 10 |
peter |
45 |
|
2305 |
02 Aug 10 |
peter |
46 |
correct.resize(3); |
2305 |
02 Aug 10 |
peter |
47 |
correct[0]="b"; |
2305 |
02 Aug 10 |
peter |
48 |
correct[1]="n"; |
2305 |
02 Aug 10 |
peter |
49 |
correct[2]="n"; |
2305 |
02 Aug 10 |
peter |
50 |
test_split(correct, "banan", 'a', suite); |
2305 |
02 Aug 10 |
peter |
51 |
|
2305 |
02 Aug 10 |
peter |
52 |
correct.resize(3); |
2305 |
02 Aug 10 |
peter |
53 |
correct[0]="ba"; |
2305 |
02 Aug 10 |
peter |
54 |
correct[1]="a"; |
2305 |
02 Aug 10 |
peter |
55 |
correct[2]=""; |
2305 |
02 Aug 10 |
peter |
56 |
test_split(correct, "banan", 'n', suite); |
2305 |
02 Aug 10 |
peter |
57 |
|
2879 |
16 Nov 12 |
peter |
58 |
correct[0]="split"; |
2879 |
16 Nov 12 |
peter |
59 |
correct[1]="me"; |
2879 |
16 Nov 12 |
peter |
60 |
correct[2]="please"; |
2879 |
16 Nov 12 |
peter |
61 |
test_split(correct, "split,me;please", ";,", suite); |
2879 |
16 Nov 12 |
peter |
62 |
|
2879 |
16 Nov 12 |
peter |
63 |
|
2305 |
02 Aug 10 |
peter |
64 |
return suite.return_value(); |
2305 |
02 Aug 10 |
peter |
65 |
} |
2305 |
02 Aug 10 |
peter |
66 |
|
2879 |
16 Nov 12 |
peter |
67 |
template<typename T> |
2305 |
02 Aug 10 |
peter |
68 |
void test_split(const std::vector<std::string>& correct, const std::string& str, |
2879 |
16 Nov 12 |
peter |
69 |
T delim, test::Suite& suite) |
2305 |
02 Aug 10 |
peter |
70 |
{ |
2305 |
02 Aug 10 |
peter |
71 |
suite.out() << "split(vec, \"" << str << "\", '" << delim << "')\n"; |
2305 |
02 Aug 10 |
peter |
72 |
std::vector<std::string> vec; |
2305 |
02 Aug 10 |
peter |
73 |
utility::split(vec, str, delim); |
2305 |
02 Aug 10 |
peter |
74 |
bool ok=true; |
2305 |
02 Aug 10 |
peter |
75 |
if (vec.size() != correct.size()) { |
2305 |
02 Aug 10 |
peter |
76 |
ok = false; |
4200 |
19 Aug 22 |
peter |
77 |
suite.err() << "error: incorrect size: " << vec.size() |
2305 |
02 Aug 10 |
peter |
78 |
<< " expected: " << correct.size() << std::endl; |
2305 |
02 Aug 10 |
peter |
79 |
} |
2305 |
02 Aug 10 |
peter |
80 |
else if (vec != correct) { |
2305 |
02 Aug 10 |
peter |
81 |
ok = false; |
2305 |
02 Aug 10 |
peter |
82 |
suite.err() << "error: not equal" << std::endl; |
2305 |
02 Aug 10 |
peter |
83 |
} |
2305 |
02 Aug 10 |
peter |
84 |
if (!ok) { |
2305 |
02 Aug 10 |
peter |
85 |
suite.err() << "result:\n"; |
2305 |
02 Aug 10 |
peter |
86 |
for (size_t i=0; i<vec.size(); ++i) |
2305 |
02 Aug 10 |
peter |
87 |
suite.err() << i << "\t" << vec[i] << "\n"; |
2305 |
02 Aug 10 |
peter |
88 |
suite.err() << "correct:\n"; |
2305 |
02 Aug 10 |
peter |
89 |
for (size_t i=0; i<correct.size(); ++i) |
2305 |
02 Aug 10 |
peter |
90 |
suite.err() << i << "\t" << correct[i] << "\n"; |
2305 |
02 Aug 10 |
peter |
91 |
} |
2305 |
02 Aug 10 |
peter |
92 |
|
4196 |
14 Aug 22 |
peter |
93 |
std::vector<std::string> vec2(1); |
4196 |
14 Aug 22 |
peter |
94 |
utility::split(vec2, str, delim); |
4196 |
14 Aug 22 |
peter |
95 |
if (vec2.size() != correct.size()+1) { |
4196 |
14 Aug 22 |
peter |
96 |
ok = false; |
4196 |
14 Aug 22 |
peter |
97 |
suite.err() << "error: incorrect vec2 size: " << vec2.size() |
4196 |
14 Aug 22 |
peter |
98 |
<< " expected: " << (correct.size()+1) << std::endl; |
4196 |
14 Aug 22 |
peter |
99 |
} |
4196 |
14 Aug 22 |
peter |
100 |
else if (! std::equal(vec2.begin()+1, vec2.end(), correct.begin())) { |
4196 |
14 Aug 22 |
peter |
101 |
ok = false; |
4196 |
14 Aug 22 |
peter |
102 |
for (size_t i=0; i<vec2.size(); ++i) { |
4196 |
14 Aug 22 |
peter |
103 |
suite.err() << i << " vec2: " << vec2[i] << " expected"; |
4196 |
14 Aug 22 |
peter |
104 |
if (i==0) |
4196 |
14 Aug 22 |
peter |
105 |
suite.err() << "\n"; |
4196 |
14 Aug 22 |
peter |
106 |
else |
4196 |
14 Aug 22 |
peter |
107 |
suite.err() << correct[i-1] << "\n"; |
4196 |
14 Aug 22 |
peter |
108 |
} |
4196 |
14 Aug 22 |
peter |
109 |
} |
2305 |
02 Aug 10 |
peter |
110 |
|
2305 |
02 Aug 10 |
peter |
111 |
suite.add(ok); |
2305 |
02 Aug 10 |
peter |
112 |
} |