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