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