Debian Codegraph @

tests/CppUTest/SimpleMutexTest.cpp

raw
1 /*
2 * Copyright (c) 2014, Michael Feathers, James Grenning, Bas Vodde and Chen YewMing
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the <organization> nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include "CppUTest/TestHarness.h"
29 #include "CppUTest/SimpleMutex.h"
30 #include "CppUTest/PlatformSpecificFunctions.h"
31
32 static int mutexCreateCount = 0;
33 static int mutexLockCount = 0;
34 static int mutexUnlockCount = 0;
35 static int mutexDestroyCount = 0;
36
37 static PlatformSpecificMutex StubMutexCreate(void)
38 {
39 mutexCreateCount++;
40 return NULLPTR;
41 }
42
43 static void StubMutexLock(PlatformSpecificMutex)
44 {
45 mutexLockCount++;
46 }
47
48 static void StubMutexUnlock(PlatformSpecificMutex)
49 {
50 mutexUnlockCount++;
51 }
52
53 static void StubMutexDestroy(PlatformSpecificMutex)
54 {
55 mutexDestroyCount++;
56 }
57
58
59
60 TEST_GROUP(SimpleMutexTest)
61 {
62 void setup()
63 {
64 UT_PTR_SET(PlatformSpecificMutexCreate, StubMutexCreate);
65 UT_PTR_SET(PlatformSpecificMutexLock, StubMutexLock);
66 UT_PTR_SET(PlatformSpecificMutexUnlock, StubMutexUnlock);
67 UT_PTR_SET(PlatformSpecificMutexDestroy, StubMutexDestroy);
68
69 mutexCreateCount = 0;
70 mutexDestroyCount = 0;
71 mutexLockCount = 0;
72 mutexUnlockCount = 0;
73 }
74
75 void teardown()
76 {
77 }
78 };
79
80 TEST(SimpleMutexTest, CreateAndDestroy)
81 {
82 {
83 SimpleMutex mtx;
84 }
85
86 CHECK_EQUAL(1, mutexCreateCount);
87 CHECK_EQUAL(1, mutexDestroyCount);
88 CHECK_EQUAL(0, mutexLockCount);
89 CHECK_EQUAL(0, mutexUnlockCount);
90 }
91
92 TEST(SimpleMutexTest, LockUnlockTest)
93 {
94 {
95 SimpleMutex mtx;
96 mtx.Lock();
97 mtx.Unlock();
98 }
99
100 CHECK_EQUAL(1, mutexCreateCount);
101 CHECK_EQUAL(1, mutexLockCount);
102 CHECK_EQUAL(1, mutexUnlockCount);
103 CHECK_EQUAL(1, mutexDestroyCount);
104 }