Skip to content

Commit ebd9f3a

Browse files
add some fake semaphore support to the library
1 parent 0d06ca5 commit ebd9f3a

File tree

7 files changed

+180
-12
lines changed

7 files changed

+180
-12
lines changed

cpputest-for-freertos-lib/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ add_library(cpputest-for-freertos-lib
4141
src/cpputest_freertos_assert.cpp
4242
src/cpputest_freertos_timers.cpp
4343
src/cpputest_main.cpp
44+
src/cpputest_freertos_semaphore.cpp
4445
)
4546

4647
add_subdirectory(tests)

cpputest-for-freertos-lib/port/include/portmacro.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ typedef uint64_t TickType_t;
9797

9898
#endif /* if ( configNUMBER_OF_CORES == 1 ) */
9999

100-
extern void vPortYield( void );
101-
#define portYIELD() vPortYield()
100+
#define portYIELD() do {} while(0)
101+
#define portYIELD_FROM_ISR(x) do {} while(0)
102102

103103
/* Task function macros as described on the FreeRTOS.org WEB site. */
104104
#define portTASK_FUNCTION_PROTO( vFunction, pvParameters ) void vFunction( void * pvParameters )
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
#ifndef CPPUTEST_FOR_FREERTOS_LIB_CPPUTEST_FREERTOS_FAKE_QUEUE_HPP
3+
#define CPPUTEST_FOR_FREERTOS_LIB_CPPUTEST_FREERTOS_FAKE_QUEUE_HPP
4+
5+
#include <deque>
6+
#include <vector>
7+
#include "FreeRTOS.h"
8+
9+
typedef struct QueueDefinition
10+
{
11+
UBaseType_t queueLength = {};
12+
UBaseType_t itemSize = {};
13+
uint8_t queueType = {};
14+
std::deque<std::vector<uint8_t>> queue = {};
15+
} FakeQueue;
16+
17+
#endif //CPPUTEST_FOR_FREERTOS_LIB_CPPUTEST_FREERTOS_FAKE_QUEUE_HPP

cpputest-for-freertos-lib/src/cpputest_freertos_queue.cpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,11 @@
2424
///***************************************************************************
2525
/// @endcond
2626

27-
#include <deque>
28-
#include <vector>
27+
#include "cpputest_freertos_fake_queue.hpp"
2928
#include <cstring>
3029
#include "FreeRTOS.h"
3130
#include "queue.h"
3231

33-
typedef struct QueueDefinition
34-
{
35-
UBaseType_t queueLength = {};
36-
UBaseType_t itemSize = {};
37-
uint8_t queueType = {};
38-
std::deque<std::vector<uint8_t>> queue = {};
39-
} FakeQueue;
40-
4132
extern "C" QueueHandle_t xQueueGenericCreate(const UBaseType_t queueLength,
4233
const UBaseType_t itemSize,
4334
const uint8_t queueType)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include "cpputest_freertos_fake_queue.hpp"
2+
#include <cstring>
3+
#include "queue.h"
4+
#include "semphr.h"
5+
6+
extern "C" BaseType_t xQueueSemaphoreTake(QueueHandle_t queue, TickType_t ticks)
7+
{
8+
(void) ticks; //ignore in cpputest fake semaphore
9+
10+
uint64_t dummy;
11+
return xQueueReceive(queue, &dummy, ticks);
12+
}
13+
14+
15+
extern "C" BaseType_t xQueueGiveFromISR(QueueHandle_t queue,
16+
BaseType_t * const pxHigherPriorityTaskWoken)
17+
{
18+
(void)pxHigherPriorityTaskWoken;
19+
return xSemaphoreGive(queue);
20+
}
21+
22+
extern "C" QueueHandle_t xQueueCreateCountingSemaphore(const UBaseType_t maxCount,
23+
const UBaseType_t initialCount)
24+
{
25+
QueueHandle_t sema = xQueueCreate(maxCount, 0);
26+
if (sema == nullptr)
27+
{
28+
return nullptr;
29+
}
30+
31+
for (int i = 0; i < initialCount; ++i)
32+
{
33+
xSemaphoreGive(sema);
34+
}
35+
36+
return sema;
37+
}

cpputest-for-freertos-lib/tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ set(TEST_SOURCES
55
cpputest_freertos_timers_tests.cpp
66
cpputest_freertos_queue_tests.cpp
77
cpputest_freertos_task_tests.cpp
8+
cpputest_freertos_semaphore_tests.cpp
89
)
910

1011
# this include expects TEST_SOURCES and TEST_APP_NAME to be
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
#include "FreeRTOS.h"
2+
#include "semphr.h"
3+
#include "CppUTest/TestHarness.h"
4+
5+
TEST_GROUP(SemaphoreTests)
6+
{
7+
SemaphoreHandle_t mSemaUnderTest;
8+
9+
void setup() final
10+
{
11+
}
12+
13+
void teardown() final
14+
{
15+
if (mSemaUnderTest != nullptr)
16+
{
17+
vSemaphoreDelete(mSemaUnderTest);
18+
}
19+
}
20+
21+
void CreateBinarySemaphore()
22+
{
23+
mSemaUnderTest = xSemaphoreCreateBinary();
24+
CHECK_TRUE(mSemaUnderTest != nullptr);
25+
}
26+
27+
void CreateCountingSemaphore(uint32_t count = 10, uint32_t initial = 0)
28+
{
29+
mSemaUnderTest = xSemaphoreCreateCounting(count, initial);
30+
CHECK_TRUE(mSemaUnderTest != nullptr);
31+
}
32+
};
33+
34+
TEST(SemaphoreTests, can_create_a_binary_semaphore)
35+
{
36+
CreateBinarySemaphore();
37+
}
38+
39+
TEST(SemaphoreTests, can_read_binary_semaphore_count)
40+
{
41+
CreateBinarySemaphore();
42+
43+
auto count = uxSemaphoreGetCount(mSemaUnderTest);
44+
CHECK_EQUAL(0, count);
45+
}
46+
47+
TEST(SemaphoreTests, can_read_binary_semaphore_count_after_give)
48+
{
49+
CreateBinarySemaphore();
50+
CHECK_EQUAL(0, uxSemaphoreGetCount(mSemaUnderTest));
51+
52+
xSemaphoreGive(mSemaUnderTest);
53+
CHECK_EQUAL(1, uxSemaphoreGetCount(mSemaUnderTest));
54+
}
55+
56+
TEST(SemaphoreTests, binary_semaphore_count_is_one_after_multiple_gives)
57+
{
58+
CreateBinarySemaphore();
59+
CHECK_EQUAL(0, uxSemaphoreGetCount(mSemaUnderTest));
60+
CHECK_EQUAL(pdTRUE, xSemaphoreGive(mSemaUnderTest));
61+
CHECK_EQUAL(pdFALSE, xSemaphoreGive(mSemaUnderTest));
62+
CHECK_EQUAL(pdFALSE, xSemaphoreGive(mSemaUnderTest));
63+
CHECK_EQUAL(1, uxSemaphoreGetCount(mSemaUnderTest));
64+
}
65+
66+
TEST(SemaphoreTests, binary_semaphore_count_is_zero_after_give_then_take)
67+
{
68+
CreateBinarySemaphore();
69+
CHECK_EQUAL(0, uxSemaphoreGetCount(mSemaUnderTest));
70+
71+
xSemaphoreGive(mSemaUnderTest);
72+
CHECK_EQUAL(1, uxSemaphoreGetCount(mSemaUnderTest));
73+
74+
CHECK_EQUAL(pdTRUE, xSemaphoreTake(mSemaUnderTest, 1000));
75+
CHECK_EQUAL(0, uxSemaphoreGetCount(mSemaUnderTest));
76+
}
77+
78+
TEST(SemaphoreTests, binary_semaphore_take_rtn_false_when_not_yet_signaled)
79+
{
80+
CreateBinarySemaphore();
81+
CHECK_EQUAL(0, uxSemaphoreGetCount(mSemaUnderTest));
82+
CHECK_EQUAL(pdFALSE, xSemaphoreTake(mSemaUnderTest, 1000));
83+
}
84+
85+
TEST(SemaphoreTests, can_create_a_counting_semaphore)
86+
{
87+
CreateCountingSemaphore();
88+
}
89+
90+
TEST(SemaphoreTests, counting_semaphore_honors_initial_count)
91+
{
92+
CreateCountingSemaphore(10, 3);
93+
CHECK_EQUAL(3, uxSemaphoreGetCount(mSemaUnderTest));
94+
}
95+
96+
TEST(SemaphoreTests, counting_semaphore_honors_initial_count_when_zero)
97+
{
98+
CreateCountingSemaphore(10, 0);
99+
CHECK_EQUAL(0, uxSemaphoreGetCount(mSemaUnderTest));
100+
}
101+
102+
TEST(SemaphoreTests, counting_semaphore_rtns_false_when_exceed_give_max)
103+
{
104+
CreateCountingSemaphore(3, 0);
105+
CHECK_EQUAL(pdTRUE, xSemaphoreGive(mSemaUnderTest));
106+
CHECK_EQUAL(pdTRUE, xSemaphoreGive(mSemaUnderTest));
107+
CHECK_EQUAL(pdTRUE, xSemaphoreGive(mSemaUnderTest));
108+
CHECK_EQUAL(pdFALSE, xSemaphoreGive(mSemaUnderTest));
109+
}
110+
111+
TEST(SemaphoreTests, counting_semaphore_give_take_match)
112+
{
113+
CreateCountingSemaphore(3, 0);
114+
CHECK_EQUAL(pdTRUE, xSemaphoreGive(mSemaUnderTest));
115+
CHECK_EQUAL(pdTRUE, xSemaphoreGive(mSemaUnderTest));
116+
CHECK_EQUAL(pdTRUE, xSemaphoreGive(mSemaUnderTest));
117+
CHECK_EQUAL(pdTRUE, xSemaphoreTake(mSemaUnderTest, 1000));
118+
CHECK_EQUAL(pdTRUE, xSemaphoreTake(mSemaUnderTest, 1000));
119+
CHECK_EQUAL(pdTRUE, xSemaphoreTake(mSemaUnderTest, 1000));
120+
CHECK_EQUAL(pdFALSE, xSemaphoreTake(mSemaUnderTest, 1000));
121+
}

0 commit comments

Comments
 (0)