Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[test & fix] ostream, assignment operator #15

Merged
merged 19 commits into from
Oct 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion big-int/src/bigint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,4 @@ largeInt::largeInt(std::string nValue)
}
this->number = nValue;
}
} // namespace libbig
} // namespace libbig
8 changes: 4 additions & 4 deletions big-int/src/bigint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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

Expand Down
24 changes: 22 additions & 2 deletions big-int/src/operators/assignment_operator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,29 @@
#include <istream>
#include <ostream>

#include "../bigint.hpp"
#include <bigint.hpp>

namespace libbig
{
largeInt &largeInt::operator=(const largeInt &x)
ayaankhan98 marked this conversation as resolved.
Show resolved Hide resolved
{
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
} // namespace libbig
2 changes: 1 addition & 1 deletion big-int/src/operators/ostream_operator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 5 additions & 4 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
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})
string ( REPLACE ".cpp" "" testname ${testsourcefile} )
add_executable( ${testname} ${testsourcefile} )
target_link_libraries( ${testname} big-int )
endforeach()

add_subdirectory(operators)
add_subdirectory(operations)
add_subdirectory(big-int)
2 changes: 2 additions & 0 deletions tests/operators/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
49 changes: 49 additions & 0 deletions tests/operators/assignment_operator_test.cpp
Original file line number Diff line number Diff line change
@@ -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 <bigint.hpp>
#include <cassert>

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;
}
14 changes: 14 additions & 0 deletions tests/operators/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
1
100
10000
100000000
10000000000000000
100000000000000000000000000000000
100000000000000000000000000000000000000000000000000000000000000000
99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
-10000000000000000000000000
-1122178617862763873649236587264782365872467823
-149023932491
-34873298478923748973289476592365782689523657926958326459823659726598237503297598246583657924628934928357891
-26589237985678256786763278562876389472836578264892365427961
30 changes: 30 additions & 0 deletions tests/operators/ostream_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <cassert>
#include <fstream>
#include <iostream>
#include <sstream>

#include <bigint.hpp>

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;
}
11 changes: 11 additions & 0 deletions tests/regex/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
9 changes: 9 additions & 0 deletions tests/regex/regex_accepted_input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
0
1
-1
-97946569
27659857
-436539467
-43679760796362595017
100000000000000000000000000000000000000000000000000000000000
-100000000000000000000000000000000000000000000000000000000000
8 changes: 8 additions & 0 deletions tests/regex/regex_rejected_input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
--9
+6
+897497948
98734h987ew
$^%%*&^(*
az928758
1000000000000000000000000a0000000
-1000000000000b000000000000000000
32 changes: 32 additions & 0 deletions tests/regex/regex_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <cassert>
#include <iostream>
#include <regex>
#include <string>
#include <fstream>
#include <exception>

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;
}