From 9fe0daef5766fed7db8293456ade7c0be6d3de83 Mon Sep 17 00:00:00 2001 From: Vlad Pirlog Date: Sun, 4 Oct 2020 22:05:08 +0300 Subject: [PATCH 01/15] :art: Improve code structure --- src/bigint.hpp | 214 ++++++++++++++++++++++++------------------------- 1 file changed, 105 insertions(+), 109 deletions(-) diff --git a/src/bigint.hpp b/src/bigint.hpp index b9c485c..e9582f2 100644 --- a/src/bigint.hpp +++ b/src/bigint.hpp @@ -31,115 +31,111 @@ #include #include -namespace libbig { - class largeInt { - private: - bool sign; - std::string number; - - public: - largeInt() { - } - - largeInt(int) { - } - largeInt(int64_t) { - } - largeInt(long long) { - } - largeInt(std::string) { - } - - largeInt operator + (largeInt); - largeInt operator + (int); - largeInt operator + (int64_t); - largeInt operator + (long long); - - largeInt operator - (largeInt); - largeInt operator - (int); - largeInt operator - (int64_t); - largeInt operator - (long long); - - largeInt operator * (largeInt); - largeInt operator * (int); - largeInt operator * (int64_t); - largeInt operator * (long long); - - largeInt operator / (largeInt); - largeInt operator / (int); - largeInt operator / (int64_t); - largeInt operator / (long long); - - largeInt operator % (largeInt); - largeInt operator % (int); - largeInt operator % (int64_t); - largeInt operator % (long long); - - largeInt &operator += (largeInt); - largeInt &operator += (int); - largeInt &operator += (int64_t); - largeInt &operator += (long long); - - largeInt &operator -= (largeInt); - largeInt &operator -= (int); - largeInt &operator -= (int64_t); - largeInt &operator -= (long long); - - largeInt &operator *= (largeInt); - largeInt &operator *= (int); - largeInt &operator *= (int64_t); - largeInt &operator *= (long long); - - largeInt &operator /= (largeInt); - largeInt &operator /= (int); - largeInt &operator /= (int64_t); - largeInt &operator /= (long long); - - largeInt operator %= (largeInt); - largeInt operator %= (int); - largeInt operator %= (int64_t); - largeInt operator %= (long long); - - // PREFIX OPERATORS - - // POSTFIX OPERATORS - - void operator = (largeInt); - void operator = (int); - void operator = (int64_t); - void operator = (long long); - - bool operator == (const largeInt&); - bool operator == (int); - bool operator == (int64_t); - bool operator == (long long); - - bool operator != (largeInt); - bool operator != (int); - bool operator != (int64_t); - - bool operator < (largeInt); - bool operator < (int); - bool operator < (int64_t); - bool operator < (long long); - - bool operator > (largeInt); - bool operator > (int); - bool operator > (int64_t); - bool operator > (long long); - - bool operator <= (largeInt); - bool operator <= (int); - bool operator <= (int64_t); - bool operator <= (long long); - - bool operator >= (largeInt); - bool operator >= (int); - bool operator >= (int64_t); - bool operator >= (long long); - - friend std::istream &operator >> (std::istream &, largeInt &); - friend std::ostream &operator << (std::ostream &, largeInt &); +namespace libbig +{ + class largeInt + { + private: + bool sign; + std::string number; + + public: + largeInt(); + largeInt(int); + largeInt(int64_t); + largeInt(long long); + largeInt(std::string); + + largeInt operator+(largeInt); + largeInt operator+(int); + largeInt operator+(int64_t); + largeInt operator+(long long); + + largeInt operator-(largeInt); + largeInt operator-(int); + largeInt operator-(int64_t); + largeInt operator-(long long); + + largeInt operator*(largeInt); + largeInt operator*(int); + largeInt operator*(int64_t); + largeInt operator*(long long); + + largeInt operator/(largeInt); + largeInt operator/(int); + largeInt operator/(int64_t); + largeInt operator/(long long); + + largeInt operator%(largeInt); + largeInt operator%(int); + largeInt operator%(int64_t); + largeInt operator%(long long); + + largeInt &operator+=(largeInt); + largeInt &operator+=(int); + largeInt &operator+=(int64_t); + largeInt &operator+=(long long); + + largeInt &operator-=(largeInt); + largeInt &operator-=(int); + largeInt &operator-=(int64_t); + largeInt &operator-=(long long); + + largeInt &operator*=(largeInt); + largeInt &operator*=(int); + largeInt &operator*=(int64_t); + largeInt &operator*=(long long); + + largeInt &operator/=(largeInt); + largeInt &operator/=(int); + largeInt &operator/=(int64_t); + largeInt &operator/=(long long); + + largeInt operator%=(largeInt); + largeInt operator%=(int); + largeInt operator%=(int64_t); + largeInt operator%=(long long); + + // PREFIX OPERATORS + + // POSTFIX OPERATORS + + void operator=(largeInt); + void operator=(int); + void operator=(int64_t); + void operator=(long long); + + bool operator==(const largeInt &); + bool operator==(int); + bool operator==(int64_t); + bool operator==(long long); + + bool operator!=(largeInt); + bool operator!=(int); + bool operator!=(int64_t); + + bool operator<(largeInt); + bool operator<(int); + bool operator<(int64_t); + bool operator<(long long); + + bool operator>(largeInt); + bool operator>(int); + bool operator>(int64_t); + bool operator>(long long); + + bool operator<=(largeInt); + bool operator<=(int); + bool operator<=(int64_t); + bool operator<=(long long); + + bool operator>=(largeInt); + bool operator>=(int); + bool operator>=(int64_t); + bool operator>=(long long); + + friend std::istream &operator>>(std::istream &, largeInt &); + friend std::ostream &operator<<(std::ostream &, largeInt &); }; } // namespace libbig From f3ee8f1cfb3c7a467bde85b7f634bde258094f9e Mon Sep 17 00:00:00 2001 From: Vlad Pirlog Date: Sun, 4 Oct 2020 22:09:14 +0300 Subject: [PATCH 02/15] :sparkles: Add class constructors file --- src/bigint.cpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src/bigint.cpp diff --git a/src/bigint.cpp b/src/bigint.cpp new file mode 100644 index 0000000..b506da5 --- /dev/null +++ b/src/bigint.cpp @@ -0,0 +1,25 @@ +#include "bigint.hpp" +#include +#include + +namespace libbig +{ + largeInt::largeInt() : sign(true), number("0") {} + + largeInt::largeInt(int x) : sign(x >= 0), number(std::to_string(std::abs(x))) {} + + largeInt::largeInt(int64_t x) : sign(x >= 0), number(std::to_string(std::abs(x))) {} + + largeInt::largeInt(long long x) : sign(x >= 0), number(std::to_string(std::abs(x))) {} + + largeInt::largeInt(std::string x) + { + if (!std::regex_match(x, std::regex("(\\-)?(\\d)+"))) + throw(std::invalid_argument("Invalid syntax.")); + sign = x.at(0) != '-'; + if (x.at(0) == '-') + number = x.substr(1); + else + number = x; + } +} // namespace libbig From 2b692e2bb70fb311c5b270a026a8e1e6ea79cc6c Mon Sep 17 00:00:00 2001 From: Vlad Pirlog Date: Sun, 4 Oct 2020 22:11:07 +0300 Subject: [PATCH 03/15] :recycle: Refactor iostream operators overloading --- src/bigint.hpp | 2 +- src/operators/istream_operator.cpp | 14 +++++++++----- src/operators/ostream_operator.cpp | 13 ++++++++----- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/bigint.hpp b/src/bigint.hpp index e9582f2..cb615cb 100644 --- a/src/bigint.hpp +++ b/src/bigint.hpp @@ -135,7 +135,7 @@ namespace libbig bool operator>=(long long); friend std::istream &operator>>(std::istream &, largeInt &); - friend std::ostream &operator<<(std::ostream &, largeInt &); + friend std::ostream &operator<<(std::ostream &, const largeInt &); }; } // namespace libbig diff --git a/src/operators/istream_operator.cpp b/src/operators/istream_operator.cpp index 2dc6349..397b409 100644 --- a/src/operators/istream_operator.cpp +++ b/src/operators/istream_operator.cpp @@ -30,17 +30,21 @@ #include "../bigint.hpp" -namespace libbig { - std::istream& operator >> (std::istream& inp, largeInt& z) { - std::cin >> z.number; - if (z.number[0] == '-') { +namespace libbig +{ + std::istream &operator>>(std::istream &inp, largeInt &z) + { + inp >> z.number; + if (z.number[0] == '-') + { z.sign = false; std::string temporary = std::string(z.number.begin() + 1, z.number.end()); z.number.clear(); z.number = std::string(temporary.begin(), temporary.end()); temporary.clear(); } - else { + else + { z.sign = true; } return inp; diff --git a/src/operators/ostream_operator.cpp b/src/operators/ostream_operator.cpp index 863af69..5bdb6b3 100644 --- a/src/operators/ostream_operator.cpp +++ b/src/operators/ostream_operator.cpp @@ -30,12 +30,15 @@ #include "../bigint.hpp" -namespace libbig { - std::ostream& operator << (std::ostream& out, largeInt& z) { - if (!z.sign) { - std::cout << '-'; +namespace libbig +{ + std::ostream &operator<<(std::ostream &out, const largeInt &z) + { + if (!z.sign) + { + out << '-'; } - std::cout << z.number; + out << z.number; return out; } } // namespace libbig From 07290af4fecf7335be581535163806bed320f91e Mon Sep 17 00:00:00 2001 From: Vlad Pirlog Date: Sun, 4 Oct 2020 22:12:21 +0300 Subject: [PATCH 04/15] :sparkles: Overload assignment operator --- src/operators/assignment_operator.cpp | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/operators/assignment_operator.cpp b/src/operators/assignment_operator.cpp index f2a0542..8edf3ae 100644 --- a/src/operators/assignment_operator.cpp +++ b/src/operators/assignment_operator.cpp @@ -30,7 +30,29 @@ #include "../bigint.hpp" -namespace libbig { +namespace libbig +{ + void largeInt::operator=(largeInt x) + { + sign = x.sign; + number = x.number; + } -} // namespace libbig + void largeInt::operator=(int x) + { + sign = x >= 0; + number = std::to_string(std::abs(x)); + } + + void largeInt::operator=(int64_t x) + { + sign = x >= 0; + number = std::to_string(std::abs(x)); + } + void largeInt::operator=(long long x) + { + sign = x >= 0; + number = std::to_string(std::abs(x)); + } +} // namespace libbig From 0d42c7ba376ecce10d8d72a6e9003d9adeafbd27 Mon Sep 17 00:00:00 2001 From: Vlad Pirlog Date: Sun, 4 Oct 2020 22:13:44 +0300 Subject: [PATCH 05/15] :white_check_mark: Add tests for << operator --- tests/operators/ostream_test.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tests/operators/ostream_test.cpp b/tests/operators/ostream_test.cpp index e69de29..0db959e 100644 --- a/tests/operators/ostream_test.cpp +++ b/tests/operators/ostream_test.cpp @@ -0,0 +1,28 @@ +#include +#include +#include +#include + +#include "../../src/bigint.hpp" + +int main() +{ + libbig::largeInt a; + std::stringstream buffer; + std::ifstream f("./tests/operators/input.txt"); + std::string line; + + if (f.is_open()) + { + while (std::getline(f, line)) + { + a = libbig::largeInt(line); + buffer << a; + assert(buffer.str().compare(std::string(line)) == 0); + buffer.str(std::string()); + } + f.close(); + } + + return 0; +} From e625c249f81f03ff23d8a97541a0932ac100dcc5 Mon Sep 17 00:00:00 2001 From: Vlad Pirlog Date: Mon, 5 Oct 2020 17:58:56 +0300 Subject: [PATCH 06/15] :recycle: Refactor code and formatting --- big-int/src/bigint.cpp | 23 ++++++++++++++ big-int/src/bigint.hpp | 31 ++++++------------- big-int/src/operators/assignment_operator.cpp | 12 +++---- big-int/src/operators/ostream_operator.cpp | 15 ++++----- tests/operators/ostream_test.cpp | 25 +++++++++++++++ 5 files changed, 67 insertions(+), 39 deletions(-) create mode 100644 big-int/src/bigint.cpp create mode 100644 tests/operators/ostream_test.cpp diff --git a/big-int/src/bigint.cpp b/big-int/src/bigint.cpp new file mode 100644 index 0000000..5be692e --- /dev/null +++ b/big-int/src/bigint.cpp @@ -0,0 +1,23 @@ +#include "bigint.hpp" +#include +#include + +namespace libbig { + largeInt::largeInt() : sign(true), number("0") {} + + largeInt::largeInt(int x) : sign(x >= 0), number(std::to_string(std::abs(x))) {} + + largeInt::largeInt(int64_t x) : sign(x >= 0), number(std::to_string(std::abs(x))) {} + + largeInt::largeInt(long long x) : sign(x >= 0), number(std::to_string(std::abs(x))) {} + + largeInt::largeInt(std::string x) { + if (!std::regex_match(x, std::regex("(\\-)?(\\d)+"))) + throw(std::invalid_argument("Invalid syntax.")); + sign = x.at(0) != '-'; + if (x.at(0) == '-') + number = x.substr(1); + else + number = x; + } +} // namespace libbig diff --git a/big-int/src/bigint.hpp b/big-int/src/bigint.hpp index 9d8f030..e18bc9f 100644 --- a/big-int/src/bigint.hpp +++ b/big-int/src/bigint.hpp @@ -31,31 +31,18 @@ #include #include -namespace libbig -{ -class largeInt -{ +namespace libbig { +class largeInt { private: bool sign; std::string number; public: - largeInt() - { - } - - largeInt(int) - { - } - largeInt(int64_t) - { - } - largeInt(long long) - { - } - largeInt(std::string) - { - } + largeInt(); + largeInt(int); + largeInt(int64_t); + largeInt(long long); + largeInt(std::string); largeInt operator+(largeInt); largeInt operator+(int); @@ -111,7 +98,7 @@ class largeInt // POSTFIX OPERATORS - void operator=(largeInt); + void operator=(const largeInt &); void operator=(int); void operator=(int64_t); void operator=(long long); @@ -146,7 +133,7 @@ class largeInt bool operator>=(long long); friend std::istream &operator>>(std::istream &, largeInt &); - friend std::ostream &operator<<(std::ostream &, largeInt &); + friend std::ostream &operator<<(std::ostream &, const largeInt &); }; } // namespace libbig diff --git a/big-int/src/operators/assignment_operator.cpp b/big-int/src/operators/assignment_operator.cpp index affe3f6..60f9a57 100644 --- a/big-int/src/operators/assignment_operator.cpp +++ b/big-int/src/operators/assignment_operator.cpp @@ -32,26 +32,22 @@ namespace libbig { - void largeInt::operator=(largeInt x) - { + void largeInt::operator=(const largeInt &x) { sign = x.sign; number = x.number; } - void largeInt::operator=(int x) - { + void largeInt::operator=(int x) { sign = x >= 0; number = std::to_string(std::abs(x)); } - void largeInt::operator=(int64_t x) - { + void largeInt::operator=(int64_t x) { sign = x >= 0; number = std::to_string(std::abs(x)); } - void largeInt::operator=(long long x) - { + void largeInt::operator=(long long x) { sign = x >= 0; number = std::to_string(std::abs(x)); } diff --git a/big-int/src/operators/ostream_operator.cpp b/big-int/src/operators/ostream_operator.cpp index 627d734..5574d98 100644 --- a/big-int/src/operators/ostream_operator.cpp +++ b/big-int/src/operators/ostream_operator.cpp @@ -30,15 +30,12 @@ #include "../bigint.hpp" -namespace libbig -{ -std::ostream &operator<<(std::ostream &out, largeInt &z) -{ - if (!z.sign) - { - std::cout << '-'; +namespace libbig { + std::ostream &operator<<(std::ostream &out, const largeInt &z) { + if (!z.sign) { + out << '-'; } - std::cout << z.number; + out << z.number; return out; -} + } } // namespace libbig diff --git a/tests/operators/ostream_test.cpp b/tests/operators/ostream_test.cpp new file mode 100644 index 0000000..057e1da --- /dev/null +++ b/tests/operators/ostream_test.cpp @@ -0,0 +1,25 @@ +#include +#include +#include +#include + +#include "../big-int/src/bigint.hpp" + +int main() { + libbig::largeInt a; + std::stringstream buffer; + std::ifstream f("./tests/operators/input.txt"); + std::string line; + + if (f.is_open()) { + while (std::getline(f, line)) { + a = libbig::largeInt(line); + buffer << a; + assert(buffer.str().compare(std::string(line)) == 0); + buffer.str(std::string()); + } + f.close(); + } + + return 0; +} From bce397e67034eb248944ef4da833a05bd6cfa04c Mon Sep 17 00:00:00 2001 From: Clang Format Date: Mon, 5 Oct 2020 15:04:59 +0000 Subject: [PATCH 07/15] apply clang-format --- big-int/src/bigint.cpp | 30 ++++++++++------ big-int/src/bigint.hpp | 6 ++-- big-int/src/operators/assignment_operator.cpp | 20 ++++++----- big-int/src/operators/istream_operator.cpp | 16 ++++----- big-int/src/operators/ostream_operator.cpp | 13 ++++--- src/bigint.cpp | 28 +++++++++------ tests/operators/ostream_test.cpp | 35 ++++++++++--------- 7 files changed, 89 insertions(+), 59 deletions(-) diff --git a/big-int/src/bigint.cpp b/big-int/src/bigint.cpp index 5be692e..a3b0b25 100644 --- a/big-int/src/bigint.cpp +++ b/big-int/src/bigint.cpp @@ -2,22 +2,32 @@ #include #include -namespace libbig { - largeInt::largeInt() : sign(true), number("0") {} +namespace libbig +{ +largeInt::largeInt() : sign(true), number("0") +{ +} - largeInt::largeInt(int x) : sign(x >= 0), number(std::to_string(std::abs(x))) {} +largeInt::largeInt(int x) : sign(x >= 0), number(std::to_string(std::abs(x))) +{ +} - largeInt::largeInt(int64_t x) : sign(x >= 0), number(std::to_string(std::abs(x))) {} +largeInt::largeInt(int64_t x) : sign(x >= 0), number(std::to_string(std::abs(x))) +{ +} - largeInt::largeInt(long long x) : sign(x >= 0), number(std::to_string(std::abs(x))) {} +largeInt::largeInt(long long x) : sign(x >= 0), number(std::to_string(std::abs(x))) +{ +} - largeInt::largeInt(std::string x) { +largeInt::largeInt(std::string x) +{ if (!std::regex_match(x, std::regex("(\\-)?(\\d)+"))) - throw(std::invalid_argument("Invalid syntax.")); + throw(std::invalid_argument("Invalid syntax.")); sign = x.at(0) != '-'; if (x.at(0) == '-') - number = x.substr(1); + number = x.substr(1); else - number = x; - } + number = x; +} } // namespace libbig diff --git a/big-int/src/bigint.hpp b/big-int/src/bigint.hpp index e18bc9f..658978b 100644 --- a/big-int/src/bigint.hpp +++ b/big-int/src/bigint.hpp @@ -31,8 +31,10 @@ #include #include -namespace libbig { -class largeInt { +namespace libbig +{ +class largeInt +{ private: bool sign; std::string number; diff --git a/big-int/src/operators/assignment_operator.cpp b/big-int/src/operators/assignment_operator.cpp index 60f9a57..76e493d 100644 --- a/big-int/src/operators/assignment_operator.cpp +++ b/big-int/src/operators/assignment_operator.cpp @@ -32,23 +32,27 @@ namespace libbig { - void largeInt::operator=(const largeInt &x) { +void largeInt::operator=(const largeInt &x) +{ sign = x.sign; number = x.number; - } +} - void largeInt::operator=(int x) { +void largeInt::operator=(int x) +{ sign = x >= 0; number = std::to_string(std::abs(x)); - } +} - void largeInt::operator=(int64_t x) { +void largeInt::operator=(int64_t x) +{ sign = x >= 0; number = std::to_string(std::abs(x)); - } +} - void largeInt::operator=(long long x) { +void largeInt::operator=(long long x) +{ sign = x >= 0; number = std::to_string(std::abs(x)); - } +} } // namespace libbig diff --git a/big-int/src/operators/istream_operator.cpp b/big-int/src/operators/istream_operator.cpp index 4686590..8426c4c 100644 --- a/big-int/src/operators/istream_operator.cpp +++ b/big-int/src/operators/istream_operator.cpp @@ -32,20 +32,20 @@ namespace libbig { - std::istream &operator>>(std::istream &inp, largeInt &z) - { +std::istream &operator>>(std::istream &inp, largeInt &z) +{ inp >> z.number; if (z.number[0] == '-') { - z.sign = false; - std::string temporary = std::string(z.number.begin() + 1, z.number.end()); - z.number.clear(); - z.number = std::string(temporary.begin(), temporary.end()); - temporary.clear(); + z.sign = false; + std::string temporary = std::string(z.number.begin() + 1, z.number.end()); + z.number.clear(); + z.number = std::string(temporary.begin(), temporary.end()); + temporary.clear(); } else { - z.sign = true; + z.sign = true; } return inp; } diff --git a/big-int/src/operators/ostream_operator.cpp b/big-int/src/operators/ostream_operator.cpp index 5574d98..6ff7bf9 100644 --- a/big-int/src/operators/ostream_operator.cpp +++ b/big-int/src/operators/ostream_operator.cpp @@ -30,12 +30,15 @@ #include "../bigint.hpp" -namespace libbig { - std::ostream &operator<<(std::ostream &out, const largeInt &z) { - if (!z.sign) { - out << '-'; +namespace libbig +{ +std::ostream &operator<<(std::ostream &out, const largeInt &z) +{ + if (!z.sign) + { + out << '-'; } out << z.number; return out; - } +} } // namespace libbig diff --git a/src/bigint.cpp b/src/bigint.cpp index b506da5..a3b0b25 100644 --- a/src/bigint.cpp +++ b/src/bigint.cpp @@ -4,22 +4,30 @@ namespace libbig { - largeInt::largeInt() : sign(true), number("0") {} +largeInt::largeInt() : sign(true), number("0") +{ +} - largeInt::largeInt(int x) : sign(x >= 0), number(std::to_string(std::abs(x))) {} +largeInt::largeInt(int x) : sign(x >= 0), number(std::to_string(std::abs(x))) +{ +} - largeInt::largeInt(int64_t x) : sign(x >= 0), number(std::to_string(std::abs(x))) {} +largeInt::largeInt(int64_t x) : sign(x >= 0), number(std::to_string(std::abs(x))) +{ +} - largeInt::largeInt(long long x) : sign(x >= 0), number(std::to_string(std::abs(x))) {} +largeInt::largeInt(long long x) : sign(x >= 0), number(std::to_string(std::abs(x))) +{ +} - largeInt::largeInt(std::string x) - { +largeInt::largeInt(std::string x) +{ if (!std::regex_match(x, std::regex("(\\-)?(\\d)+"))) - throw(std::invalid_argument("Invalid syntax.")); + throw(std::invalid_argument("Invalid syntax.")); sign = x.at(0) != '-'; if (x.at(0) == '-') - number = x.substr(1); + number = x.substr(1); else - number = x; - } + number = x; +} } // namespace libbig diff --git a/tests/operators/ostream_test.cpp b/tests/operators/ostream_test.cpp index 057e1da..eb1e2e3 100644 --- a/tests/operators/ostream_test.cpp +++ b/tests/operators/ostream_test.cpp @@ -1,25 +1,28 @@ -#include #include -#include #include +#include +#include #include "../big-int/src/bigint.hpp" -int main() { - libbig::largeInt a; - std::stringstream buffer; - std::ifstream f("./tests/operators/input.txt"); - std::string line; +int main() +{ + libbig::largeInt a; + std::stringstream buffer; + std::ifstream f("./tests/operators/input.txt"); + std::string line; - if (f.is_open()) { - while (std::getline(f, line)) { - a = libbig::largeInt(line); - buffer << a; - assert(buffer.str().compare(std::string(line)) == 0); - buffer.str(std::string()); + if (f.is_open()) + { + while (std::getline(f, line)) + { + a = libbig::largeInt(line); + buffer << a; + assert(buffer.str().compare(std::string(line)) == 0); + buffer.str(std::string()); + } + f.close(); } - f.close(); - } - return 0; + return 0; } From 8de6eb6c6275225c0bc4f6df5ffba7317559eafb Mon Sep 17 00:00:00 2001 From: Vlad Pirlog Date: Mon, 5 Oct 2020 18:56:40 +0300 Subject: [PATCH 08/15] :ambulance: Fix codacy issues --- big-int/src/bigint.hpp | 16 ++++++++-------- big-int/src/operators/assignment_operator.cpp | 12 ++++++++---- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/big-int/src/bigint.hpp b/big-int/src/bigint.hpp index 658978b..76d1d8c 100644 --- a/big-int/src/bigint.hpp +++ b/big-int/src/bigint.hpp @@ -41,10 +41,10 @@ class largeInt public: largeInt(); - largeInt(int); - largeInt(int64_t); - largeInt(long long); - largeInt(std::string); + explicit largeInt(int); + explicit largeInt(int64_t); + explicit largeInt(long long); + explicit largeInt(std::string); largeInt operator+(largeInt); largeInt operator+(int); @@ -100,10 +100,10 @@ class largeInt // POSTFIX OPERATORS - void operator=(const largeInt &); - void operator=(int); - void operator=(int64_t); - void operator=(long long); + largeInt& operator=(const largeInt &); + largeInt& operator=(int); + largeInt& operator=(int64_t); + largeInt& operator=(long long); bool operator==(const largeInt &); bool operator==(int); diff --git a/big-int/src/operators/assignment_operator.cpp b/big-int/src/operators/assignment_operator.cpp index 76e493d..867861e 100644 --- a/big-int/src/operators/assignment_operator.cpp +++ b/big-int/src/operators/assignment_operator.cpp @@ -32,27 +32,31 @@ namespace libbig { -void largeInt::operator=(const largeInt &x) +largeInt& largeInt::operator=(const largeInt &x) { sign = x.sign; number = x.number; + return *this; } -void largeInt::operator=(int x) +largeInt& largeInt::operator=(int x) { sign = x >= 0; number = std::to_string(std::abs(x)); + return *this; } -void largeInt::operator=(int64_t x) +largeInt& largeInt::operator=(int64_t x) { sign = x >= 0; number = std::to_string(std::abs(x)); + return *this; } -void largeInt::operator=(long long x) +largeInt& largeInt::operator=(long long x) { sign = x >= 0; number = std::to_string(std::abs(x)); + return *this; } } // namespace libbig From b7c5390253ec82bfb61cd102bf13ed18a1ed8dfe Mon Sep 17 00:00:00 2001 From: Clang Format Date: Mon, 5 Oct 2020 16:01:13 +0000 Subject: [PATCH 09/15] apply clang-format --- big-int/src/bigint.hpp | 8 ++++---- big-int/src/operators/assignment_operator.cpp | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/big-int/src/bigint.hpp b/big-int/src/bigint.hpp index 76d1d8c..1b173aa 100644 --- a/big-int/src/bigint.hpp +++ b/big-int/src/bigint.hpp @@ -100,10 +100,10 @@ class largeInt // POSTFIX OPERATORS - largeInt& operator=(const largeInt &); - largeInt& operator=(int); - largeInt& operator=(int64_t); - largeInt& operator=(long long); + largeInt &operator=(const largeInt &); + largeInt &operator=(int); + largeInt &operator=(int64_t); + largeInt &operator=(long long); bool operator==(const largeInt &); bool operator==(int); diff --git a/big-int/src/operators/assignment_operator.cpp b/big-int/src/operators/assignment_operator.cpp index 867861e..08cb415 100644 --- a/big-int/src/operators/assignment_operator.cpp +++ b/big-int/src/operators/assignment_operator.cpp @@ -32,28 +32,28 @@ namespace libbig { -largeInt& largeInt::operator=(const largeInt &x) +largeInt &largeInt::operator=(const largeInt &x) { sign = x.sign; number = x.number; return *this; } -largeInt& largeInt::operator=(int x) +largeInt &largeInt::operator=(int x) { sign = x >= 0; number = std::to_string(std::abs(x)); return *this; } -largeInt& largeInt::operator=(int64_t x) +largeInt &largeInt::operator=(int64_t x) { sign = x >= 0; number = std::to_string(std::abs(x)); return *this; } -largeInt& largeInt::operator=(long long x) +largeInt &largeInt::operator=(long long x) { sign = x >= 0; number = std::to_string(std::abs(x)); From 533686afe69768ee6081aca71eefb38be4d4182b Mon Sep 17 00:00:00 2001 From: Vlad Pirlog Date: Mon, 5 Oct 2020 19:07:17 +0300 Subject: [PATCH 10/15] :fire: Remove extra cpp file, fix import --- src/bigint.cpp | 33 -------------------------------- tests/operators/ostream_test.cpp | 2 +- 2 files changed, 1 insertion(+), 34 deletions(-) delete mode 100644 src/bigint.cpp diff --git a/src/bigint.cpp b/src/bigint.cpp deleted file mode 100644 index a3b0b25..0000000 --- a/src/bigint.cpp +++ /dev/null @@ -1,33 +0,0 @@ -#include "bigint.hpp" -#include -#include - -namespace libbig -{ -largeInt::largeInt() : sign(true), number("0") -{ -} - -largeInt::largeInt(int x) : sign(x >= 0), number(std::to_string(std::abs(x))) -{ -} - -largeInt::largeInt(int64_t x) : sign(x >= 0), number(std::to_string(std::abs(x))) -{ -} - -largeInt::largeInt(long long x) : sign(x >= 0), number(std::to_string(std::abs(x))) -{ -} - -largeInt::largeInt(std::string x) -{ - if (!std::regex_match(x, std::regex("(\\-)?(\\d)+"))) - throw(std::invalid_argument("Invalid syntax.")); - sign = x.at(0) != '-'; - if (x.at(0) == '-') - number = x.substr(1); - else - number = x; -} -} // namespace libbig diff --git a/tests/operators/ostream_test.cpp b/tests/operators/ostream_test.cpp index eb1e2e3..234275c 100644 --- a/tests/operators/ostream_test.cpp +++ b/tests/operators/ostream_test.cpp @@ -3,7 +3,7 @@ #include #include -#include "../big-int/src/bigint.hpp" +#include "../../big-int/src/bigint.hpp" int main() { From 81707274c3a5a622839158923d12547c40ce3bb7 Mon Sep 17 00:00:00 2001 From: Ayaan Khan Date: Mon, 5 Oct 2020 22:29:57 +0530 Subject: [PATCH 11/15] fix: build issue --- big-int/CMakeLists.txt | 3 ++- tests/operators/ostream_test.cpp | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/big-int/CMakeLists.txt b/big-int/CMakeLists.txt index c96923c..4cda72a 100644 --- a/big-int/CMakeLists.txt +++ b/big-int/CMakeLists.txt @@ -1,7 +1,8 @@ add_library( big-int src/bigint.hpp - + src/bigint.cpp + ## operators src/operators/assignment_operator.cpp src/operators/divide_equal.cpp diff --git a/tests/operators/ostream_test.cpp b/tests/operators/ostream_test.cpp index 234275c..dd00362 100644 --- a/tests/operators/ostream_test.cpp +++ b/tests/operators/ostream_test.cpp @@ -3,7 +3,7 @@ #include #include -#include "../../big-int/src/bigint.hpp" +#include int main() { From de9628426d17db21e8d29e6e01c291e6e419db19 Mon Sep 17 00:00:00 2001 From: Vlad Pirlog Date: Tue, 6 Oct 2020 21:30:16 +0300 Subject: [PATCH 12/15] :white_check_mark: Add regex tests --- tests/CMakeLists.txt | 1 + tests/regex/CMakeLists.txt | 11 ++++++++++ tests/regex/regex_accepted_input.txt | 9 ++++++++ tests/regex/regex_rejected_input.txt | 8 +++++++ tests/regex/regex_test.cpp | 32 ++++++++++++++++++++++++++++ 5 files changed, 61 insertions(+) create mode 100644 tests/regex/CMakeLists.txt create mode 100644 tests/regex/regex_accepted_input.txt create mode 100644 tests/regex/regex_rejected_input.txt create mode 100644 tests/regex/regex_test.cpp diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 9f495cb..8fc8491 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,5 +1,6 @@ add_subdirectory(operators) add_subdirectory(operations) +add_subdirectory(regex) enable_testing() file( GLOB TEST_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp ) foreach(testsourcefile ${TEST_SOURCES}) diff --git a/tests/regex/CMakeLists.txt b/tests/regex/CMakeLists.txt new file mode 100644 index 0000000..1cd7207 --- /dev/null +++ b/tests/regex/CMakeLists.txt @@ -0,0 +1,11 @@ +enable_testing() +file( GLOB TEST_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp ) +foreach(testsourcefile ${TEST_SOURCES}) + string ( REPLACE ".cpp" "" testname ${testsourcefile} ) + add_executable( ${testname} ${testsourcefile} ) + target_link_libraries( ${testname} big-int ) + add_test(${testname} ${testname}) +endforeach() + +file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/regex_accepted_input.txt DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/regex_accepted_input.txt) +file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/regex_rejected_input.txt DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/regex_rejected_input.txt) diff --git a/tests/regex/regex_accepted_input.txt b/tests/regex/regex_accepted_input.txt new file mode 100644 index 0000000..04b50f7 --- /dev/null +++ b/tests/regex/regex_accepted_input.txt @@ -0,0 +1,9 @@ +0 +1 +-1 +-97946569 +27659857 +-436539467 +-43679760796362595017 +100000000000000000000000000000000000000000000000000000000000 +-100000000000000000000000000000000000000000000000000000000000 \ No newline at end of file diff --git a/tests/regex/regex_rejected_input.txt b/tests/regex/regex_rejected_input.txt new file mode 100644 index 0000000..3732713 --- /dev/null +++ b/tests/regex/regex_rejected_input.txt @@ -0,0 +1,8 @@ +--9 ++6 ++897497948 +98734h987ew +$^%%*&^(* +az928758 +1000000000000000000000000a0000000 +-1000000000000b000000000000000000 \ No newline at end of file diff --git a/tests/regex/regex_test.cpp b/tests/regex/regex_test.cpp new file mode 100644 index 0000000..aeda0d4 --- /dev/null +++ b/tests/regex/regex_test.cpp @@ -0,0 +1,32 @@ +#include +#include +#include +#include +#include +#include + +int main() { + std::regex regExpr("(\\-)?(\\d)+"); + std::string line; + + std::ifstream f_accepted("regex_accepted_input.txt"); + if (f_accepted.is_open()) { + while (std::getline(f_accepted, line)) { + assert(std::regex_match(line, regExpr) == true); + } + f_accepted.close(); + } else { + throw std::runtime_error("Couldn't open regex_accepted_input.txt"); + } + + std::ifstream f_rejected("regex_rejected_input.txt"); + if (f_rejected.is_open()) { + while (std::getline(f_rejected, line)) { + assert(std::regex_match(line, regExpr) == false); + } + f_rejected.close(); + } else { + throw std::runtime_error("Couldn't open regex_rejected_input.txt"); + } + return 0; +} From 1a92018118a48f8c1d371995591fb4472741f74c Mon Sep 17 00:00:00 2001 From: Vlad Pirlog Date: Tue, 6 Oct 2020 21:32:37 +0300 Subject: [PATCH 13/15] :white_check_mark: Fix ostream tests --- tests/operators/CMakeLists.txt | 2 ++ tests/operators/input.txt | 14 ++++++++++++++ tests/operators/ostream_test.cpp | 4 +++- 3 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 tests/operators/input.txt diff --git a/tests/operators/CMakeLists.txt b/tests/operators/CMakeLists.txt index c1ee5d9..9ab7ea8 100644 --- a/tests/operators/CMakeLists.txt +++ b/tests/operators/CMakeLists.txt @@ -6,3 +6,5 @@ foreach(testsourcefile ${TEST_SOURCES}) target_link_libraries( ${testname} big-int ) add_test(${testname} ${testname}) endforeach() + +file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/input.txt DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/input.txt) \ No newline at end of file diff --git a/tests/operators/input.txt b/tests/operators/input.txt new file mode 100644 index 0000000..2bcfa4b --- /dev/null +++ b/tests/operators/input.txt @@ -0,0 +1,14 @@ +1 +100 +10000 +100000000 +10000000000000000 +100000000000000000000000000000000 +100000000000000000000000000000000000000000000000000000000000000000 +99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999 +1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +-10000000000000000000000000 +-1122178617862763873649236587264782365872467823 +-149023932491 +-34873298478923748973289476592365782689523657926958326459823659726598237503297598246583657924628934928357891 +-26589237985678256786763278562876389472836578264892365427961 \ No newline at end of file diff --git a/tests/operators/ostream_test.cpp b/tests/operators/ostream_test.cpp index dd00362..8e22244 100644 --- a/tests/operators/ostream_test.cpp +++ b/tests/operators/ostream_test.cpp @@ -9,7 +9,7 @@ int main() { libbig::largeInt a; std::stringstream buffer; - std::ifstream f("./tests/operators/input.txt"); + std::ifstream f("input.txt"); std::string line; if (f.is_open()) @@ -22,6 +22,8 @@ int main() buffer.str(std::string()); } f.close(); + } else { + throw std::runtime_error("Couldn't open input.txt"); } return 0; From ff645bbf9ef0eb5feebb332f4500d9316a787aad Mon Sep 17 00:00:00 2001 From: Vlad Pirlog Date: Tue, 6 Oct 2020 22:05:07 +0300 Subject: [PATCH 14/15] :white_check_mark: Add tests for assignment operator --- big-int/src/operators/assignment_operator.cpp | 9 +++- tests/operators/assignment_operator_test.cpp | 49 +++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 tests/operators/assignment_operator_test.cpp diff --git a/big-int/src/operators/assignment_operator.cpp b/big-int/src/operators/assignment_operator.cpp index 30a4e53..9f64f77 100644 --- a/big-int/src/operators/assignment_operator.cpp +++ b/big-int/src/operators/assignment_operator.cpp @@ -28,7 +28,7 @@ #include #include -#include "../bigint.hpp" +#include namespace libbig { @@ -46,4 +46,11 @@ largeInt &largeInt::operator=(int x) return *this; } +largeInt &largeInt::operator=(int64_t x) +{ + sign = x >= 0; + number = std::to_string(std::abs(x)); + return *this; +} + } // namespace libbig diff --git a/tests/operators/assignment_operator_test.cpp b/tests/operators/assignment_operator_test.cpp new file mode 100644 index 0000000..99e7dfb --- /dev/null +++ b/tests/operators/assignment_operator_test.cpp @@ -0,0 +1,49 @@ +/** + * Boost Software License - Version 1.0 - August 17th, 2003 + * Permission is hereby granted, free of charge, to any person + * or organization obtaining a copy of the software and + * accompanying documentation covered by this license + * (the "Software") to use, reproduce, display, distribute, + * execute, and transmit the Software, and to prepare derivative + * works of the Software, and to permit third-parties to whom the + * Software is furnished to do so, all subject to the following: + * + * The copyright notices in the Software and this entire statement, + * including the above license grant, this restriction and the following + * disclaimer, must be included in all copies of the Software, in whole or + * in part, and all derivative works of the Software, unless such copies + * or derivative works are solely in the form of machine-executable + * object code generated by a source language processor. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND + * NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR ANYONE + * DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR OTHER LIABILITY, + * WHETHER IN CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + */ + +#include +#include + +int main() +{ + libbig::largeInt a(20); + libbig::largeInt b = a; + assert(b == 20); + assert(b == a); + + b = libbig::largeInt(-100000); + a = b; + assert(a == -100000); + assert(a == b); + + b = libbig::largeInt("1000000000"); + a = b; + assert(a == 1000000000); + assert(a == b); + + return 0; +} From 17479af64927160780d10b8717f492d6c6947e91 Mon Sep 17 00:00:00 2001 From: Vlad Pirlog Date: Tue, 6 Oct 2020 22:06:11 +0300 Subject: [PATCH 15/15] :ambulance: Fix cmake subdirs --- tests/CMakeLists.txt | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 9d90f1e..2858ad9 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,6 +1,8 @@ add_subdirectory(operators) add_subdirectory(operations) add_subdirectory(regex) +add_subdirectory(big-int) + enable_testing() file( GLOB TEST_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp ) foreach(testsourcefile ${TEST_SOURCES}) @@ -8,7 +10,3 @@ foreach(testsourcefile ${TEST_SOURCES}) add_executable( ${testname} ${testsourcefile} ) target_link_libraries( ${testname} big-int ) endforeach() - -add_subdirectory(operators) -add_subdirectory(operations) -add_subdirectory(big-int) \ No newline at end of file