forked from orazaro/accelerated
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path06-00-split.cpp
78 lines (66 loc) · 1.87 KB
/
06-00-split.cpp
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
#include <iostream>
#include <vector>
#include <stdexcept>
#include <cctype>
// isspace is overloaded so we use wrapper function as argument for find_if
bool space(char c)
{
return isspace(c);
}
bool not_space(char c)
{
return !isspace(c);
}
std::vector<std::string> split(const std::string& str)
{
using namespace std;
typedef string::const_iterator iter;
vector<string> ret;
iter i = str.begin();
while(i != str.end())
{
// ignore leading spaces
i = find_if(i, str.end(), not_space);
// find end of next word
iter j = find_if(i, str.end(), space);
if(i != str.end())
ret.push_back(string(i, j));
i = j;
}
return ret;
}
/* SETUP TESTS */
#include <cppunit/extensions/HelperMacros.h>
class myTest : public CppUnit::TestFixture {
public:
void setUp() {}
void tearDown() {}
void checkResult();
CPPUNIT_TEST_SUITE( myTest );
CPPUNIT_TEST( checkResult );
CPPUNIT_TEST_SUITE_END();
};
CPPUNIT_TEST_SUITE_REGISTRATION ( myTest );
void myTest::checkResult()
{
typedef std::vector<std::string> vec_str;
vec_str vec = split(std::string(""));
CPPUNIT_ASSERT_EQUAL((vec_str::size_type)0, vec.size());
vec = split(std::string("test"));
CPPUNIT_ASSERT_EQUAL((vec_str::size_type)1, vec.size());
const std::string input(" \tone two\n tree\tfour five\t\t");
vec_str etalon;
etalon.push_back("one");
etalon.push_back("two");
etalon.push_back("tree");
etalon.push_back("four");
etalon.push_back("five");
vec = split(input);
CPPUNIT_ASSERT_EQUAL((vec_str::size_type)5, vec.size());
std::cout << std::endl << input << std::endl;
for(vec_str::size_type i = 0; i != vec.size(); ++i) {
std::cout << i << " " << vec[i] << std::endl;
CPPUNIT_ASSERT_EQUAL(etalon[i], vec[i]);
}
}
#include "cppunit_run.inc"