summaryrefslogtreecommitdiff
path: root/FreeRTOS/Test/CMock/queue/generic/queue_create_static_utest.c
blob: 4ebbfbb6f6be8a251b7db5c503adc790a7e03ec0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
 * FreeRTOS V202107.00
 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * https://www.FreeRTOS.org
 * https://github.com/FreeRTOS
 *
 */
/*! @file queue_create_static_utest.c */

/* C runtime includes. */
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>

#include "../queue_utest_common.h"

/* Queue includes */
#include "FreeRTOS.h"
#include "FreeRTOSConfig.h"
#include "queue.h"

/* ============================  GLOBAL VARIABLES =========================== */

/* ==========================  CALLBACK FUNCTIONS =========================== */

/* ============================= Unity Fixtures ============================= */

void setUp( void )
{
    commonSetUp();
}

void tearDown( void )
{
    commonTearDown();
}

void suiteSetUp()
{
    commonSuiteSetUp();
}

int suiteTearDown( int numFailures )
{
    return commonSuiteTearDown( numFailures );
}

/* ==========================  Helper functions =========================== */


static void test_long_queue( QueueHandle_t xQueue,
                             uint32_t maxItems )
{
    /* Veify that queue is empty */
    TEST_ASSERT_EQUAL( 0, uxQueueMessagesWaiting( xQueue ) );

    queue_common_add_sequential_to_queue( xQueue, maxItems );

    /* Veify that queue is full */
    TEST_ASSERT_EQUAL( 0, uxQueueSpacesAvailable( xQueue ) );

    queue_common_receive_sequential_from_queue( xQueue, maxItems, maxItems, 0 );

    /* Veify that queue is empty */
    TEST_ASSERT_EQUAL( 0, uxQueueMessagesWaiting( xQueue ) );
}


/* ==========================  Test Cases =========================== */

/**
 * @brief xQueueCreateStatic with a NULL pointer for queueStorage
 * @coverage xQueueGenericCreateStatic
 */
void test_macro_xQueueCreateStatic_null_QueueStorage_fail( void )
{
    /* Expect that xQueueCreate will assert */
    fakeAssertExpectFail();

    StaticQueue_t queueBuffer;
    QueueHandle_t xQueue = xQueueCreateStatic( MAX_QUEUE_ITEMS, sizeof( uint32_t ), NULL, &queueBuffer );

    /* Validate the queue handle */
    TEST_ASSERT_EQUAL( NULL, xQueue );

    TEST_ASSERT_EQUAL( true, fakeAssertGetFlagAndClear() );
}

/**
 * @brief xQueueCreateStatic with a NULL pointer for queueBuffer
 * @coverage xQueueGenericCreateStatic
 */
void test_macro_xQueueCreateStatic_null_queueBuffer_fail( void )
{
    /* Expect that xQueueCreate will assert */
    fakeAssertExpectFail();

    uint32_t queueStorage[ MAX_QUEUE_ITEMS ];
    QueueHandle_t xQueue = INVALID_PTR;

    xQueue = xQueueCreateStatic( MAX_QUEUE_ITEMS, sizeof( uint32_t ), ( void * ) queueStorage, NULL );

    /* Validate that the queue handle is NULL */
    TEST_ASSERT_EQUAL( NULL, xQueue );

    /* Check that configASSERT was called twice */
    fakeAssertVerifyNumAssertsAndClear( 2 );
}

/**
 * @brief Test xQueueCreateStatic with a NULL buffer, uxQueueLength=1, uxItemSize=0
 * @details This configuration is equivalent to a binary semaphore.
 * @coverage xQueueGenericCreateStatic
 */
void test_macro_xQueueCreateStatic_nullQueueStorage_oneItem_zeroLength( void )
{
    StaticQueue_t queueBuffer;
    QueueHandle_t xQueue = xQueueCreateStatic( 1, 0, NULL, &queueBuffer );

    /* validate returned queue handle */
    TEST_ASSERT_NOT_EQUAL( NULL, xQueue );

    /* Veify that new queue is empty */
    TEST_ASSERT_EQUAL( 0, uxQueueMessagesWaiting( xQueue ) );

    /* Valdiate that the queue has 1 space remaining */
    TEST_ASSERT_EQUAL( 1, uxQueueSpacesAvailable( xQueue ) );

    /* Send a test value */
    TEST_ASSERT_EQUAL( pdTRUE, xQueueSend( xQueue, NULL, 0 ) );

    /* Test receive */
    TEST_ASSERT_EQUAL( pdTRUE, xQueueReceive( xQueue, NULL, 0 ) );

    vQueueDelete( xQueue );
}

/**
 * @brief Test xQueueCreateStatic with uxQueueLength=0, uxItemSize=0
 * @details This configuration is invalid and causes a configASSERT.
 * @coverage xQueueGenericCreateStatic
 */
void test_macro_xQueueCreateStatic_validQueueStorage_zeroItem_zeroLength( void )
{
    StaticQueue_t queueBuffer;

    /* Expect that xQueueCreateStatic will assert because a zero length queue is invalid */
    fakeAssertExpectFail();
    QueueHandle_t xQueue = xQueueCreateStatic( 0, 0, NULL, &queueBuffer );

    /* verify that configASSERT was called */
    TEST_ASSERT_EQUAL( true, fakeAssertGetFlagAndClear() );

    /* validate returned queue handle */
    TEST_ASSERT_EQUAL( NULL, xQueue );
}

/**
 * @brief Test xQueueCreateStatic with a valid buffer, uxQueueLength=1, uxItemSize=0
 * @details This configuration is invalid and causes a configASSERT.
 * @coverage xQueueGenericCreateStatic
 */
void test_macro_xQueueCreateStatic_validQueueStorage_oneItem_zeroLength( void )
{
    StaticQueue_t queueBuffer;
    uint32_t queueData;

    /* Expect that xQueueCreateStatic will assert because data storage is
     *   prohibited for a zero itemLength queue */
    fakeAssertExpectFail();
    QueueHandle_t xQueue = xQueueCreateStatic( 1, 0, ( void * ) &queueData, &queueBuffer );

    /* verify that configASSERT was called */
    TEST_ASSERT_EQUAL( true, fakeAssertGetFlagAndClear() );

    /* validate returned queue handle */
    TEST_ASSERT_EQUAL( NULL, xQueue );
}

/**
 * @brief xQueueCreateStatic with a large queue.
 * @coverage xQueueGenericCreateStatic
 */
void test_macro_xQueueCreateStatic_large( void )
{
    uint32_t queueStorage[ MAX_QUEUE_ITEMS ];
    StaticQueue_t queueBuffer;
    QueueHandle_t xQueue = xQueueCreateStatic( MAX_QUEUE_ITEMS, sizeof( uint32_t ), ( void * ) queueStorage, &queueBuffer );

    test_long_queue( xQueue, MAX_QUEUE_ITEMS );
    vQueueDelete( xQueue );
}