Rtos API example

Committer:
marcozecchini
Date:
Sat Feb 23 12:13:36 2019 +0000
Revision:
0:9fca2b23d0ba
final commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
marcozecchini 0:9fca2b23d0ba 1 /* mbed Microcontroller Library
marcozecchini 0:9fca2b23d0ba 2 * Copyright (c) 2017 ARM Limited
marcozecchini 0:9fca2b23d0ba 3 *
marcozecchini 0:9fca2b23d0ba 4 * Licensed under the Apache License, Version 2.0 (the "License");
marcozecchini 0:9fca2b23d0ba 5 * you may not use this file except in compliance with the License.
marcozecchini 0:9fca2b23d0ba 6 * You may obtain a copy of the License at
marcozecchini 0:9fca2b23d0ba 7 *
marcozecchini 0:9fca2b23d0ba 8 * http://www.apache.org/licenses/LICENSE-2.0
marcozecchini 0:9fca2b23d0ba 9 *
marcozecchini 0:9fca2b23d0ba 10 * Unless required by applicable law or agreed to in writing, software
marcozecchini 0:9fca2b23d0ba 11 * distributed under the License is distributed on an "AS IS" BASIS,
marcozecchini 0:9fca2b23d0ba 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
marcozecchini 0:9fca2b23d0ba 13 * See the License for the specific language governing permissions and
marcozecchini 0:9fca2b23d0ba 14 * limitations under the License.
marcozecchini 0:9fca2b23d0ba 15 */
marcozecchini 0:9fca2b23d0ba 16 #include "mbed.h"
marcozecchini 0:9fca2b23d0ba 17 #include "greentea-client/test_env.h"
marcozecchini 0:9fca2b23d0ba 18 #include "unity.h"
marcozecchini 0:9fca2b23d0ba 19 #include "utest.h"
marcozecchini 0:9fca2b23d0ba 20 #include "rtos.h"
marcozecchini 0:9fca2b23d0ba 21
marcozecchini 0:9fca2b23d0ba 22 #if defined(MBED_RTOS_SINGLE_THREAD)
marcozecchini 0:9fca2b23d0ba 23 #error [NOT_SUPPORTED] test not supported
marcozecchini 0:9fca2b23d0ba 24 #endif
marcozecchini 0:9fca2b23d0ba 25
marcozecchini 0:9fca2b23d0ba 26 using namespace utest::v1;
marcozecchini 0:9fca2b23d0ba 27
marcozecchini 0:9fca2b23d0ba 28 #define THREAD_STACK_SIZE 320 /* larger stack cause out of heap memory on some 16kB RAM boards in multi thread test*/
marcozecchini 0:9fca2b23d0ba 29 #define QUEUE_SIZE 16
marcozecchini 0:9fca2b23d0ba 30 #define THREAD_1_ID 1
marcozecchini 0:9fca2b23d0ba 31 #define THREAD_2_ID 2
marcozecchini 0:9fca2b23d0ba 32 #define THREAD_3_ID 3
marcozecchini 0:9fca2b23d0ba 33 #define QUEUE_PUT_DELAY_1 5
marcozecchini 0:9fca2b23d0ba 34 #define QUEUE_PUT_DELAY_2 50
marcozecchini 0:9fca2b23d0ba 35 #define QUEUE_PUT_DELAY_3 100
marcozecchini 0:9fca2b23d0ba 36 #define DATA_BASE 100
marcozecchini 0:9fca2b23d0ba 37
marcozecchini 0:9fca2b23d0ba 38
marcozecchini 0:9fca2b23d0ba 39 typedef struct {
marcozecchini 0:9fca2b23d0ba 40 uint16_t data;
marcozecchini 0:9fca2b23d0ba 41 uint8_t thread_id;
marcozecchini 0:9fca2b23d0ba 42 } mail_t;
marcozecchini 0:9fca2b23d0ba 43
marcozecchini 0:9fca2b23d0ba 44
marcozecchini 0:9fca2b23d0ba 45 template<uint8_t thread_id, uint32_t wait_ms, uint32_t send_count>
marcozecchini 0:9fca2b23d0ba 46 void send_thread(Mail<mail_t, QUEUE_SIZE> *m)
marcozecchini 0:9fca2b23d0ba 47 {
marcozecchini 0:9fca2b23d0ba 48 uint32_t data = thread_id * DATA_BASE;
marcozecchini 0:9fca2b23d0ba 49
marcozecchini 0:9fca2b23d0ba 50 for (uint32_t i = 0; i < send_count; i++) {
marcozecchini 0:9fca2b23d0ba 51 mail_t *mail = m->alloc();
marcozecchini 0:9fca2b23d0ba 52 mail->thread_id = thread_id;
marcozecchini 0:9fca2b23d0ba 53 mail->data = data++;
marcozecchini 0:9fca2b23d0ba 54 m->put(mail);
marcozecchini 0:9fca2b23d0ba 55 Thread::wait(wait_ms);
marcozecchini 0:9fca2b23d0ba 56 }
marcozecchini 0:9fca2b23d0ba 57 }
marcozecchini 0:9fca2b23d0ba 58
marcozecchini 0:9fca2b23d0ba 59 template<uint8_t thread_id, uint32_t queue_size, uint32_t wait_ms>
marcozecchini 0:9fca2b23d0ba 60 void receive_thread(Mail<mail_t, queue_size> *m)
marcozecchini 0:9fca2b23d0ba 61 {
marcozecchini 0:9fca2b23d0ba 62 int result_counter = 0;
marcozecchini 0:9fca2b23d0ba 63 uint32_t data = thread_id * DATA_BASE;
marcozecchini 0:9fca2b23d0ba 64
marcozecchini 0:9fca2b23d0ba 65 Thread::wait(wait_ms);
marcozecchini 0:9fca2b23d0ba 66 for (uint32_t i = 0; i < queue_size; i++) {
marcozecchini 0:9fca2b23d0ba 67 osEvent evt = m->get();
marcozecchini 0:9fca2b23d0ba 68 if (evt.status == osEventMail) {
marcozecchini 0:9fca2b23d0ba 69 mail_t *mail = (mail_t*)evt.value.p;
marcozecchini 0:9fca2b23d0ba 70 const uint8_t id = mail->thread_id;
marcozecchini 0:9fca2b23d0ba 71
marcozecchini 0:9fca2b23d0ba 72 // verify thread id
marcozecchini 0:9fca2b23d0ba 73 TEST_ASSERT_TRUE(id == thread_id);
marcozecchini 0:9fca2b23d0ba 74 // verify sent data
marcozecchini 0:9fca2b23d0ba 75 TEST_ASSERT_TRUE(mail->data == data++);
marcozecchini 0:9fca2b23d0ba 76
marcozecchini 0:9fca2b23d0ba 77 m->free(mail);
marcozecchini 0:9fca2b23d0ba 78 result_counter++;
marcozecchini 0:9fca2b23d0ba 79 }
marcozecchini 0:9fca2b23d0ba 80 }
marcozecchini 0:9fca2b23d0ba 81 TEST_ASSERT_EQUAL(queue_size, result_counter);
marcozecchini 0:9fca2b23d0ba 82 }
marcozecchini 0:9fca2b23d0ba 83
marcozecchini 0:9fca2b23d0ba 84 /** Test single thread Mail usage and order
marcozecchini 0:9fca2b23d0ba 85
marcozecchini 0:9fca2b23d0ba 86 Given mailbox and one additional thread
marcozecchini 0:9fca2b23d0ba 87 When messages are put in to the Mail box by this thread
marcozecchini 0:9fca2b23d0ba 88 Then messages are received in main thread in the same order as was sent and the data sent is valid
marcozecchini 0:9fca2b23d0ba 89 */
marcozecchini 0:9fca2b23d0ba 90 void test_single_thread_order(void)
marcozecchini 0:9fca2b23d0ba 91 {
marcozecchini 0:9fca2b23d0ba 92 uint16_t data = DATA_BASE;
marcozecchini 0:9fca2b23d0ba 93 int result_counter = 0;
marcozecchini 0:9fca2b23d0ba 94 Mail<mail_t, QUEUE_SIZE> mail_box;
marcozecchini 0:9fca2b23d0ba 95
marcozecchini 0:9fca2b23d0ba 96 // mail send thread creation
marcozecchini 0:9fca2b23d0ba 97 Thread thread(osPriorityNormal, THREAD_STACK_SIZE);
marcozecchini 0:9fca2b23d0ba 98 thread.start(callback(send_thread<THREAD_1_ID, QUEUE_PUT_DELAY_1, QUEUE_SIZE>, &mail_box));
marcozecchini 0:9fca2b23d0ba 99
marcozecchini 0:9fca2b23d0ba 100 // wait for some mail to be collected
marcozecchini 0:9fca2b23d0ba 101 Thread::wait(10);
marcozecchini 0:9fca2b23d0ba 102
marcozecchini 0:9fca2b23d0ba 103 for (uint32_t i = 0; i < QUEUE_SIZE; i++) {
marcozecchini 0:9fca2b23d0ba 104 // mail receive (main thread)
marcozecchini 0:9fca2b23d0ba 105 osEvent evt = mail_box.get();
marcozecchini 0:9fca2b23d0ba 106 if (evt.status == osEventMail) {
marcozecchini 0:9fca2b23d0ba 107 mail_t *mail = (mail_t*)evt.value.p;
marcozecchini 0:9fca2b23d0ba 108 const uint8_t id = mail->thread_id;
marcozecchini 0:9fca2b23d0ba 109
marcozecchini 0:9fca2b23d0ba 110 // verify thread id
marcozecchini 0:9fca2b23d0ba 111 TEST_ASSERT_TRUE(id == THREAD_1_ID);
marcozecchini 0:9fca2b23d0ba 112 // verify sent data
marcozecchini 0:9fca2b23d0ba 113 TEST_ASSERT_TRUE(mail->data == data++);
marcozecchini 0:9fca2b23d0ba 114 mail_box.free(mail);
marcozecchini 0:9fca2b23d0ba 115
marcozecchini 0:9fca2b23d0ba 116 result_counter++;
marcozecchini 0:9fca2b23d0ba 117 }
marcozecchini 0:9fca2b23d0ba 118 }
marcozecchini 0:9fca2b23d0ba 119 TEST_ASSERT_EQUAL(QUEUE_SIZE, result_counter);
marcozecchini 0:9fca2b23d0ba 120 }
marcozecchini 0:9fca2b23d0ba 121
marcozecchini 0:9fca2b23d0ba 122 /** Test multi thread Mail usage and order
marcozecchini 0:9fca2b23d0ba 123
marcozecchini 0:9fca2b23d0ba 124 Given mailbox and three additional threads
marcozecchini 0:9fca2b23d0ba 125 When messages are put in to the Mail box by these threads
marcozecchini 0:9fca2b23d0ba 126 Then messages are received in main thread in the same per thread order as was sent and the data sent is valid
marcozecchini 0:9fca2b23d0ba 127 */
marcozecchini 0:9fca2b23d0ba 128 void test_multi_thread_order(void)
marcozecchini 0:9fca2b23d0ba 129 {
marcozecchini 0:9fca2b23d0ba 130 uint16_t data[4] = { 0, DATA_BASE, DATA_BASE * 2, DATA_BASE * 3 };
marcozecchini 0:9fca2b23d0ba 131 int result_counter = 0;
marcozecchini 0:9fca2b23d0ba 132 Mail<mail_t, QUEUE_SIZE> mail_box;
marcozecchini 0:9fca2b23d0ba 133
marcozecchini 0:9fca2b23d0ba 134 // mail send threads creation
marcozecchini 0:9fca2b23d0ba 135 Thread thread1(osPriorityNormal, THREAD_STACK_SIZE);
marcozecchini 0:9fca2b23d0ba 136 Thread thread2(osPriorityNormal, THREAD_STACK_SIZE);
marcozecchini 0:9fca2b23d0ba 137 Thread thread3(osPriorityNormal, THREAD_STACK_SIZE);
marcozecchini 0:9fca2b23d0ba 138 thread1.start(callback(send_thread<THREAD_1_ID, QUEUE_PUT_DELAY_1, 7>, &mail_box));
marcozecchini 0:9fca2b23d0ba 139 thread2.start(callback(send_thread<THREAD_2_ID, QUEUE_PUT_DELAY_2, 5>, &mail_box));
marcozecchini 0:9fca2b23d0ba 140 thread3.start(callback(send_thread<THREAD_3_ID, QUEUE_PUT_DELAY_3, 4>, &mail_box));
marcozecchini 0:9fca2b23d0ba 141
marcozecchini 0:9fca2b23d0ba 142 // wait for some mail to be collected
marcozecchini 0:9fca2b23d0ba 143 Thread::wait(10);
marcozecchini 0:9fca2b23d0ba 144
marcozecchini 0:9fca2b23d0ba 145 for (uint32_t i = 0; i < QUEUE_SIZE; i++) {
marcozecchini 0:9fca2b23d0ba 146 // mail receive (main thread)
marcozecchini 0:9fca2b23d0ba 147 osEvent evt = mail_box.get();
marcozecchini 0:9fca2b23d0ba 148 if (evt.status == osEventMail) {
marcozecchini 0:9fca2b23d0ba 149 mail_t *mail = (mail_t*)evt.value.p;
marcozecchini 0:9fca2b23d0ba 150 const uint8_t id = mail->thread_id;
marcozecchini 0:9fca2b23d0ba 151
marcozecchini 0:9fca2b23d0ba 152 // verify thread id
marcozecchini 0:9fca2b23d0ba 153 TEST_ASSERT_TRUE((id == THREAD_1_ID) || (id == THREAD_2_ID) || (id == THREAD_3_ID));
marcozecchini 0:9fca2b23d0ba 154 // verify sent data
marcozecchini 0:9fca2b23d0ba 155 TEST_ASSERT_TRUE(mail->data == data[id]++);
marcozecchini 0:9fca2b23d0ba 156 mail_box.free(mail);
marcozecchini 0:9fca2b23d0ba 157
marcozecchini 0:9fca2b23d0ba 158 result_counter++;
marcozecchini 0:9fca2b23d0ba 159 }
marcozecchini 0:9fca2b23d0ba 160 }
marcozecchini 0:9fca2b23d0ba 161 TEST_ASSERT_EQUAL(QUEUE_SIZE, result_counter);
marcozecchini 0:9fca2b23d0ba 162 }
marcozecchini 0:9fca2b23d0ba 163
marcozecchini 0:9fca2b23d0ba 164 /** Test multi thread multi Mail usage and order
marcozecchini 0:9fca2b23d0ba 165
marcozecchini 0:9fca2b23d0ba 166 Given 3 mailbox and three additional threads
marcozecchini 0:9fca2b23d0ba 167 When messages are put in to the mail boxes by main thread
marcozecchini 0:9fca2b23d0ba 168 Then messages are received by threads in the same per mail box order as was sent and the data sent is valid
marcozecchini 0:9fca2b23d0ba 169 */
marcozecchini 0:9fca2b23d0ba 170 void test_multi_thread_multi_mail_order(void)
marcozecchini 0:9fca2b23d0ba 171 {
marcozecchini 0:9fca2b23d0ba 172 Mail<mail_t, 4> mail_box[4]; /* mail_box[0] not used */
marcozecchini 0:9fca2b23d0ba 173 uint16_t data[4] = { 0, DATA_BASE, DATA_BASE * 2, DATA_BASE * 3 };
marcozecchini 0:9fca2b23d0ba 174 mail_t *mail;
marcozecchini 0:9fca2b23d0ba 175 uint8_t id;
marcozecchini 0:9fca2b23d0ba 176
marcozecchini 0:9fca2b23d0ba 177 // mail receive threads creation
marcozecchini 0:9fca2b23d0ba 178 Thread thread1(osPriorityNormal, THREAD_STACK_SIZE);
marcozecchini 0:9fca2b23d0ba 179 Thread thread2(osPriorityNormal, THREAD_STACK_SIZE);
marcozecchini 0:9fca2b23d0ba 180 Thread thread3(osPriorityNormal, THREAD_STACK_SIZE);
marcozecchini 0:9fca2b23d0ba 181 thread1.start(callback(receive_thread<THREAD_1_ID, 4, 0>, mail_box + 1));
marcozecchini 0:9fca2b23d0ba 182 thread2.start(callback(receive_thread<THREAD_2_ID, 4, 10>, mail_box + 2));
marcozecchini 0:9fca2b23d0ba 183 thread3.start(callback(receive_thread<THREAD_3_ID, 4, 100>, mail_box + 3));
marcozecchini 0:9fca2b23d0ba 184
marcozecchini 0:9fca2b23d0ba 185 for (uint32_t i = 0; i < 4; i++) {
marcozecchini 0:9fca2b23d0ba 186 id = THREAD_1_ID;
marcozecchini 0:9fca2b23d0ba 187 mail = mail_box[id].alloc();
marcozecchini 0:9fca2b23d0ba 188 mail->thread_id = id;
marcozecchini 0:9fca2b23d0ba 189 mail->data = data[id]++;
marcozecchini 0:9fca2b23d0ba 190 mail_box[id].put(mail);
marcozecchini 0:9fca2b23d0ba 191
marcozecchini 0:9fca2b23d0ba 192 id = THREAD_2_ID;
marcozecchini 0:9fca2b23d0ba 193 mail = mail_box[id].alloc();
marcozecchini 0:9fca2b23d0ba 194 mail->thread_id = id;
marcozecchini 0:9fca2b23d0ba 195 mail->data = data[id]++;
marcozecchini 0:9fca2b23d0ba 196 mail_box[id].put(mail);
marcozecchini 0:9fca2b23d0ba 197
marcozecchini 0:9fca2b23d0ba 198 id = THREAD_3_ID;
marcozecchini 0:9fca2b23d0ba 199 mail = mail_box[id].alloc();
marcozecchini 0:9fca2b23d0ba 200 mail->thread_id = id;
marcozecchini 0:9fca2b23d0ba 201 mail->data = data[id]++;
marcozecchini 0:9fca2b23d0ba 202 mail_box[id].put(mail);
marcozecchini 0:9fca2b23d0ba 203
marcozecchini 0:9fca2b23d0ba 204 Thread::wait(i * 10);
marcozecchini 0:9fca2b23d0ba 205 }
marcozecchini 0:9fca2b23d0ba 206
marcozecchini 0:9fca2b23d0ba 207 thread1.join();
marcozecchini 0:9fca2b23d0ba 208 thread2.join();
marcozecchini 0:9fca2b23d0ba 209 thread3.join();
marcozecchini 0:9fca2b23d0ba 210 }
marcozecchini 0:9fca2b23d0ba 211
marcozecchini 0:9fca2b23d0ba 212 /** Test message memory deallocation with block out of the scope
marcozecchini 0:9fca2b23d0ba 213
marcozecchini 0:9fca2b23d0ba 214 Given an empty mailbox
marcozecchini 0:9fca2b23d0ba 215 When try to free out of the scope memory block
marcozecchini 0:9fca2b23d0ba 216 Then it return appropriate error code
marcozecchini 0:9fca2b23d0ba 217 */
marcozecchini 0:9fca2b23d0ba 218 void test_free_wrong()
marcozecchini 0:9fca2b23d0ba 219 {
marcozecchini 0:9fca2b23d0ba 220 osStatus status;
marcozecchini 0:9fca2b23d0ba 221 Mail<uint32_t, 4> mail_box;
marcozecchini 0:9fca2b23d0ba 222 uint32_t *mail, data;
marcozecchini 0:9fca2b23d0ba 223
marcozecchini 0:9fca2b23d0ba 224 mail = &data;
marcozecchini 0:9fca2b23d0ba 225 status = mail_box.free(mail);
marcozecchini 0:9fca2b23d0ba 226 TEST_ASSERT_EQUAL(osErrorParameter, status);
marcozecchini 0:9fca2b23d0ba 227
marcozecchini 0:9fca2b23d0ba 228 mail = mail_box.alloc();
marcozecchini 0:9fca2b23d0ba 229 TEST_ASSERT_NOT_EQUAL(NULL, mail);
marcozecchini 0:9fca2b23d0ba 230
marcozecchini 0:9fca2b23d0ba 231 mail = &data;
marcozecchini 0:9fca2b23d0ba 232 status = mail_box.free(mail);
marcozecchini 0:9fca2b23d0ba 233 TEST_ASSERT_EQUAL(osErrorParameter, status);
marcozecchini 0:9fca2b23d0ba 234 }
marcozecchini 0:9fca2b23d0ba 235
marcozecchini 0:9fca2b23d0ba 236 /** Test message memory deallocation with null block
marcozecchini 0:9fca2b23d0ba 237
marcozecchini 0:9fca2b23d0ba 238 Given an empty mailbox
marcozecchini 0:9fca2b23d0ba 239 When try to free null ptr
marcozecchini 0:9fca2b23d0ba 240 Then it return appropriate error code
marcozecchini 0:9fca2b23d0ba 241 */
marcozecchini 0:9fca2b23d0ba 242 void test_free_null()
marcozecchini 0:9fca2b23d0ba 243 {
marcozecchini 0:9fca2b23d0ba 244 osStatus status;
marcozecchini 0:9fca2b23d0ba 245 Mail<uint32_t, 4> mail_box;
marcozecchini 0:9fca2b23d0ba 246 uint32_t *mail;
marcozecchini 0:9fca2b23d0ba 247
marcozecchini 0:9fca2b23d0ba 248 mail = NULL;
marcozecchini 0:9fca2b23d0ba 249 status = mail_box.free(mail);
marcozecchini 0:9fca2b23d0ba 250 TEST_ASSERT_EQUAL(osErrorParameter, status);
marcozecchini 0:9fca2b23d0ba 251
marcozecchini 0:9fca2b23d0ba 252 mail = mail_box.alloc();
marcozecchini 0:9fca2b23d0ba 253 TEST_ASSERT_NOT_EQUAL(NULL, mail);
marcozecchini 0:9fca2b23d0ba 254
marcozecchini 0:9fca2b23d0ba 255 mail = NULL;
marcozecchini 0:9fca2b23d0ba 256 status = mail_box.free(mail);
marcozecchini 0:9fca2b23d0ba 257 TEST_ASSERT_EQUAL(osErrorParameter, status);
marcozecchini 0:9fca2b23d0ba 258 }
marcozecchini 0:9fca2b23d0ba 259
marcozecchini 0:9fca2b23d0ba 260 /** Test same message memory deallocation twice
marcozecchini 0:9fca2b23d0ba 261
marcozecchini 0:9fca2b23d0ba 262 Given an empty mailbox
marcozecchini 0:9fca2b23d0ba 263 Then allocate message memory
marcozecchini 0:9fca2b23d0ba 264 When try to free it second time
marcozecchini 0:9fca2b23d0ba 265 Then it return appropriate error code
marcozecchini 0:9fca2b23d0ba 266 */
marcozecchini 0:9fca2b23d0ba 267 void test_free_twice()
marcozecchini 0:9fca2b23d0ba 268 {
marcozecchini 0:9fca2b23d0ba 269 osStatus status;
marcozecchini 0:9fca2b23d0ba 270 Mail<uint32_t, 4> mail_box;
marcozecchini 0:9fca2b23d0ba 271
marcozecchini 0:9fca2b23d0ba 272 uint32_t *mail = mail_box.alloc();
marcozecchini 0:9fca2b23d0ba 273 TEST_ASSERT_NOT_EQUAL(NULL, mail);
marcozecchini 0:9fca2b23d0ba 274
marcozecchini 0:9fca2b23d0ba 275 status = mail_box.free(mail);
marcozecchini 0:9fca2b23d0ba 276 TEST_ASSERT_EQUAL(osOK, status);
marcozecchini 0:9fca2b23d0ba 277
marcozecchini 0:9fca2b23d0ba 278 status = mail_box.free(mail);
marcozecchini 0:9fca2b23d0ba 279 TEST_ASSERT_EQUAL(osErrorResource, status);
marcozecchini 0:9fca2b23d0ba 280 }
marcozecchini 0:9fca2b23d0ba 281
marcozecchini 0:9fca2b23d0ba 282 /** Test get from empty mailbox with timeout set
marcozecchini 0:9fca2b23d0ba 283
marcozecchini 0:9fca2b23d0ba 284 Given an empty mailbox
marcozecchini 0:9fca2b23d0ba 285 When @a get is called on the mailbox with timeout of 50
marcozecchini 0:9fca2b23d0ba 286 Then mailbox returns status of osOK, but no data after specified amount of time
marcozecchini 0:9fca2b23d0ba 287 */
marcozecchini 0:9fca2b23d0ba 288 void test_get_empty_timeout()
marcozecchini 0:9fca2b23d0ba 289 {
marcozecchini 0:9fca2b23d0ba 290 Mail<uint32_t, 4> mail_box;
marcozecchini 0:9fca2b23d0ba 291 Timer timer;
marcozecchini 0:9fca2b23d0ba 292
marcozecchini 0:9fca2b23d0ba 293 timer.start();
marcozecchini 0:9fca2b23d0ba 294 osEvent evt = mail_box.get(50);
marcozecchini 0:9fca2b23d0ba 295 TEST_ASSERT_UINT32_WITHIN(5000, 50000, timer.read_us());
marcozecchini 0:9fca2b23d0ba 296 TEST_ASSERT_EQUAL(osEventTimeout, evt.status);
marcozecchini 0:9fca2b23d0ba 297 }
marcozecchini 0:9fca2b23d0ba 298
marcozecchini 0:9fca2b23d0ba 299 /** Test get from empty mailbox with 0 timeout
marcozecchini 0:9fca2b23d0ba 300
marcozecchini 0:9fca2b23d0ba 301 Given an empty mailbox
marcozecchini 0:9fca2b23d0ba 302 When @a get is called on the mailbox with timeout of 0
marcozecchini 0:9fca2b23d0ba 303 Then mailbox returns status of osOK, but no data
marcozecchini 0:9fca2b23d0ba 304 */
marcozecchini 0:9fca2b23d0ba 305 void test_get_empty_no_timeout()
marcozecchini 0:9fca2b23d0ba 306 {
marcozecchini 0:9fca2b23d0ba 307 Mail<uint32_t, 4> mail_box;
marcozecchini 0:9fca2b23d0ba 308
marcozecchini 0:9fca2b23d0ba 309 osEvent evt = mail_box.get(0);
marcozecchini 0:9fca2b23d0ba 310 TEST_ASSERT_EQUAL(osOK, evt.status);
marcozecchini 0:9fca2b23d0ba 311 }
marcozecchini 0:9fca2b23d0ba 312
marcozecchini 0:9fca2b23d0ba 313 /** Test mail order
marcozecchini 0:9fca2b23d0ba 314
marcozecchini 0:9fca2b23d0ba 315 Given an mailbox for uint32_t values
marcozecchini 0:9fca2b23d0ba 316 Then allocate two mails and put them in to mailbox
marcozecchini 0:9fca2b23d0ba 317 When call @a get it returns previously put mails
marcozecchini 0:9fca2b23d0ba 318 Then mails should be in the same order as put
marcozecchini 0:9fca2b23d0ba 319 */
marcozecchini 0:9fca2b23d0ba 320 void test_order(void)
marcozecchini 0:9fca2b23d0ba 321 {
marcozecchini 0:9fca2b23d0ba 322 osStatus status;
marcozecchini 0:9fca2b23d0ba 323 osEvent evt;
marcozecchini 0:9fca2b23d0ba 324 Mail<int32_t, 4> mail_box;
marcozecchini 0:9fca2b23d0ba 325 const int32_t TEST_VAL1 = 123;
marcozecchini 0:9fca2b23d0ba 326 const int32_t TEST_VAL2 = 456;
marcozecchini 0:9fca2b23d0ba 327
marcozecchini 0:9fca2b23d0ba 328 int32_t *mail1 = mail_box.alloc();
marcozecchini 0:9fca2b23d0ba 329 TEST_ASSERT_NOT_EQUAL(NULL, mail1);
marcozecchini 0:9fca2b23d0ba 330
marcozecchini 0:9fca2b23d0ba 331 *mail1 = TEST_VAL1;
marcozecchini 0:9fca2b23d0ba 332 status = mail_box.put(mail1);
marcozecchini 0:9fca2b23d0ba 333 TEST_ASSERT_EQUAL(osOK, status);
marcozecchini 0:9fca2b23d0ba 334
marcozecchini 0:9fca2b23d0ba 335 int32_t *mail2 = mail_box.alloc();
marcozecchini 0:9fca2b23d0ba 336 TEST_ASSERT_NOT_EQUAL(NULL, mail2);
marcozecchini 0:9fca2b23d0ba 337
marcozecchini 0:9fca2b23d0ba 338 *mail2 = TEST_VAL2;
marcozecchini 0:9fca2b23d0ba 339 status = mail_box.put(mail2);
marcozecchini 0:9fca2b23d0ba 340 TEST_ASSERT_EQUAL(osOK, status);
marcozecchini 0:9fca2b23d0ba 341
marcozecchini 0:9fca2b23d0ba 342
marcozecchini 0:9fca2b23d0ba 343 evt = mail_box.get();
marcozecchini 0:9fca2b23d0ba 344 TEST_ASSERT_EQUAL(evt.status, osEventMail);
marcozecchini 0:9fca2b23d0ba 345
marcozecchini 0:9fca2b23d0ba 346 mail1 = (int32_t*)evt.value.p;
marcozecchini 0:9fca2b23d0ba 347 TEST_ASSERT_EQUAL(TEST_VAL1, *mail1);
marcozecchini 0:9fca2b23d0ba 348
marcozecchini 0:9fca2b23d0ba 349 evt = mail_box.get();
marcozecchini 0:9fca2b23d0ba 350 TEST_ASSERT_EQUAL(evt.status, osEventMail);
marcozecchini 0:9fca2b23d0ba 351
marcozecchini 0:9fca2b23d0ba 352 mail2 = (int32_t*)evt.value.p;
marcozecchini 0:9fca2b23d0ba 353 TEST_ASSERT_EQUAL(TEST_VAL2, *mail2);
marcozecchini 0:9fca2b23d0ba 354
marcozecchini 0:9fca2b23d0ba 355
marcozecchini 0:9fca2b23d0ba 356 status = mail_box.free(mail1);
marcozecchini 0:9fca2b23d0ba 357 TEST_ASSERT_EQUAL(osOK, status);
marcozecchini 0:9fca2b23d0ba 358
marcozecchini 0:9fca2b23d0ba 359 status = mail_box.free(mail2);
marcozecchini 0:9fca2b23d0ba 360 TEST_ASSERT_EQUAL(osOK, status);
marcozecchini 0:9fca2b23d0ba 361 }
marcozecchini 0:9fca2b23d0ba 362
marcozecchini 0:9fca2b23d0ba 363 /** Test Mail box max size limit
marcozecchini 0:9fca2b23d0ba 364
marcozecchini 0:9fca2b23d0ba 365 Given an Mail box with max size of 4 elements
marcozecchini 0:9fca2b23d0ba 366 When call @a alloc four times it returns memory blocks
marcozecchini 0:9fca2b23d0ba 367 Then the memory blocks should be valid
marcozecchini 0:9fca2b23d0ba 368 When call @a alloc one more time it returns memory blocks
marcozecchini 0:9fca2b23d0ba 369 Then the memory blocks should be not valid (NULL - no memory available)
marcozecchini 0:9fca2b23d0ba 370 */
marcozecchini 0:9fca2b23d0ba 371 void test_max_size()
marcozecchini 0:9fca2b23d0ba 372 {
marcozecchini 0:9fca2b23d0ba 373 osStatus status;
marcozecchini 0:9fca2b23d0ba 374 Mail<uint32_t, 4> mail_box;
marcozecchini 0:9fca2b23d0ba 375 const uint32_t TEST_VAL = 123;
marcozecchini 0:9fca2b23d0ba 376
marcozecchini 0:9fca2b23d0ba 377 // 1 OK
marcozecchini 0:9fca2b23d0ba 378 uint32_t *mail1 = mail_box.alloc();
marcozecchini 0:9fca2b23d0ba 379 TEST_ASSERT_NOT_EQUAL(NULL, mail1);
marcozecchini 0:9fca2b23d0ba 380
marcozecchini 0:9fca2b23d0ba 381 // 2 OK
marcozecchini 0:9fca2b23d0ba 382 uint32_t *mail2 = mail_box.alloc();
marcozecchini 0:9fca2b23d0ba 383 TEST_ASSERT_NOT_EQUAL(NULL, mail2);
marcozecchini 0:9fca2b23d0ba 384
marcozecchini 0:9fca2b23d0ba 385 // 3 OK
marcozecchini 0:9fca2b23d0ba 386 uint32_t *mail3 = mail_box.alloc();
marcozecchini 0:9fca2b23d0ba 387 TEST_ASSERT_NOT_EQUAL(NULL, mail3);
marcozecchini 0:9fca2b23d0ba 388
marcozecchini 0:9fca2b23d0ba 389 // 4 OK
marcozecchini 0:9fca2b23d0ba 390 uint32_t *mail4 = mail_box.alloc();
marcozecchini 0:9fca2b23d0ba 391 TEST_ASSERT_NOT_EQUAL(NULL, mail4);
marcozecchini 0:9fca2b23d0ba 392
marcozecchini 0:9fca2b23d0ba 393 // 5 KO
marcozecchini 0:9fca2b23d0ba 394 uint32_t *mail5 = mail_box.alloc();
marcozecchini 0:9fca2b23d0ba 395 TEST_ASSERT_EQUAL(NULL, mail5);
marcozecchini 0:9fca2b23d0ba 396
marcozecchini 0:9fca2b23d0ba 397
marcozecchini 0:9fca2b23d0ba 398 status = mail_box.free(mail1);
marcozecchini 0:9fca2b23d0ba 399 TEST_ASSERT_EQUAL(osOK, status);
marcozecchini 0:9fca2b23d0ba 400
marcozecchini 0:9fca2b23d0ba 401 status = mail_box.free(mail2);
marcozecchini 0:9fca2b23d0ba 402 TEST_ASSERT_EQUAL(osOK, status);
marcozecchini 0:9fca2b23d0ba 403
marcozecchini 0:9fca2b23d0ba 404 status = mail_box.free(mail3);
marcozecchini 0:9fca2b23d0ba 405 TEST_ASSERT_EQUAL(osOK, status);
marcozecchini 0:9fca2b23d0ba 406
marcozecchini 0:9fca2b23d0ba 407 status = mail_box.free(mail4);
marcozecchini 0:9fca2b23d0ba 408 TEST_ASSERT_EQUAL(osOK, status);
marcozecchini 0:9fca2b23d0ba 409 }
marcozecchini 0:9fca2b23d0ba 410
marcozecchini 0:9fca2b23d0ba 411 /** Test mailbox of T type data
marcozecchini 0:9fca2b23d0ba 412
marcozecchini 0:9fca2b23d0ba 413 Given an mailbox with T memory block type
marcozecchini 0:9fca2b23d0ba 414 When allocate/put/get/free memory block
marcozecchini 0:9fca2b23d0ba 415 Then all operations should succeed
marcozecchini 0:9fca2b23d0ba 416 */
marcozecchini 0:9fca2b23d0ba 417 template<typename T>
marcozecchini 0:9fca2b23d0ba 418 void test_data_type(void)
marcozecchini 0:9fca2b23d0ba 419 {
marcozecchini 0:9fca2b23d0ba 420 osStatus status;
marcozecchini 0:9fca2b23d0ba 421 Mail<T, 4> mail_box;
marcozecchini 0:9fca2b23d0ba 422 const T TEST_VAL = 123;
marcozecchini 0:9fca2b23d0ba 423
marcozecchini 0:9fca2b23d0ba 424 T *mail = mail_box.alloc();
marcozecchini 0:9fca2b23d0ba 425 TEST_ASSERT_NOT_EQUAL(NULL, mail);
marcozecchini 0:9fca2b23d0ba 426
marcozecchini 0:9fca2b23d0ba 427 *mail = TEST_VAL;
marcozecchini 0:9fca2b23d0ba 428 status = mail_box.put(mail);
marcozecchini 0:9fca2b23d0ba 429 TEST_ASSERT_EQUAL(osOK, status);
marcozecchini 0:9fca2b23d0ba 430
marcozecchini 0:9fca2b23d0ba 431 osEvent evt = mail_box.get();
marcozecchini 0:9fca2b23d0ba 432 TEST_ASSERT_EQUAL(evt.status, osEventMail);
marcozecchini 0:9fca2b23d0ba 433
marcozecchini 0:9fca2b23d0ba 434 mail = (T*)evt.value.p;
marcozecchini 0:9fca2b23d0ba 435 TEST_ASSERT_EQUAL(TEST_VAL, *mail);
marcozecchini 0:9fca2b23d0ba 436
marcozecchini 0:9fca2b23d0ba 437
marcozecchini 0:9fca2b23d0ba 438 status = mail_box.free(mail);
marcozecchini 0:9fca2b23d0ba 439 TEST_ASSERT_EQUAL(osOK, status);
marcozecchini 0:9fca2b23d0ba 440 }
marcozecchini 0:9fca2b23d0ba 441
marcozecchini 0:9fca2b23d0ba 442 /** Test calloc - memory block allocation with resetting
marcozecchini 0:9fca2b23d0ba 443
marcozecchini 0:9fca2b23d0ba 444 Given an empty Mail box
marcozecchini 0:9fca2b23d0ba 445 When call @a calloc it returns allocated memory block
marcozecchini 0:9fca2b23d0ba 446 Then the memory block should be valid and filled with zeros
marcozecchini 0:9fca2b23d0ba 447 */
marcozecchini 0:9fca2b23d0ba 448 void test_calloc()
marcozecchini 0:9fca2b23d0ba 449 {
marcozecchini 0:9fca2b23d0ba 450 Mail<uint32_t, 1> mail_box;
marcozecchini 0:9fca2b23d0ba 451
marcozecchini 0:9fca2b23d0ba 452 uint32_t *mail = mail_box.calloc();
marcozecchini 0:9fca2b23d0ba 453 TEST_ASSERT_NOT_EQUAL(NULL, mail);
marcozecchini 0:9fca2b23d0ba 454 TEST_ASSERT_EQUAL(0, *mail);
marcozecchini 0:9fca2b23d0ba 455 }
marcozecchini 0:9fca2b23d0ba 456
marcozecchini 0:9fca2b23d0ba 457 /** Test mail empty
marcozecchini 0:9fca2b23d0ba 458
marcozecchini 0:9fca2b23d0ba 459 Given a mail of uint32_t data
marcozecchini 0:9fca2b23d0ba 460 before data is inserted the mail should be empty
marcozecchini 0:9fca2b23d0ba 461 after data is inserted the mail shouldn't be empty
marcozecchini 0:9fca2b23d0ba 462 */
marcozecchini 0:9fca2b23d0ba 463 void test_mail_empty()
marcozecchini 0:9fca2b23d0ba 464 {
marcozecchini 0:9fca2b23d0ba 465 Mail<mail_t, 1> m;
marcozecchini 0:9fca2b23d0ba 466
marcozecchini 0:9fca2b23d0ba 467 mail_t *mail = m.alloc();
marcozecchini 0:9fca2b23d0ba 468
marcozecchini 0:9fca2b23d0ba 469 TEST_ASSERT_EQUAL(true, m.empty());
marcozecchini 0:9fca2b23d0ba 470
marcozecchini 0:9fca2b23d0ba 471 m.put(mail);
marcozecchini 0:9fca2b23d0ba 472
marcozecchini 0:9fca2b23d0ba 473 TEST_ASSERT_EQUAL(false, m.empty());
marcozecchini 0:9fca2b23d0ba 474 }
marcozecchini 0:9fca2b23d0ba 475
marcozecchini 0:9fca2b23d0ba 476 /** Test mail empty
marcozecchini 0:9fca2b23d0ba 477
marcozecchini 0:9fca2b23d0ba 478 Given a mail of uint32_t data with size of 1
marcozecchini 0:9fca2b23d0ba 479 before data is inserted the mail shouldn't be full
marcozecchini 0:9fca2b23d0ba 480 after data is inserted the mail should be full
marcozecchini 0:9fca2b23d0ba 481 */
marcozecchini 0:9fca2b23d0ba 482 void test_mail_full()
marcozecchini 0:9fca2b23d0ba 483 {
marcozecchini 0:9fca2b23d0ba 484 Mail<mail_t, 1> m;
marcozecchini 0:9fca2b23d0ba 485
marcozecchini 0:9fca2b23d0ba 486 mail_t *mail = m.alloc();
marcozecchini 0:9fca2b23d0ba 487
marcozecchini 0:9fca2b23d0ba 488 TEST_ASSERT_EQUAL(false, m.full());
marcozecchini 0:9fca2b23d0ba 489
marcozecchini 0:9fca2b23d0ba 490 m.put(mail);
marcozecchini 0:9fca2b23d0ba 491
marcozecchini 0:9fca2b23d0ba 492 TEST_ASSERT_EQUAL(true, m.full());
marcozecchini 0:9fca2b23d0ba 493 }
marcozecchini 0:9fca2b23d0ba 494
marcozecchini 0:9fca2b23d0ba 495 utest::v1::status_t test_setup(const size_t number_of_cases)
marcozecchini 0:9fca2b23d0ba 496 {
marcozecchini 0:9fca2b23d0ba 497 GREENTEA_SETUP(10, "default_auto");
marcozecchini 0:9fca2b23d0ba 498 return verbose_test_setup_handler(number_of_cases);
marcozecchini 0:9fca2b23d0ba 499 }
marcozecchini 0:9fca2b23d0ba 500
marcozecchini 0:9fca2b23d0ba 501 Case cases[] = {
marcozecchini 0:9fca2b23d0ba 502 Case("Test calloc", test_calloc),
marcozecchini 0:9fca2b23d0ba 503 Case("Test message type uint8", test_data_type<uint8_t>),
marcozecchini 0:9fca2b23d0ba 504 Case("Test message type uint16", test_data_type<uint16_t>),
marcozecchini 0:9fca2b23d0ba 505 Case("Test message type uint32", test_data_type<uint32_t>),
marcozecchini 0:9fca2b23d0ba 506 Case("Test mailbox max size", test_max_size),
marcozecchini 0:9fca2b23d0ba 507 Case("Test message send order", test_order),
marcozecchini 0:9fca2b23d0ba 508 Case("Test get with timeout on empty mailbox", test_get_empty_timeout),
marcozecchini 0:9fca2b23d0ba 509 Case("Test get without timeout on empty mailbox", test_get_empty_no_timeout),
marcozecchini 0:9fca2b23d0ba 510 Case("Test message free twice", test_free_twice),
marcozecchini 0:9fca2b23d0ba 511 Case("Test null message free", test_free_null),
marcozecchini 0:9fca2b23d0ba 512 Case("Test invalid message free", test_free_wrong),
marcozecchini 0:9fca2b23d0ba 513 Case("Test message send/receive single thread and order", test_single_thread_order),
marcozecchini 0:9fca2b23d0ba 514 Case("Test message send/receive multi-thread and per thread order", test_multi_thread_order),
marcozecchini 0:9fca2b23d0ba 515 Case("Test message send/receive multi-thread, multi-Mail and per thread order", test_multi_thread_multi_mail_order),
marcozecchini 0:9fca2b23d0ba 516 Case("Test mail empty", test_mail_empty),
marcozecchini 0:9fca2b23d0ba 517 Case("Test mail full", test_mail_full)
marcozecchini 0:9fca2b23d0ba 518 };
marcozecchini 0:9fca2b23d0ba 519
marcozecchini 0:9fca2b23d0ba 520 Specification specification(test_setup, cases);
marcozecchini 0:9fca2b23d0ba 521
marcozecchini 0:9fca2b23d0ba 522 int main()
marcozecchini 0:9fca2b23d0ba 523 {
marcozecchini 0:9fca2b23d0ba 524 return !Harness::run(specification);
marcozecchini 0:9fca2b23d0ba 525 }