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