diff --git a/YASI_12/YASI_12.vcxproj b/YASI_12/YASI_12.vcxproj index ae46f3d..ad1e614 100644 --- a/YASI_12/YASI_12.vcxproj +++ b/YASI_12/YASI_12.vcxproj @@ -92,7 +92,7 @@ false - + @@ -106,7 +106,9 @@ true + + diff --git a/YASI_12/YASI_12.vcxproj.filters b/YASI_12/YASI_12.vcxproj.filters index 445f702..c1f9f23 100644 --- a/YASI_12/YASI_12.vcxproj.filters +++ b/YASI_12/YASI_12.vcxproj.filters @@ -104,9 +104,6 @@ Header Files\Common - - Header Files\Common - Header Files\Common @@ -143,5 +140,14 @@ Header Files\Algorithms + + Header Files\Common + + + Header Files\Common + + + Header Files\Common + \ No newline at end of file diff --git a/YASI_12/common.h b/YASI_12/common.h index 831c447..02a6a7a 100644 --- a/YASI_12/common.h +++ b/YASI_12/common.h @@ -25,7 +25,14 @@ #define DETECT_MEM_LEAKS() #endif // _WIN32 + + #include + +/*--------- Availability of Google C++ Test Framework ----------*/ + +// whether using Google C++ Test Framework +#define GTEST 1 #include "test.h" diff --git a/YASI_12/ds.iterator.h b/YASI_12/ds.iterator.h index 6ce732b..f5c3f8d 100644 --- a/YASI_12/ds.iterator.h +++ b/YASI_12/ds.iterator.h @@ -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{ @@ -14,7 +17,7 @@ namespace yasi{ class Iterator > class IteratorBase{ ////////////////// enable testing //////////////// - friend class IteratorTest; + FRIEND_TEST_CLASS(IteratorTest); typedef IteratorBase self; protected: @@ -44,7 +47,7 @@ namespace yasi{ class Iterator > class ForwardIterator :public virtual IteratorBase{ ////////////////// enable testing //////////////// - friend class IteratorTest; + FRIEND_TEST_CLASS(IteratorTest); typedef IteratorBase base; typedef ForwardIterator self; @@ -67,7 +70,7 @@ namespace yasi{ class Iterator> class BackwardIterator :public virtual IteratorBase{ ////////////////// enable testing //////////////// - friend class IteratorTest; + FRIEND_TEST_CLASS(IteratorTest); typedef IteratorBase base; typedef BackwardIterator self; @@ -94,7 +97,7 @@ namespace yasi{ public virtual ForwardIterator, public virtual BackwardIterator{ ////////////////// enable testing //////////////// - friend class IteratorTest; + FRIEND_TEST_CLASS(IteratorTest); typedef ForwardIterator base1; typedef BackwardIterator base2; @@ -115,7 +118,7 @@ namespace yasi{ template class NodeIteratorBase : public virtual ForwardIterator{ ////////////////// enable testing //////////////// - friend class IteratorTest; + FRIEND_TEST_CLASS(IteratorTest); typedef ForwardIterator base; typedef NodeIteratorBase self; @@ -137,7 +140,7 @@ namespace yasi{ template // E is the value type class ForwardNodeIterator : public NodeIteratorBase{ ////////////////// enable testing //////////////// - friend class IteratorTest; + FRIEND_TEST_CLASS(IteratorTest); typedef NodeIteratorBase base; typedef ForwardNodeIterator self; @@ -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; } }; @@ -163,6 +170,9 @@ namespace yasi{ public virtual NodeIteratorBase, public virtual BidirectionalIterator{ + ////////////////// enable testing //////////////// + FRIEND_TEST_CLASS(IteratorTest); + typedef NodeIteratorBase base; typedef ForwardIterator base1; // for virtual inheritance typedef BackwardIterator base2; // for virtual inheritance diff --git a/YASI_12/ds.iterator_test.h b/YASI_12/ds.iterator.test.h similarity index 55% rename from YASI_12/ds.iterator_test.h rename to YASI_12/ds.iterator.test.h index 1a0dd58..fafe2e5 100644 --- a/YASI_12/ds.iterator_test.h +++ b/YASI_12/ds.iterator.test.h @@ -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 @@ -14,7 +16,7 @@ namespace yasi{ typedef ForwardNodeIterator < E, Node, ForwardNodeIteratorDerived > base; public: virtual ~ForwardNodeIteratorDerived(){} - ForwardNodeIteratorDerived(Node* pNode) : base(pNode){} + ForwardNodeIteratorDerived(Node* pNode) : base(pNode), IteratorBase(pNode){} }; public: @@ -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 + } } \ No newline at end of file diff --git a/YASI_12/ds.node.h b/YASI_12/ds.node.h index 3d7d65b..fb44089 100644 --- a/YASI_12/ds.node.h +++ b/YASI_12/ds.node.h @@ -55,7 +55,7 @@ namespace yasi{ typedef SListNode self; public: SListNode(){} - SListNode(const E& elem) : base(elem){} + SListNode(const E& elem) : base(elem), NodeBase(elem){} ///////////////////////////////////// //// very important: since we are doing virtual inheritance, //// we need to specify all vertual constructors in the initializer list diff --git a/YASI_12/main.cpp b/YASI_12/main.cpp index 30f4fe3..86828c6 100644 --- a/YASI_12/main.cpp +++ b/YASI_12/main.cpp @@ -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" @@ -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(); } diff --git a/YASI_12/test.h b/YASI_12/test.h index 5a2ad18..62e992f 100644 --- a/YASI_12/test.h +++ b/YASI_12/test.h @@ -1,4 +1,8 @@ #pragma once + +// available only if Google C++ Test Framework is available +#if GTEST + #include "gtest\gtest.h" namespace yasi{ @@ -30,4 +34,6 @@ namespace yasi{ public: virtual ~Test(){} }; -} +} // namespace yasi + +#endif // GTEST diff --git a/YASI_12/test.this.module.h b/YASI_12/test.this.module.h new file mode 100644 index 0000000..2fa5b6e --- /dev/null +++ b/YASI_12/test.this.module.h @@ -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 \ No newline at end of file diff --git a/YASI_12/utils.h b/YASI_12/utils.h index 9ae4a17..726bfc1 100644 --- a/YASI_12/utils.h +++ b/YASI_12/utils.h @@ -7,6 +7,9 @@ #include using namespace std; +/****** enable-disable testing classes in this file ******/ +#include "test.this.module.h" + namespace yasi{ // compares two arrays @@ -155,73 +158,5 @@ typename enable_if