diff --git a/big-int/src/bigint.cpp b/big-int/src/bigint.cpp index b16ca20..bf6456b 100644 --- a/big-int/src/bigint.cpp +++ b/big-int/src/bigint.cpp @@ -80,4 +80,4 @@ largeInt::largeInt(std::string nValue) } this->number = nValue; } -} // namespace libbig \ No newline at end of file +} // namespace libbig diff --git a/big-int/src/bigint.hpp b/big-int/src/bigint.hpp index d2988a8..a9cb938 100644 --- a/big-int/src/bigint.hpp +++ b/big-int/src/bigint.hpp @@ -94,9 +94,9 @@ class largeInt // POSTFIX OPERATORS - void operator=(largeInt); - void operator=(int); - void operator=(int64_t); + largeInt &operator=(const largeInt &); + largeInt &operator=(int); + largeInt &operator=(int64_t); bool operator==(const largeInt &); bool operator==(int); @@ -123,7 +123,7 @@ class largeInt bool operator>=(int64_t); 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 f5c6018..9f64f77 100644 --- a/big-int/src/operators/assignment_operator.cpp +++ b/big-int/src/operators/assignment_operator.cpp @@ -28,9 +28,29 @@ #include #include -#include "../bigint.hpp" +#include namespace libbig { +largeInt &largeInt::operator=(const largeInt &x) +{ + sign = x.sign; + number = x.number; + return *this; +} + +largeInt &largeInt::operator=(int x) +{ + sign = x >= 0; + number = std::to_string(std::abs(x)); + return *this; +} + +largeInt &largeInt::operator=(int64_t x) +{ + sign = x >= 0; + number = std::to_string(std::abs(x)); + return *this; +} -} // namespace libbig \ No newline at end of file +} // namespace libbig diff --git a/big-int/src/operators/ostream_operator.cpp b/big-int/src/operators/ostream_operator.cpp index 4412449..3d6db5e 100644 --- a/big-int/src/operators/ostream_operator.cpp +++ b/big-int/src/operators/ostream_operator.cpp @@ -32,7 +32,7 @@ namespace libbig { -std::ostream &operator<<(std::ostream &out, largeInt &z) +std::ostream &operator<<(std::ostream &out, const largeInt &z) { // checks if this->sign is negative, then inserts number in ostream if (z.sign == NEGATIVE) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 6656470..2858ad9 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,3 +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}) @@ -5,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 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/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; +} 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 new file mode 100644 index 0000000..8e22244 --- /dev/null +++ b/tests/operators/ostream_test.cpp @@ -0,0 +1,30 @@ +#include +#include +#include +#include + +#include + +int main() +{ + libbig::largeInt a; + std::stringstream buffer; + std::ifstream f("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(); + } else { + throw std::runtime_error("Couldn't open input.txt"); + } + + return 0; +} 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; +}