xref: /haiku/src/tests/kits/support/blocker/ConstructionTest1.cpp (revision c90684742e7361651849be4116d0e5de3a817194)
1 /*
2 	$Id: ConstructionTest1.cpp 301 2002-07-18 05:32:00Z tylerdauwalder $
3 
4 	This file implements a test class for testing BLocker construction
5 	functionality.  It checks the "Construction 1", "Construction 2" and
6 	"Sem" use cases.  It does so by testing all the documented constructors
7 	and uses the Sem() member function to confirm that the name and style
8 	were set correctly.
9 
10 	*/
11 
12 
13 #include "ConstructionTest1.h"
14 #include <cppunit/TestCaller.h>
15 #include <OS.h>
16 #include <Locker.h>
17 
18 
19 /*
20  *  Method:  ConstructionTest1::ConstructionTest1()
21  *   Descr:  This is the constructor for this class.
22  */
23 
24 
25 	ConstructionTest1::ConstructionTest1(std::string name) :
26 		LockerTestCase(name, true)
27 {
28 	}
29 
30 
31 /*
32  *  Method:  ConstructionTest1::~ConstructionTest1()
33  *   Descr:  This is the desctructor for this BLocker test class.
34  */
35 
36 
37 	ConstructionTest1::~ConstructionTest1()
38 {
39 	}
40 
41 
42 /*
43  *  Method:  ConstructionTest1::NameMatches()
44  *   Descr:  This member checks that the semaphore owned by the lock
45  *           passed in has the name passed in.
46  */
47 
48 bool
49 	ConstructionTest1::NameMatches(const char *name,
50 									       BLocker *lockerArg)
51 {
52 	sem_info theSemInfo;
53 
54 	assert(get_sem_info(lockerArg->Sem(), &theSemInfo) == B_OK);
55 	return(strcmp(name, theSemInfo.name) == 0);
56 }
57 
58 
59 /*
60  *  Method:  ConstructionTest1::IsBenaphore()
61  *   Descr:  This member attempts to confirm that the BLocker passed in
62  *           is a benaphore or a semaphore style locker.  It returns true
63  *           if it is a benaphore, false if it is a semaphore.  An
64  *           assertion is raised if an error occurs.
65  */
66 
67 bool
68 	ConstructionTest1::IsBenaphore(BLocker *lockerArg)
69 {
70 	int32 semCount;
71 
72 	assert(get_sem_count(lockerArg->Sem(), &semCount) == B_OK);
73 	switch (semCount) {
74 		case 0: return(true);
75 				break;
76 		case 1: return(false);
77 				break;
78 		default:
79 				// This should not happen.  The semaphore count should be
80 				// 0 for a benaphore, 1 for a semaphore.  No other value
81 				// is legal in this case.
82 				assert(false);
83 				break;
84 		}
85 	return(false);
86 	}
87 
88 
89 /*
90  *  Method:  ConstructionTest1::PerformTest()
91  *   Descr:  This member function is used to test each of the constructors
92  *           for the BLocker.  The resulting BLocker is tested to show
93  *           that the BLocker was constructed correctly.
94  */
95 
96 void ConstructionTest1::PerformTest(void)
97 {
98 	NextSubTest();
99 	assert(NameMatches("some BLocker", theLocker));
100 	assert(IsBenaphore(theLocker));
101 
102 	NextSubTest();
103 	BLocker locker1("test string");
104 	assert(NameMatches("test string", &locker1));
105 	assert(IsBenaphore(&locker1));
106 
107 	NextSubTest();
108 	BLocker locker2(false);
109 	assert(NameMatches("some BLocker", &locker2));
110 	assert(!IsBenaphore(&locker2));
111 
112 	NextSubTest();
113 	BLocker locker3(true);
114 	assert(NameMatches("some BLocker", &locker3));
115 	assert(IsBenaphore(&locker3));
116 
117 	NextSubTest();
118 	BLocker locker4("test string", false);
119 	assert(NameMatches("test string", &locker4));
120 	assert(!IsBenaphore(&locker4));
121 
122 	NextSubTest();
123 	BLocker locker5("test string", true);
124 	assert(NameMatches("test string", &locker5));
125 	assert(IsBenaphore(&locker5));
126 }
127 
128 
129 /*
130  *  Method:  ConstructionTest1::suite()
131  *   Descr:  This static member function returns a threaded test caller for
132  *           performing "ConstructionTest1".  The threaded test caller
133  *           only has a single thread pointint to the PerformTest() member
134  *           function of this class.
135  */
136 
137 CppUnit::Test *ConstructionTest1::suite(void)
138 {
139 	typedef CppUnit::TestCaller <ConstructionTest1 >
140 		ConstructionTest1Caller;
141 
142 	return new ConstructionTest1Caller("BLocker::Construction Test", &ConstructionTest1::PerformTest);
143 }
144 
145 
146 
147