COOLFluiD  Release kernel
COOLFluiD is a Collaborative Simulation Environment (CSE) focused on complex MultiPhysics simulations.
utest-string-ops.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010-2013 von Karman Institute for Fluid Dynamics, Belgium
2 //
3 // This software is distributed under the terms of the
4 // GNU Lesser General Public License version 3 (LGPLv3).
5 // See doc/lgpl.txt and doc/gpl.txt for the license text.
6 
7 #define BOOST_TEST_DYN_LINK
8 #define BOOST_TEST_MODULE "Test module for string operations"
9 
10 #include <boost/test/unit_test.hpp>
11 
12 #include <boost/algorithm/string.hpp>
13 
15 #include "common/URI.hpp"
16 
17 using namespace std;
18 using namespace boost;
19 using namespace cf3;
20 using namespace cf3::common;
21 
22 BOOST_AUTO_TEST_SUITE( String_TestSuite )
23 
24 BOOST_AUTO_TEST_CASE( boostFunctions )
25 {
26  // the goal of the following tests is not to test the boost library
27  // but to show some useful functions that were implemented in
28  // CF 2.x.x and were removed in CF K3
29 
30  string parts[] = {"one", "two", "three"};
31  string str = " HeLLo World 123 ";
32  string tmpStr;
33  vector<string> myVector;
34 
35  // join
36  BOOST_CHECK_EQUAL(algorithm::join(parts, " "), "one two three");
37 
38  // toLower
39  tmpStr = str;
40  algorithm::to_lower(tmpStr);
41  BOOST_CHECK_EQUAL(tmpStr, " hello world 123 ");
42 
43  // toUpper
44  tmpStr = str;
45  algorithm::to_upper(tmpStr);
46  BOOST_CHECK_EQUAL(tmpStr, " HELLO WORLD 123 ");
47 
48  // subst
49  tmpStr = str;
50  algorithm::replace_all(tmpStr, "World", "User");
51  BOOST_CHECK_EQUAL(tmpStr, " HeLLo User 123 ");
52 
53  // trimFront
54  tmpStr = str;
55  algorithm::trim_left(tmpStr);
56  BOOST_CHECK_EQUAL(tmpStr, "HeLLo World 123 ");
57 
58  // trimRear
59  tmpStr = str;
60  algorithm::trim_right(tmpStr);
61  BOOST_CHECK_EQUAL(tmpStr, " HeLLo World 123");
62 
63  // trim
64  tmpStr = str;
65  algorithm::trim(tmpStr);
66  BOOST_CHECK_EQUAL(tmpStr, "HeLLo World 123");
67 
68  // getWords
69  algorithm::split(myVector, str, algorithm::is_any_of(" "), token_compress_on);
70  BOOST_CHECK_EQUAL(myVector.size(), (unsigned int) 5);
71  BOOST_CHECK_EQUAL(myVector[0], "");
72  BOOST_CHECK_EQUAL(myVector[1], "HeLLo");
73  BOOST_CHECK_EQUAL(myVector[2], "World");
74  BOOST_CHECK_EQUAL(myVector[3], "123");
75  BOOST_CHECK_EQUAL(myVector[4], "");
76 
77  // startsWith
78  BOOST_CHECK(algorithm::starts_with(str, " HeLLo"));
79 
80  // endsWith
81  BOOST_CHECK(algorithm::ends_with(str, "World 123 "));
82 
83 }
84 
85 BOOST_AUTO_TEST_CASE( test__to_str )
86 {
87  BOOST_CHECK_EQUAL(to_str(42), "42");
88  BOOST_CHECK(boost::starts_with(to_str(3.14), "3.14000"));
89 }
90 
91 BOOST_AUTO_TEST_CASE( test__from_str )
92 {
93  BOOST_CHECK_EQUAL(from_str<int>("42"), 42);
94  BOOST_CHECK_EQUAL(from_str<cf3::Real>("3.14"), 3.14);
95 }
96 
98 {
99  URI uri("file://hostname");
100  uri /= "file_name.txt";
101  BOOST_CHECK(uri.is_absolute());
102  BOOST_CHECK_EQUAL(uri.string(),"file://hostname/file_name.txt");
103  BOOST_CHECK_EQUAL(uri.base_path().string(),"file://hostname");
104 
105  URI uri2("file_name.txt", URI::Scheme::FILE);
106  BOOST_CHECK(uri2.is_relative());
107  BOOST_CHECK_EQUAL(uri2.string(),"file:file_name.txt");
108 
109  URI uri3("cpath:/hostname");
110  uri3 /= "component";
111  BOOST_CHECK(uri3.is_absolute());
112  BOOST_CHECK_EQUAL(uri3.string(),"cpath:/hostname/component");
113  BOOST_CHECK_EQUAL(uri3.base_path().string(),"cpath:/hostname");
114 
115 }
116 BOOST_AUTO_TEST_SUITE_END()
URI base_path() const
Definition: URI.cpp:199
external boost library namespace
STL namespace.
common::URI uri(ComponentWrapper &self)
Conversions from and to std::string.
Common_API std::string to_str(const T &v)
Converts to std::string.
BOOST_AUTO_TEST_CASE(boostFunctions)
Uniform Resource Identifier (see http://en.wikipedia.org/wiki/Uniform_Resource_Identifier) ...
Top-level namespace for coolfluid.
Definition: Action.cpp:18
Common_API int from_str< int >(const std::string &str)
bool is_absolute() const
Definition: URI.cpp:194
Most basic kernel library.
Definition: Action.cpp:19
std::string string() const
Definition: URI.cpp:258
Send comments to:
COOLFluiD Web Admin