First public contribution.
1 // Copyright (c) 1999-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
18 #include "T_BitFlags.h"
24 _LIT(KTestTitle, "TestBitFlags");
26 RTest TheTest(KTestTitle);
29 void TestBitFlags::TestSetAll()
34 void TestBitFlags::TestClearAll()
36 iTestFlags.ClearAll();
39 void TestBitFlags::TestSetL(TInt aColor)
41 iTestFlags.Set(aColor);
42 if (!(iTestFlags.IsSet(aColor)))
43 User::Leave(KErrGeneral);
46 void TestBitFlags::TestClearL(TInt aColor)
48 iTestFlags.Clear(aColor);
49 if (iTestFlags.IsSet(aColor))
50 User::Leave(KErrGeneral);
53 void TestBitFlags::TestAssign(TInt aColor, TBool aSetOrClear)
55 iTestFlags.Assign(aColor, aSetOrClear);
58 void TestBitFlags::TestToggleL(TInt aColor)
60 TBool isSet = iTestFlags.IsSet(aColor);
61 iTestFlags.Toggle(aColor);
62 if (isSet == iTestFlags.IsSet(aColor))
63 User::Leave(KErrGeneral);
66 TBool TestBitFlags::TestOperator1(TInt aColor) //testing operator []
68 return iTestFlags.operator[](aColor);
71 void TestBitFlags::TestOperator2() // test operator =
73 iFlagA.Set(TestBitFlags::EBlue);
75 TheTest(iFlagB == iFlagA);
77 iFlagB.Set(TestBitFlags::ERed);
78 TheTest(!(iFlagB == iFlagA));
81 void TestBitFlags::TestOperator3() //test operator ==
83 iFlagA.Set(TestBitFlags::EBlue);
84 iFlagB.Set(TestBitFlags::EBlue);
85 TheTest(iFlagA == iFlagB);
87 iFlagB.Set(TestBitFlags::ERed);
88 TheTest(!(iFlagB == iFlagA));
91 TBool TestBitFlags::TestIsSet(TInt aColor)
93 return iTestFlags.IsSet(aColor);
96 TBool TestBitFlags::TestIsClear(TInt aColor)
98 return iTestFlags.IsClear(aColor);
101 TInt TestBitFlags::TestValue()
103 return iTestFlags.Value();
107 @SYMTestCaseID SYSLIB-BAFL-CT-0468
108 @SYMTestCaseDesc Tests the behaviour of bits
109 @SYMTestPriority High
110 @SYMTestActions Tests for bits by setting and clearing
111 @SYMTestExpectedResults Test must not fail
116 TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-BAFL-CT-0468 Pure Set and Clear and related functions only "));
118 TBool checkTestFlags;
121 TRAPD(errCode, test.TestSetL(TestBitFlags::EBlue));
122 TheTest(errCode==KErrNone);
124 checkTestFlags = test.TestIsSet(TestBitFlags::EBlue); //expect 1
125 checkValue = test.TestValue(); //expect 1 (hex)
126 TheTest(checkTestFlags == 1);
127 TheTest(checkValue == 1);
129 TRAP(errCode, test.TestSetL(TestBitFlags::EGreen));
130 TheTest(errCode==KErrNone);
131 checkTestFlags = test.TestIsSet(TestBitFlags::EGreen); //expect 4
132 TheTest(checkTestFlags == 4);
133 checkTestFlags = test.TestIsClear(TestBitFlags::EGreen); //expect 0 - means it hasnt been cleared
134 checkValue = test.TestValue(); //expect 5 (hex)
135 TheTest(checkTestFlags == 0);
136 TheTest(checkValue == 5);
138 TRAP(errCode, test.TestSetL(TestBitFlags::EYellow));
139 TheTest(errCode==KErrNone);
140 checkTestFlags = test.TestIsSet(TestBitFlags::EYellow); //expect 8
141 checkValue = test.TestValue(); //expect d (hex)
142 TheTest(checkTestFlags == 8);
143 TheTest(checkValue == 13);
145 test.TestClearL(TestBitFlags::EGreen);
146 checkTestFlags = test.TestIsClear(TestBitFlags::EGreen); //expect 1
147 checkValue = test.TestValue(); //expect 9 (hex)
148 TheTest(checkTestFlags == 1);
149 TheTest(checkValue == 9);
151 checkTestFlags = test.TestIsClear(TestBitFlags::ERed); //expect 1 - means it has been cleared
152 checkValue = test.TestValue(); //expect 9 (hex)
153 TheTest(checkTestFlags == 1);
154 TheTest(checkValue == 9);
157 checkTestFlags = test.TestIsSet(TestBitFlags::EBlue);
158 TheTest(checkTestFlags == 1);
159 checkTestFlags = test.TestIsSet(TestBitFlags::ERed);
160 TheTest(checkTestFlags == 2);
161 checkTestFlags = test.TestIsSet(TestBitFlags::EGreen);
162 TheTest(checkTestFlags == 4);
163 checkTestFlags = test.TestIsSet(TestBitFlags::EYellow);
164 TheTest(checkTestFlags == 8);
165 checkTestFlags = test.TestIsSet(TestBitFlags::EPurple);
166 TheTest(checkTestFlags == 16);
167 checkTestFlags = test.TestIsSet(TestBitFlags::EBlack);
168 TheTest(checkTestFlags == 32);
169 checkTestFlags = test.TestIsSet(TestBitFlags::EWhite);
170 TheTest(checkTestFlags == 64);
171 checkTestFlags = test.TestIsSet(TestBitFlags::EGrey);
172 TheTest(checkTestFlags == 128);
173 checkValue = test.TestValue(); //expect 0xffffffff (hex)
174 TheTest(checkValue == -1);
177 test.TestClearL(TestBitFlags::EBlue);
178 checkTestFlags = test.TestIsClear(TestBitFlags::EBlue);
179 TheTest(checkTestFlags == 1);
180 test.TestClearL(TestBitFlags::ERed);
181 checkTestFlags = test.TestIsClear(TestBitFlags::ERed);
182 TheTest(checkTestFlags == 1);
183 test.TestClearL(TestBitFlags::EGreen);
184 checkTestFlags = test.TestIsClear(TestBitFlags::EGreen);
185 TheTest(checkTestFlags == 1);
186 test.TestClearL(TestBitFlags::EYellow);
187 checkTestFlags = test.TestIsClear(TestBitFlags::EYellow);
188 TheTest(checkTestFlags == 1);
189 test.TestClearL(TestBitFlags::EPurple);
190 checkTestFlags = test.TestIsClear(TestBitFlags::EPurple);
191 TheTest(checkTestFlags == 1);
192 test.TestClearL(TestBitFlags::EBlack);
193 checkTestFlags = test.TestIsClear(TestBitFlags::EBlack);
194 TheTest(checkTestFlags == 1);
195 test.TestClearL(TestBitFlags::EWhite);
196 checkTestFlags = test.TestIsClear(TestBitFlags::EWhite);
197 TheTest(checkTestFlags == 1);
198 test.TestClearL(TestBitFlags::EGrey);
199 checkTestFlags = test.TestIsClear(TestBitFlags::EGrey);
200 TheTest(checkTestFlags == 1);
201 checkValue = test.TestValue(); //expect 0xffffff00
202 TheTest(checkValue == -256);
205 checkValue = test.TestValue();
206 TheTest(checkValue ==0);
210 @SYMTestCaseID SYSLIB-BAFL-CT-0469
211 @SYMTestCaseDesc Tests the behaviour of bits
212 @SYMTestPriority High
213 @SYMTestActions Tests for bits by reading them back (assign function)
214 @SYMTestExpectedResults Test must not fail
219 TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-BAFL-CT-0469 Assign function "));
221 TBool checkTestFlags;
224 test.TestAssign(TestBitFlags::ERed, 1);
225 checkTestFlags = test.TestIsSet(TestBitFlags::ERed); //expect 2
226 checkValue = test.TestValue(); //expect 2
227 TheTest(checkTestFlags == 2);
228 TheTest(checkValue == 2);
230 test.TestAssign(TestBitFlags::EWhite, 1);
231 checkTestFlags = test.TestIsSet(TestBitFlags::EWhite); //expect 64
232 checkValue = test.TestValue(); //expect 66
233 TheTest(checkTestFlags == 64);
234 TheTest(checkValue == 66);
236 test.TestAssign(TestBitFlags::ERed, 0);
237 checkTestFlags = test.TestIsClear(TestBitFlags::ERed); //expect 1
238 checkValue = test.TestValue(); //expect 64
239 TheTest(checkTestFlags == 1);
240 TheTest(checkValue == 64);
242 test.TestAssign(TestBitFlags::ERed, 3); //test anything other than 1 will activate Set function
243 checkTestFlags = test.TestIsSet(TestBitFlags::ERed); //expect 2
244 checkValue = test.TestValue(); //expect 66
245 TheTest(checkTestFlags == 2);
246 TheTest(checkValue == 66);
250 @SYMTestCaseID SYSLIB-BAFL-CT-0470
251 @SYMTestCaseDesc Tests the behaviour of bits
252 @SYMTestPriority High
253 @SYMTestActions Tests for [] operator
254 @SYMTestExpectedResults Test must not fail
259 TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-BAFL-CT-0470 operator [] "));
262 TBool checkTestFlags;
264 TRAPD(errCode, test.TestSetL(TestBitFlags::EBlue));
265 TheTest(errCode==KErrNone);
267 checkTestFlags = test.TestOperator1(TestBitFlags::ERed); //0
268 TheTest(checkTestFlags == 0);
270 TRAP(errCode, test.TestSetL(TestBitFlags::EYellow));
271 TheTest(errCode==KErrNone);
273 checkTestFlags = test.TestOperator1(TestBitFlags::EGreen); //0
274 TheTest(checkTestFlags == 0);
276 checkTestFlags = test.TestOperator1(TestBitFlags::EBlack); //0
277 TheTest(checkTestFlags == 0);
279 checkTestFlags = test.TestOperator1(TestBitFlags::EYellow); //8
280 TheTest(checkTestFlags == 8);
282 checkTestFlags = test.TestOperator1(TestBitFlags::EBlue); //1
283 TheTest(checkTestFlags == 1);
287 @SYMTestCaseID SYSLIB-BAFL-CT-0471
288 @SYMTestCaseDesc Tests the behaviour of bits
289 @SYMTestPriority High
290 @SYMTestActions Tests for == operator
291 @SYMTestExpectedResults Test must not fail
296 TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-BAFL-CT-0471 Operator== "));
298 test.TestOperator3();
302 @SYMTestCaseID SYSLIB-BAFL-CT-0472
303 @SYMTestCaseDesc Tests the behaviour of bits
304 @SYMTestPriority High
305 @SYMTestActions Tests for toggling the value
306 @SYMTestExpectedResults Test must not fail
311 TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-BAFL-CT-0472 Toggle "));
315 test.TestToggleL(TestBitFlags::EBlue);
316 checkTestFlags = test.TestValue();
317 TheTest(checkTestFlags == 1);
319 test.TestToggleL(TestBitFlags::ERed);
320 checkTestFlags = test.TestValue();
321 TheTest(checkTestFlags == 3);
323 test.TestToggleL(TestBitFlags::EBlue);
324 checkTestFlags = test.TestValue();
325 TheTest(checkTestFlags == 2);
327 test.TestToggleL(TestBitFlags::EGreen);
328 checkTestFlags = test.TestValue();
329 TheTest(checkTestFlags == 6);
331 test.TestToggleL(TestBitFlags::ERed);
332 checkTestFlags = test.TestValue();
333 TheTest(checkTestFlags == 4);
335 test.TestToggleL(TestBitFlags::EGreen);
336 checkTestFlags = test.TestValue();
337 TheTest(checkTestFlags == 0);
339 test.TestToggleL(TestBitFlags::EYellow);
340 checkTestFlags = test.TestValue();
341 TheTest(checkTestFlags == 8);
345 @SYMTestCaseID SYSLIB-BAFL-CT-0473
346 @SYMTestCaseDesc Tests the behaviour of bits
347 @SYMTestPriority High
348 @SYMTestActions Tests for assignment operator
349 @SYMTestExpectedResults Test must not fail
354 TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-BAFL-CT-0473 Operator= "));
356 test.TestOperator2();
361 TTestEnum KEnumArray[] =
363 ETest1, ETest2, ETest3, ETest4, ETest5, ETest6, ETest7, ETest8,
364 ETest9, ETest10, ETest11, ETest12, ETest13, ETest14, ETest15, ETest16,
365 ETest17, ETest18, ETest19, ETest20, ETest21, ETest22, ETest23, ETest24,
366 ETest25, ETest26, ETest27, ETest28, ETest29, ETest30, ETest31, ETest32
370 @SYMTestCaseID SYSLIB-BAFL-CT-3387
371 @SYMTestCaseDesc Tests the behaviour of template class when type is an enum.
372 @SYMTestPriority High
373 @SYMTestActions For each bit test each function in class for when T is an enum.
374 It is important that this test includes the MSB bit because enums
375 are stored as an unsigned int and the MSB is the signed bit.
376 @SYMTestExpectedResults Flags must be set and reset to the expected values
381 TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-BAFL-CT-3387 Tests for DEF102233 "));
385 for(TInt x = 0; x <= 31; x++)
391 TBitFlagsT<TTestEnum> myBitFlagMSB1(KEnumArray[arrayPos]);
392 TheTest(myBitFlagMSB1.IsSet(bitPos));
395 TBitFlagsT<TTestEnum> myBitFlagMSB2(myBitFlagMSB1);
396 TheTest(myBitFlagMSB2.IsSet(bitPos));
399 myBitFlagMSB1.SetAll();
400 TheTest(myBitFlagMSB1.IsSet(bitPos));
403 myBitFlagMSB1.ClearAll();
404 TheTest(!myBitFlagMSB1.IsSet(bitPos));
407 myBitFlagMSB1.Set(bitPos);
408 TheTest(myBitFlagMSB1.IsSet(bitPos));
409 myBitFlagMSB1.Clear(bitPos);
410 TheTest(!myBitFlagMSB1.IsSet(bitPos));
413 myBitFlagMSB1.Assign(bitPos, ETrue);
414 TheTest(myBitFlagMSB1.IsSet(bitPos));
415 myBitFlagMSB1.Assign(bitPos, EFalse);
416 TheTest(!myBitFlagMSB1.IsSet(bitPos));
419 myBitFlagMSB1.Toggle(bitPos);
420 TheTest(myBitFlagMSB1.IsSet(bitPos));
421 myBitFlagMSB1.Toggle(bitPos);
422 TheTest(!myBitFlagMSB1.IsSet(bitPos));
425 TheTest(!myBitFlagMSB1[arrayPos]);
426 myBitFlagMSB1.Set(bitPos);
427 TheTest(myBitFlagMSB1[arrayPos]);
430 myBitFlagMSB2 = myBitFlagMSB1;
431 TheTest(myBitFlagMSB2[arrayPos]);
432 myBitFlagMSB1.Toggle(bitPos);
433 myBitFlagMSB2 = myBitFlagMSB1;
434 TheTest(!myBitFlagMSB2[arrayPos]);
437 TheTest(myBitFlagMSB1 == myBitFlagMSB2);
438 myBitFlagMSB1.Toggle(bitPos);
439 myBitFlagMSB2.Toggle(bitPos);
440 TheTest(myBitFlagMSB1 == myBitFlagMSB2);
441 myBitFlagMSB1.Toggle(bitPos);
442 TheTest(!(myBitFlagMSB1 == myBitFlagMSB2));
445 TheTest(myBitFlagMSB1.IsClear(bitPos));
446 myBitFlagMSB1.Toggle(bitPos);
447 TheTest(myBitFlagMSB1.IsSet(bitPos));
449 // Value and SetValue
450 myBitFlagMSB1.ClearAll();
451 myBitFlagMSB1.SetValue(KEnumArray[arrayPos]);
452 TheTest(myBitFlagMSB1.Value() == KEnumArray[arrayPos]);
454 // Value against a signed integer
455 TInt signedInteger = KEnumArray[arrayPos];
456 TheTest(myBitFlagMSB1.Value() == signedInteger);
458 // Value against an unsigned integer
459 TUint unsignedInteger = KEnumArray[arrayPos];
460 TheTest(myBitFlagMSB1.Value() == unsignedInteger);
463 myBitFlagMSB1.ClearAll();
464 myBitFlagMSB1.iFlags = KEnumArray[arrayPos];
465 TheTest(myBitFlagMSB1.Value() == KEnumArray[arrayPos]);
467 // check MSB was tested
468 TheTest(KEnumArray[arrayPos] == ETest32);
469 TheTest(bitPos == 31);
473 @SYMTestCaseID BASESRVCS-BAFL-CT-4078
474 @SYMTestCaseDesc tests the IsSet API for out of bounds index values.
475 @SYMTestPriority High
476 @SYMTestActions query flag state statinig an index that is out of bound
477 @SYMTestExpectedResults should return EFalse for out of bound values
482 // type is considered as an enum here
483 TheTest.Next(_L(" @SYMTestCaseID:BASESRVCS-BAFL-CT-4078 Tests for DEF130663 "));
487 for(TInt x = 0; x <= 31; x++)
493 TBitFlagsT<TTestEnum> myBitFlag(KEnumArray[arrayPos]);
494 TheTest(myBitFlag.IsSet(bitPos));
496 //clear all and set one paricular bit
497 myBitFlag.ClearAll();
498 myBitFlag.Set(bitPos);
500 // check "out of bounds" values as well
501 // expected behavior is that for all out of bound values the
502 // result should be EFalse for IsSet("out of bounds") call.
503 // IsSet and all the functions directly calling IsSet are being tested here
504 // this test in conjuction with the other cts listed here
505 // should be sufficient to validate the fix
506 for(TInt i = 0; i < 64; i++)
510 TheTest(myBitFlag.IsSet(i));
511 TheTest(!myBitFlag.IsClear(i));
512 TheTest(myBitFlag[i]);
516 TheTest(!myBitFlag.IsSet(i));
517 TheTest(myBitFlag.IsClear(i));
518 TheTest(!myBitFlag[i]);
522 // check MSB was tested
523 TheTest(KEnumArray[arrayPos] == ETest32);
524 TheTest(bitPos == 31);
543 CTrapCleanup* cleanup = CTrapCleanup::New();
547 TheTest.Start(_L("TBITFLAGS "));
550 TRAPD(err, RunTestsL());
551 TheTest(err == KErrNone);