summaryrefslogtreecommitdiff
path: root/projects/SelfTest/IntrospectiveTests/String.tests.cpp
blob: ae21bb3ce8274321a1112e0f54d505be483b3e53 (plain)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include "internal/catch_stringref.h"

#include "catch.hpp"

#include <cstring>

namespace Catch {

    // Implementation of test accessors
    struct StringRefTestAccess {
        static auto isOwned( StringRef const& stringRef ) -> bool {
            return stringRef.isOwned();
        }
        static auto isSubstring( StringRef const& stringRef ) -> bool {
            return stringRef.isSubstring();
        }
    };


    namespace {
    auto isOwned( StringRef const& stringRef ) -> bool {
        return StringRefTestAccess::isOwned( stringRef );
    }
    auto isSubstring( StringRef const& stringRef ) -> bool {
        return StringRefTestAccess::isSubstring( stringRef );
    }
    } // end anonymous namespace

} // namespace Catch

TEST_CASE( "StringRef", "[Strings][StringRef]" ) {

    using Catch::StringRef;
    using Catch::isOwned; using Catch::isSubstring;

    SECTION( "Empty string" ) {
        StringRef empty;
        REQUIRE( empty.empty() );
        REQUIRE( empty.size() == 0 );
        REQUIRE( std::strcmp( empty.c_str(), "" ) == 0 );
    }

    SECTION( "From string literal" ) {
        StringRef s = "hello";
        REQUIRE( s.empty() == false );
        REQUIRE( s.size() == 5 );
        REQUIRE( isSubstring( s ) == false );

        auto rawChars = s.currentData();
        REQUIRE( std::strcmp( rawChars, "hello" ) == 0 );

        SECTION( "c_str() does not cause copy" ) {
            REQUIRE( isOwned( s ) == false );

            REQUIRE( s.c_str() == rawChars );

            REQUIRE( isOwned( s ) == false );
        }
    }
    SECTION( "From sub-string" ) {
        StringRef original = StringRef( "original string" ).substr(0, 8);
        REQUIRE( original == "original" );
        REQUIRE( isSubstring( original ) );
        REQUIRE( isOwned( original ) == false );

        original.c_str(); // Forces it to take ownership

        REQUIRE( isSubstring( original ) == false );
        REQUIRE( isOwned( original ) );
    }


    SECTION( "Substrings" ) {
        StringRef s = "hello world!";
        StringRef ss = s.substr(0, 5);

        SECTION( "zero-based substring" ) {
            REQUIRE( ss.empty() == false );
            REQUIRE( ss.size() == 5 );
            REQUIRE( std::strcmp( ss.c_str(), "hello" ) == 0 );
            REQUIRE( ss == "hello" );
        }
        SECTION( "c_str() causes copy" ) {
            REQUIRE( isSubstring( ss ) );
            REQUIRE( isOwned( ss ) == false );

            auto rawChars = ss.currentData();
            REQUIRE( rawChars == s.currentData() ); // same pointer value
            REQUIRE( ss.c_str() != rawChars );

            REQUIRE( isSubstring( ss ) == false );
            REQUIRE( isOwned( ss ) );

            REQUIRE( ss.currentData() != s.currentData() ); // different pointer value
        }

        SECTION( "non-zero-based substring") {
            ss = s.substr( 6, 6 );
            REQUIRE( ss.size() == 6 );
            REQUIRE( std::strcmp( ss.c_str(), "world!" ) == 0 );
        }

        SECTION( "Pointer values of full refs should match" ) {
            StringRef s2 = s;
            REQUIRE( s.c_str() == s2.c_str() );
        }

        SECTION( "Pointer values of substring refs should not match" ) {
            REQUIRE( s.c_str() != ss.c_str() );
        }
    }

    SECTION( "Comparisons" ) {
        REQUIRE( StringRef("hello") == StringRef("hello") );
        REQUIRE( StringRef("hello") != StringRef("cello") );
    }

    SECTION( "from std::string" ) {
        std::string stdStr = "a standard string";

        SECTION( "implicitly constructed" ) {
            StringRef sr = stdStr;
            REQUIRE( sr == "a standard string" );
            REQUIRE( sr.size() == stdStr.size() );
        }
        SECTION( "explicitly constructed" ) {
            StringRef sr( stdStr );
            REQUIRE( sr == "a standard string" );
            REQUIRE( sr.size() == stdStr.size() );
        }
        SECTION( "assigned" ) {
            StringRef sr;
            sr = stdStr;
            REQUIRE( sr == "a standard string" );
            REQUIRE( sr.size() == stdStr.size() );
        }
    }

    SECTION( "to std::string" ) {
        StringRef sr = "a stringref";

        SECTION( "implicitly constructed" ) {
            std::string stdStr = sr;
            REQUIRE( stdStr == "a stringref" );
            REQUIRE( stdStr.size() == sr.size() );
        }
        SECTION( "explicitly constructed" ) {
            std::string stdStr( sr );
            REQUIRE( stdStr == "a stringref" );
            REQUIRE( stdStr.size() == sr.size() );
        }
        SECTION( "assigned" ) {
            std::string stdStr;
            stdStr = sr;
            REQUIRE( stdStr == "a stringref" );
            REQUIRE( stdStr.size() == sr.size() );
        }
    }

    SECTION( "Counting utf-8 codepoints" ) {
        StringRef ascii = "just a plain old boring ascii string...";
        REQUIRE(ascii.numberOfCharacters() == ascii.size());

        StringRef simpleu8 = u8"Trocha češtiny nikoho nezabila";
        REQUIRE(simpleu8.numberOfCharacters() == 30);

        StringRef emojis = u8"Here be 👾";
        REQUIRE(emojis.numberOfCharacters() == 9);
    }

}

TEST_CASE( "replaceInPlace", "[Strings][StringManip]" ) {
    std::string letters = "abcdefcg";
    SECTION( "replace single char" ) {
        CHECK( Catch::replaceInPlace( letters, "b", "z" ) );
        CHECK( letters == "azcdefcg" );
    }
    SECTION( "replace two chars" ) {
        CHECK( Catch::replaceInPlace( letters, "c", "z" ) );
        CHECK( letters == "abzdefzg" );
    }
    SECTION( "replace first char" ) {
        CHECK( Catch::replaceInPlace( letters, "a", "z" ) );
        CHECK( letters == "zbcdefcg" );
    }
    SECTION( "replace last char" ) {
        CHECK( Catch::replaceInPlace( letters, "g", "z" ) );
        CHECK( letters == "abcdefcz" );
    }
    SECTION( "replace all chars" ) {
        CHECK( Catch::replaceInPlace( letters, letters, "replaced" ) );
        CHECK( letters == "replaced" );
    }
    SECTION( "replace no chars" ) {
        CHECK_FALSE( Catch::replaceInPlace( letters, "x", "z" ) );
        CHECK( letters == letters );
    }
    SECTION( "escape '" ) {
        std::string s = "didn't";
        CHECK( Catch::replaceInPlace( s, "'", "|'" ) );
        CHECK( s == "didn|'t" );
    }
}