2
// Copyright (c) 2012 Linaro Limited
4
// All rights reserved. This program and the accompanying materials
5
// are made available under the terms of the MIT License which accompanies
6
// this distribution, and is available at
7
// http://www.opensource.org/licenses/mit-license.php
10
// Alexandros Frantzis - original implementation.
15
#include "libmatrix_test.h"
16
#include "util_split_test.h"
24
template <typename T> static bool
25
areVectorsEqual(vector<T>& vec1, vector<T>& vec2)
27
if (vec1.size() != vec2.size())
30
for (unsigned int i = 0; i < vec1.size(); i++)
32
if (vec1[i] != vec2[i])
39
template <typename T> static void
40
printVector(vector<T>& vec)
43
for (unsigned int i = 0; i < vec.size(); i++)
45
cout << '"' << vec[i] << '"';
46
if (i < vec.size() - 1)
53
UtilSplitTestNormal::run(const Options& options)
55
const string test1("abc def ghi");
56
const string test2(" abc: def :ghi ");
57
vector<string> expected1;
58
vector<string> expected2;
59
vector<string> results;
61
expected1.push_back("abc");
62
expected1.push_back("def");
63
expected1.push_back("ghi");
65
expected2.push_back(" abc");
66
expected2.push_back(" def ");
67
expected2.push_back("ghi ");
69
if (options.beVerbose())
71
cout << "Testing string \"" << test1 << "\"" << endl;
74
Util::split(test1, ' ', results, Util::SplitModeNormal);
76
if (options.beVerbose())
78
cout << "Split result: ";
80
cout << endl << "Expected: ";
81
printVector(expected1);
85
if (!areVectorsEqual(results, expected1))
92
if (options.beVerbose())
94
cout << "Testing string \"" << test2 << "\"" << endl;
97
Util::split(test2, ':', results, Util::SplitModeNormal);
99
if (options.beVerbose())
101
cout << "Split result: ";
102
printVector(results);
103
cout << endl << "Expected: ";
104
printVector(expected2);
108
if (!areVectorsEqual(results, expected2))
117
UtilSplitTestQuoted::run(const Options& options)
119
const string test1("abc \"def' ghi\" klm\\ nop -b qr:title='123 \"456'");
120
const string test2("abc: def='1:2:3:'ghi : \":jk\"");
121
vector<string> expected1;
122
vector<string> expected2;
123
vector<string> results;
125
expected1.push_back("abc");
126
expected1.push_back("def' ghi");
127
expected1.push_back("klm nop");
128
expected1.push_back("-b");
129
expected1.push_back("qr:title=123 \"456");
131
expected2.push_back("abc");
132
expected2.push_back(" def=1:2:3:ghi ");
133
expected2.push_back(" :jk");
135
if (options.beVerbose())
137
cout << "Testing string \"" << test1 << "\"" << endl;
140
Util::split(test1, ' ', results, Util::SplitModeQuoted);
142
if (options.beVerbose())
144
cout << "Split result: ";
145
printVector(results);
146
cout << endl << "Expected: ";
147
printVector(expected1);
151
if (!areVectorsEqual(results, expected1))
158
if (options.beVerbose())
160
cout << "Testing string \"" << test2 << "\"" << endl;
163
Util::split(test2, ':', results, Util::SplitModeQuoted);
165
if (options.beVerbose())
167
cout << "Split result: ";
168
printVector(results);
169
cout << endl << "Expected: ";
170
printVector(expected2);
174
if (!areVectorsEqual(results, expected2))