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