Skip to content

Commit 7673ccf

Browse files
enable queue sets (work in progress)
1 parent 405c71b commit 7673ccf

File tree

8 files changed

+296
-1
lines changed

8 files changed

+296
-1
lines changed

cpputest-for-freertos-lib/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ set(FREERTOS_KERNEL_PATH ${CMS_FREERTOS_KERNEL_TOP_DIR} CACHE INTERNAL "")
3838
add_library(cpputest-for-freertos-lib
3939
src/cpputest_for_freertos_task.cpp
4040
src/cpputest_for_freertos_queue.cpp
41+
src/cpputest_for_freertos_queue_set.cpp
4142
src/cpputest_for_freertos_assert.cpp
4243
src/cpputest_for_freertos_timers.cpp
4344
src/cpputest_main.cpp
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/// @brief Support methods to help with unit testing for FreeRTOS, memory allocation
2+
/// related support, such as unique_ptr types for allocated queues, etc.
3+
/// @ingroup
4+
/// @cond
5+
///***************************************************************************
6+
///
7+
/// Copyright (C) 2024 Matthew Eshleman. All rights reserved.
8+
///
9+
/// This program is open source software: you can redistribute it and/or
10+
/// modify it under the terms of the GNU General Public License as published
11+
/// by the Free Software Foundation, either version 3 of the License, or
12+
/// (at your option) any later version.
13+
///
14+
/// Alternatively, upon written permission from Matthew Eshleman, this program
15+
/// may be distributed and modified under the terms of a Commercial
16+
/// License. For further details, see the Contact Information below.
17+
///
18+
/// Contact Information:
19+
/// Matthew Eshleman
20+
/// https://covemountainsoftware.com
21+
22+
///***************************************************************************
23+
/// @endcond
24+
#ifndef CPPUTEST_FOR_FREERTOS_LIB_CPPUTEST_FOR_FREERTOS_MEMORY_HPP
25+
#define CPPUTEST_FOR_FREERTOS_LIB_CPPUTEST_FOR_FREERTOS_MEMORY_HPP
26+
27+
#include "FreeRTOS.h"
28+
#include "queue.h"
29+
#include <memory>
30+
#include <functional>
31+
32+
namespace cms {
33+
namespace test {
34+
35+
struct FreeRTOSQueueDeleter
36+
{
37+
void operator()(struct QueueDefinition * handle)
38+
{
39+
if (handle != nullptr)
40+
{
41+
vQueueDelete(handle);
42+
}
43+
}
44+
};
45+
46+
using unique_queue = std::unique_ptr<struct QueueDefinition, FreeRTOSQueueDeleter>;
47+
} //namespace test
48+
} //namespace cms
49+
50+
#endif //CPPUTEST_FOR_FREERTOS_LIB_CPPUTEST_FOR_FREERTOS_MEMORY_HPP

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ void cmsAssertCalled( const char * pcFile, unsigned long ulLine );
594594
#define configUSE_MUTEXES 1
595595
#define configUSE_RECURSIVE_MUTEXES 1
596596
#define configUSE_COUNTING_SEMAPHORES 1
597-
#define configUSE_QUEUE_SETS 0
597+
#define configUSE_QUEUE_SETS 1
598598
#define configUSE_APPLICATION_TASK_TAG 0
599599

600600
/* Set the following INCLUDE_* constants to 1 to incldue the named API function,

cpputest-for-freertos-lib/src/cpputest_for_freertos_fake_queue.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ typedef struct QueueDefinition
1414
std::deque<std::vector<uint8_t>> queue = {};
1515
uint64_t recursiveCallCount = {};
1616
const char * registryName = nullptr;
17+
struct QueueDefinition * queueSetContainer = nullptr;
1718
} FakeQueue;
1819

1920
namespace cms {

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,11 @@ extern "C" BaseType_t xQueueGenericSend(QueueHandle_t queue,
139139
configASSERT(true == false);
140140
}
141141

142+
if (queue->queueSetContainer != nullptr)
143+
{
144+
xQueueGenericSend(queue->queueSetContainer, &queue, ticks, queueSEND_TO_BACK);
145+
}
146+
142147
return pdTRUE;
143148
}
144149

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/// @brief Provides an implementation of a fake FreeRTOS queue set
2+
/// cpputest-for-freertos-lib assumes that a queue set should be
3+
/// functional, i.e. not a mock.
4+
///
5+
/// @ingroup
6+
/// @cond
7+
///***************************************************************************
8+
///
9+
/// Copyright (C) 2024 Matthew Eshleman. All rights reserved.
10+
///
11+
/// This program is open source software: you can redistribute it and/or
12+
/// modify it under the terms of the GNU General Public License as published
13+
/// by the Free Software Foundation, either version 3 of the License, or
14+
/// (at your option) any later version.
15+
///
16+
/// Alternatively, upon written permission from Matthew Eshleman, this program
17+
/// may be distributed and modified under the terms of a Commercial
18+
/// License. For further details, see the Contact Information below.
19+
///
20+
/// Contact Information:
21+
/// Matthew Eshleman
22+
/// https://covemountainsoftware.com
23+
24+
///***************************************************************************
25+
/// @endcond
26+
27+
#include "cpputest_for_freertos_fake_queue.hpp"
28+
#include "FreeRTOS.h"
29+
#include "queue.h"
30+
31+
extern "C" QueueSetHandle_t xQueueCreateSet(const UBaseType_t eventQueueLength)
32+
{
33+
return xQueueGenericCreate(eventQueueLength, sizeof(QueueHandle_t), queueQUEUE_TYPE_SET);
34+
}
35+
36+
extern "C" BaseType_t xQueueAddToSet(QueueSetMemberHandle_t itemToAdd, QueueSetHandle_t set)
37+
{
38+
auto fakeItemToAdd = static_cast<FakeQueue *>(itemToAdd);
39+
auto fakeSet = static_cast<FakeQueue *>(set);
40+
41+
configASSERT(fakeItemToAdd != nullptr);
42+
configASSERT(fakeSet != nullptr);
43+
44+
if ((fakeItemToAdd->queueSetContainer != nullptr) ||
45+
(!fakeItemToAdd->queue.empty()))
46+
{
47+
return pdFAIL;
48+
}
49+
else
50+
{
51+
fakeItemToAdd->queueSetContainer = fakeSet;
52+
return pdPASS;
53+
}
54+
}
55+
56+
extern "C" BaseType_t xQueueRemoveFromSet(QueueSetMemberHandle_t itemToRemove, QueueSetHandle_t set)
57+
{
58+
auto fakeItemToRemove = static_cast<FakeQueue *>(itemToRemove);
59+
60+
configASSERT(fakeItemToRemove != nullptr);
61+
configASSERT(set != nullptr);
62+
63+
if ((fakeItemToRemove->queueSetContainer != set) ||
64+
(!fakeItemToRemove->queue.empty()))
65+
{
66+
return pdFAIL;
67+
}
68+
else
69+
{
70+
fakeItemToRemove->queueSetContainer = nullptr;
71+
return pdPASS;
72+
}
73+
}
74+
75+
extern "C" QueueSetMemberHandle_t xQueueSelectFromSet(QueueSetHandle_t queueSet, const TickType_t ticksToWait)
76+
{
77+
QueueSetMemberHandle_t rtnItem = nullptr;
78+
79+
auto result = xQueueReceive(queueSet, &rtnItem, ticksToWait );
80+
if (result == pdTRUE)
81+
{
82+
return rtnItem;
83+
}
84+
85+
return nullptr;
86+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ set(TEST_SOURCES
44
cpputest_for_freertos_assert_tests.cpp
55
cpputest_for_freertos_timers_tests.cpp
66
cpputest_for_freertos_queue_tests.cpp
7+
cpputest_for_freertos_queue_set_tests.cpp
78
cpputest_for_freertos_task_tests.cpp
89
cpputest_for_freertos_semaphore_tests.cpp
910
cpputest_for_freertos_mutex_tests.cpp
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
/// @brief Tests of CppUTest for FreeRTOS queue sets.
2+
/// @ingroup
3+
/// @cond
4+
///***************************************************************************
5+
///
6+
/// Copyright (C) 2024 Matthew Eshleman. All rights reserved.
7+
///
8+
/// This program is open source software: you can redistribute it and/or
9+
/// modify it under the terms of the GNU General Public License as published
10+
/// by the Free Software Foundation, either version 3 of the License, or
11+
/// (at your option) any later version.
12+
///
13+
/// Alternatively, upon written permission from Matthew Eshleman, this program
14+
/// may be distributed and modified under the terms of a Commercial
15+
/// License. For further details, see the Contact Information below.
16+
///
17+
/// Contact Information:
18+
/// Matthew Eshleman
19+
/// https://covemountainsoftware.com
20+
21+
///***************************************************************************
22+
/// @endcond
23+
24+
#include "FreeRTOS.h"
25+
#include "queue.h"
26+
#include "cpputest_for_freertos_memory.hpp"
27+
28+
//must be last
29+
#include "CppUTest/TestHarness.h"
30+
31+
TEST_GROUP(QueueSetTests)
32+
{
33+
cms::test::unique_queue mUnderTest = nullptr;
34+
35+
void setup() final
36+
{
37+
}
38+
39+
void teardown() final
40+
{
41+
}
42+
43+
void CreateSetUnderTest(UBaseType_t length)
44+
{
45+
auto set = xQueueCreateSet(length);
46+
mUnderTest = cms::test::unique_queue(set);
47+
CHECK_TRUE(mUnderTest != nullptr);
48+
}
49+
50+
static cms::test::unique_queue CreateQueue(UBaseType_t len, UBaseType_t itemSize)
51+
{
52+
auto rtn = xQueueCreate(len, itemSize);
53+
CHECK_TRUE(rtn != nullptr);
54+
cms::test::unique_queue queue(rtn);
55+
return queue;
56+
}
57+
58+
cms::test::unique_queue CreateQueueAndAddToUnderTestSet(UBaseType_t len, UBaseType_t itemSize) const
59+
{
60+
auto created = xQueueCreate(len, itemSize);
61+
CHECK_TRUE(created != nullptr);
62+
63+
auto rtn = xQueueAddToSet(created, mUnderTest.get());
64+
CHECK_EQUAL(pdPASS, rtn);
65+
66+
cms::test::unique_queue queue(created);
67+
return queue;
68+
}
69+
};
70+
71+
TEST(QueueSetTests, can_create_a_queue_set)
72+
{
73+
CreateSetUnderTest(2);
74+
}
75+
76+
TEST(QueueSetTests, can_add_a_queue_to_a_set_and_remove_from_set)
77+
{
78+
CreateSetUnderTest(2);
79+
80+
auto queue = CreateQueue(2, 2);
81+
auto rtn = xQueueAddToSet(queue.get(), mUnderTest.get());
82+
CHECK_EQUAL(pdPASS, rtn);
83+
84+
rtn = xQueueRemoveFromSet(queue.get(), mUnderTest.get());
85+
CHECK_EQUAL(pdPASS, rtn);
86+
}
87+
88+
TEST(QueueSetTests, adding_a_queue_with_events_to_a_set_fails)
89+
{
90+
const int testValue = 234;
91+
CreateSetUnderTest(2);
92+
auto queue = CreateQueue(2, sizeof(int));
93+
auto rtn = xQueueSendToBack(queue.get(), &testValue, portMAX_DELAY);
94+
CHECK_EQUAL(pdTRUE, rtn);
95+
96+
rtn = xQueueAddToSet(queue.get(), mUnderTest.get());
97+
CHECK_EQUAL(pdFAIL, rtn);
98+
}
99+
100+
TEST(QueueSetTests, adding_a_queue_already_added_to_a_set_fails)
101+
{
102+
CreateSetUnderTest(2);
103+
auto queue = CreateQueue(2, sizeof(int));
104+
auto rtn = xQueueAddToSet(queue.get(), mUnderTest.get());
105+
CHECK_EQUAL(pdPASS, rtn);
106+
107+
//add it again should fail
108+
rtn = xQueueAddToSet(queue.get(), mUnderTest.get());
109+
CHECK_EQUAL(pdFAIL, rtn);
110+
}
111+
112+
TEST(QueueSetTests, can_add_a_queue_to_a_set_and_fails_to_remove_from_another_set)
113+
{
114+
CreateSetUnderTest(2);
115+
cms::test::unique_queue otherSet(xQueueCreateSet(2));
116+
CHECK_TRUE(otherSet != nullptr);
117+
118+
auto queue = CreateQueueAndAddToUnderTestSet(2, 2);
119+
120+
auto rtn = xQueueRemoveFromSet(queue.get(), otherSet.get());
121+
CHECK_EQUAL(pdFAIL, rtn);
122+
}
123+
124+
TEST(QueueSetTests, select_from_set_will_not_block_and_return_null_if_no_events)
125+
{
126+
CreateSetUnderTest(2);
127+
auto queue = CreateQueueAndAddToUnderTestSet(2, sizeof(int));
128+
129+
auto selectResult = xQueueSelectFromSet(mUnderTest.get(), portMAX_DELAY);
130+
CHECK_EQUAL(nullptr, selectResult);
131+
}
132+
133+
TEST(QueueSetTests, select_from_set_will_return_expected_queue_with_event)
134+
{
135+
const uint16_t testValue = 4321;
136+
137+
CreateSetUnderTest(2);
138+
auto queue = CreateQueueAndAddToUnderTestSet(2, sizeof(testValue));
139+
140+
auto rtn = xQueueSendToBack(queue.get(), &testValue, 1000);
141+
CHECK_EQUAL(pdTRUE, rtn);
142+
143+
auto selectResult = xQueueSelectFromSet(mUnderTest.get(), portMAX_DELAY);
144+
CHECK_TRUE(selectResult != nullptr);
145+
CHECK_EQUAL(queue.get(), selectResult);
146+
147+
uint16_t receivedValue = 0;
148+
rtn = xQueueReceive(selectResult, &receivedValue, portMAX_DELAY);
149+
CHECK_EQUAL(pdTRUE, rtn);
150+
CHECK_EQUAL(testValue, receivedValue);
151+
}

0 commit comments

Comments
 (0)