Added Base10_2_X wrapper, and tests
This commit is contained in:
parent
932bd62477
commit
eaa406c9e6
@ -126,3 +126,16 @@ std::string GeneralUtility::BaseX_2_Y(const std::string &num, const std::string
|
|||||||
|
|
||||||
return ss.str();
|
return ss.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string GeneralUtility::Base10_2_X(const std::uint64_t &num, const std::string &set, const std::uint32_t minOutLen) {
|
||||||
|
// Convert num to a string
|
||||||
|
std::stringstream ss;
|
||||||
|
ss << num;
|
||||||
|
const std::string numStr = ss.str();
|
||||||
|
|
||||||
|
// Use BaseX_2_Y to convert to outbase
|
||||||
|
const std::string convertedNum = BaseX_2_Y(numStr, "0123456789", set, minOutLen);
|
||||||
|
|
||||||
|
// return it
|
||||||
|
return convertedNum;
|
||||||
|
}
|
||||||
|
@ -29,13 +29,20 @@ public:
|
|||||||
//! \return A 64-bit integer representing the number
|
//! \return A 64-bit integer representing the number
|
||||||
static std::uint64_t BaseX_2_10(const std::string& num, const std::string& set);
|
static std::uint64_t BaseX_2_10(const std::string& num, const std::string& set);
|
||||||
|
|
||||||
|
//! Will convert a number to an arbitrary base.
|
||||||
|
//! This just a wrapper for BaseX_2_Y.
|
||||||
|
//! \param num The number to be converted
|
||||||
|
//! \param set The desired set/base for the output to be in
|
||||||
|
//! \return `num` in base `set`
|
||||||
|
static std::string Base10_2_X(const std::uint64_t& num, const std::string& set, const std::uint32_t minOutLen = 1);
|
||||||
|
|
||||||
//! Will convert a number from an arbitrary base to another arbitrary base.
|
//! Will convert a number from an arbitrary base to another arbitrary base.
|
||||||
//! \param num A string representation of a number
|
//! \param num A string representation of a number
|
||||||
//! \param set_in The set/base of the input
|
//! \param set_in The set/base of the input
|
||||||
//! \param set_out The desired set/base to output
|
//! \param set_out The desired set/base to output
|
||||||
//! \param minLen The minimum output length. Setting this will result in zero-padded output (Like, 00000001 instead of 1)
|
//! \param minLen The minimum output length. Setting this will result in zero-padded output (Like, 00000001 instead of 1)
|
||||||
//! \return `num` in base `set_out`
|
//! \return `num` in base `set_out`
|
||||||
static std::string BaseX_2_Y(const std::string& num, const std::string& set_in, const std::string& set_out, const std::uint32_t minLen = 1);
|
static std::string BaseX_2_Y(const std::string& num, const std::string& set_in, const std::string& set_out, const std::uint32_t minOutLen = 1);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// No instantiation! >:(
|
// No instantiation! >:(
|
||||||
|
77
Test/Base10_2_X.cpp
Normal file
77
Test/Base10_2_X.cpp
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
#include <GeneralUtility.h>
|
||||||
|
#include "Catch2.h"
|
||||||
|
|
||||||
|
// Tests base 10 to 10
|
||||||
|
TEST_CASE(__FILE__"/Base10_to_10", "[Base10_2_X]")
|
||||||
|
{
|
||||||
|
// Setup
|
||||||
|
const std::string set = "0123456789";
|
||||||
|
const std::uint64_t in = 1990381;
|
||||||
|
const std::string expected_out = "1990381";
|
||||||
|
|
||||||
|
// Exercise
|
||||||
|
const std::string out = GeneralUtility::Base10_2_X(in, set);
|
||||||
|
|
||||||
|
// Verify
|
||||||
|
REQUIRE(out == expected_out);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tests base 10 to 16
|
||||||
|
TEST_CASE(__FILE__"/Base10_to_16", "[Base10_2_X]")
|
||||||
|
{
|
||||||
|
// Setup
|
||||||
|
const std::string set = "0123456789abcdef";
|
||||||
|
const std::uint64_t in = 0x1990381;
|
||||||
|
const std::string expected_out = "1990381";
|
||||||
|
|
||||||
|
// Exercise
|
||||||
|
const std::string out = GeneralUtility::Base10_2_X(in, set);
|
||||||
|
|
||||||
|
// Verify
|
||||||
|
REQUIRE(out == expected_out);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tests base 10 to 2
|
||||||
|
TEST_CASE(__FILE__"/Base10_to_2", "[Base10_2_X]")
|
||||||
|
{
|
||||||
|
// Setup
|
||||||
|
const std::string set = "01";
|
||||||
|
const std::uint64_t in = 0b10111011;
|
||||||
|
const std::string expected_out = "10111011";
|
||||||
|
|
||||||
|
// Exercise
|
||||||
|
const std::string out = GeneralUtility::Base10_2_X(in, set);
|
||||||
|
|
||||||
|
// Verify
|
||||||
|
REQUIRE(out == expected_out);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tests base 10 to fucking big
|
||||||
|
TEST_CASE(__FILE__"/Base10_to_FuckingBig", "[Base10_2_X]")
|
||||||
|
{
|
||||||
|
// Setup
|
||||||
|
const std::string set = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
||||||
|
const std::uint64_t in = 189434143264827;
|
||||||
|
const std::string expected_out = "rn5qZuTD";
|
||||||
|
|
||||||
|
// Exercise
|
||||||
|
const std::string out = GeneralUtility::Base10_2_X(in, set);
|
||||||
|
|
||||||
|
// Verify
|
||||||
|
REQUIRE(out == expected_out);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tests that padding works
|
||||||
|
TEST_CASE(__FILE__"/TestPadding", "[Base10_2_X]")
|
||||||
|
{
|
||||||
|
// Setup
|
||||||
|
const std::string set = "01";
|
||||||
|
const std::uint64_t in = 0b101;
|
||||||
|
const std::string expected_out = "00000101";
|
||||||
|
|
||||||
|
// Exercise
|
||||||
|
const std::string out = GeneralUtility::Base10_2_X(in, set, 8);
|
||||||
|
|
||||||
|
// Verify
|
||||||
|
REQUIRE(out == expected_out);
|
||||||
|
}
|
@ -13,6 +13,7 @@ add_executable(Test
|
|||||||
Ord.cpp
|
Ord.cpp
|
||||||
StringDivision.cpp
|
StringDivision.cpp
|
||||||
BaseX_2_10.cpp
|
BaseX_2_10.cpp
|
||||||
|
Base10_2_X.cpp
|
||||||
BaseX_2_Y.cpp
|
BaseX_2_Y.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user