1 #include "ExampleTest.h" 2 3 #include <TestUtils.h> 4 #include <ThreadedTestCaller.h> 5 #include <cppunit/Test.h> 6 #include <cppunit/TestCaller.h> 7 #include <cppunit/TestSuite.h> 8 #include <iostream> 9 #include <kernel/OS.h> 10 #include <stdio.h> 11 12 13 ExampleTest::ExampleTest(std::string name) 14 : BThreadedTestCase(name), fLocker(new BLocker()) 15 { 16 } 17 18 19 CppUnit::Test* 20 ExampleTest::Suite() 21 { 22 CppUnit::TestSuite* suite = new CppUnit::TestSuite("Yo"); 23 BThreadedTestCaller<ExampleTest>* caller; 24 25 // Add a multithreaded test 26 ExampleTest* test 27 = new ExampleTest("This name is never used, just so you know :-)"); 28 caller = new BThreadedTestCaller<ExampleTest>( 29 "ExampleTests::MultiThreaded Test #1", test); 30 caller->addThread("A", &ExampleTest::TestFunc1); 31 caller->addThread("B", &ExampleTest::TestFunc2); 32 caller->addThread("C", &ExampleTest::TestFunc3); 33 suite->addTest(caller); 34 35 // And another 36 caller = new BThreadedTestCaller<ExampleTest>( 37 "ExampleTests::MultiThreaded Test #2"); 38 caller->addThread("Thread1", &ExampleTest::TestFunc1); 39 caller->addThread("Thread2", &ExampleTest::TestFunc1); 40 caller->addThread("Thread3", &ExampleTest::TestFunc1); 41 suite->addTest(caller); 42 43 // And one that fails, if you're so inclined 44 caller = new BThreadedTestCaller<ExampleTest>( 45 "ExampleTests::MultiThreaded Failing Test"); 46 caller->addThread("GoodThread1", &ExampleTest::TestFunc1); 47 caller->addThread("GoodThread2", &ExampleTest::TestFunc2); 48 caller->addThread("BadThread", &ExampleTest::FailureFunc); 49 suite->addTest(caller); 50 51 // And some single threaded ones 52 suite->addTest(new CppUnit::TestCaller<ExampleTest>( 53 "ExampleTests::SingleThreaded Test #1", &ExampleTest::TestFunc1)); 54 suite->addTest(new CppUnit::TestCaller<ExampleTest>( 55 "ExampleTests::SingleThreaded Test #2", &ExampleTest::TestFunc2)); 56 57 return suite; 58 } 59 60 61 const int sleeptime = 10000; 62 63 64 void 65 ExampleTest::TestFunc1() 66 { 67 for (int i = 0; i < 10; i++) { 68 // Get the lock and do our business 69 NextSubTest(); 70 fLocker->Lock(); 71 fNum += 10; 72 fLocker->Unlock(); 73 snooze(sleeptime); 74 // Outputf("(1:%d)", i); 75 } 76 } 77 78 79 void 80 ExampleTest::TestFunc2() 81 { 82 for (int i = 0; i < 13; i++) { 83 // Get the lock and do our business 84 NextSubTest(); 85 fLocker->Lock(); 86 fNum *= 2; 87 fLocker->Unlock(); 88 snooze(sleeptime); 89 // Outputf("(2:%d)", i); 90 } 91 } 92 93 94 void 95 ExampleTest::TestFunc3() 96 { 97 for (int i = 0; i < 15; i++) { 98 // Get the lock and do our business 99 NextSubTest(); 100 fLocker->Lock(); 101 fNum += 10; 102 fLocker->Unlock(); 103 snooze(sleeptime); 104 // Outputf("(3:%d)", i); 105 } 106 } 107 108 109 void 110 ExampleTest::FailureFunc() 111 { 112 CHK(true == false); 113 } 114