File: constexpr_base64_tests.cpp

package info (click to toggle)
simdutf 8.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,524 kB
  • sloc: cpp: 64,498; ansic: 15,347; python: 3,592; sh: 366; makefile: 12
file content (329 lines) | stat: -rw-r--r-- 10,148 bytes parent folder | download
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#include "simdutf.h"

#include <algorithm>
#include <array>
#include <cstddef>
#include <tuple>

#include <tests/helpers/fixed_string.h>
#include <tests/helpers/test.h>

#if SIMDUTF_CPLUSPLUS23

TEST(compile_time_length_from_binary) {
  using namespace simdutf::tests::helpers;
  const auto binary = "Abracadabra!"_latin1;
  const auto encoded = "QWJyYWNhZGFicmEh"_latin1;
  constexpr auto encoded_length =
      simdutf::base64_length_from_binary(binary.size());
  static_assert(encoded_length == encoded.size());
}

TEST(compile_time_maximal_binary_length) {
  using namespace simdutf::tests::helpers;
  constexpr auto binary = "Abracadabra!"_latin1;

  constexpr auto encoded = "QWJyYWNhZGFicmEh"_latin1;
  // char
  static_assert(simdutf::maximal_binary_length_from_base64(encoded) ==
                binary.size());
  // unsigned char
  static_assert(simdutf::maximal_binary_length_from_base64(
                    encoded.as_array<unsigned char>()) == binary.size());
  // signed char
  static_assert(simdutf::maximal_binary_length_from_base64(
                    encoded.as_array<signed char>()) == binary.size());
}

TEST(compile_time_maximal_binary_length16) {
  using namespace simdutf::tests::helpers;
  constexpr auto binary = "Abracadabra!"_latin1;
  constexpr auto encoded = u"QWJyYWNhZGFicmEh"_utf16;
  static_assert(simdutf::maximal_binary_length_from_base64(encoded) ==
                binary.size());
}

namespace {

template <auto input>
  requires simdutf::tests::helpers::any_ctstring<decltype(input)>
constexpr auto b64_to_bin_impl() {
  using namespace simdutf::tests::helpers;
  constexpr auto Nmax = simdutf::maximal_binary_length_from_base64(input);
  CTString<char, Nmax> buffer{};
  auto res = simdutf::base64_to_binary(input, buffer);
  if (res.is_err()) {
    throw "failed convert";
  }
  if (res.count > Nmax) {
    throw "weird";
  }
  return std::tuple(res.count, buffer);
}

/**
 * converts base64 to binary. ideally the input should be passed as a
 * function parameter but I could not get that to work.
 */
template <auto input>
  requires simdutf::tests::helpers::any_ctstring<decltype(input)>
constexpr auto b64_to_binary() {
  constexpr auto r = b64_to_bin_impl<input>();
  constexpr auto N = std::get<0>(r);
  constexpr auto ret = std::get<1>(r);
  if constexpr (ret.size() != N) {
    return ret.template shrink<N>();
  } else {
    return ret;
  }
}

} // namespace

TEST(compile_time_length_from_binary_with_lines) {
  static_assert(4 == simdutf::base64_length_from_binary_with_lines(1));
  static_assert(137 == simdutf::base64_length_from_binary_with_lines(100));
}

TEST(compile_time_base64_to_binary) {
  using namespace simdutf::tests::helpers;
  constexpr auto binary = "Abracadabra!"_latin1;

  // tightly packed base64 works fine
  {
    constexpr auto base64 = "QWJyYWNhZGFicmEh"_latin1;
    constexpr auto binary_again = b64_to_binary<base64>();
    static_assert(binary_again == binary);
  }

  // extra spaces is no problem
  {
    constexpr auto base64 = "   QWJyYWNhZGF icmEh   "_latin1;
    constexpr auto binary_again = b64_to_binary<base64>();
    static_assert(binary_again == binary);
  }
}

TEST(compile_time_base64_utf16_to_binary) {
  using namespace simdutf::tests::helpers;
  constexpr auto binary = "Abracadabra!"_latin1;

  // tightly packed base64 works fine
  {
    constexpr auto base64 = u"QWJyYWNhZGFicmEh"_utf16;
    constexpr auto binary_again = b64_to_binary<base64>();
    static_assert(binary_again == binary);
  }

  // extra spaces is no problem
  {
    constexpr auto base64 = u"   QWJyYWNhZGF icmEh   "_utf16;
    constexpr auto binary_again = b64_to_binary<base64>();
    static_assert(binary_again == binary);
  }
}

namespace {
template <auto input>
  requires simdutf::tests::helpers::any_ctstring<decltype(input)>
constexpr auto binary_to_b64() {
  using namespace simdutf::tests::helpers;
  constexpr auto N = simdutf::base64_length_from_binary(input.size());
  CTString<char, N> buffer{};
  const auto r1 = simdutf::binary_to_base64(input, buffer);
  if (r1 != N) {
    throw "oops, size mismatch";
  }
  return buffer;
}
} // namespace

TEST(compile_time_binary_to_base64_char) {
  using namespace simdutf::tests::helpers;
  constexpr auto binary = "Abracadabra!"_latin1;
  constexpr auto expected = "QWJyYWNhZGFicmEh"_latin1;
  constexpr auto encoded = binary_to_b64<binary>();
  static_assert(expected == encoded);
}

namespace {
// this is just to demo that it is possible to do _base64 literals.
template <std::size_t N> struct Base64LiteralHelper {
  char storage[N - 1];

  static constexpr std::size_t size() noexcept { return N - 1; }

  constexpr Base64LiteralHelper(const char (&str)[N]) {
    static_assert(N > 1, "weird size");
    std::copy(str, str + size(), storage);
  }
};

template <Base64LiteralHelper a> constexpr auto operator""_base64() {
  using namespace simdutf::tests::helpers;
  constexpr auto N = a.size();
  constexpr std::span data(a.storage);
  constexpr CTString<char, N> tmp(data);
  return b64_to_binary<tmp>().template as_array<std::uint8_t>();
}

} // namespace

TEST(compile_time_base64_literal_demo) {
  using namespace simdutf::tests::helpers;

  constexpr std::array decoded = "QWJyYWNhZGFicmEh"_base64;
  const auto readable = std::string(begin(decoded), end(decoded));
  ASSERT_EQUAL(readable, "Abracadabra!");

  static_assert(decoded.size() == 12);
  static_assert(decoded[0] == 'A');
  static_assert(decoded[1] == 'b');
  static_assert(decoded[2] == 'r');
  static_assert(decoded[3] == 'a');
  static_assert(decoded[4] == 'c');
  static_assert(decoded[5] == 'a');
  static_assert(decoded[6] == 'd');
  static_assert(decoded[7] == 'a');
  static_assert(decoded[8] == 'b');
  static_assert(decoded[9] == 'r');
  static_assert(decoded[10] == 'a');
  static_assert(decoded[11] == '!');
}

namespace {
template <auto input, std::size_t lines>
  requires simdutf::tests::helpers::any_ctstring<decltype(input)>
constexpr auto binary_to_b64_with_lines() {
  using namespace simdutf::tests::helpers;
  constexpr auto N = simdutf::base64_length_from_binary_with_lines(
      input.size(), simdutf::base64_default, lines);
  CTString<char, N> buffer{};
  const auto r1 = simdutf::binary_to_base64_with_lines(input, buffer, lines,
                                                       simdutf::base64_default);
  if (r1 != N) {
    throw "oops, size mismatch";
  }
  return buffer;
}
} // namespace

TEST(compile_time_binary_to_base64_with_lines_char) {
  using namespace simdutf::tests::helpers;

  constexpr std::size_t lines = 4;
  constexpr auto binary = "Abracadabra!"_latin1;
  constexpr auto expected = "QWJy\nYWNh\nZGFi\ncmEh"_latin1;
  constexpr auto encoded = binary_to_b64_with_lines<binary, lines>();
  static_assert(expected == encoded);
}

TEST(compile_time_base64_ignorable) {
  static_assert(simdutf::base64_ignorable(' '));
  static_assert(not simdutf::base64_ignorable('x'));

  static_assert(simdutf::base64_ignorable(char16_t{' '}));
  static_assert(not simdutf::base64_ignorable(char16_t{'x'}));
}

TEST(compile_time_base64_valid) {
  static_assert(not simdutf::base64_valid(' '));
  static_assert(simdutf::base64_valid('x'));

  static_assert(not simdutf::base64_valid(char16_t{' '}));
  static_assert(simdutf::base64_valid(char16_t{'x'}));
}

TEST(compile_time_base64_valid_or_padding) {
  static_assert(not simdutf::base64_valid_or_padding(' '));
  static_assert(simdutf::base64_valid_or_padding('x'));
  static_assert(simdutf::base64_valid_or_padding('='));

  static_assert(not simdutf::base64_valid_or_padding(char16_t{' '}));
  static_assert(simdutf::base64_valid_or_padding(char16_t{'x'}));
  static_assert(simdutf::base64_valid_or_padding(char16_t{'='}));
}

namespace {

template <auto input>
  requires simdutf::tests::helpers::any_ctstring<decltype(input)>
constexpr auto b64_to_bin_safe_impl() {
  using namespace simdutf::tests::helpers;
  constexpr auto Nmax = simdutf::maximal_binary_length_from_base64(input);
  CTString<char, Nmax> buffer{};
  auto [res, outlen] = simdutf::base64_to_binary_safe(input, buffer);
  if (res.is_err()) {
    throw "failed convert";
  }
  if (res.count > input.size()) {
    throw "res.count > input.size()";
  }
  if (outlen > Nmax) {
    throw "outlen > Nmax";
  }
  return std::tuple(res.count, buffer, outlen);
}

/**
 * converts base64 to binary. ideally the input should be passed as a
 * function parameter but I could not get that to work.
 */
template <auto input>
  requires simdutf::tests::helpers::any_ctstring<decltype(input)>
constexpr auto b64_to_binary_safe() {
  constexpr auto r = b64_to_bin_safe_impl<input>();
  constexpr auto N_read_from_input = std::get<0>(r);
  constexpr auto ret = std::get<1>(r);
  constexpr auto outputbuffer_size = ret.size();
  constexpr auto outlen = std::get<2>(r);
  static_assert(N_read_from_input <= input.size());
  static_assert(outputbuffer_size >= outlen);
  if constexpr (ret.size() != outlen) {
    return ret.template shrink<outlen>();
  } else {
    return ret;
  }
}

} // namespace

TEST(compile_time_base64_to_binary_safe) {
  using namespace simdutf::tests::helpers;
  constexpr auto binary = "Abracadabra!"_latin1;
  static_assert(binary.size() == 12);

  // tightly packed base64 works fine
  {
    constexpr auto base64 = "QWJyYWNhZGFicmEh"_latin1;
    constexpr auto binary_again = b64_to_binary_safe<base64>();
    static_assert(binary_again == binary);
  }

  // extra spaces is no problem
  {
    constexpr auto base64 = "   QWJyYWNhZGF icmEh   "_latin1;
    constexpr auto binary_again = b64_to_binary_safe<base64>();
    static_assert(binary_again == binary);
  }

  // tightly packed base64 works fine also in utf-16
  {
    constexpr auto base64 = u"QWJyYWNhZGFicmEh"_utf16;
    constexpr auto binary_again = b64_to_binary_safe<base64>();
    static_assert(binary_again == binary);
  }

  // extra spaces is no problem also in utf-16
  {
    constexpr auto base64 = u"   QWJyYWNhZGF icmEh   "_utf16;
    constexpr auto binary_again = b64_to_binary_safe<base64>();
    static_assert(binary_again == binary);
  }
}

#else
TEST(no_compile_time_tests_below_cpp23) {}
#endif

TEST_MAIN