preCICE v3.2.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
StringTest.cpp
Go to the documentation of this file.
1#include <boost/test/tools/interface.hpp>
2#include <boost/test/tools/old/interface.hpp>
3#include <string>
4#include "math/constants.hpp"
6#include "testing/Testing.hpp"
7#include "utils/String.hpp"
8
9using namespace precice;
10using namespace precice::utils;
11
12BOOST_AUTO_TEST_SUITE(UtilsTests)
13BOOST_AUTO_TEST_SUITE(StringTests)
14
17{
19 std::string text("123456 1234567 12345678");
20 std::string wrapped = wrapText(text, 4, 0);
21 BOOST_TEST(wrapped == std::string("123456\n1234567\n12345678"));
22
23 text = "1234 1234 1234";
24 wrapped = wrapText(text, 5, 0);
25 BOOST_TEST(wrapped == std::string("1234\n1234\n1234"));
26
27 text = "1234 123 5";
28 wrapped = wrapText(text, 5, 0);
29 BOOST_TEST(wrapped == std::string("1234\n123 5"));
30
31 text = "1234 1234 1234";
32 wrapped = wrapText(text, 7, 2);
33 BOOST_TEST(wrapped == std::string("1234\n 1234\n 1234"));
34
35 text = "1234 1234 1234";
36 wrapped = wrapText(text, 5, 2);
37 BOOST_TEST(wrapped == std::string("1234\n 1234\n 1234"));
38
39 text = "12345678 1234 1 1234";
40 wrapped = wrapText(text, 8, 2);
41 BOOST_TEST(wrapped == std::string("12345678\n 1234 1\n 1234"));
42}
43
45BOOST_AUTO_TEST_CASE(StringAppendExtension)
46{
48 std::string filename("somefile");
49 std::string extension(".xyz");
50
51 std::string result(filename);
52 checkAppendExtension(result, extension);
53 BOOST_TEST(result.compare(filename + extension) == 0);
54
55 result = filename + extension;
56 checkAppendExtension(result, extension);
57 BOOST_TEST(result.compare(filename + extension) == 0);
58
59 result = filename + extension + ".zyx";
60 checkAppendExtension(result, extension);
61 BOOST_TEST(result.compare(filename + extension + ".zyx" + extension) == 0);
62}
63
65BOOST_AUTO_TEST_CASE(ConvertStringToBool)
66{
68 BOOST_TEST(convertStringToBool("tRUe") == true);
69 BOOST_TEST(convertStringToBool("FALSE") == false);
70 BOOST_TEST(convertStringToBool("oN") == true);
71 BOOST_TEST(convertStringToBool("off") == false);
72 BOOST_TEST(convertStringToBool("1") == true);
73 BOOST_TEST(convertStringToBool("0") == false);
74 BOOST_TEST(convertStringToBool("yes") == true);
75 BOOST_TEST(convertStringToBool("no") == false);
76}
77
80{
83 BOOST_REQUIRE(sm.data() != nullptr);
84 BOOST_TEST(*sm.data() == '\0');
85 BOOST_TEST(sm.str().empty());
86
87 auto cstr = "1234";
88 std::copy(cstr, cstr + 4, sm.data());
89 BOOST_TEST(*sm.data() == '1');
90
91 auto str = sm.str();
92 BOOST_TEST(str.size() == 4);
93 BOOST_TEST(str == cstr);
94
95 sm.data()[2] = '\0';
96 auto str2 = sm.str();
97 BOOST_TEST(str2.size() == 2);
98 BOOST_TEST(str2 == "12");
99}
100
101PRECICE_TEST_SETUP(1_rank)
103{
104 PRECICE_TEST();
106 BOOST_TEST(editDistance("left", "theft") == 2);
107 BOOST_TEST(editDistance("aaa", "aa") == 1);
108 BOOST_TEST(editDistance("aAa", "aaa") == 1);
109 BOOST_TEST(editDistance("same", "same") == 0);
110}
111
112PRECICE_TEST_SETUP(1_rank)
113BOOST_AUTO_TEST_CASE(ComputeMatches)
114{
115 PRECICE_TEST();
116 std::set names{"left", "right", "up", "down"};
117 auto matches = utils::computeMatches("lewp", names);
118 BOOST_TEST_REQUIRE(matches.size() == 4);
119
120 BOOST_TEST(matches[0].name == "left");
121 BOOST_TEST(matches[0].distance == 2);
122
123 BOOST_TEST(matches[1].name == "down");
124 BOOST_TEST(matches[1].distance == 3);
125
126 BOOST_TEST(matches[2].name == "up");
127 BOOST_TEST(matches[2].distance == 3);
128
129 BOOST_TEST(matches[3].name == "right");
130 BOOST_TEST(matches[3].distance == 5);
131}
132
std::size_t distance
BOOST_AUTO_TEST_SUITE(PreProcess)
BOOST_AUTO_TEST_SUITE_END()
std::string name
BOOST_AUTO_TEST_CASE(StringWrap)
#define PRECICE_TEST()
Definition Testing.hpp:39
#define PRECICE_TEST_SETUP(...)
Creates and attaches a TestSetup to a Boost test case.
Definition Testing.hpp:29
Utility class to build a string from C functions with output pointers and static maximum length.
Definition String.hpp:14
std::string str() const
Definition String.hpp:35
T compare(T... args)
T copy(T... args)
T empty(T... args)
contains precice-related utilities.
std::size_t editDistance(std::string_view s1, std::string_view s2)
Definition String.cpp:79
bool convertStringToBool(std::string const &value)
Evaluates a string to find out if it represents a bool.
Definition String.cpp:56
std::string wrapText(const std::string &text, int linewidth, int indentation)
Definition String.cpp:13
std::string & checkAppendExtension(std::string &filename, const std::string &extension)
Checks if filename has the given extension, if not appends it.
Definition String.cpp:45
std::vector< StringMatch > computeMatches(std::string_view given, const Container &expected)
Definition String.hpp:95
Main namespace of the precice library.