Skip to content

Commit

Permalink
Add convert_to_base_64 function to convert string integer to magnitudes
Browse files Browse the repository at this point in the history
  • Loading branch information
sahmad98 committed May 19, 2018
1 parent 20c8173 commit 7b6df13
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ add_executable(Functions.Random.Test
test/functions/random.test.cpp)
target_link_libraries(Functions.Random.Test TestRunner)

add_executable(Utility.Helper.Test
test/functions/utility.test.cpp)
target_link_libraries(Utility.Helper.Test TestRunner)

# Operators:
add_executable(Operators.ArithmeticAssignment.Test
test/operators/arithmetic_assignment.test.cpp)
Expand Down
15 changes: 3 additions & 12 deletions include/constructors/constructors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,7 @@ BigInt::BigInt(const std::string& num) {
if (num[0] == '+' or num[0] == '-') { // check for sign
std::string num_magnitude = num.substr(1);
if (is_valid_number(num_magnitude)) {
/*
TODO
----
magnitude = convert_to_base_2_to_the_64(num_magnitude);
*/
magnitude = {0};
magnitude = convert_to_base_64(num_magnitude);
is_negative = num[0] == '-';
}
else {
Expand All @@ -67,12 +62,8 @@ BigInt::BigInt(const std::string& num) {
}
else { // if no sign is specified
if (is_valid_number(num)) {
/*
TODO
----
magnitude = convert_to_base_2_to_the_64(num_magnitude);
*/
magnitude = {0};
std::string num_magnitude(num);
magnitude = convert_to_base_64(num_magnitude);
is_negative = false; // positive by default
}
else {
Expand Down
26 changes: 26 additions & 0 deletions include/functions/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,30 @@ bool is_power_of_10(const std::string& num){
return true; // first digit is 1 and the following digits are all 0
}


/*
conver_to_base_64
----------------------
Converts a string represented BigInt into the Base64 magnitudes
*/

std::vector<unsigned long long> convert_to_base_64(std::string& num) {
std::vector<unsigned long long> magnitude;
auto start_it = num.size() - 1; // Starting from Backward direction
auto stop_it = num.size();
uint64_t component = 0;
while(start_it != -1) {
try {
std::string num_slice = num.substr(start_it, stop_it);
component = std::stoull(num_slice);
} catch (std::out_of_range& e) { //Using out_of_range to determine overflow
magnitude.push_back(component);
stop_it = start_it + 1;
}
--start_it;
}
magnitude.push_back(component);
return magnitude;
}

#endif // BIG_INT_UTILITY_FUNCTIONS_HPP
25 changes: 25 additions & 0 deletions test/functions/utility.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <string>
#include <vector>

#include "function/utility.hpp"

#include "third_party/catch.hpp"

TEST_CASE("Conversion to Base64 magnitudes from String", "[Utility][Helper][Base64]") {
std::string num = "481482486096808911670147153572883801";
// Magnitude should be {11670147153572883801, 4814824860968089}
auto magnitude = convert_to_base_64(num);
REQUIRE(magnitude.size() == 2);
REQUIRE(magnitude[0] == uint64_t(11670147153572883801));
REQUIRE(magnitude[1] == uint64_t(4814824860968089));

num = "1";
magnitude = convert_to_base_64(num);
REQUIRE(magnitude.size() == 1);
REQUIRE(magnitude[0] == uint64_t(1));

num = "";
magnitude = convert_to_base_64(num);
REQUIRE(magnitude.size() == 1);
REQUIRE(magnitude[0] == uint64_t(0));
}

0 comments on commit 7b6df13

Please sign in to comment.