preCICE v3.1.1
Loading...
Searching...
No Matches
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
16{
17 PRECICE_TEST(1_rank);
18 std::string text("123456 1234567 12345678");
19 std::string wrapped = wrapText(text, 4, 0);
20 BOOST_TEST(wrapped == std::string("123456\n1234567\n12345678"));
21
22 text = "1234 1234 1234";
23 wrapped = wrapText(text, 5, 0);
24 BOOST_TEST(wrapped == std::string("1234\n1234\n1234"));
25
26 text = "1234 123 5";
27 wrapped = wrapText(text, 5, 0);
28 BOOST_TEST(wrapped == std::string("1234\n123 5"));
29
30 text = "1234 1234 1234";
31 wrapped = wrapText(text, 7, 2);
32 BOOST_TEST(wrapped == std::string("1234\n 1234\n 1234"));
33
34 text = "1234 1234 1234";
35 wrapped = wrapText(text, 5, 2);
36 BOOST_TEST(wrapped == std::string("1234\n 1234\n 1234"));
37
38 text = "12345678 1234 1 1234";
39 wrapped = wrapText(text, 8, 2);
40 BOOST_TEST(wrapped == std::string("12345678\n 1234 1\n 1234"));
41}
42
43BOOST_AUTO_TEST_CASE(StringAppendExtension)
44{
45 PRECICE_TEST(1_rank);
46 std::string filename("somefile");
47 std::string extension(".xyz");
48
49 std::string result(filename);
50 checkAppendExtension(result, extension);
51 BOOST_TEST(result.compare(filename + extension) == 0);
52
53 result = filename + extension;
54 checkAppendExtension(result, extension);
55 BOOST_TEST(result.compare(filename + extension) == 0);
56
57 result = filename + extension + ".zyx";
58 checkAppendExtension(result, extension);
59 BOOST_TEST(result.compare(filename + extension + ".zyx" + extension) == 0);
60}
61
62BOOST_AUTO_TEST_CASE(ConvertStringToBool)
63{
64 PRECICE_TEST(1_rank);
65 BOOST_TEST(convertStringToBool("tRUe") == true);
66 BOOST_TEST(convertStringToBool("FALSE") == false);
67 BOOST_TEST(convertStringToBool("oN") == true);
68 BOOST_TEST(convertStringToBool("off") == false);
69 BOOST_TEST(convertStringToBool("1") == true);
70 BOOST_TEST(convertStringToBool("0") == false);
71 BOOST_TEST(convertStringToBool("yes") == true);
72 BOOST_TEST(convertStringToBool("no") == false);
73}
74
76{
77 PRECICE_TEST(1_rank);
79 BOOST_REQUIRE(sm.data() != nullptr);
80 BOOST_TEST(*sm.data() == '\0');
81 BOOST_TEST(sm.str().empty());
82
83 auto cstr = "1234";
84 std::copy(cstr, cstr + 4, sm.data());
85 BOOST_TEST(*sm.data() == '1');
86
87 auto str = sm.str();
88 BOOST_TEST(str.size() == 4);
89 BOOST_TEST(str == cstr);
90
91 sm.data()[2] = '\0';
92 auto str2 = sm.str();
93 BOOST_TEST(str2.size() == 2);
94 BOOST_TEST(str2 == "12");
95}
96
98{
99 PRECICE_TEST(1_rank);
101 BOOST_TEST(editDistance("left", "theft") == 2);
102 BOOST_TEST(editDistance("aaa", "aa") == 1);
103 BOOST_TEST(editDistance("aAa", "aaa") == 1);
104 BOOST_TEST(editDistance("same", "same") == 0);
105}
106
107BOOST_AUTO_TEST_CASE(ComputeMatches)
108{
109 PRECICE_TEST(1_rank);
110 std::set names{"left", "right", "up", "down"};
111 auto matches = utils::computeMatches("lewp", names);
112 BOOST_TEST_REQUIRE(matches.size() == 4);
113
114 BOOST_TEST(matches[0].name == "left");
115 BOOST_TEST(matches[0].distance == 2);
116
117 BOOST_TEST(matches[1].name == "down");
118 BOOST_TEST(matches[1].distance == 3);
119
120 BOOST_TEST(matches[2].name == "up");
121 BOOST_TEST(matches[2].distance == 3);
122
123 BOOST_TEST(matches[3].name == "right");
124 BOOST_TEST(matches[3].distance == 5);
125}
126
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:27
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:78
bool convertStringToBool(std::string const &value)
Evaluates a string to find out if it represents a bool.
Definition String.cpp:55
std::string wrapText(const std::string &text, int linewidth, int indentation)
Definition String.cpp:12
std::string & checkAppendExtension(std::string &filename, const std::string &extension)
Checks if filename has the given extension, if not appends it.
Definition String.cpp:44
std::vector< StringMatch > computeMatches(std::string_view given, const Container &expected)
Definition String.hpp:95
Main namespace of the precice library.