Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#include "../unit-test-framework/unit_test_framework.h"
#include "../dlist.h"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
/********************************************************
Public methods
*********************************************************/
void testEmpty(const Dlist<int> &list)
{
test_description("Tests if a Dlist is empty");
if( list.isEmpty() )
test_passed("Dlist is empty!");
else
test_failed("Dlist is not empty.");
}
void testFront(Dlist<int> &list, const int element)
{
test_description("Tests if a Dlist's front manipulation techniques work");
int *a = list.removeFront();
if( assertEqual(*a, element) )
test_passed("insertFront and removeFront work!");
else
test_failed("insertFront and removeFront do not work.");
}
void testBack(Dlist<int> &list, const int element)
{
test_description("Tests if a Dlist's back manipulation techniques work");
int *b = list.removeBack();
if( assertEqual(*b, element) )
test_passed("insertBack and removeBack work!");
else
test_failed("insertBack and removeBack do not work.");
}
int getRandInRange(const int n)
{
/* initialize random seed: */
srand ( time(NULL) );
// random int from 0 to n
return ( rand() % n + 1 );
}
void testFrontRemoval()
{
test_description("Tests removal from front");
Dlist<int> list;
int *a = new int(getRandInRange(1000));
int *b = new int(getRandInRange(1000));
int *c = new int(getRandInRange(1000));
list.insertFront(a);
list.insertFront(b);
list.insertFront(c);
bool first, second, third;
first = (list.removeFront() == c);
second = (list.removeFront() == b);
third = (list.removeFront() == a);
if (first && second && third)
test_passed("all inserted and removed correctly, removal from front works!");
else
test_failed("removal from back failed.");
}
void testBackRemoval()
{
test_description("Tests removal from back");
Dlist<int> list;
int *a = new int(getRandInRange(1000));
int *b = new int(getRandInRange(1000));
int *c = new int(getRandInRange(1000));
list.insertFront(a);
list.insertFront(b);
list.insertFront(c);
bool first, second, third;
first = (list.removeBack() == a);
second = (list.removeBack() == b);
third = (list.removeBack() == c);
if (first && second && third)
test_passed("all inserted and removed correctly, removal from back works!");
else
test_failed("removal from back failed.");
}
void testIsEmpty()
{
test_description("Tests isEmpty");
Dlist<int> list;
int times = getRandInRange(10), count = 0;
for (int i = 0; i < times; ++i)
list.insertFront( (new int(getRandInRange(1000))) );
while( !list.isEmpty() )
{
list.removeBack();
count++;
}
if (count == times)
test_passed("isEmpty works, insertion and removal works!");
else
test_failed("removal from back failed.");
}
void testAssignmentCopy()
{
test_description("Tests assignment copy");
int times = getRandInRange(10), count = 0, second = 0;
Dlist<int> list;
for (int i = 0; i < times; ++i)
list.insertFront( (new int(getRandInRange(1000))) );
Dlist<int> copy = list;
while( !copy.isEmpty() )
{
copy.removeBack();
count++;
}
while( !list.isEmpty() )
{
list.removeBack();
second++;
}
if ( (times == count) && (count == second) )
test_passed("Assignment copy works!");
else
test_failed("Assignment copy failed.");
}
void testHeapCopy()
{
test_description("Tests heap copy");
int times = getRandInRange(10), count = 0, second = 0;
Dlist<int> list;
for (int i = 0; i < times; ++i)
list.insertFront( (new int(getRandInRange(1000))) );
Dlist<int>* copy = new Dlist<int>(list);
while( !copy->isEmpty() )
{
copy->removeBack();
count++;
}
while( !list.isEmpty() )
{
list.removeBack();
second++;
}
if ( (times == count) && (count == second) )
test_passed("Heap copy works!");
else
test_failed("Heap copy failed.");
}
void testAssignmentEmptyCopy()
{
test_description("Tests empty assignment copy");
int times = 0, count = 0, second = 0;
Dlist<int> list;
Dlist<int> copy = list;
while( !copy.isEmpty() )
{
copy.removeBack();
count++;
}
while( !list.isEmpty() )
{
list.removeBack();
second++;
}
if ( (times == count) && (count == second) )
test_passed("Empty assignment copy works!");
else
test_failed("Empty assignment copy failed.");
}
void testHeapEmptyCopy()
{
test_description("Tests empty heap copy");
int times = 0, count = 0, second = 0;
Dlist<int> list;
Dlist<int>* copy = new Dlist<int>(list);
while( !copy->isEmpty() )
{
copy->removeBack();
count++;
}
while( !list.isEmpty() )
{
list.removeBack();
second++;
}
if ( (times == count) && (count == second) )
test_passed("empty heap copy works!");
else
test_failed("empty heap copy failed.");
}
int main(int argc, char const *argv[])
{
start_suite("Start Dlist test suite:");
// Create a new scope to test destructor
{
// Create a new interger Dlist
Dlist<int> list;
// Should be true, we haven't added anything
testEmpty(list);
int *a = new int(3);
list.insertFront(a);
testFront(list, *a);
int *b = new int(47);
list.insertBack(b);
testBack(list, *b);
testFrontRemoval();
testBackRemoval();
testIsEmpty();
testAssignmentCopy();
testHeapCopy();
testAssignmentEmptyCopy();
testHeapEmptyCopy();
}
test_description("Tests if destructor is called correctly");
test_passed("Destructor works!");
end_suite();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.