sourcehook/test/testlist.cpp
author Pavol Marko <pavolmarko@gmail.com>
Fri Sep 23 20:14:10 2005 +0000 (2005-09-23)
changeset 111 aec58c99dafc
child 117 e322da4af2d2
permissions -rw-r--r--
SourceHook tests pass again! Added test for list (and a small bit of tinyhash). Woot.
     1 #include <string>
     2 #include "sh_list.h"
     3 #include "sh_tinyhash.h"
     4 
     5 // TEST LIST
     6 // Tests sh_list, sh_tinyhash, sh_vector
     7 
     8 // :TODO: vector test
     9 
    10 #define CHECK_COND(c, err) if (!(c)) { error = err; return false; }
    11 
    12 namespace
    13 {
    14 	struct Hmm
    15 	{
    16 		Hmm *m_This;
    17 		int m_Int;
    18 		Hmm(const Hmm &other) : m_This(this), m_Int(other.m_Int)
    19 		{
    20 		}
    21 		Hmm(int i) : m_This(this), m_Int(i)
    22 		{
    23 		}
    24 		Hmm() : m_This(this), m_Int(0)
    25 		{
    26 		}
    27 		void operator = (const Hmm &other)
    28 		{
    29 			m_Int = other.m_Int;
    30 		}
    31 		operator int () const
    32 		{
    33 			return m_Int;
    34 		}
    35 	};
    36 	
    37 	#define LIST_THIS_CHECK(lst, err) \
    38 		for (ListType::iterator iter = lst.begin(); iter != lst.end(); ++iter) \
    39 			CHECK_COND(&(*iter) == iter->m_This, err);
    40 	
    41 	bool DoTestList(std::string &error)
    42 	{
    43 		typedef SourceHook::List<Hmm> ListType;
    44 		ListType lst;
    45 	
    46 		CHECK_COND(lst.empty(), "Part1");
    47 		
    48 		for (int i = 1; i <= 100; ++i)
    49 			lst.push_back(i);
    50 			
    51 		LIST_THIS_CHECK(lst, "PartA1");
    52 		
    53 		CHECK_COND(!lst.empty(), "Part2");
    54 		
    55 		lst.clear();
    56 		
    57 		CHECK_COND(lst.empty(), "Part3");
    58 		
    59 		for (int i = 1; i <= 100; ++i)
    60 			lst.push_back(i);
    61 		
    62 		CHECK_COND(lst.back() == 100, "Part4");
    63 		LIST_THIS_CHECK(lst, "PartA2");
    64 		
    65 		int ver = 1;
    66 		for (ListType::iterator iter = lst.begin(); iter != lst.end(); ++iter)
    67 			CHECK_COND(*iter == ver++, "Part5");
    68 		
    69 		CHECK_COND(ver == 101, "Part 6");
    70 		
    71 		ListType::iterator iter50 = lst.find(50);
    72 		CHECK_COND(*iter50 == 50, "Part7");
    73 		
    74 		iter50 = lst.erase(iter50);
    75 		CHECK_COND(*iter50 == 51, "Part8");
    76 		CHECK_COND(*--iter50 == 49, "Part8.2");
    77 		
    78 		lst.remove(80);
    79 		
    80 		ver = 1;
    81 		for (ListType::iterator iter = lst.begin(); iter != lst.end(); ++iter)
    82 		{
    83 			CHECK_COND(*iter == ver++, "Part9");
    84 			if (ver == 50 || ver == 80)
    85 				++ver;
    86 		}
    87 		CHECK_COND(ver == 101, "Part10");
    88 		LIST_THIS_CHECK(lst, "PartA3");
    89 		
    90 		ListType lst2;
    91 		lst = lst2;
    92 		CHECK_COND(lst.empty(), "Part11");
    93 		
    94 		for (int i = 1; i <= 100; ++i)
    95 			lst.push_back(i);
    96 		lst2 = lst;
    97 		CHECK_COND(lst2.size() == 100, "Part11.2");
    98 		
    99 		LIST_THIS_CHECK(lst, "PartA4");
   100 		LIST_THIS_CHECK(lst2, "PartA5");
   101 		
   102 		ver = 1;
   103 		for (ListType::iterator iter = lst2.begin(); iter != lst2.end(); ++iter)
   104 			CHECK_COND(*iter == ver++, "Part12");
   105 		
   106 		lst.clear();
   107 		for (int i = 401; i <= 500; ++i)
   108 			lst.push_back(i);
   109 		lst = lst2;
   110 		CHECK_COND(lst2.size() == 100, "Part13");
   111 		
   112 		ver = 1;
   113 		for (ListType::iterator iter = lst.begin(); iter != lst.end(); ++iter)
   114 			CHECK_COND(*iter == ver++, "Part14");
   115 
   116 		LIST_THIS_CHECK(lst, "PartA6");
   117 		LIST_THIS_CHECK(lst2, "PartA7");
   118 				
   119 		return true;
   120 	}
   121 	
   122 	bool DoTestTinyHash(std::string &error)
   123 	{
   124 		const int mymax = 5000;
   125 		
   126 		typedef SourceHook::THash<int, int> HashType;
   127 		HashType hash;
   128 		
   129 		for (int i = 1; i <= mymax; ++i)
   130 			hash[i] = i + 5000;
   131 		
   132 		for (int i = 1; i <= mymax; ++i)
   133 			CHECK_COND(hash[i] == i + 5000, "Part1");
   134 		
   135 		// Find
   136 		int ver = 1;
   137 		for (HashType::iterator iter = hash.begin(); iter != hash.end(); ++iter)
   138 			CHECK_COND(iter->key == ver && iter->val == (ver++) + 5000, "Part2");
   139 		
   140 		CHECK_COND(ver == mymax+1, "Part2.1");
   141 		
   142 		HashType::iterator iter = hash.find(300);
   143 		CHECK_COND(iter != hash.end() && iter->val == 300+5000, "Part3.1");
   144 		iter = hash.find(mymax+200);
   145 		CHECK_COND(iter == hash.end(), "Part3.2");
   146 		
   147 		HashType hash2;
   148 		for (int i = 1; i <= mymax; ++i)
   149 			hash2[i] = i + 5000;
   150 		
   151 		return true;
   152 	}
   153 }
   154 
   155 bool TestList(std::string &error)
   156 {
   157 	if (!DoTestList(error))
   158 		return false;
   159 	
   160 	if (!DoTestTinyHash(error))
   161 		return false;
   162 	
   163 	
   164 	return true;
   165 }