35 const int base = aspec.
base();
38 const char min_cp = aspec[0];
39 const char max_cp = aspec[base-1];
45 const double machine_epsilon = std::numeric_limits<double>::epsilon();
47 REQUIRE(base-1 == max_s);
52 REQUIRE(std::string()+min_cp+min_cp+min_cp == r1_min);
53 REQUIRE(std::string()+min_cp == r1_min_s);
57 REQUIRE(std::string()+min_cp+min_cp+max_cp == r1_max);
58 REQUIRE(std::string()+max_cp == r1_max_s);
61 REQUIRE(std::string()+max_cp+max_cp+max_cp == r3_max);
63 fprintf(stderr,
"Test32Bit base %d, %s: [%d .. %d] <-> ['%s' .. '%s'], %d years (max/365d) \n",
78 REQUIRE(r1_max == v1_s);
79 REQUIRE(base-1 == v0_d);
84 REQUIRE(r3_max == v1_s);
87 for(
int iter=
min; iter<=
max; ++iter) {
91 fprintf(stderr,
"test base %d: iter %d, rad '%s' %03d %03d %03d, dec %zd\n",
92 base, iter, rad.c_str(), (
int)(0xFF & rad[0]), (
int)(0xFF & rad[1]), (
int)(0xFF & rad[2]), (ssize_t)dec);
97 if( natural86_alphabet == aspec ) {
99 fprintf(stderr,
"Natural 0-9: ");
100 for(
int iter=0; iter<=9; ++iter) {
102 fprintf(stderr,
"%s, ", rad.c_str());
103 const char c = (char)(
'0'+iter);
104 REQUIRE(std::string()+c == rad);
106 fprintf(stderr,
"\n");
111 const int int64_max_enc_width = 11;
112 const int base = aspec.
base();
115 const char min_cp = aspec[0];
116 const char max_cp = aspec[base-1];
125 REQUIRE(base-1 == max_s);
130 REQUIRE(std::string()+min_cp+min_cp+min_cp+min_cp+min_cp+min_cp+min_cp+min_cp+min_cp+min_cp+min_cp == r1_min);
131 REQUIRE(std::string()+min_cp == r1_min_s);
135 REQUIRE(std::string()+min_cp+min_cp+min_cp+min_cp+min_cp+min_cp+min_cp+min_cp+min_cp+min_cp+max_cp == r1_max);
136 REQUIRE(std::string()+max_cp == r1_max_s);
138 fprintf(stderr,
"int (sz %zu) == int32_t (sz %zu): %d, int (sz %zu) == int64_t (sz %zu): %d\n",
139 sizeof(
int),
sizeof(int32_t), std::is_same_v<int, std::int32_t>,
140 sizeof(
int),
sizeof(int64_t), std::is_same_v<int, std::int64_t>);
142 fprintf(stderr,
"Test64bit base %d, %s: [%" PRIi64
" .. %" PRIi64
"] <-> ['%s' .. '%s'], %" PRIi64
" years (max/365d) \n",
146 fprintf(stderr,
"- range: [%" PRIi64
" .. %" PRIi64
"] <-> ['%s' .. '%s']\n",
155 REQUIRE(r1_max == v1_s);
156 REQUIRE(base-1 == v0_d);
158 for(int64_t iter=
std::max(0_i64, test_min-1); iter<test_max; ) {
163 fprintf(stderr,
"test base %d: iter %" PRIi64
", rad '%s', dec %" PRIi64
"\n", base, iter, rad.c_str(), dec);
165 REQUIRE(iter == dec);
173 testRadix_int64(aspec, 0x7FFFFFFFFFFFFFF0_i64, 0x7FFFFFFFFFFFFFFF_i64);
181 testRadix_int64(aspec, 0x7FFFFFFFFFFFFFF0_i64, 0x7FFFFFFFFFFFFFFF_i64);
185TEST_CASE(
"Integer Base 38 Encoding Test 01",
"[integer][type]" ) {
189TEST_CASE(
"Integer Base 64 Encoding Test 02",
"[integer][type]" ) {
195TEST_CASE(
"Integer Base 86 Encoding Test 03",
"[integer][type]" ) {
202 static inline constexpr const std::string_view data =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
204 static int s_code_point(
const char c)
noexcept {
205 if (
'A' <= c && c <=
'Z') {
207 }
else if (
'a' <= c && c <=
'z') {
209 }
else if (
'0' <= c && c <=
'9') {
211 }
else if (
'+' == c) {
213 }
else if (
'/' == c) {
222 :
alphabet("base64", 64, data, 0, s_code_point) {}
232 std::vector<uint8_t> octets = { };
234 REQUIRE(
"" == encstr);
236 REQUIRE( octets == dec_octets );
239 std::vector<uint8_t> octets = {
'f' };
241 REQUIRE(
"Zg==" == encstr);
243 REQUIRE( octets == dec_octets );
246 std::vector<uint8_t> octets = {
'f',
'o' };
248 REQUIRE(
"Zm8=" == encstr);
250 REQUIRE( octets == dec_octets );
253 std::vector<uint8_t> octets = {
'f',
'o',
'o' };
255 REQUIRE(
"Zm9v" == encstr);
257 REQUIRE( octets == dec_octets );
260 std::vector<uint8_t> octets = {
'f',
'o',
'o',
'b' };
262 REQUIRE(
"Zm9vYg==" == encstr);
264 REQUIRE( octets == dec_octets );
267 std::vector<uint8_t> octets = {
'f',
'o',
'o',
'b',
'a' };
269 REQUIRE(
"Zm9vYmE=" == encstr);
271 REQUIRE( octets == dec_octets );
276 std::vector<uint8_t> octets = {
'a' };
278 REQUIRE(
"YQ==" == encstr);
280 REQUIRE( octets == dec_octets );
283 std::vector<uint8_t> octets = {
'a',
'b' };
285 REQUIRE(
"YWI=" == encstr);
287 REQUIRE( octets == dec_octets );
290 std::vector<uint8_t> octets = {
'a',
'b',
'c' };
292 REQUIRE(
"YWJj" == encstr);
294 REQUIRE( octets == dec_octets );
297 std::vector<uint8_t> octets = {
'a',
'b',
'c',
'd' };
299 REQUIRE(
"YWJjZA==" == encstr);
301 REQUIRE( octets == dec_octets );
304 std::vector<uint8_t> octets = {
'a',
'b',
'c',
'd',
'e' };
306 REQUIRE(
"YWJjZGU=" == encstr);
308 REQUIRE( octets == dec_octets );
311 std::vector<uint8_t> octets = {
'a',
'b',
'c',
'd',
'e',
'f',
'g' };
313 REQUIRE(
"YWJjZGVmZw==" == encstr);
315 REQUIRE( octets == dec_octets );
319 std::vector<uint8_t> octets = {
'a',
'b',
'c',
'd',
'e',
'f',
'g' };
321 REQUIRE(
"YWJjZGVmZw" == encstr);
325 REQUIRE( octets == dec_octets );
330 REQUIRE( dec_octets.empty() );
335 std::vector<uint8_t> octets = {
'a' };
337 REQUIRE(
"YQ" == encstr);
341 REQUIRE( octets == dec_octets );
346 REQUIRE( dec_octets.empty() );
351 std::vector<uint8_t> octets = {
'a',
'b',
'c',
'd',
'e' };
353 REQUIRE(
"YWJjZGU" == encstr);
357 REQUIRE( octets == dec_octets );
362 REQUIRE( dec_octets.empty() );
367 std::vector<uint8_t> octets = {
'a',
'b' };
369 REQUIRE(
"YWI" == encstr);
373 REQUIRE( octets == dec_octets );
378 REQUIRE( dec_octets.empty() );
383 std::vector<uint8_t> octets = {
'a',
'b',
'c' };
385 REQUIRE(
"YWJj" == encstr);
389 REQUIRE( octets == dec_octets );
394 REQUIRE( octets == dec_octets );
398 std::string in_str =
"aaaaaaaaaaaaaaaaa";
399 std::vector<uint8_t> octets(in_str.begin(), in_str.end());
401 REQUIRE(
"YWFhYWFhYWFhYWFhYWFhYWE=" == encstr);
403 REQUIRE( octets == dec_octets );
408 std::vector<uint8_t> octets = { 0x03, 0xef, 0xff, 0xf9 };
410 REQUIRE(
"A+//+Q==" == encstr);
412 REQUIRE( octets == dec_octets );
416 std::vector<uint8_t> octets = { 0x03, 0xef, 0xff, 0xf9 };
418 REQUIRE(
"A-__-Q==" == encstr);
420 REQUIRE( octets == dec_octets );
424 std::string in_str =
"one two three four five six seven eight nine ten eleven twelve thirteen fourteen fivteen sixteen seventeen eighteen nineteen twenty twenty-one";
425 std::string exp_encstr =
"b25lIHR3byB0aHJlZSBmb3VyIGZpdmUgc2l4IHNldmVuIGVpZ2h0IG5pbmUgdGVuIGVsZXZlbiB0"
426 "d2VsdmUgdGhpcnRlZW4gZm91cnRlZW4gZml2dGVlbiBzaXh0ZWVuIHNldmVudGVlbiBlaWdodGVl"
427 "biBuaW5ldGVlbiB0d2VudHkgdHdlbnR5LW9uZQ==";
428 std::vector<uint8_t> octets(in_str.begin(), in_str.end());
430 REQUIRE( exp_encstr == encstr);
432 REQUIRE( octets == dec_octets );
435 std::string in_str =
"one two three four five six seven eight nine ten eleven twelve thirteen fourteen fivteen sixteen seventeen eighteen nineteen twenty twenty-one";
436 std::string exp_encstr =
"b25lIHR3byB0aHJlZSBmb3VyIGZpdmUgc2l4IHNldmVuIGVpZ2h0IG5pbmUgdGVuIGVsZXZlbiB0\n"
437 "d2VsdmUgdGhpcnRlZW4gZm91cnRlZW4gZml2dGVlbiBzaXh0ZWVuIHNldmVudGVlbiBlaWdodGVl\n"
438 "biBuaW5ldGVlbiB0d2VudHkgdHdlbnR5LW9uZQ==";
439 std::vector<uint8_t> octets(in_str.begin(), in_str.end());
441 REQUIRE( exp_encstr == encstr);
443 REQUIRE( octets == dec_octets );
446 std::string in_str =
"one two three four five six seven eight nine ten eleven twelve thirteen fourteen fivteen sixteen seventeen eighteen nineteen twenty twenty-one";
447 std::string exp_encstr =
"b25lIHR3byB0aHJlZSBmb3VyIGZpdmUgc2l4IHNldmVuIGVpZ2h0IG5pbmUgdGVu\n"
448 "IGVsZXZlbiB0d2VsdmUgdGhpcnRlZW4gZm91cnRlZW4gZml2dGVlbiBzaXh0ZWVu\n"
449 "IHNldmVudGVlbiBlaWdodGVlbiBuaW5ldGVlbiB0d2VudHkgdHdlbnR5LW9uZQ==";
450 std::vector<uint8_t> octets(in_str.begin(), in_str.end());
452 REQUIRE( exp_encstr == encstr);
454 REQUIRE( octets == dec_octets );
459 std::string encstr =
"!@#$%^&*()";
461 REQUIRE( dec_octets.empty() );
466TEST_CASE(
"Binary Base 64 Encoding Test 11",
"[binary][type]" ) {
base64_alphabet_nopadding() noexcept
Base Alphabet Specification providing the alphabet for encode() and decode().
constexpr int base() const noexcept
The fixed base used for this alphabet.
std::string to_string() const noexcept
alphabet(std::string _name, int _base, std::string_view _symbols, char _padding64, code_point_func _cpf) noexcept
Safe base 38 alphabet with ASCII code-point sorting order.
Safe base 64 alphabet with ASCII code-point sorting order.
Base 86 alphabet with ASCII code-point sorting order.
Safe canonical base64 alphabet, without ASCII code-point sorting order.
Safe canonical base64url alphabet, without ASCII code-point sorting order.
Natural base 86 alphabet, without ASCII code-point sorting order.
std::string encode(int num, const alphabet &aspec, const unsigned int min_width=0) noexcept
Encodes a given positive decimal number to a symbolic string representing a given alphabet and its ba...
std::string encode64_mime(const void *in_octets, size_t in_len, const alphabet &aspec) noexcept
Encodes given octets using the given alphabet and fixed base 64 encoding according to base64 RFC 4648...
std::string encode64(const void *in_octets, size_t in_len, const alphabet &aspec) noexcept
Encodes given octets using the given alphabet and fixed base 64 encoding according to base64 RFC 4648...
std::vector< uint8_t > decode64(const std::string_view &str, const alphabet &aspec) noexcept
Decodes a given symbolic string representing using given alphabet and fixed base 64 to octets accordi...
std::string encode64_pem(const void *in_octets, size_t in_len, const alphabet &aspec) noexcept
Encodes given octets using the given alphabet and fixed base 64 encoding according to base64 RFC 4648...
std::vector< uint8_t > decode64_lf(const std::string_view &str, const alphabet &aspec) noexcept
Decodes a given symbolic string representing using given alphabet and fixed base 64 to octets accordi...
int64_t decode(const std::string_view &str, const alphabet &aspec) noexcept
Decodes a given symbolic string representing a given alphabet and its base to a positive decimal numb...
constexpr T min(const T x, const T y) noexcept
Returns the minimum of two integrals (w/ branching) in O(1)
constexpr T max(const T x, const T y) noexcept
Returns the maximum of two integrals (w/ branching) in O(1)
mp::BigInt pow(mp::BigInt b, mp::BigInt e)
constexpr T abs(const T x) noexcept
Returns the absolute value of an arithmetic number (w/ branching) in O(1)
static void testIntegerBase86(const jau::codec::base::alphabet &aspec)
static void testIntegerBase64(const jau::codec::base::alphabet &aspec)
TEST_CASE("Integer Base 38 Encoding Test 01", "[integer][type]")
static void testBinaryBase64()
static void testRadix_int64(const jau::codec::base::alphabet &aspec, const int64_t test_min, const int64_t test_max)
static void testRadix_3digits_int32(const jau::codec::base::alphabet &aspec)