summaryrefslogtreecommitdiffstats
path: root/tests/test_str.cpp
blob: 597138e5cc5e08f25ca7e31f9a08aa1d6ccd14f8 (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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#include <frozen/string.h>
#include <frozen/algorithm.h>
#include <string>
#include <iostream>

#ifdef FROZEN_LETITGO_HAS_STRING_VIEW
#include <string_view>
#include <type_traits>
#include <tuple>
#endif

#include "bench.hpp"
#include "catch.hpp"

using namespace frozen::string_literals;
using namespace std::literals;

template<typename Char>
void test_string_view() {
#ifdef FROZEN_LETITGO_HAS_STRING_VIEW
  constexpr auto strings = []() -> std::tuple<
    const Char(&)[12], frozen::basic_string<Char>, std::basic_string_view<Char>,
    const Char(&)[23], frozen::basic_string<Char>, std::basic_string_view<Char>
  > {
    if constexpr (std::is_same_v<char, Char>) {
      return {
        "Let it go !", "Let it go !"_s, "Let it go !"sv,
        "Let it go, let it go !", "Let it go, let it go !"_s, "Let it go, let it go !"sv
      };
    } else if constexpr (std::is_same_v<wchar_t, Char>) {
      return {
        L"Let it go !", L"Let it go !"_s, L"Let it go !"sv,
        L"Let it go, let it go !", L"Let it go, let it go !"_s, L"Let it go, let it go !"sv
      };
    } else if constexpr (std::is_same_v<char16_t, Char>) {
      return {
        u"Let it go !", u"Let it go !"_s, u"Let it go !"sv,
        u"Let it go, let it go !", u"Let it go, let it go !"_s, u"Let it go, let it go !"sv
      };
    } else if constexpr (std::is_same_v<char32_t, Char>) {
      return {
        U"Let it go !", U"Let it go !"_s, U"Let it go !"sv,
        U"Let it go, let it go !", U"Let it go, let it go !"_s, U"Let it go, let it go !"sv
      };
    }
#ifdef FROZEN_LETITGO_HAS_CHAR8T
    else if constexpr (std::is_same_v<char8_t, Char>) {
      return {
        u8"Let it go !", u8"Let it go !"_s, u8"Let it go !"sv,
        u8"Let it go, let it go !", u8"Let it go, let it go !"_s, u8"Let it go, let it go !"sv
      };
    }
#endif
  }();
  
  const auto [
    letitgo,
    letitgo_s,
    letitgo_sv,
    letitgoletitgo,
    letitgoletitgo_s,
    letitgoletitgo_sv
  ] = strings;

  {
    frozen::basic_string<Char> letItGo = letitgo_sv;
    REQUIRE(letItGo == letitgo);
    REQUIRE(letItGo == letitgo_s);
    REQUIRE(letItGo == letitgo_sv);

    letItGo = letitgoletitgo_sv;
    REQUIRE(letItGo == letitgoletitgo);
    REQUIRE(letItGo == letitgoletitgo_s);
    REQUIRE(letItGo == letitgoletitgo_sv);
  }

  {
    constexpr frozen::basic_string<Char> letItGo = std::get<2>(strings);
    static_assert(letItGo == std::get<0>(strings), "frozen::string constexpr");
    static_assert(letItGo == std::get<1>(strings), "frozen::string constexpr literal");
    static_assert(letItGo == std::get<2>(strings), "frozen::string constexpr string view");
  }
#endif
}

TEST_CASE("Various string operation", "[string]") {
  {
    frozen::string letItGo = "Let it go !";
    REQUIRE(letItGo == "Let it go !");
    REQUIRE(letItGo == "Let it go !"_s);

    letItGo = "Let it go, let it go !";
    REQUIRE(letItGo == "Let it go, let it go !");
    REQUIRE(letItGo == "Let it go, let it go !"_s);
  }

  {
    constexpr frozen::string letItGo = "Let it go !";
    static_assert(letItGo == "Let it go !",   "frozen::string constexpr");
    static_assert(letItGo == "Let it go !"_s, "frozen::string constexpr literal");
  }
  
  test_string_view<char>();
}

TEST_CASE("Various wstring operation", "[string]") {
  {
    frozen::wstring letItGo = L"Let it go !";
    REQUIRE(letItGo == L"Let it go !");
    REQUIRE(letItGo == L"Let it go !"_s);

    letItGo = L"Let it go, let it go !";
    REQUIRE(letItGo == L"Let it go, let it go !");
    REQUIRE(letItGo == L"Let it go, let it go !"_s);
  }

  {
    constexpr frozen::wstring letItGo = L"Let it go !";
    static_assert(letItGo == L"Let it go !",   "frozen::wstring constexpr");
    static_assert(letItGo == L"Let it go !"_s, "frozen::wstring constexpr literal");
  }
  
  test_string_view<wchar_t>();
}

TEST_CASE("Various u16string operation", "[string]") {
  {
    frozen::u16string letItGo = u"Let it go !";
    REQUIRE(letItGo == u"Let it go !");
    REQUIRE(letItGo == u"Let it go !"_s);

    letItGo = u"Let it go, let it go !";
    REQUIRE(letItGo == u"Let it go, let it go !");
    REQUIRE(letItGo == u"Let it go, let it go !"_s);
  }

  {
    constexpr frozen::u16string letItGo = u"Let it go !";
    static_assert(letItGo == u"Let it go !",   "frozen::u16string constexpr");
    static_assert(letItGo == u"Let it go !"_s, "frozen::u16string constexpr literal");
  }
  
  test_string_view<char16_t>();
}

TEST_CASE("Various u32string operation", "[string]") {
  {
    frozen::u32string letItGo = U"Let it go !";
    REQUIRE(letItGo == U"Let it go !");
    REQUIRE(letItGo == U"Let it go !"_s);

    letItGo = U"Let it go, let it go !";
    REQUIRE(letItGo == U"Let it go, let it go !");
    REQUIRE(letItGo == U"Let it go, let it go !"_s);
  }

  {
    constexpr frozen::u32string letItGo = U"Let it go !";
    static_assert(letItGo == U"Let it go !",   "frozen::u32string constexpr");
    static_assert(letItGo == U"Let it go !"_s, "frozen::u32string constexpr literal");
  }
  
  test_string_view<char32_t>();
}

#ifdef FROZEN_LETITGO_HAS_CHAR8T
TEST_CASE("Various u8string operation", "[string]") {
  {
    frozen::u8string letItGo = u8"Let it go !";
    REQUIRE(letItGo == u8"Let it go !");
    REQUIRE(letItGo == u8"Let it go !"_s);

    letItGo = u8"Let it go, let it go !";
    REQUIRE(letItGo == u8"Let it go, let it go !");
    REQUIRE(letItGo == u8"Let it go, let it go !"_s);
  }

  {
    constexpr frozen::u8string letItGo = u8"Let it go !";
    static_assert(letItGo == u8"Let it go !",   "frozen::u8string constexpr");
    static_assert(letItGo == u8"Let it go !"_s, "frozen::u8string constexpr literal");
  }
  
  test_string_view<char8_t>();
}
#endif

TEST_CASE("Knuth-Morris-Pratt str search", "[str-search]") {

  {
    std::string haystack = "n";
    auto index = frozen::search(haystack.begin(), haystack.end(), frozen::make_knuth_morris_pratt_searcher("n"));
    REQUIRE(index == haystack.begin());
  }

  {
    std::string haystack = "nmnn";
    auto index = frozen::search(haystack.begin(), haystack.end(), frozen::make_knuth_morris_pratt_searcher("nn"));
    REQUIRE(std::distance(haystack.begin(), index) == 2);
  }

  {
    std::string haystack = "nmnn";
    auto index = frozen::search(haystack.begin(), haystack.end(), frozen::make_knuth_morris_pratt_searcher("mm"));
    REQUIRE(index == haystack.end());
  }


  {
    std::string haystack = "ABC ABCDAB ABCDABCDABDE";
    auto index = frozen::search(haystack.begin(), haystack.end(), frozen::make_knuth_morris_pratt_searcher("ABCDABD"));
    REQUIRE(std::distance(haystack.begin(), index) == 15);
  }

}

TEST_CASE("Boyer-Moore str search", "[str-search]") {

  {
    std::string haystack = "n";
    auto index = frozen::search(haystack.begin(), haystack.end(), frozen::make_boyer_moore_searcher("n"));
    REQUIRE(index == haystack.begin());
  }

  {
    std::string haystack = "nmnn";
    auto index = frozen::search(haystack.begin(), haystack.end(), frozen::make_boyer_moore_searcher("nn"));
    REQUIRE(std::distance(haystack.begin(), index) == 2);
  }

  {
    std::string haystack = "nmnn";
    auto index = frozen::search(haystack.begin(), haystack.end(), frozen::make_boyer_moore_searcher("mm"));
    REQUIRE(index == haystack.end());
  }


  {
    std::string haystack = "ABC ABCDAB ABCDABCDABDE";
    auto index = frozen::search(haystack.begin(), haystack.end(), frozen::make_boyer_moore_searcher("ABCDABD"));
    REQUIRE(std::distance(haystack.begin(), index) == 15);
  }

}