source: trunk/test/split.cc @ 2879

Last change on this file since 2879 was 2879, checked in by Peter, 10 years ago

split function that splits on several chars

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