Created
December 9, 2012 14:19
-
-
Save Hydrotoast/4245142 to your computer and use it in GitHub Desktop.
Simple C++ Unit Tester
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "SortedListTests.h" | |
int main(int argc, char* argv[]) { | |
SortedListTests tests; | |
tests.runTests(); | |
system("pause"); | |
return EXIT_SUCCESS; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sorted List Tests Running | |
========================= | |
canAdd SUCCESS | |
canIterate SUCCESS | |
canClear SUCCESS | |
canPrintReverse SUCCESS | |
canPrintStrings SUCCESS | |
canCopy SUCCESS | |
canAssign SUCCESS |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Gio Borje | |
/// 41894135 | |
#include "SortedListTests.h" | |
#include "AssertFailureException.h" | |
#include <functional> | |
using namespace std; | |
void SortedListTests::runTests() { | |
cout << "Sorted List Tests Running" << endl | |
<< "=========================" << endl << endl; | |
runTest(bind(mem_fn(&SortedListTests::canAdd), this), "canAdd"); | |
runTest(bind(mem_fn(&SortedListTests::canIterate), this), "canIterate"); | |
runTest(bind(mem_fn(&SortedListTests::canClear), this), "canClear"); | |
runTest(bind(mem_fn(&SortedListTests::canPrintReverse), this), "canPrintReverse"); | |
runTest(bind(mem_fn(&SortedListTests::canPrintStrings), this), "canPrintStrings"); | |
runTest(bind(mem_fn(&SortedListTests::canCopy), this), "canCopy"); | |
runTest(bind(mem_fn(&SortedListTests::canAssign), this), "canAssign"); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Gio Borje | |
/// 41894135 | |
#ifndef SORTED_LIST_TESTS_H | |
#define SORTED_LIST_TESTS_H | |
#include "TestRunner.h" | |
#include "sorted_list.h" | |
/// Unit tests for the sorted_list | |
class SortedListTests : protected TestRunner { | |
public: | |
void canAdd(); | |
void canIterate(); | |
void canClear(); | |
void canPrintReverse(); | |
void canPrintStrings(); | |
void canCopy(); | |
void canAssign(); | |
virtual void runTests(); | |
}; | |
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Gio Borje | |
/// 41894135 | |
#include "TestRunner.h" | |
#include <iostream> | |
#include <iomanip> | |
using namespace std; | |
void TestRunner::assertTrue(bool expression) { | |
if (!expression) | |
throw AssertFailureException(); | |
} | |
void TestRunner::assertFalse(bool expression) { | |
if (expression) | |
throw AssertFailureException(); | |
} | |
void TestRunner::fail() { | |
throw AssertFailureException(); | |
} | |
template <> | |
void TestRunner::assertEquals(const std::string& op1, const std::string& op2) { | |
if (op1.compare(op2) != 0) | |
throw AssertFailureException(); | |
} | |
template <> | |
void TestRunner::assertArrayEquals(const std::string op1[], const std::string op2[], size_t size) { | |
for (size_t i = 0; i < size; ++i) | |
if (op1[i] != op2[i]) | |
throw AssertFailureException(); | |
} | |
void TestRunner::runTest(function<void()> test, string testName) { | |
try { | |
test(); | |
cout << setw(20) << testName; | |
cout << setw(10) << "SUCCESS" << endl; | |
} catch (const AssertFailureException&) { | |
cout << setw(10) << "FAILURE" << endl; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Gio Borje | |
/// 41894135 | |
#ifndef TEST_RUNNER_H | |
#define TEST_RUNNER_H | |
#include "AssertFailureException.h" | |
#include <functional> | |
#include <string> | |
/// Foundation for a unit test | |
class TestRunner { | |
protected: | |
void assertTrue(bool expression); | |
void assertFalse(bool expression); | |
void fail(); | |
template <typename T> | |
void assertNullptr(const T* pointer); | |
template <typename T> | |
void assertEquals(const T& op1, const T& op2); | |
template <typename T> | |
void assertArrayEquals(const T op1[], const T op2[], size_t size); | |
void runTest(std::function<void()> test, std::string testName); | |
virtual void runTests() = 0; | |
}; | |
template <typename T> | |
void TestRunner::assertNullptr(const T* pointer) { | |
if (pointer != nullptr) | |
return AssertFailureException(); | |
} | |
template <> | |
void TestRunner::assertEquals(const std::string& op1, const std::string& op2); | |
template <typename T> | |
void TestRunner::assertEquals(const T& op1, const T& op2) { | |
if (op1 != op2) | |
throw AssertFailureException(); | |
} | |
template <> | |
void TestRunner::assertArrayEquals(const std::string op1[], const std::string op2[], size_t size); | |
template <typename T> | |
void TestRunner::assertArrayEquals(const T op1[], const T op2[], size_t size) { | |
for (size_t i = 0; i < size; ++i) | |
if (op1[i] != op2[i]) | |
throw AssertFailureException(); | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment