diff --git a/Src/GeneralUtility.cpp b/Src/GeneralUtility.cpp index d239804..7cfda9e 100644 --- a/Src/GeneralUtility.cpp +++ b/Src/GeneralUtility.cpp @@ -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 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!"); diff --git a/Test/BaseX_2_10.cpp b/Test/BaseX_2_10.cpp new file mode 100644 index 0000000..cc90eec --- /dev/null +++ b/Test/BaseX_2_10.cpp @@ -0,0 +1,70 @@ +#include +#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); +} diff --git a/Test/CMakeLists.txt b/Test/CMakeLists.txt index 7e00b6c..be3c85c 100644 --- a/Test/CMakeLists.txt +++ b/Test/CMakeLists.txt @@ -12,6 +12,7 @@ add_executable(Test Ord.cpp StringDivision.cpp + BaseX_2_10.cpp ) target_link_libraries(Test GeneralUtility)