// ------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All rights reserved. // ------------------------------------------------------------ #include "pch.h" #include #include #include "CppUnitTest.h" namespace cdb_core_test { using namespace std::literals; using Assert = Microsoft::VisualStudio::CppUnitTestFramework::Assert; using Logger = Microsoft::VisualStudio::CppUnitTestFramework::Logger; TEST_CLASS(Base64UnitTests) { public: template void Roundtrip() { std::array bytes{}; std::array bytes2{}; std::array(bytes.size()))> chars{}; std::random_device seedGenerator{}; std::mt19937 rand{seedGenerator()}; const std::uniform_int_distribution distribution{}; cdb_core::Span bb = cdb_core::MemoryMarshal::Cast(cdb_core::Span{bytes}); for (auto& b : bb) { b = distribution(rand); } std::fill(chars.begin(), chars.end(), '\0'); cdb_core::Span encoded = cdb_core::Base64::Encode(bytes, cdb_core::Span{chars}); Logger::WriteMessage(&encoded[0]); cdb_core::Span decoded = cdb_core::Base64::Decode(cdb_core::ReadOnlySpan{encoded}, cdb_core::Span{bytes2}); Assert::AreEqual(static_cast(bytes.size()), decoded.Length()); for (uint32_t i = 0; i < decoded.Length(); i++) { Assert::AreEqual(bytes[i], decoded[i]); } } /// /// Tests whether a byte string properly round-trips as Base64 text. /// TEST_METHOD(RoundtripA) { Roundtrip(); } /// /// Tests whether a byte string properly round-trips as Base64 wide text. /// TEST_METHOD(RoundtripW) { Roundtrip(); } }; } // Additional string function for test diagnostics. namespace Microsoft::VisualStudio::CppUnitTestFramework { template<> inline std::wstring ToString(const std::byte& q) { return cdb_core::make_string(L"{%u}", q); } }