From 16b2a6f792a96466f47da2483967b27b3c57678f Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Tue, 29 Jun 2021 12:00:12 -0400 Subject: [PATCH] Fixed bug in toBin function --- Algorithms.hpp | 2 +- testAlgorithms.cpp | 18 +++++++++++++----- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/Algorithms.hpp b/Algorithms.hpp index 710e9e2..e49d7b5 100644 --- a/Algorithms.hpp +++ b/Algorithms.hpp @@ -608,7 +608,7 @@ bool isPalindrome(std::string str){ template std::string toBin(T num){ //Convert the number to a binary string - std::string fullString = std::bitset(num).to_string(); + std::string fullString = std::bitset(num).to_string(); //Remove leading zeros int loc = 0; for(loc = 0;(loc < fullString.size()) && (fullString[loc] == '0');++loc); diff --git a/testAlgorithms.cpp b/testAlgorithms.cpp index 634725e..cd3afa8 100644 --- a/testAlgorithms.cpp +++ b/testAlgorithms.cpp @@ -553,21 +553,29 @@ bool testToBin(){ } //Test 2 - uint64_t num2 = 8; - correctAnswer = "1000"; - answer = mee::toBin(num2); + num = 0; + correctAnswer = "0"; + answer = mee::toBin(num); if(correctAnswer != answer){ return false; } //Test 3 - num = 0; - correctAnswer = "0"; + num = 1000000; + correctAnswer = "11110100001001000000"; answer = mee::toBin(num); if(correctAnswer != answer){ return false; } + //Test 4 + uint64_t num2 = 8; + correctAnswer = "1000"; + answer = mee::toBin(num2); + if(correctAnswer != answer){ + return false; + } + //If it hasn't failed a test then return true for passing all the tests return true; }