source: trunk/test/split.cc @ 2370

Last change on this file since 2370 was 2370, checked in by Peter, 12 years ago

closes #646

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.2 KB
Line 
1// $Id: split.cc 2370 2010-12-11 04:00:49Z peter $
2
3/*
4  Copyright (C) 2010 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
31void test_split(const std::vector<std::string>& correct, const std::string& str,
32                char delim, test::Suite& suite);
33
34int main(int argc, char* argv[])
35{ 
36  test::Suite suite(argc, argv);
37
38  std::vector<std::string> correct;
39  correct.resize(1);
40  correct[0]="banan";
41  test_split(correct, "banan", 'x', suite);
42
43  correct.resize(3);
44  correct[0]="b";
45  correct[1]="n";
46  correct[2]="n";
47  test_split(correct, "banan", 'a', suite);
48
49  correct.resize(3);
50  correct[0]="ba";
51  correct[1]="a";
52  correct[2]="";
53  test_split(correct, "banan", 'n', suite);
54
55  return suite.return_value();
56}
57
58void test_split(const std::vector<std::string>& correct, const std::string& str,
59                char delim, test::Suite& suite)
60{
61  suite.out() << "split(vec, \"" << str << "\", '" << delim << "')\n";
62  std::vector<std::string> vec;
63  utility::split(vec, str, delim);
64  bool ok=true;
65  if (vec.size() != correct.size()) {
66    ok = false;
67    suite.err() << "error: incorrect size: " << vec.size() 
68                << " expected: " << correct.size() << std::endl;
69  }
70  else if (vec != correct) {
71    ok = false;
72    suite.err() << "error: not equal" << std::endl;
73  }
74  if (!ok) {
75    suite.err() << "result:\n";
76    for (size_t i=0; i<vec.size(); ++i)
77      suite.err() << i << "\t" << vec[i] << "\n";
78    suite.err() << "correct:\n";
79    for (size_t i=0; i<correct.size(); ++i)
80      suite.err() << i << "\t" << correct[i] << "\n";
81  }
82
83
84  suite.add(ok);
85}
Note: See TracBrowser for help on using the repository browser.