-
Notifications
You must be signed in to change notification settings - Fork 0
/
teststringwriter.cpp
123 lines (110 loc) · 2.81 KB
/
teststringwriter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#define BOOST_TEST_MAIN
#include <boost/test/included/unit_test.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <iostream>
#include <cstring>
#include "stringwriter.h"
using namespace std;
template<typename T, typename FromString, typename ToString, typename ToCharPtr>
T& strtransform(const std::string& ref, T& str, FromString fs, ToString ts, ToCharPtr ptr)
{
fs(str,ref);
BOOST_CHECK(ts(str) == ref);
auto c1 = ptr(str);
c1 += 4;
auto c1x = c1 + 3;
*c1 = 'x';
*c1x = 'y';
for(unsigned i=0; c1[i]; ++i)
if (c1[i] == 'i') {
c1[i] = 'a';
//cout << i << endl;
}
while (auto c = strstr(c1, "\n")) {
*c = '_';
}
//c1x[1000] = 'z'; <-- may crash with char*, will checked with StringWriter
BOOST_CHECK(ts(str) != ref);
return str;
}
BOOST_AUTO_TEST_SUITE( TestStringWriter )
BOOST_AUTO_TEST_CASE(testStringWriterStrcpy)
{
const std::string s{"This is some silly text without any meaning."};
std::string s1{s};
StringWriter sw1{s1};
sw1 += 17;
char *s2 = strdup(s.c_str());
char *sw2 = s2;
sw2 += 17;
BOOST_CHECK(strlen(s1) == strlen(s2));
BOOST_CHECK(!strcmp(s1.c_str(), s2));
BOOST_CHECK(!strcmp(sw1.c_str(), sw2));
strcpy(sw1, "hallo");
strcpy(sw2, "hallo");
BOOST_CHECK(strlen(sw1) == strlen(sw2));
BOOST_CHECK(!strcmp(s1.c_str(), s2));
sw1[5] = ' '; // replace the \0 byte
sw2[5] = ' ';
BOOST_CHECK(s1 == s2);
}
BOOST_AUTO_TEST_CASE(testStringWriter)
{
std::string s{"This a text\nwith several line\nbreaks and some longer lines\n and shorter ones."};
std::string s2;
std::string s2x = strtransform(s, s2,
[](std::string& str, const std::string& s){ str = s; },
[](const std::string& s){ return s; },
[](std::string& s){ return StringWriter(s,0); }
);
//cout << s2 << endl;
char *s3 = new char[s.size()+1];
strtransform(s, s3,
[](char *str, const std::string& s){ strcpy(str,s.c_str()); },
[](char *s){ return std::string(s); },
[](char *s){ return s; }
);
//cout << s3 << endl;
BOOST_CHECK(std::string(s3) == s2);
BOOST_CHECK(s2 == s2x);
}
// examples from the README.md
void g1(char *s)
{
char *sc = s+3;
*sc = 'a';
while (char *scc = strstr(sc, "b"))
*scc = 'c';
}
void g2(char *s)
{
auto sc = s+3;
*sc = 'a';
while (auto scc = strstr(sc, "b"))
*scc = 'c';
}
void g3(std::string& str)
{
StringWriter s{str};
auto sc = s+3;
*sc = 'a';
while (auto scc = strstr(sc, "b"))
*scc = 'c';
}
BOOST_AUTO_TEST_CASE(testExamples)
{
std::string s{"This is another simple text"};
char sg1[s.size()+1];
strcpy(sg1, s.c_str());
g1(sg1);
char sg2[s.size()+1];
strcpy(sg2, s.c_str());
g2(sg2);
std::string sg3{s};
g3(sg3);
BOOST_CHECK(sg3 != s);
BOOST_CHECK(sg3 == sg2);
BOOST_CHECK(sg3 == sg1);
BOOST_CHECK(!strcmp(sg1, sg2));
}
BOOST_AUTO_TEST_SUITE_END()