Skip to content

Commit

Permalink
Separeted unit tests for utils.h and ds.iterator.h in separate files.
Browse files Browse the repository at this point in the history
Fixed a bug in ds.iterator.h due to virtual inheritance in SListNode/DListNode constructors.

Tests are now available only when GTEST is 1 and YASI_TEST_THIS_MODULE is 1

Signed-off-by: unknown <saq10002@iteb-219.ad.engr.uconn.edu>
  • Loading branch information
saq10002 committed Oct 10, 2014
1 parent 5c038bf commit 4d213c7
Show file tree
Hide file tree
Showing 11 changed files with 208 additions and 111 deletions.
4 changes: 3 additions & 1 deletion YASI_12/YASI_12.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
<ClInclude Include="ds.iterator.h">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">false</ExcludedFromBuild>
</ClInclude>
<ClInclude Include="ds.iterator_test.h" />
<ClInclude Include="ds.iterator.test.h" />
<ClInclude Include="ds.kvpair.h" />
<ClInclude Include="ds.LinearProbingHashTable.h" />
<ClInclude Include="ds.list.h" />
Expand All @@ -106,7 +106,9 @@
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
</ClInclude>
<ClInclude Include="test.h" />
<ClInclude Include="test.this.module.h" />
<ClInclude Include="utils.h" />
<ClInclude Include="utils.test.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
Expand Down
12 changes: 9 additions & 3 deletions YASI_12/YASI_12.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,6 @@
<ClInclude Include="ds.iterator.h">
<Filter>Header Files\Common</Filter>
</ClInclude>
<ClInclude Include="ds.iterator_test.h">
<Filter>Header Files\Common</Filter>
</ClInclude>
<ClInclude Include="ds.kvpair.h">
<Filter>Header Files\Common</Filter>
</ClInclude>
Expand Down Expand Up @@ -143,5 +140,14 @@
<ClInclude Include="alg.graph.h">
<Filter>Header Files\Algorithms</Filter>
</ClInclude>
<ClInclude Include="ds.iterator.test.h">
<Filter>Header Files\Common</Filter>
</ClInclude>
<ClInclude Include="test.this.module.h">
<Filter>Header Files\Common</Filter>
</ClInclude>
<ClInclude Include="utils.test.h">
<Filter>Header Files\Common</Filter>
</ClInclude>
</ItemGroup>
</Project>
7 changes: 7 additions & 0 deletions YASI_12/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,14 @@
#define DETECT_MEM_LEAKS()

#endif // _WIN32


#include <cassert>

/*--------- Availability of Google C++ Test Framework ----------*/

// whether using Google C++ Test Framework
#define GTEST 1
#include "test.h"


Expand Down
26 changes: 18 additions & 8 deletions YASI_12/ds.iterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
#include "common.h"
#include "utils.h"
#include "ds.node.h"
#include "test.h"

using namespace std;

/******** enable-disable testing *******/
#include "test.this.module.h"


namespace yasi{
namespace ds{

Expand All @@ -14,7 +17,7 @@ namespace yasi{
class Iterator >
class IteratorBase{
////////////////// enable testing ////////////////
friend class IteratorTest;
FRIEND_TEST_CLASS(IteratorTest);

typedef IteratorBase<Elem,Iterated,Iterator> self;
protected:
Expand Down Expand Up @@ -44,7 +47,7 @@ namespace yasi{
class Iterator >
class ForwardIterator :public virtual IteratorBase<E,Iterated,Iterator>{
////////////////// enable testing ////////////////
friend class IteratorTest;
FRIEND_TEST_CLASS(IteratorTest);

typedef IteratorBase<E, Iterated, Iterator> base;
typedef ForwardIterator<E,Iterated,Iterator> self;
Expand All @@ -67,7 +70,7 @@ namespace yasi{
class Iterator>
class BackwardIterator :public virtual IteratorBase<E,Iterated,Iterator>{
////////////////// enable testing ////////////////
friend class IteratorTest;
FRIEND_TEST_CLASS(IteratorTest);

typedef IteratorBase<E, Iterated, Iterator> base;
typedef BackwardIterator<E, Iterated, Iterator> self;
Expand All @@ -94,7 +97,7 @@ namespace yasi{
public virtual ForwardIterator<E, Iterated, Iterator>,
public virtual BackwardIterator<E, Iterated, Iterator>{
////////////////// enable testing ////////////////
friend class IteratorTest;
FRIEND_TEST_CLASS(IteratorTest);

typedef ForwardIterator<E, Iterated, Iterator> base1;
typedef BackwardIterator<E, Iterated, Iterator> base2;
Expand All @@ -115,7 +118,7 @@ namespace yasi{
template<class E, class Node, class Iterator>
class NodeIteratorBase : public virtual ForwardIterator<E, Node, Iterator>{
////////////////// enable testing ////////////////
friend class IteratorTest;
FRIEND_TEST_CLASS(IteratorTest);

typedef ForwardIterator<E, Node, Iterator> base;
typedef NodeIteratorBase<E,Node,Iterator> self;
Expand All @@ -137,7 +140,7 @@ namespace yasi{
template<class E, class Node, class Iterator > // E is the value type
class ForwardNodeIterator : public NodeIteratorBase<E, Node,Iterator >{
////////////////// enable testing ////////////////
friend class IteratorTest;
FRIEND_TEST_CLASS(IteratorTest);

typedef NodeIteratorBase<E, Node, Iterator > base;
typedef ForwardNodeIterator<E, Node, Iterator> self;
Expand All @@ -152,7 +155,11 @@ namespace yasi{
// operators
//virtual E& operator* () const { return pNode->element; };
self& operator = (const self& other){ this->pNode = other.pNode; return *this; }
self operator ++ (int){ self temp(this->pNode); incr(); return temp; }
self operator ++ (int){
self temp(this->pNode);
incr();
return temp;
}
self& operator ++ (){ incr(); return *this; }
};

Expand All @@ -163,6 +170,9 @@ namespace yasi{
public virtual NodeIteratorBase<E, Node, Iterator >,
public virtual BidirectionalIterator<E,Node, Iterator>{

////////////////// enable testing ////////////////
FRIEND_TEST_CLASS(IteratorTest);

typedef NodeIteratorBase<E, Node, Iterator > base;
typedef ForwardIterator<E, Node, Iterator > base1; // for virtual inheritance
typedef BackwardIterator<E, Node, Iterator > base2; // for virtual inheritance
Expand Down
34 changes: 26 additions & 8 deletions YASI_12/ds.iterator_test.h → YASI_12/ds.iterator.test.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#pragma once
#include "ds.iterator.h"
#include "ds.node.h"
using namespace std;

namespace yasi{
namespace ds{

///////////////// test ///////////////
#if GTEST && YASI_TEST_THIS_MODULE

class IteratorTest :public yasi::Test{

// derive from forward node iterator
Expand All @@ -14,7 +16,7 @@ namespace yasi{
typedef ForwardNodeIterator < E, Node, ForwardNodeIteratorDerived<E, Node> > base;
public:
virtual ~ForwardNodeIteratorDerived(){}
ForwardNodeIteratorDerived(Node* pNode) : base(pNode){}
ForwardNodeIteratorDerived(Node* pNode) : base(pNode), IteratorBase(pNode){}
};

public:
Expand All @@ -31,20 +33,36 @@ namespace yasi{

// create iterator
Iterator it(n[0]);
Node* node;
int i = 0;
do{
ASSERT_EQ(it.pNode, n[i]) << "wrong iterator node at i=" << i;
ASSERT_EQ(*it, i) << "wrong iterator value at i=" << i;
ASSERT_EQ(it.pNode, (it++).pNode) << "wrong postfix increment at i=" << i;
ASSERT_EQ(it.pNode->next(), (++it).pNode) << "wrong postfix increment at i=" << i;
// it already incremente
Iterator tempIter = it;
// dereference
ASSERT_EQ(*tempIter, i) << "wrong iterator value at i=" << i;
ASSERT_EQ(tempIter.pNode, n[i]) << "wrong iterator node at i=" << i;
// postfix++
node = tempIter.pNode;
ASSERT_EQ(node, (tempIter++).pNode) << "wrong postfix increment at i=" << i;
// prefix++
tempIter = it;
node = tempIter.pNode->next();
ASSERT_EQ(node, (++tempIter).pNode) << "wrong prefix increment at i=" << i;
// next iteration
i++;
it++;
} while (it.pNode != NULL);
ASSERT_EQ(11, i) << "i not 11";
ASSERT_EQ(10, i) << "i not 10";

// cleanup
for (int i = 0; i < 10; i++){
DELETE_SAFE(n[i]);
}
}
};

ADD_TEST_F(IteratorTest, forwardNodeIterator);

#endif // GTEST && YASI_TEST_THIS_MODULE

}
}
2 changes: 1 addition & 1 deletion YASI_12/ds.node.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ namespace yasi{
typedef SListNode<E> self;
public:
SListNode(){}
SListNode(const E& elem) : base(elem){}
SListNode(const E& elem) : base(elem), NodeBase<E>(elem){}
/////////////////////////////////////
//// very important: since we are doing virtual inheritance,
//// we need to specify all vertual constructors in the initializer list
Expand Down
53 changes: 32 additions & 21 deletions YASI_12/main.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
#include "common.h"
#include "utils.h"
#include "ds.singlylinkedlist.h"
#include "ds.doublylinkedlist.h"
#include "ds.arraybinarytree.h"
#include "ds.binaryheap.h"
#include "ds.binarytree.h"
#include "ds.binarysearchtree.h"
#include "ds.priorityqueue.h"
#include "ds.BSTDictionary.h"
#include "ds.separatechaininghashtable.h"
#include "ds.intlinearprobinghashtable.h"
#include "ds.linearprobinghashtable.h"
#include "ds.hopscotchhashtable.h"
#include "ds.graph.h"
#include "alg.graph.h"
//#include "utils.h"
//#include "ds.singlylinkedlist.h"
//#include "ds.doublylinkedlist.h"
//#include "ds.arraybinarytree.h"
//#include "ds.binaryheap.h"
//#include "ds.binarytree.h"
//#include "ds.binarysearchtree.h"
//#include "ds.priorityqueue.h"
//#include "ds.BSTDictionary.h"
//#include "ds.separatechaininghashtable.h"
//#include "ds.intlinearprobinghashtable.h"
//#include "ds.linearprobinghashtable.h"
//#include "ds.hopscotchhashtable.h"
//#include "ds.graph.h"
//#include "alg.graph.h"

//#include "Sorter.h"

Expand All @@ -26,23 +26,34 @@
using namespace yasi;
using namespace std;

void testSort();
/*------------- Testing ---------------*/
#define YASI_TEST_THIS_MODULE 1
#include "utils.test.h"
#include "ds.iterator.test.h"

int testAll(int* argc, char** argv){

int main(int argc, char** argv){

// currently only supported on Windows systems
DETECT_MEM_LEAKS();

::testing::InitGoogleTest(&argc, argv); // init google test
::testing::InitGoogleTest(argc, argv); // init google test

int retVal = RUN_ALL_TESTS(); // run all test cases

return retVal;
}

int main(int argc, char** argv){

// currently only supported on Windows systems
DETECT_MEM_LEAKS();

int retVal = testAll(& argc, argv);

cout << endl;
cout << "Press any key..." << endl;
getchar();
return retVal;
//testSort();
//testArrayBinaryTree();
}


Expand Down
8 changes: 7 additions & 1 deletion YASI_12/test.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#pragma once

// available only if Google C++ Test Framework is available
#if GTEST

#include "gtest\gtest.h"

namespace yasi{
Expand Down Expand Up @@ -30,4 +34,6 @@ namespace yasi{
public:
virtual ~Test(){}
};
}
} // namespace yasi

#endif // GTEST
23 changes: 23 additions & 0 deletions YASI_12/test.this.module.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// deliberately not using #pragma once

// Are we required to test classes defined in this file?
#if YASI_TEST_THIS_MODULE

#if GTEST

#define FRIEND_TEST_CLASS(className) \
friend class className

#else // GTEST
// GTest not available
#define FRIEND_TEST_CLASS(className)

#endif // GTEST

#else // YASI_TEST_THIS_MODULE

// We don't want to test classes defined in this file
#define FRIEND_TEST_CLASS(className)


#endif // YASI_TEST_THIS_MODULE
Loading

0 comments on commit 4d213c7

Please sign in to comment.