Added tests for baseX_2_10

This commit is contained in:
Leonetienne 2022-02-27 16:09:22 +01:00
parent a0a77f51fe
commit 88d78a472b
3 changed files with 76 additions and 1 deletions

View File

@ -16,6 +16,10 @@ namespace {
}
std::uint64_t GeneralUtility::BaseX_2_10(const std::string& num, const std::string& set) {
// If base is 0, throw logic error
if (set.length() == 0)
throw std::logic_error("Can't convert form base0! Please supply a nonempty set!");
unsigned long long int buf = 0;
for (std::size_t i = 0; i < num.length(); i++) {
for (std::size_t j = 0; j < set.length(); j++) {
@ -34,7 +38,7 @@ std::pair<std::string, int>
GeneralUtility::StringDivision(const std::string& dividend, const unsigned int divisor, const std::string& set) {
// No set? Throw logic error
if (set.length() == 0)
throw std::logic_error("Can't divide a number of base0! Please supple a nonempty set!");
throw std::logic_error("Can't divide a number of base0! Please supply a nonempty set!");
// No division by 0
if (divisor == 0)
throw std::overflow_error("Division by zero!");

70
Test/BaseX_2_10.cpp Normal file
View File

@ -0,0 +1,70 @@
#include <GeneralUtility.h>
#include "Catch2.h"
// Tests base 10 to 10
TEST_CASE(__FILE__"/Base10_to_10", "[BaseX_2_10]")
{
// Setup
const std::string set = "0123456789";
const std::string in = "1990381";
const std::uint64_t expected_out = 1990381;
// Exercise
const std::uint64_t out = GeneralUtility::BaseX_2_10(in, set);
// Verify
REQUIRE(out == expected_out);
}
// Tests base 16 to 10
TEST_CASE(__FILE__"/Base16_to_10", "[BaseX_2_10]")
{
// Setup
const std::string set = "0123456789abcdef";
const std::string in = "1a83f9cefa";
const std::uint64_t expected_out = 0x1a83f9cefa;
// Exercise
const std::uint64_t out = GeneralUtility::BaseX_2_10(in, set);
// Verify
REQUIRE(out == expected_out);
}
// Tests base 2 to 10
TEST_CASE(__FILE__"/Base2_to_10", "[BaseX_2_10]")
{
// Setup
const std::string set = "01";
const std::string in = "10101010110000111111";
const std::uint64_t expected_out = 0b10101010110000111111;
// Exercise
const std::uint64_t out = GeneralUtility::BaseX_2_10(in, set);
// Verify
REQUIRE(out == expected_out);
}
// Tests base fucking big to 10
TEST_CASE(__FILE__"/BaseFuckingBig_to_10", "[BaseX_2_10]")
{
// Setup
const std::string set = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
const std::string in = "rn5qZuTD";
const std::uint64_t expected_out = 189434143264827;
// Exercise
const std::uint64_t out = GeneralUtility::BaseX_2_10(in, set);
// Verify
REQUIRE(out == expected_out);
}
// Tests that a logic error is thrown when the supplied set is empty
TEST_CASE(__FILE__"/NoSetSupplied", "[BaseX_2_10]")
{
REQUIRE_THROWS_AS(
GeneralUtility::BaseX_2_10("699", "")
, std::logic_error);
}

View File

@ -12,6 +12,7 @@ add_executable(Test
Ord.cpp
StringDivision.cpp
BaseX_2_10.cpp
)
target_link_libraries(Test GeneralUtility)