DCCL v5
Loading...
Searching...
No Matches
test.cpp
1// Copyright 2024:
2// GobySoft, LLC (2013-)
3// Community contributors (see AUTHORS file)
4// File authors:
5// Toby Schneider <toby@gobysoft.org>
6//
7//
8// This file is part of the Dynamic Compact Control Language Library
9// ("DCCL").
10//
11// DCCL is free software: you can redistribute it and/or modify
12// it under the terms of the GNU Lesser General Public License as published by
13// the Free Software Foundation, either version 2.1 of the License, or
14// (at your option) any later version.
15//
16// DCCL is distributed in the hope that it will be useful,
17// but WITHOUT ANY WARRANTY; without even the implied warranty of
18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19// GNU Lesser General Public License for more details.
20//
21// You should have received a copy of the GNU Lesser General Public License
22// along with DCCL. If not, see <http://www.gnu.org/licenses/>.
23#include <cassert>
24#include <iostream>
25#include <numeric>
26#include <string>
27
28#include "../../binary.h"
29
30#include "dccl/logger.h"
31
32int main(int argc, char* argv[])
33{
34 bool verbose = false;
35 for (int i = 1; i < argc; ++i)
36 {
37 if (argv[i] && argv[i][0] == '-' && argv[i][1] == 'v' && argv[i][2] == '\0')
38 verbose = true;
39 }
40
41 dccl::dlog.connect(verbose ? dccl::logger::ALL : dccl::logger::WARN_PLUS, &std::cerr);
42
43 // Basic encode/decode round-trip
44 {
45 std::string original = "Hello, World!";
46 std::string encoded = dccl::b64_encode(original);
47 std::string decoded = dccl::b64_decode(encoded);
48 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "original: " << original << std::endl;
49 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "encoded: " << encoded << std::endl;
50 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "decoded: " << decoded << std::endl;
51 assert(encoded == "SGVsbG8sIFdvcmxkIQ==");
52 assert(decoded == original);
53 }
54
55 // Binary data round-trip
56 {
57 std::string binary_data("\x00\x01\x02\x03\xff\xfe\xfd", 7);
58 std::string encoded = dccl::b64_encode(binary_data);
59 std::string decoded = dccl::b64_decode(encoded);
60 assert(decoded == binary_data);
61 }
62
63 // Empty string
64 {
65 std::string empty;
66 std::string encoded = dccl::b64_encode(empty);
67 std::string decoded = dccl::b64_decode(encoded);
68 assert(encoded.empty());
69 assert(decoded.empty());
70 }
71
72 // Known base64 values
73 {
74 assert(dccl::b64_encode("Man") == "TWFu");
75 assert(dccl::b64_encode("Ma") == "TWE=");
76 assert(dccl::b64_encode("M") == "TQ==");
77 assert(dccl::b64_decode("TWFu") == "Man");
78 assert(dccl::b64_decode("TWE=") == "Ma");
79 assert(dccl::b64_decode("TQ==") == "M");
80 }
81
82 // Long string round-trip (1000 bytes)
83 {
84 std::string long_data(1000, '\0');
85 std::iota(long_data.begin(), long_data.end(), 0); // fill with 0,1,2,...,231,232,...,255,0,1,...
86 std::string encoded = dccl::b64_encode(long_data);
87 std::string decoded = dccl::b64_decode(encoded);
88 // encoded length must be a multiple of 4
89 assert(encoded.size() % 4 == 0);
90 assert(decoded == long_data);
91 }
92
93 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "all tests passed" << std::endl;
94
95 return 0;
96}
bool is(logger::Verbosity verbosity, logger::Group group=logger::GENERAL)
Indicates the verbosity of the Logger until the next std::flush or std::endl. The boolean return is u...
Definition logger.h:191
void connect(int verbosity_mask, Slot slot)
Connect the output of one or more given verbosities to a slot (function pointer or similar)
Definition logger.h:213
Dynamic Compact Control Language namespace.
Definition any.h:28