1 /////////////////////////////////////////////////////////////////////////////// 2 // Unit Test for Loki 3 // 4 // Copyright Terje Sletteb� and Pavel Vozenilek 2002. 5 // 6 // Permission to use, copy, modify, and distribute this software for any 7 // purpose is hereby granted without fee, provided that this copyright and 8 // permissions notice appear in all copies and derivatives. 9 // 10 // This software is provided "as is" without express or implied warranty. 11 // 12 /////////////////////////////////////////////////////////////////////////////// 13 #ifndef TYPELISTTEST_H 14 #define TYPELISTTEST_H 15 16 // $Id: TypelistTest.h 760 2006-10-17 20:36:13Z syntheticpp $ 17 18 19 #include <loki/Typelist.h> 20 #include <loki/Sequence.h> 21 22 /////////////////////////////////////////////////////////////////////////////// 23 // TypelistTest 24 /////////////////////////////////////////////////////////////////////////////// 25 26 class TypelistTest : public Test 27 { 28 public: TypelistTest()29 TypelistTest() : Test("Typelist.h") {} 30 execute(TestResult & result)31 virtual void execute(TestResult &result) 32 { 33 printName(result); 34 35 using namespace Loki; 36 using namespace Loki::TL; 37 38 #ifndef LOKI_DISABLE_TYPELIST_MACROS 39 typedef LOKI_TYPELIST_1(char) CharList; 40 typedef LOKI_TYPELIST_3(char,int,double) CharIntDoubleList; 41 typedef LOKI_TYPELIST_4(char,int,double,char) CharIntDoubleCharList; 42 typedef LOKI_TYPELIST_3(Base,Derived1,Derived2) BaseDerived1Derived2List; 43 typedef LOKI_TYPELIST_3(Derived2,Derived1,Base) Derived2Derived1BaseList; 44 typedef LOKI_TYPELIST_4(Base,Derived1,Base,Derived2) BaseDerived1BaseDerived2List; 45 typedef LOKI_TYPELIST_4(Derived1,Base,Derived1,Derived2) Derived1BaseDerived1Derived2List; 46 #else 47 typedef Seq<char>::Type CharList; 48 typedef Seq<char,int,double>::Type CharIntDoubleList; 49 typedef Seq<char,int,double,char>::Type CharIntDoubleCharList; 50 typedef Seq<Base,Derived1,Derived2>::Type BaseDerived1Derived2List; 51 typedef Seq<Derived2,Derived1,Base>::Type Derived2Derived1BaseList; 52 typedef Seq<Base,Derived1,Base,Derived2>::Type BaseDerived1BaseDerived2List; 53 typedef Seq<Derived1,Base,Derived1,Derived2>::Type Derived1BaseDerived1Derived2List; 54 #endif 55 56 bool r; 57 58 r=Length<NullType>::value==0 && 59 Length<CharList>::value==1 && 60 Length<CharIntDoubleList>::value==3; 61 62 testAssert("Length",r,result); 63 64 r=SameType<TypeAt<CharList,0>::Result,char>::value && 65 SameType<TypeAt<CharIntDoubleList,2>::Result,double>::value; 66 67 testAssert("TypeAt",r,result); 68 69 #if !(defined(_MSC_VER) && !defined(__INTEL_COMPILER) && !defined(__MWERKS__) && _MSC_VER < 1300) 70 71 // TypeAtNonStrict works like TypeAt on MSVC 6.0 72 73 r=SameType<TypeAtNonStrict<NullType,0>::Result,NullType>::value && 74 SameType<TypeAtNonStrict<CharList,0>::Result,char>::value && 75 SameType<TypeAtNonStrict<CharIntDoubleList,2>::Result,double>::value && 76 SameType<TypeAtNonStrict<CharIntDoubleList,3>::Result,NullType>::value && 77 SameType<TypeAtNonStrict<CharList,1,long>::Result,long>::value; 78 79 testAssert("TypeAtNonStrict",r,result); 80 81 #else 82 83 testAssert("TypeAtNonStrict",false,result,false); 84 85 #endif 86 87 r=IndexOf<NullType,char>::value==-1 && 88 IndexOf<CharList,char>::value==0 && 89 IndexOf<CharIntDoubleList,double>::value==2 && 90 IndexOf<CharIntDoubleList,long>::value==-1; 91 92 testAssert("IndexOf",r,result); 93 94 #if !(defined(_MSC_VER) && !defined(__INTEL_COMPILER) && !defined(__MWERKS__) && _MSC_VER < 1300) 95 96 // Append, Erase, EraseAll, NoDuplicates, Replace, ReplaceAll, Reverse, 97 // MostDerived and DerivedToFront doesn't work on MSVC 6.0 98 99 #ifndef LOKI_DISABLE_TYPELIST_MACROS 100 101 r=SameType<Append<NullType,NullType>::Result,NullType>::value && 102 SameType<Append<NullType,char>::Result,LOKI_TYPELIST_1(char)>::value && 103 SameType<Append<NullType,CharList>::Result,CharList>::value && 104 SameType<Append<CharList,NullType>::Result,CharList>::value && 105 SameType<Append<CharList,int>::Result,LOKI_TYPELIST_2(char,int)>::value && 106 SameType<Append<CharList,CharIntDoubleList>::Result,LOKI_TYPELIST_4(char,char,int,double)>::value; 107 108 testAssert("Append",r,result); 109 110 r=SameType<Erase<NullType,char>::Result,NullType>::value && 111 SameType<Erase<CharList,char>::Result,NullType>::value && 112 SameType<Erase<CharList,long>::Result,CharList>::value && 113 SameType<Erase<CharIntDoubleList,int>::Result,LOKI_TYPELIST_2(char,double)>::value && 114 SameType<Erase<CharIntDoubleList,double>::Result,LOKI_TYPELIST_2(char,int)>::value; 115 116 testAssert("Erase",r,result); 117 118 r=SameType<EraseAll<NullType,char>::Result,NullType>::value && 119 SameType<EraseAll<CharList,char>::Result,NullType>::value && 120 SameType<EraseAll<CharList,long>::Result,CharList>::value && 121 SameType<EraseAll<CharIntDoubleList,int>::Result,LOKI_TYPELIST_2(char,double)>::value && 122 SameType<EraseAll<CharIntDoubleList,double>::Result,LOKI_TYPELIST_2(char,int)>::value && 123 SameType<EraseAll<CharIntDoubleCharList,char>::Result,LOKI_TYPELIST_2(int,double)>::value && 124 SameType<EraseAll<CharIntDoubleCharList,int>::Result,LOKI_TYPELIST_3(char,double,char)>::value && 125 SameType<EraseAll<CharIntDoubleCharList,double>::Result,LOKI_TYPELIST_3(char,int,char)>::value; 126 127 testAssert("EraseAll",r,result); 128 129 r=SameType<NoDuplicates<NullType>::Result,NullType>::value && 130 SameType<NoDuplicates<CharList>::Result,CharList>::value && 131 SameType<NoDuplicates<CharIntDoubleList>::Result,CharIntDoubleList>::value && 132 SameType<NoDuplicates<CharIntDoubleCharList>::Result,CharIntDoubleList>::value; 133 134 testAssert("NoDuplicates",r,result); 135 136 r=SameType<Replace<NullType,char,long>::Result,NullType>::value && 137 SameType<Replace<CharList,char,long>::Result,LOKI_TYPELIST_1(long)>::value && 138 SameType<Replace<CharList,int,long>::Result,CharList>::value && 139 SameType<Replace<CharIntDoubleList,char,long>::Result,LOKI_TYPELIST_3(long,int,double)>::value && 140 SameType<Replace<CharIntDoubleList,long,char[16]>::Result,CharIntDoubleList>::value && 141 SameType<Replace<CharIntDoubleCharList,char,long>::Result,LOKI_TYPELIST_4(long,int,double,char)>::value; 142 143 testAssert("Replace",r,result); 144 145 r=SameType<ReplaceAll<NullType,char,long>::Result,NullType>::value && 146 SameType<ReplaceAll<CharList,char,long>::Result,LOKI_TYPELIST_1(long)>::value && 147 SameType<ReplaceAll<CharList,int,long>::Result,CharList>::value && 148 SameType<ReplaceAll<CharIntDoubleList,char,long>::Result,LOKI_TYPELIST_3(long,int,double)>::value && 149 SameType<ReplaceAll<CharIntDoubleList,long,char[16]>::Result,CharIntDoubleList>::value && 150 SameType<ReplaceAll<CharIntDoubleCharList,char,long>::Result,LOKI_TYPELIST_4(long,int,double,long)>::value; 151 152 testAssert("ReplaceAll",r,result); 153 154 r=SameType<Reverse<NullType>::Result,NullType>::value && 155 SameType<Reverse<CharList>::Result,CharList>::value && 156 SameType<Reverse<CharIntDoubleList>::Result,LOKI_TYPELIST_3(double,int,char)>::value; 157 158 testAssert("Reverse",r,result); 159 160 r=SameType<MostDerived<NullType,Base>::Result,Base>::value && 161 SameType<MostDerived<BaseDerived1Derived2List,Base>::Result,Derived2>::value && 162 SameType<MostDerived<BaseDerived1Derived2List,Derived1>::Result,Derived2>::value && 163 SameType<MostDerived<BaseDerived1Derived2List,Derived2>::Result,Derived2>::value && 164 SameType<MostDerived<Derived2Derived1BaseList,Base>::Result,Derived2>::value && 165 SameType<MostDerived<Derived2Derived1BaseList,Derived1>::Result,Derived2>::value && 166 SameType<MostDerived<Derived2Derived1BaseList,Derived2>::Result,Derived2>::value; 167 168 testAssert("MostDerived",r,result); 169 170 r=SameType<DerivedToFront<NullType>::Result,NullType>::value && 171 SameType<DerivedToFront<CharList>::Result,CharList>::value && 172 SameType<DerivedToFront<CharIntDoubleList>::Result,CharIntDoubleList>::value && 173 SameType<DerivedToFront<CharIntDoubleCharList>::Result,CharIntDoubleCharList>::value && 174 SameType<DerivedToFront<BaseDerived1Derived2List>::Result,Derived2Derived1BaseList>::value && 175 SameType<DerivedToFront<Derived2Derived1BaseList>::Result,Derived2Derived1BaseList>::value && 176 SameType<DerivedToFront<BaseDerived1BaseDerived2List>::Result,LOKI_TYPELIST_4(Derived2,Derived1,Base,Base)>::value && 177 SameType<DerivedToFront<Derived1BaseDerived1Derived2List>::Result,LOKI_TYPELIST_4(Derived2,Derived1,Derived1,Base)>::value; 178 179 testAssert("DerivedToFront",r,result); 180 181 #else //LOKI_DISABLE_TYPELIST_MACROS 182 183 r=SameType<Append<NullType,NullType>::Result,NullType>::value && 184 SameType<Append<NullType,char>::Result,Seq<char>::Type >::value && 185 SameType<Append<NullType,CharList>::Result,CharList>::value && 186 SameType<Append<CharList,NullType>::Result,CharList>::value && 187 SameType<Append<CharList,int>::Result,Seq<char,int>::Type >::value && 188 SameType<Append<CharList,CharIntDoubleList>::Result,Seq<char,char,int,double>::Type >::value; 189 190 testAssert("Append",r,result); 191 192 r=SameType<Erase<NullType,char>::Result,NullType>::value && 193 SameType<Erase<CharList,char>::Result,NullType>::value && 194 SameType<Erase<CharList,long>::Result,CharList>::value && 195 SameType<Erase<CharIntDoubleList,int>::Result,Seq<char,double>::Type >::value && 196 SameType<Erase<CharIntDoubleList,double>::Result,Seq<char,int>::Type >::value; 197 198 testAssert("Erase",r,result); 199 200 r=SameType<EraseAll<NullType,char>::Result,NullType>::value && 201 SameType<EraseAll<CharList,char>::Result,NullType>::value && 202 SameType<EraseAll<CharList,long>::Result,CharList>::value && 203 SameType<EraseAll<CharIntDoubleList,int>::Result,Seq<char,double>::Type >::value && 204 SameType<EraseAll<CharIntDoubleList,double>::Result,Seq<char,int>::Type >::value && 205 SameType<EraseAll<CharIntDoubleCharList,char>::Result,Seq<int,double>::Type >::value && 206 SameType<EraseAll<CharIntDoubleCharList,int>::Result,Seq<char,double,char>::Type >::value && 207 SameType<EraseAll<CharIntDoubleCharList,double>::Result,Seq<char,int,char>::Type >::value; 208 209 testAssert("EraseAll",r,result); 210 211 r=SameType<NoDuplicates<NullType>::Result,NullType>::value && 212 SameType<NoDuplicates<CharList>::Result,CharList>::value && 213 SameType<NoDuplicates<CharIntDoubleList>::Result,CharIntDoubleList>::value && 214 SameType<NoDuplicates<CharIntDoubleCharList>::Result,CharIntDoubleList>::value; 215 216 testAssert("NoDuplicates",r,result); 217 218 r=SameType<Replace<NullType,char,long>::Result,NullType>::value && 219 SameType<Replace<CharList,char,long>::Result,Seq<long>::Type >::value && 220 SameType<Replace<CharList,int,long>::Result,CharList>::value && 221 SameType<Replace<CharIntDoubleList,char,long>::Result,Seq<long,int,double>::Type >::value && 222 SameType<Replace<CharIntDoubleList,long,char[16]>::Result,CharIntDoubleList>::value && 223 SameType<Replace<CharIntDoubleCharList,char,long>::Result,Seq<long,int,double,char>::Type >::value; 224 225 testAssert("Replace",r,result); 226 227 r=SameType<ReplaceAll<NullType,char,long>::Result,NullType>::value && 228 SameType<ReplaceAll<CharList,char,long>::Result,Seq<long>::Type >::value && 229 SameType<ReplaceAll<CharList,int,long>::Result,CharList>::value && 230 SameType<ReplaceAll<CharIntDoubleList,char,long>::Result,Seq<long,int,double>::Type >::value && 231 SameType<ReplaceAll<CharIntDoubleList,long,char[16]>::Result,CharIntDoubleList>::value && 232 SameType<ReplaceAll<CharIntDoubleCharList,char,long>::Result,Seq<long,int,double,long>::Type >::value; 233 234 testAssert("ReplaceAll",r,result); 235 236 r=SameType<Reverse<NullType>::Result,NullType>::value && 237 SameType<Reverse<CharList>::Result,CharList>::value && 238 SameType<Reverse<CharIntDoubleList>::Result,Seq<double,int,char>::Type >::value; 239 240 testAssert("Reverse",r,result); 241 242 r=SameType<MostDerived<NullType,Base>::Result,Base>::value && 243 SameType<MostDerived<BaseDerived1Derived2List,Base>::Result,Derived2>::value && 244 SameType<MostDerived<BaseDerived1Derived2List,Derived1>::Result,Derived2>::value && 245 SameType<MostDerived<BaseDerived1Derived2List,Derived2>::Result,Derived2>::value && 246 SameType<MostDerived<Derived2Derived1BaseList,Base>::Result,Derived2>::value && 247 SameType<MostDerived<Derived2Derived1BaseList,Derived1>::Result,Derived2>::value && 248 SameType<MostDerived<Derived2Derived1BaseList,Derived2>::Result,Derived2>::value; 249 250 testAssert("MostDerived",r,result); 251 252 r=SameType<DerivedToFront<NullType>::Result,NullType>::value && 253 SameType<DerivedToFront<CharList>::Result,CharList>::value && 254 SameType<DerivedToFront<CharIntDoubleList>::Result,CharIntDoubleList>::value && 255 SameType<DerivedToFront<CharIntDoubleCharList>::Result,CharIntDoubleCharList>::value && 256 SameType<DerivedToFront<BaseDerived1Derived2List>::Result,Derived2Derived1BaseList>::value && 257 SameType<DerivedToFront<Derived2Derived1BaseList>::Result,Derived2Derived1BaseList>::value && 258 SameType<DerivedToFront<BaseDerived1BaseDerived2List>::Result,Seq<Derived2,Derived1,Base,Base>::Type >::value && 259 SameType<DerivedToFront<Derived1BaseDerived1Derived2List>::Result,Seq<Derived2,Derived1,Derived1,Base>::Type >::value; 260 261 testAssert("DerivedToFront",r,result); 262 263 #endif //LOKI_DISABLE_TYPELIST_MACROS 264 265 266 267 #else 268 269 testAssert("Append",false,result,false); 270 testAssert("Erase",false,result,false); 271 testAssert("EraseAll",false,result,false); 272 testAssert("NoDuplicates",false,result,false); 273 testAssert("Replace",false,result,false); 274 testAssert("Reverse",false,result,false); 275 testAssert("MostDerived",false,result,false); 276 testAssert("DerivedToFront",false,result,false); 277 278 #endif 279 280 std::cout << '\n'; 281 } 282 283 private: 284 struct Base { char c; }; 285 struct Derived1 : Base { char c; }; 286 struct Derived2 : Derived1 { char c; }; 287 } typelistTest; 288 #endif 289