Files
libbasen/test/baseN.cpp
SEK1RO 210a689cee
Some checks failed
Deploy documentation / deploy (push) Has been cancelled
Meson instead of Make
2026-01-13 13:18:37 +03:00

86 lines
2.3 KiB
C++

#include <utility>
#include <basen/base58.hpp>
#include <basen/baseN.hpp>
#include <basen/Exception.hpp>
#include <basen/hex.hpp>
#include <gtest/gtest.h>
namespace baseN {
TEST(baseN, digitsMap)
{
uint8_t map[256];
digitsMap(base58::digits, 58, map);
EXPECT_TRUE(std::equal(map, map + 256, base58::map));
EXPECT_THROW(digitsMap("11", 2, map), basen::Exception);
}
TEST(baseN, isValid)
{
std::vector<std::pair<bool, std::string>> tests = {
{true, "123"},
{false, "@#$"},
};
for (auto it : tests)
EXPECT_EQ(it.first, isValid(it.second, base58::map));
}
TEST(baseN, sizeEncoded)
{
std::vector<std::pair<size_t, std::string>> tests = {
{6, "12341234"},
{5, "00000000"},
};
for (auto it : tests)
EXPECT_EQ(it.first, sizeEncoded(hex::decode(it.second), 58));
EXPECT_THROW(sizeEncoded(hex::decode(""), 0), basen::Exception);
}
TEST(baseN, sizeDecoded)
{
std::vector<std::pair<size_t, std::string>> tests = {
{3, "qwer"},
{5, "1111"},
};
for (auto it : tests)
EXPECT_EQ(it.first, sizeDecoded(it.second, 58, base58::digits));
EXPECT_THROW(sizeDecoded("", 0, base58::digits), basen::Exception);
}
std::vector<std::pair<std::string, std::string>> tests = {
{"", ""},
{"Ky", "044c"},
{"KyK", "f94a"},
{"KyKX", "387ae2"},
{"KyKXa", "0ccbd755"},
{"KyKXaa", "02e62ec963"},
{"111KyKX", "000000387ae2"},
{"4uqWDRyJZUpS6KKwLAiitndmv7TPFt2bfxVVfhJhgTn3Rh6aQtGHQY6PhhNDpCwSNU8a",
"057902f9cebebb68879911002aae743280140a78c4a077405b057902f9cebebb68879911002aae743280140a78c4a077405b"},
};
TEST(baseN, encode)
{
for (auto it : tests)
EXPECT_EQ(it.first, encode(hex::decode(it.second), 58, base58::digits));
}
TEST(baseN, encode_1e3)
{
std::vector<uint8_t> data(1e3);
std::fill(data.begin(), data.end(), 1);
encode(data, 58, base58::digits);
}
TEST(baseN, decode)
{
for (auto it : tests)
EXPECT_EQ(hex::encode(decode(it.first, 58, base58::digits, base58::map)), it.second);
EXPECT_THROW(decode("!@#", 58, base58::digits, base58::map), basen::Exception);
}
TEST(baseN, decode_1e3)
{
std::string str(1e3, '2');
decode(str, 58, base58::digits, base58::map);
}
}