sourcehook/test/testlist.cpp
author Pavol Marko <pavolmarko@gmail.com>
Fri Dec 23 23:00:55 2005 +0000 (2005-12-23)
changeset 163 377bdd31c28d
parent 137 edafdaacae96
child 204 e6cfa97ef258
permissions -rw-r--r--
Added insert(iterator where, const T &what) to sh_list and simplified it on the occasion, hook managers now have (optional) version numbers, everything somehow works! Also changed _NEWPARAMS macro syntax a bit
     1 #include <string>
     2 #include "sh_list.h"
     3 #include "sh_stack.h"
     4 #include "sh_tinyhash.h"
     5 #include "testevents.h"
     6 
     7 // TEST LIST
     8 // Tests sh_list, sh_tinyhash, sh_vector
     9 
    10 // :TODO: vector test, list insert test
    11 
    12 namespace
    13 {
    14 	struct Hmm
    15 	{
    16 		Hmm *m_This;
    17 		int m_Int;
    18 		Hmm(const Hmm &other) : m_Int(other.m_Int)
    19 		{
    20 			m_This = this;
    21 		}
    22 		Hmm(int i) : m_Int(i)
    23 		{
    24 			m_This = this;
    25 		}
    26 		Hmm() : m_Int(0)
    27 		{
    28 			m_This = this;
    29 		}
    30 		void operator = (const Hmm &other)
    31 		{
    32 			m_Int = other.m_Int;
    33 		}
    34 		operator int () const
    35 		{
    36 			return m_Int;
    37 		}
    38 	};
    39 
    40 	#define LIST_THIS_CHECK(lst, err) \
    41 		for (ListType::iterator iter = lst.begin(); iter != lst.end(); ++iter) \
    42 			CHECK_COND(&(*iter) == iter->m_This, err);
    43 
    44 	bool DoTestList(std::string &error)
    45 	{
    46 		typedef SourceHook::List<Hmm> ListType;
    47 		ListType lst;
    48 
    49 		CHECK_COND(lst.empty(), "Part1");
    50 
    51 		for (int i = 1; i <= 100; ++i)
    52 			lst.push_back(i);
    53 
    54 		LIST_THIS_CHECK(lst, "PartA1");
    55 
    56 		CHECK_COND(!lst.empty(), "Part2");
    57 
    58 		lst.clear();
    59 
    60 		CHECK_COND(lst.empty(), "Part3");
    61 
    62 		for (int i = 1; i <= 100; ++i)
    63 			lst.push_back(i);
    64 
    65 		CHECK_COND(lst.back() == 100, "Part4");
    66 		LIST_THIS_CHECK(lst, "PartA2");
    67 
    68 		int ver = 1;
    69 		for (ListType::iterator iter = lst.begin(); iter != lst.end(); ++iter)
    70 			CHECK_COND(*iter == ver++, "Part5");
    71 
    72 		CHECK_COND(ver == 101, "Part 6");
    73 
    74 		ListType::iterator iter50 = lst.find(50);
    75 		CHECK_COND(*iter50 == 50, "Part7");
    76 
    77 		iter50 = lst.erase(iter50);
    78 		CHECK_COND(*iter50 == 51, "Part8");
    79 		CHECK_COND(*--iter50 == 49, "Part8.2");
    80 
    81 		lst.remove(80);
    82 
    83 		ver = 1;
    84 		for (ListType::iterator iter = lst.begin(); iter != lst.end(); ++iter)
    85 		{
    86 			CHECK_COND(*iter == ver++, "Part9");
    87 			if (ver == 50 || ver == 80)
    88 				++ver;
    89 		}
    90 		CHECK_COND(ver == 101, "Part10");
    91 		LIST_THIS_CHECK(lst, "PartA3");
    92 
    93 		ListType lst2;
    94 		lst = lst2;
    95 		CHECK_COND(lst.empty(), "Part11");
    96 
    97 		for (int i = 1; i <= 100; ++i)
    98 			lst.push_back(i);
    99 		lst2 = lst;
   100 		CHECK_COND(lst2.size() == 100, "Part11.2");
   101 
   102 		LIST_THIS_CHECK(lst, "PartA4");
   103 		LIST_THIS_CHECK(lst2, "PartA5");
   104 
   105 		ver = 1;
   106 		for (ListType::iterator iter = lst2.begin(); iter != lst2.end(); ++iter)
   107 			CHECK_COND(*iter == ver++, "Part12");
   108 
   109 		lst.clear();
   110 		for (int i = 401; i <= 500; ++i)
   111 			lst.push_back(i);
   112 		lst = lst2;
   113 		CHECK_COND(lst2.size() == 100, "Part13");
   114 
   115 		ver = 1;
   116 		for (ListType::iterator iter = lst.begin(); iter != lst.end(); ++iter)
   117 			CHECK_COND(*iter == ver++, "Part14");
   118 
   119 		LIST_THIS_CHECK(lst, "PartA6");
   120 		LIST_THIS_CHECK(lst2, "PartA7");
   121 
   122 		return true;
   123 	}
   124 
   125 	bool DoTestTinyHash(std::string &error)
   126 	{
   127 		const int mymax = 5000;
   128 
   129 		typedef SourceHook::THash<int, int> HashType;
   130 		HashType hash;
   131 
   132 		for (int i = 1; i <= mymax; ++i)
   133 			hash[i] = i + 5000;
   134 
   135 		for (int i = 1; i <= mymax; ++i)
   136 			CHECK_COND(hash[i] == i + 5000, "Part1");
   137 
   138 		// Find
   139 		int ver = 1;
   140 		HashType::iterator iter;
   141 		for (iter = hash.begin(); iter != hash.end(); ++iter)
   142 			CHECK_COND(iter->key == ver && iter->val == (ver++) + 5000, "Part2");
   143 
   144 		CHECK_COND(ver == mymax+1, "Part2.1");
   145 
   146 		iter = hash.find(300);
   147 		CHECK_COND(iter != hash.end() && iter->val == 300+5000, "Part3.1");
   148 		iter = hash.find(mymax+200);
   149 		CHECK_COND(iter == hash.end(), "Part3.2");
   150 
   151 		HashType hash2;
   152 		for (int i = 1; i <= mymax; ++i)
   153 			hash2[i] = i + 5000;
   154 
   155 		hash2.erase(mymax - 100);
   156 		CHECK_COND(hash2.find(mymax - 101) != hash2.end(), "Part 4.1");
   157 		CHECK_COND(hash2.find(mymax - 99) != hash2.end(), "Part 4.2");
   158 		CHECK_COND(hash2.find(mymax - 100) == hash2.end(), "Part 4.3");
   159 		hash2.erase(mymax - 99);
   160 		CHECK_COND(hash2.find(mymax - 101) != hash2.end(), "Part 4.4");
   161 		CHECK_COND(hash2.find(mymax - 99) == hash2.end(), "Part 4.5");
   162 		CHECK_COND(hash2.find(mymax - 100) == hash2.end(), "Part 4.6");
   163 
   164 		return true;
   165 	}
   166 
   167 	bool DoTestStack(std::string &error)
   168 	{
   169 		typedef SourceHook::CStack<int> IntStack;
   170 		IntStack stk;
   171 		int i;
   172 
   173 		CHECK_COND(stk.size() == 0 && stk.empty(), "A0");
   174 
   175 		for (i = 0; i < 5000; ++i)
   176 			stk.push(i);
   177 
   178 		CHECK_COND(stk.front() == 4999, "1");
   179 		CHECK_COND(stk.size() == 5000 && !stk.empty(), "A1");
   180 
   181 		IntStack::iterator iter;
   182 		i = 0;
   183 		for (iter = stk.begin(); iter != stk.end(); ++iter, ++i)
   184 			CHECK_COND(*iter == i, "2");
   185 
   186 		i = 0;
   187 		for (iter = stk.begin(); iter != stk.end(); iter++, ++i)
   188 			CHECK_COND(*iter == i, "3");
   189 
   190 		--iter;
   191 		iter--;
   192 		*iter = 'g'+'a'+'b'+'e'+'n';
   193 		stk.pop();
   194 
   195 		CHECK_COND(stk.size() == 4999 && !stk.empty(), "A2");
   196 		CHECK_COND(stk.front() == 'g'+'a'+'b'+'e'+'n', "4");
   197 		
   198 		IntStack stk2(stk);
   199 		CHECK_COND(stk2.size() == 4999 && !stk2.empty(), "A3");
   200 
   201 		IntStack::iterator iter2 = stk2.begin();
   202 		for (iter = stk.begin(); iter != stk.end(); ++iter, iter2++)
   203 			CHECK_COND(*iter == *iter2, "5");
   204 
   205 		while (!stk2.empty())
   206 			stk2.pop();
   207 		CHECK_COND(stk2.size() == 0 && stk2.empty(), "A4");
   208 		stk = stk2;
   209 		CHECK_COND(stk.size() == 0 && stk.empty(), "A5");
   210 
   211 		return true;
   212 	}
   213 }
   214 
   215 bool TestList(std::string &error)
   216 {
   217 	if (!DoTestList(error))
   218 		return false;
   219 
   220 	if (!DoTestTinyHash(error))
   221 		return false;
   222 
   223 	if (!DoTestStack(error))
   224 		return false;
   225 
   226 	return true;
   227 }