Skip to content

Instantly share code, notes, and snippets.

@itotallyrock
Created March 13, 2018 02:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save itotallyrock/b5fb720c3ad91512cb4f766f093bab9c to your computer and use it in GitHub Desktop.
Save itotallyrock/b5fb720c3ad91512cb4f766f093bab9c to your computer and use it in GitHub Desktop.
CC=g++
CFLAGS=-c -g -Wall --std=c++11
LDFLAGS=-g
# All of the source files to compile
SOURCES=pa05.cpp BigInteger.cpp
DEPENDENCIES=BigInteger.h MyList.h MyList.hpp
OBJECTS=$(SOURCES:.cpp=.o)
# Output binary
EXECUTABLE=pa05
.PHONY: build clean test difftest difftestfailed
# The main make to run
build: $(EXECUTABLE)
# Compile each file individually
$(EXECUTABLE): $(OBJECTS)
$(CC) $(LDFLAGS) $(OBJECTS) -o $@
# Join all object files
%.o: %.cpp $(DEPENDENCIES)
$(CC) $(CFLAGS) $< -o $@
clean:
rm $(OBJECTS) $(EXECUTABLE)
TEST_INPUT=myInput.txt
TEST_OUTPUT=myOutput.txt
DIFF=/usr/bin/diff
test: difftest $(EXECUTABLE)
difftest: $(TEST_INPUT) $(TEST_OUTPUT) $(EXECUTABLE)
cat $(TEST_INPUT) | ./$(EXECUTABLE) | $(DIFF) -q $(TEST_OUTPUT) - || make difftestfailed
$(info Outputs correctly matched)
difftestfailed:
$(error Sample input didn\'t give expect output)
/* Sample tester
* We've included std::list below to show you how your types should behave.
* You can use that for debugging.
* As in the assignment description, if you use the std::list for your submitted code,
* you will get a 0.
* Compile with g++ pa05.cpp BigInteger.cpp -std=c++11
* Add the -g flag if you want to use GDB like we learned in lab this week.
*/
// USE THIS TO TEST, BUT REMOVE LATER
#include <list>
#include <iterator>
#include <iostream>
#include <string>
#include "BigInteger.h"
#include "MyList.h"
// #define USING_STL 1
using std::cout;
using std::cerr;
using std::endl;
void Assert(bool success, std::string error) {
if (!success) throw error;
}
int main() {
const std::string COLOR_RED = "\033[1;31m";
const std::string COLOR_GREEN = "\033[1;32m";
const std::string COLOR_RESET = "\033[0m";
const std::string SEPARATOR = ": ";
const std::string PASS = COLOR_GREEN + "\xE2\x9C\x94" + COLOR_RESET;
const std::string FAIL = COLOR_RED + "\u2718" + COLOR_RESET;
cout << "Testing using: ";
#ifdef USING_STL
cout << "std::list";
#else
cout << "MyList";
#endif
cout << endl;
#ifndef USING_STL
MyList<int> l;
#else
std::list<int> l;
#endif
try {
cout << "default works" << SEPARATOR;
Assert(l.size() == 0, "l.size() != 0");
Assert(l.empty(), "l.empty() != true");
#ifndef USING_STL
Assert(l.end() == 0, "l.end() != 0");
// Test l.begin once because it will always be 0
Assert(l.begin() == 0, "l.begin() != 0");
#endif
cout << PASS << endl;
l.push_back(4000);
cout << "push_back works" << SEPARATOR;
Assert(l.size() == 1, "l.size() != 1");
Assert(!l.empty(), "l.empty() != false");
Assert(l.back() == 4000, "l.back() != 4000");
Assert(l.front() == 4000, "l.front() != 4000");
#ifndef USING_STL
Assert(l.end() == 1, "l.end() != 1");
#endif
cout << PASS << endl;
l.push_back(200);
l.push_back(100);
cout << "multiple push_back works" << SEPARATOR;
Assert(l.size() == 3, "l.size() != 3");
Assert(!l.empty(), "l.empty() != false");
Assert(l.front() == 4000, "l.front() != 4000");
Assert(l.back() == 100, "l.back() != 100");
#ifndef USING_STL
Assert(l.end() == 3, "l.end() != 3");
#endif
cout << PASS << endl;
l.push_front(423);
cout << "push_front works" << SEPARATOR;
Assert(l.size() == 4, "l.size() != 4");
Assert(!l.empty(), "l.empty() != false");
Assert(l.front() == 423, "l.front() != 423");
Assert(l.back() == 100, "l.back() != 100");
#ifndef USING_STL
Assert(l.end() == 4, "l.end() != 4");
#endif
cout << PASS << endl;
l.pop_back();
cout << "pop_back works" << SEPARATOR;
Assert(l.size() == 3, "l.size() != 3");
Assert(!l.empty(), "l.empty() != false");
Assert(l.front() == 423, "l.front() != 423");
Assert(l.back() == 200, "l.back() != 200");
#ifndef USING_STL
Assert(l.end() == 3, "l.end() != 3");
#endif
cout << PASS << endl;
l.pop_front();
cout << "pop_front works" << SEPARATOR;
Assert(l.size() == 2, "l.size() != 2");
Assert(!l.empty(), "l.empty() != false");
Assert(l.front() == 4000, "l.front() != 4000");
Assert(l.back() == 200, "l.back() != 200");
#ifndef USING_STL
Assert(l.end() == 2, "l.end() != 2");
#endif
cout << PASS << endl;
l.resize(10);
cout << "resize larger works" << SEPARATOR;
Assert(l.size() == 10, "l.size() != 10");
Assert(!l.empty(), "l.empty() != false");
Assert(l.front() == 4000, "l.front() != 4000");
Assert(l.back() == 0, "l.back() != 0");
#ifndef USING_STL
Assert(l.end() == 10, "l.end() != 10");
#endif
cout << PASS << endl;
l.resize(1);
cout << "resize smaller works" << SEPARATOR;
Assert(l.size() == 1, "l.size() != 1");
Assert(!l.empty(), "l.empty() != false");
Assert(l.front() == 4000, "l.front() != 4000");
Assert(l.back() == 4000, "l.back() != 4000");
#ifndef USING_STL
Assert(l.end() == 1, "l.end() != 3");
#endif
cout << PASS << endl;
l.erase(l.begin());
cout << "erase works" << SEPARATOR;
Assert(l.size() == 0, "l.size() != 0");
Assert(l.empty(), "l.empty() != true");
#ifndef USING_STL
Assert(l.end() == 0, "l.end() != 0");
#endif
cout << PASS << endl;
l.assign(100, 420);
cout << "assign works" << SEPARATOR;
Assert(l.size() == 100, "l.size() != 100");
Assert(!l.empty(), "l.empty() != false");
Assert(l.front() == 420, "l.front() != 420");
Assert(l.back() == 420, "l.back() != 420");
#ifndef USING_STL
Assert(l.end() == 100, "l.end() != 100");
#endif
cout << PASS << endl;
l.clear();
cout << "clear works" << SEPARATOR;
Assert(l.size() == 0, "l.size() != 0");
Assert(l.empty(), "l.empty() != true");
#ifndef USING_STL
Assert(l.end() == 0, "l.end() != 0");
#endif
cout << PASS << endl;
l.insert(l.begin(), 666);
cout << "insert empty works" << SEPARATOR;
Assert(l.size() == 1, "l.size() != 1");
Assert(!l.empty(), "l.empty() != false");
Assert(l.front() == 666, "l.front() != 666");
Assert(l.back() == 666, "l.back() != 666");
#ifndef USING_STL
Assert(l.end() == 1, "l.end() != 1");
#endif
cout << PASS << endl;
l.clear();
l.insert(l.end(), 222);
cout << "insert empty end works" << SEPARATOR;
Assert(l.size() == 1, "l.size() != 1");
Assert(!l.empty(), "l.empty() != false");
Assert(l.front() == 222, "l.front() != 222");
Assert(l.back() == 222, "l.back() != 222");
#ifndef USING_STL
Assert(l.end() == 1, "l.end() != 1");
#endif
cout << PASS << endl;
l.remove(222);
cout << "remove works" << SEPARATOR;
Assert(l.size() == 0, "l.size() != 0");
Assert(l.empty(), "l.empty() != true");
#ifndef USING_STL
Assert(l.end() == 0, "l.end() != 0");
#endif
cout << PASS << endl;
l.assign(20, 99);
l.remove(99);
cout << "remove multiple works" << SEPARATOR;
Assert(l.size() == 0, "l.size() != 0");
Assert(l.empty(), "l.empty() != true");
#ifndef USING_STL
Assert(l.end() == 0, "l.end() != 0");
#endif
cout << PASS << endl;
l.assign(12, 69);
auto t = l.begin();
#ifdef USING_STL
std::advance(t, 7);
#else
t += 6;
#endif
l.insert(t, 42);
cout << "insert random works" << SEPARATOR;
Assert(l.size() == 13, "l.size() != 13");
Assert(!l.empty(), "l.empty() != false");
Assert(l.front() == 69, "l.front() != 69");
Assert(l.back() == 69, "l.back() != 69");
#ifndef USING_STL
Assert(l.end() == 13, "l.end() != 13");
for (size_t i = 0; i < (size_t)t + 1; i++) {
l.pop_front();
}
Assert(l.front() == 42, "l[6] != 42");
#else
// Decrease one since we added an element
std::advance(t, -1);
Assert(*(t) == 42, "l[6] != 42");
#endif
cout << PASS << endl;
// Add more items to make up for smaller list
l.push_back(55);
l.push_front(72);
#ifndef USING_STL
MyList<int> sink = l;
MyList<int> knis;
#else
std::list<int> sink = l;
std::list<int> knis;
#endif
knis = l;
cout << "copy constructor works" << SEPARATOR;
Assert(sink.size() == l.size(), "sink.size() != l.size()");
Assert(sink.empty() == l.empty(), "sink.empty() != l.empty()");
Assert(sink.front() == l.front(), "sink.front() != l.front()");
Assert(sink.back() == l.back(), "sink.back() != l.back()");
#ifndef USING_STL
Assert(sink.begin() == l.begin(), "sink.begin() != l.begin()");
Assert(sink.end() == l.end(), "sink.end() != l.end()");
#endif
cout << PASS << endl;
cout << "assignment operator works" << SEPARATOR;
Assert(knis.size() == l.size(), "knis.size() != l.size()");
Assert(knis.empty() == l.empty(), "knis.empty() != l.empty()");
Assert(knis.front() == l.front(), "knis.front() != l.front()");
Assert(knis.back() == l.back(), "knis.back() != l.back()");
#ifndef USING_STL
Assert(knis.begin() == l.begin(), "knis.begin() != l.begin()");
Assert(knis.end() == l.end(), "knis.end() != l.end()");
#endif
cout << PASS << endl;
l.pop_back();
cout << "copy constructor not linked" << SEPARATOR;
Assert(sink.size() == l.size() + 1, "sink.size() != l.size()");
cout << PASS << endl;
cout << "assignment operator not linked" << SEPARATOR;
Assert(knis.size() == l.size() + 1, "knis.size() != l.size()");
cout << PASS << endl;
knis.reverse();
cout << "reverse works" << SEPARATOR;
Assert(knis.size() == sink.size(), "knis.size() != sink.size()");
Assert(knis.front() == sink.back(), "knis.front() != sink.back()");
Assert(knis.back() == sink.front(), "knis.back() != sink.front()");
#ifndef USING_STL
Assert(knis.begin() == sink.begin(), "knis.begin() != sink.begin()");
Assert(knis.end() == sink.end(), "knis.end() != sink.end()");
#endif
cout << PASS << endl;
} catch (std::string err) {
cerr << COLOR_RED << FAIL << " " << COLOR_RESET << "(" << err << ")" << endl;
return 1;
}
// Test all your BigInteger functions here:
/*
std::string init_string("2342323492423");
BigInteger my_big_int(init_string);
*/
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment