btrfs: Attempt to fix GCC2 build.
[haiku.git] / src / tests / ExampleTest.cpp
blob647218b47278b5b1258f82480894e76b6cddefd8
1 #include "ExampleTest.h"
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>
12 ExampleTest::ExampleTest(std::string name)
13 : BThreadedTestCase(name)
14 , fLocker(new BLocker())
18 CppUnit::Test*
19 ExampleTest::Suite() {
20 CppUnit::TestSuite *suite = new CppUnit::TestSuite("Yo");
21 BThreadedTestCaller<ExampleTest> *caller;
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);
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);
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);
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));
49 return suite;
52 const int sleeptime = 10000;
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);
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);
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);
93 void
94 ExampleTest::FailureFunc() {
95 CHK(true == false);