Marco Zecchini
/
Example_RTOS
Rtos API example
mbed-os/TESTS/mbedmicro-rtos-mbed/queue/main.cpp@0:9fca2b23d0ba, 2019-02-23 (annotated)
- Committer:
- marcozecchini
- Date:
- Sat Feb 23 12:13:36 2019 +0000
- Revision:
- 0:9fca2b23d0ba
final commit
Who changed what in which revision?
User | Revision | Line number | New 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 512 |
marcozecchini | 0:9fca2b23d0ba | 29 | #define TEST_UINT_MSG 0xDEADBEEF |
marcozecchini | 0:9fca2b23d0ba | 30 | #define TEST_UINT_MSG2 0xE1EE7 |
marcozecchini | 0:9fca2b23d0ba | 31 | #define TEST_TIMEOUT 50 |
marcozecchini | 0:9fca2b23d0ba | 32 | |
marcozecchini | 0:9fca2b23d0ba | 33 | template <uint32_t ms> |
marcozecchini | 0:9fca2b23d0ba | 34 | void thread_put_uint_msg(Queue<uint32_t, 1> *q) |
marcozecchini | 0:9fca2b23d0ba | 35 | { |
marcozecchini | 0:9fca2b23d0ba | 36 | Thread::wait(ms); |
marcozecchini | 0:9fca2b23d0ba | 37 | osStatus stat = q->put((uint32_t*) TEST_UINT_MSG); |
marcozecchini | 0:9fca2b23d0ba | 38 | TEST_ASSERT_EQUAL(osOK, stat); |
marcozecchini | 0:9fca2b23d0ba | 39 | } |
marcozecchini | 0:9fca2b23d0ba | 40 | |
marcozecchini | 0:9fca2b23d0ba | 41 | template <uint32_t ms, uint32_t val> |
marcozecchini | 0:9fca2b23d0ba | 42 | void thread_get_uint_msg(Queue<uint32_t, 1> *q) |
marcozecchini | 0:9fca2b23d0ba | 43 | { |
marcozecchini | 0:9fca2b23d0ba | 44 | Thread::wait(ms); |
marcozecchini | 0:9fca2b23d0ba | 45 | osEvent evt = q->get(); |
marcozecchini | 0:9fca2b23d0ba | 46 | TEST_ASSERT_EQUAL(osEventMessage, evt.status); |
marcozecchini | 0:9fca2b23d0ba | 47 | TEST_ASSERT_EQUAL(val, evt.value.v); |
marcozecchini | 0:9fca2b23d0ba | 48 | } |
marcozecchini | 0:9fca2b23d0ba | 49 | |
marcozecchini | 0:9fca2b23d0ba | 50 | /** Test pass uint msg |
marcozecchini | 0:9fca2b23d0ba | 51 | |
marcozecchini | 0:9fca2b23d0ba | 52 | Given a queue for uint32_t messages with one slot |
marcozecchini | 0:9fca2b23d0ba | 53 | When a uin32_t value is inserted into the queue |
marcozecchini | 0:9fca2b23d0ba | 54 | and a message is extracted from the queue |
marcozecchini | 0:9fca2b23d0ba | 55 | Then the extracted message is the same as previously inserted message |
marcozecchini | 0:9fca2b23d0ba | 56 | */ |
marcozecchini | 0:9fca2b23d0ba | 57 | void test_pass_uint() |
marcozecchini | 0:9fca2b23d0ba | 58 | { |
marcozecchini | 0:9fca2b23d0ba | 59 | Queue<uint32_t, 1> q; |
marcozecchini | 0:9fca2b23d0ba | 60 | osStatus stat = q.put((uint32_t*)TEST_UINT_MSG); |
marcozecchini | 0:9fca2b23d0ba | 61 | TEST_ASSERT_EQUAL(osOK, stat); |
marcozecchini | 0:9fca2b23d0ba | 62 | |
marcozecchini | 0:9fca2b23d0ba | 63 | osEvent evt = q.get(); |
marcozecchini | 0:9fca2b23d0ba | 64 | TEST_ASSERT_EQUAL(osEventMessage, evt.status); |
marcozecchini | 0:9fca2b23d0ba | 65 | TEST_ASSERT_EQUAL(TEST_UINT_MSG, evt.value.v); |
marcozecchini | 0:9fca2b23d0ba | 66 | } |
marcozecchini | 0:9fca2b23d0ba | 67 | |
marcozecchini | 0:9fca2b23d0ba | 68 | /** Test pass uint msg twice |
marcozecchini | 0:9fca2b23d0ba | 69 | |
marcozecchini | 0:9fca2b23d0ba | 70 | Given a queue for uint32_t messages with one slot |
marcozecchini | 0:9fca2b23d0ba | 71 | When a uin32_t value is inserted into the queue |
marcozecchini | 0:9fca2b23d0ba | 72 | and a message is extracted from the queue |
marcozecchini | 0:9fca2b23d0ba | 73 | and the procedure is repeated with different message |
marcozecchini | 0:9fca2b23d0ba | 74 | Then the extracted message is the same as previously inserted message for both iterations |
marcozecchini | 0:9fca2b23d0ba | 75 | |
marcozecchini | 0:9fca2b23d0ba | 76 | */ |
marcozecchini | 0:9fca2b23d0ba | 77 | void test_pass_uint_twice() |
marcozecchini | 0:9fca2b23d0ba | 78 | { |
marcozecchini | 0:9fca2b23d0ba | 79 | Queue<uint32_t, 1> q; |
marcozecchini | 0:9fca2b23d0ba | 80 | osStatus stat = q.put((uint32_t*)TEST_UINT_MSG); |
marcozecchini | 0:9fca2b23d0ba | 81 | TEST_ASSERT_EQUAL(osOK, stat); |
marcozecchini | 0:9fca2b23d0ba | 82 | |
marcozecchini | 0:9fca2b23d0ba | 83 | osEvent evt = q.get(); |
marcozecchini | 0:9fca2b23d0ba | 84 | TEST_ASSERT_EQUAL(osEventMessage, evt.status); |
marcozecchini | 0:9fca2b23d0ba | 85 | TEST_ASSERT_EQUAL(TEST_UINT_MSG, evt.value.v); |
marcozecchini | 0:9fca2b23d0ba | 86 | |
marcozecchini | 0:9fca2b23d0ba | 87 | stat = q.put((uint32_t*)TEST_UINT_MSG2); |
marcozecchini | 0:9fca2b23d0ba | 88 | TEST_ASSERT_EQUAL(osOK, stat); |
marcozecchini | 0:9fca2b23d0ba | 89 | |
marcozecchini | 0:9fca2b23d0ba | 90 | evt = q.get(); |
marcozecchini | 0:9fca2b23d0ba | 91 | TEST_ASSERT_EQUAL(osEventMessage, evt.status); |
marcozecchini | 0:9fca2b23d0ba | 92 | TEST_ASSERT_EQUAL(TEST_UINT_MSG2, evt.value.v); |
marcozecchini | 0:9fca2b23d0ba | 93 | } |
marcozecchini | 0:9fca2b23d0ba | 94 | |
marcozecchini | 0:9fca2b23d0ba | 95 | /** Test pass ptr msg |
marcozecchini | 0:9fca2b23d0ba | 96 | |
marcozecchini | 0:9fca2b23d0ba | 97 | Given a queue for pointers to uint32_t messages with one slot |
marcozecchini | 0:9fca2b23d0ba | 98 | When a pointer to an uint32_t is inserted into the queue |
marcozecchini | 0:9fca2b23d0ba | 99 | and a message is extracted from the queue |
marcozecchini | 0:9fca2b23d0ba | 100 | Then the extracted message is the same as previously inserted message |
marcozecchini | 0:9fca2b23d0ba | 101 | */ |
marcozecchini | 0:9fca2b23d0ba | 102 | void test_pass_ptr() |
marcozecchini | 0:9fca2b23d0ba | 103 | { |
marcozecchini | 0:9fca2b23d0ba | 104 | Queue<uint32_t, 1> q; |
marcozecchini | 0:9fca2b23d0ba | 105 | uint32_t msg = TEST_UINT_MSG; |
marcozecchini | 0:9fca2b23d0ba | 106 | |
marcozecchini | 0:9fca2b23d0ba | 107 | osStatus stat = q.put(&msg); |
marcozecchini | 0:9fca2b23d0ba | 108 | TEST_ASSERT_EQUAL(osOK, stat); |
marcozecchini | 0:9fca2b23d0ba | 109 | |
marcozecchini | 0:9fca2b23d0ba | 110 | osEvent evt = q.get(); |
marcozecchini | 0:9fca2b23d0ba | 111 | TEST_ASSERT_EQUAL(osEventMessage, evt.status); |
marcozecchini | 0:9fca2b23d0ba | 112 | TEST_ASSERT_EQUAL(&msg, evt.value.p); |
marcozecchini | 0:9fca2b23d0ba | 113 | } |
marcozecchini | 0:9fca2b23d0ba | 114 | |
marcozecchini | 0:9fca2b23d0ba | 115 | /** Test get from empty queue |
marcozecchini | 0:9fca2b23d0ba | 116 | |
marcozecchini | 0:9fca2b23d0ba | 117 | Given an empty queue for uint32_t values |
marcozecchini | 0:9fca2b23d0ba | 118 | When @a get is called on the queue with timeout of 0 |
marcozecchini | 0:9fca2b23d0ba | 119 | Then queue returns status of osOK, but no data |
marcozecchini | 0:9fca2b23d0ba | 120 | */ |
marcozecchini | 0:9fca2b23d0ba | 121 | void test_get_empty_no_timeout() |
marcozecchini | 0:9fca2b23d0ba | 122 | { |
marcozecchini | 0:9fca2b23d0ba | 123 | Queue<uint32_t, 1> q; |
marcozecchini | 0:9fca2b23d0ba | 124 | |
marcozecchini | 0:9fca2b23d0ba | 125 | osEvent evt = q.get(0); |
marcozecchini | 0:9fca2b23d0ba | 126 | TEST_ASSERT_EQUAL(osOK, evt.status); |
marcozecchini | 0:9fca2b23d0ba | 127 | } |
marcozecchini | 0:9fca2b23d0ba | 128 | |
marcozecchini | 0:9fca2b23d0ba | 129 | /** Test get from empty queue with timeout |
marcozecchini | 0:9fca2b23d0ba | 130 | |
marcozecchini | 0:9fca2b23d0ba | 131 | Given an empty queue for uint32_t values |
marcozecchini | 0:9fca2b23d0ba | 132 | When @a get is called on the queue with timeout of 50ms |
marcozecchini | 0:9fca2b23d0ba | 133 | Then queue returns status of osEventTimeout after about 50ms wait |
marcozecchini | 0:9fca2b23d0ba | 134 | */ |
marcozecchini | 0:9fca2b23d0ba | 135 | void test_get_empty_timeout() |
marcozecchini | 0:9fca2b23d0ba | 136 | { |
marcozecchini | 0:9fca2b23d0ba | 137 | Queue<uint32_t, 1> q; |
marcozecchini | 0:9fca2b23d0ba | 138 | Timer timer; |
marcozecchini | 0:9fca2b23d0ba | 139 | timer.start(); |
marcozecchini | 0:9fca2b23d0ba | 140 | |
marcozecchini | 0:9fca2b23d0ba | 141 | osEvent evt = q.get(50); |
marcozecchini | 0:9fca2b23d0ba | 142 | TEST_ASSERT_EQUAL(osEventTimeout, evt.status); |
marcozecchini | 0:9fca2b23d0ba | 143 | TEST_ASSERT_UINT32_WITHIN(5000, 50000, timer.read_us()); |
marcozecchini | 0:9fca2b23d0ba | 144 | } |
marcozecchini | 0:9fca2b23d0ba | 145 | |
marcozecchini | 0:9fca2b23d0ba | 146 | /** Test get empty wait forever |
marcozecchini | 0:9fca2b23d0ba | 147 | |
marcozecchini | 0:9fca2b23d0ba | 148 | Given a two threads A & B and a queue for uint32_t values |
marcozecchini | 0:9fca2b23d0ba | 149 | When thread A calls @a get on an empty queue with osWaitForever |
marcozecchini | 0:9fca2b23d0ba | 150 | Then the thread A waits for a message to appear in the queue |
marcozecchini | 0:9fca2b23d0ba | 151 | When thread B puts a message in the queue |
marcozecchini | 0:9fca2b23d0ba | 152 | Then thread A wakes up and receives it |
marcozecchini | 0:9fca2b23d0ba | 153 | */ |
marcozecchini | 0:9fca2b23d0ba | 154 | void test_get_empty_wait_forever() |
marcozecchini | 0:9fca2b23d0ba | 155 | { |
marcozecchini | 0:9fca2b23d0ba | 156 | Thread t(osPriorityNormal, THREAD_STACK_SIZE); |
marcozecchini | 0:9fca2b23d0ba | 157 | Queue<uint32_t, 1> q; |
marcozecchini | 0:9fca2b23d0ba | 158 | |
marcozecchini | 0:9fca2b23d0ba | 159 | t.start(callback(thread_put_uint_msg<TEST_TIMEOUT>, &q)); |
marcozecchini | 0:9fca2b23d0ba | 160 | |
marcozecchini | 0:9fca2b23d0ba | 161 | Timer timer; |
marcozecchini | 0:9fca2b23d0ba | 162 | timer.start(); |
marcozecchini | 0:9fca2b23d0ba | 163 | |
marcozecchini | 0:9fca2b23d0ba | 164 | osEvent evt = q.get(); |
marcozecchini | 0:9fca2b23d0ba | 165 | TEST_ASSERT_EQUAL(osEventMessage, evt.status); |
marcozecchini | 0:9fca2b23d0ba | 166 | TEST_ASSERT_EQUAL(TEST_UINT_MSG, evt.value.v); |
marcozecchini | 0:9fca2b23d0ba | 167 | TEST_ASSERT_UINT32_WITHIN(TEST_TIMEOUT * 100, TEST_TIMEOUT * 1000, timer.read_us()); |
marcozecchini | 0:9fca2b23d0ba | 168 | } |
marcozecchini | 0:9fca2b23d0ba | 169 | |
marcozecchini | 0:9fca2b23d0ba | 170 | /** Test put full no timeout |
marcozecchini | 0:9fca2b23d0ba | 171 | * |
marcozecchini | 0:9fca2b23d0ba | 172 | * Given a queue with one slot for uint32_t data |
marcozecchini | 0:9fca2b23d0ba | 173 | * When a thread tries to insert two messages |
marcozecchini | 0:9fca2b23d0ba | 174 | * Then first operation succeeds and second fails with @a osErrorResource |
marcozecchini | 0:9fca2b23d0ba | 175 | */ |
marcozecchini | 0:9fca2b23d0ba | 176 | void test_put_full_no_timeout() |
marcozecchini | 0:9fca2b23d0ba | 177 | { |
marcozecchini | 0:9fca2b23d0ba | 178 | Queue<uint32_t, 1> q; |
marcozecchini | 0:9fca2b23d0ba | 179 | |
marcozecchini | 0:9fca2b23d0ba | 180 | osStatus stat = q.put((uint32_t*) TEST_UINT_MSG); |
marcozecchini | 0:9fca2b23d0ba | 181 | TEST_ASSERT_EQUAL(osOK, stat); |
marcozecchini | 0:9fca2b23d0ba | 182 | |
marcozecchini | 0:9fca2b23d0ba | 183 | stat = q.put((uint32_t*) TEST_UINT_MSG); |
marcozecchini | 0:9fca2b23d0ba | 184 | TEST_ASSERT_EQUAL(osErrorResource, stat); |
marcozecchini | 0:9fca2b23d0ba | 185 | } |
marcozecchini | 0:9fca2b23d0ba | 186 | |
marcozecchini | 0:9fca2b23d0ba | 187 | /** Test put full timeout |
marcozecchini | 0:9fca2b23d0ba | 188 | * |
marcozecchini | 0:9fca2b23d0ba | 189 | * Given a queue with one slot for uint32_t data |
marcozecchini | 0:9fca2b23d0ba | 190 | * When a thread tries to insert two messages with @ TEST_TIMEOUT timeout |
marcozecchini | 0:9fca2b23d0ba | 191 | * Then first operation succeeds and second fails with @a osErrorTimeout |
marcozecchini | 0:9fca2b23d0ba | 192 | */ |
marcozecchini | 0:9fca2b23d0ba | 193 | void test_put_full_timeout() |
marcozecchini | 0:9fca2b23d0ba | 194 | { |
marcozecchini | 0:9fca2b23d0ba | 195 | Queue<uint32_t, 1> q; |
marcozecchini | 0:9fca2b23d0ba | 196 | |
marcozecchini | 0:9fca2b23d0ba | 197 | osStatus stat = q.put((uint32_t*) TEST_UINT_MSG, TEST_TIMEOUT); |
marcozecchini | 0:9fca2b23d0ba | 198 | TEST_ASSERT_EQUAL(osOK, stat); |
marcozecchini | 0:9fca2b23d0ba | 199 | |
marcozecchini | 0:9fca2b23d0ba | 200 | Timer timer; |
marcozecchini | 0:9fca2b23d0ba | 201 | timer.start(); |
marcozecchini | 0:9fca2b23d0ba | 202 | |
marcozecchini | 0:9fca2b23d0ba | 203 | stat = q.put((uint32_t*) TEST_UINT_MSG, TEST_TIMEOUT); |
marcozecchini | 0:9fca2b23d0ba | 204 | TEST_ASSERT_EQUAL(osErrorTimeout, stat); |
marcozecchini | 0:9fca2b23d0ba | 205 | TEST_ASSERT_UINT32_WITHIN(TEST_TIMEOUT * 100, TEST_TIMEOUT * 1000, timer.read_us()); |
marcozecchini | 0:9fca2b23d0ba | 206 | } |
marcozecchini | 0:9fca2b23d0ba | 207 | |
marcozecchini | 0:9fca2b23d0ba | 208 | /** Test put full wait forever |
marcozecchini | 0:9fca2b23d0ba | 209 | * |
marcozecchini | 0:9fca2b23d0ba | 210 | * Given two threads A & B and a queue with one slot for uint32_t data |
marcozecchini | 0:9fca2b23d0ba | 211 | * When thread A puts a message to the queue and tries to put second one with @a osWaitForever timeout |
marcozecchini | 0:9fca2b23d0ba | 212 | * Then thread waits for a slot to become empty in the queue |
marcozecchini | 0:9fca2b23d0ba | 213 | * When thread B takes one message out of the queue |
marcozecchini | 0:9fca2b23d0ba | 214 | * Then thread A successfully inserts message into the queue |
marcozecchini | 0:9fca2b23d0ba | 215 | */ |
marcozecchini | 0:9fca2b23d0ba | 216 | void test_put_full_waitforever() |
marcozecchini | 0:9fca2b23d0ba | 217 | { |
marcozecchini | 0:9fca2b23d0ba | 218 | Thread t(osPriorityNormal, THREAD_STACK_SIZE); |
marcozecchini | 0:9fca2b23d0ba | 219 | Queue<uint32_t, 1> q; |
marcozecchini | 0:9fca2b23d0ba | 220 | |
marcozecchini | 0:9fca2b23d0ba | 221 | t.start(callback(thread_get_uint_msg<TEST_TIMEOUT, TEST_UINT_MSG>, &q)); |
marcozecchini | 0:9fca2b23d0ba | 222 | |
marcozecchini | 0:9fca2b23d0ba | 223 | osStatus stat = q.put((uint32_t*) TEST_UINT_MSG); |
marcozecchini | 0:9fca2b23d0ba | 224 | TEST_ASSERT_EQUAL(osOK, stat); |
marcozecchini | 0:9fca2b23d0ba | 225 | |
marcozecchini | 0:9fca2b23d0ba | 226 | Timer timer; |
marcozecchini | 0:9fca2b23d0ba | 227 | timer.start(); |
marcozecchini | 0:9fca2b23d0ba | 228 | stat = q.put((uint32_t*) TEST_UINT_MSG, osWaitForever); |
marcozecchini | 0:9fca2b23d0ba | 229 | TEST_ASSERT_EQUAL(osOK, stat); |
marcozecchini | 0:9fca2b23d0ba | 230 | TEST_ASSERT_UINT32_WITHIN(TEST_TIMEOUT * 100, TEST_TIMEOUT * 1000, timer.read_us()); |
marcozecchini | 0:9fca2b23d0ba | 231 | |
marcozecchini | 0:9fca2b23d0ba | 232 | t.join(); |
marcozecchini | 0:9fca2b23d0ba | 233 | } |
marcozecchini | 0:9fca2b23d0ba | 234 | |
marcozecchini | 0:9fca2b23d0ba | 235 | /** Test message ordering |
marcozecchini | 0:9fca2b23d0ba | 236 | |
marcozecchini | 0:9fca2b23d0ba | 237 | Given a queue of uint32_t data |
marcozecchini | 0:9fca2b23d0ba | 238 | When two messages are inserted with equal priority |
marcozecchini | 0:9fca2b23d0ba | 239 | Then messages should be returned in the exact order they were inserted |
marcozecchini | 0:9fca2b23d0ba | 240 | */ |
marcozecchini | 0:9fca2b23d0ba | 241 | void test_msg_order() |
marcozecchini | 0:9fca2b23d0ba | 242 | { |
marcozecchini | 0:9fca2b23d0ba | 243 | Queue<uint32_t, 2> q; |
marcozecchini | 0:9fca2b23d0ba | 244 | |
marcozecchini | 0:9fca2b23d0ba | 245 | osStatus stat = q.put((uint32_t*) TEST_UINT_MSG, TEST_TIMEOUT); |
marcozecchini | 0:9fca2b23d0ba | 246 | TEST_ASSERT_EQUAL(osOK, stat); |
marcozecchini | 0:9fca2b23d0ba | 247 | |
marcozecchini | 0:9fca2b23d0ba | 248 | stat = q.put((uint32_t*) TEST_UINT_MSG2, TEST_TIMEOUT); |
marcozecchini | 0:9fca2b23d0ba | 249 | TEST_ASSERT_EQUAL(osOK, stat); |
marcozecchini | 0:9fca2b23d0ba | 250 | |
marcozecchini | 0:9fca2b23d0ba | 251 | osEvent evt = q.get(); |
marcozecchini | 0:9fca2b23d0ba | 252 | TEST_ASSERT_EQUAL(osEventMessage, evt.status); |
marcozecchini | 0:9fca2b23d0ba | 253 | TEST_ASSERT_EQUAL(TEST_UINT_MSG, evt.value.v); |
marcozecchini | 0:9fca2b23d0ba | 254 | |
marcozecchini | 0:9fca2b23d0ba | 255 | evt = q.get(); |
marcozecchini | 0:9fca2b23d0ba | 256 | TEST_ASSERT_EQUAL(osEventMessage, evt.status); |
marcozecchini | 0:9fca2b23d0ba | 257 | TEST_ASSERT_EQUAL(TEST_UINT_MSG2, evt.value.v); |
marcozecchini | 0:9fca2b23d0ba | 258 | } |
marcozecchini | 0:9fca2b23d0ba | 259 | |
marcozecchini | 0:9fca2b23d0ba | 260 | /** Test message priority |
marcozecchini | 0:9fca2b23d0ba | 261 | |
marcozecchini | 0:9fca2b23d0ba | 262 | Given a queue of uint32_t data |
marcozecchini | 0:9fca2b23d0ba | 263 | When two messages are inserted with ascending priority |
marcozecchini | 0:9fca2b23d0ba | 264 | Then messages should be returned in descending priority order |
marcozecchini | 0:9fca2b23d0ba | 265 | */ |
marcozecchini | 0:9fca2b23d0ba | 266 | void test_msg_prio() |
marcozecchini | 0:9fca2b23d0ba | 267 | { |
marcozecchini | 0:9fca2b23d0ba | 268 | Queue<uint32_t, 2> q; |
marcozecchini | 0:9fca2b23d0ba | 269 | |
marcozecchini | 0:9fca2b23d0ba | 270 | osStatus stat = q.put((uint32_t*) TEST_UINT_MSG, TEST_TIMEOUT, 0); |
marcozecchini | 0:9fca2b23d0ba | 271 | TEST_ASSERT_EQUAL(osOK, stat); |
marcozecchini | 0:9fca2b23d0ba | 272 | |
marcozecchini | 0:9fca2b23d0ba | 273 | stat = q.put((uint32_t*) TEST_UINT_MSG2, TEST_TIMEOUT, 1); |
marcozecchini | 0:9fca2b23d0ba | 274 | TEST_ASSERT_EQUAL(osOK, stat); |
marcozecchini | 0:9fca2b23d0ba | 275 | |
marcozecchini | 0:9fca2b23d0ba | 276 | osEvent evt = q.get(); |
marcozecchini | 0:9fca2b23d0ba | 277 | TEST_ASSERT_EQUAL(osEventMessage, evt.status); |
marcozecchini | 0:9fca2b23d0ba | 278 | TEST_ASSERT_EQUAL(TEST_UINT_MSG2, evt.value.v); |
marcozecchini | 0:9fca2b23d0ba | 279 | |
marcozecchini | 0:9fca2b23d0ba | 280 | evt = q.get(); |
marcozecchini | 0:9fca2b23d0ba | 281 | TEST_ASSERT_EQUAL(osEventMessage, evt.status); |
marcozecchini | 0:9fca2b23d0ba | 282 | TEST_ASSERT_EQUAL(TEST_UINT_MSG, evt.value.v); |
marcozecchini | 0:9fca2b23d0ba | 283 | } |
marcozecchini | 0:9fca2b23d0ba | 284 | |
marcozecchini | 0:9fca2b23d0ba | 285 | /** Test queue empty |
marcozecchini | 0:9fca2b23d0ba | 286 | |
marcozecchini | 0:9fca2b23d0ba | 287 | Given a queue of uint32_t data |
marcozecchini | 0:9fca2b23d0ba | 288 | before data is inserted the queue should be empty |
marcozecchini | 0:9fca2b23d0ba | 289 | after data is inserted the queue shouldn't be empty |
marcozecchini | 0:9fca2b23d0ba | 290 | */ |
marcozecchini | 0:9fca2b23d0ba | 291 | void test_queue_empty() |
marcozecchini | 0:9fca2b23d0ba | 292 | { |
marcozecchini | 0:9fca2b23d0ba | 293 | Queue<uint32_t, 1> q; |
marcozecchini | 0:9fca2b23d0ba | 294 | |
marcozecchini | 0:9fca2b23d0ba | 295 | TEST_ASSERT_EQUAL(true, q.empty()); |
marcozecchini | 0:9fca2b23d0ba | 296 | |
marcozecchini | 0:9fca2b23d0ba | 297 | q.put((uint32_t*) TEST_UINT_MSG, TEST_TIMEOUT, 1); |
marcozecchini | 0:9fca2b23d0ba | 298 | |
marcozecchini | 0:9fca2b23d0ba | 299 | TEST_ASSERT_EQUAL(false, q.empty()); |
marcozecchini | 0:9fca2b23d0ba | 300 | } |
marcozecchini | 0:9fca2b23d0ba | 301 | |
marcozecchini | 0:9fca2b23d0ba | 302 | /** Test queue empty |
marcozecchini | 0:9fca2b23d0ba | 303 | |
marcozecchini | 0:9fca2b23d0ba | 304 | Given a queue of uint32_t data with size of 1 |
marcozecchini | 0:9fca2b23d0ba | 305 | before data is inserted the queue shouldn't be full |
marcozecchini | 0:9fca2b23d0ba | 306 | after data is inserted the queue should be full |
marcozecchini | 0:9fca2b23d0ba | 307 | */ |
marcozecchini | 0:9fca2b23d0ba | 308 | void test_queue_full() |
marcozecchini | 0:9fca2b23d0ba | 309 | { |
marcozecchini | 0:9fca2b23d0ba | 310 | Queue<uint32_t, 1> q; |
marcozecchini | 0:9fca2b23d0ba | 311 | |
marcozecchini | 0:9fca2b23d0ba | 312 | TEST_ASSERT_EQUAL(false, q.full()); |
marcozecchini | 0:9fca2b23d0ba | 313 | |
marcozecchini | 0:9fca2b23d0ba | 314 | q.put((uint32_t*) TEST_UINT_MSG, TEST_TIMEOUT, 1); |
marcozecchini | 0:9fca2b23d0ba | 315 | |
marcozecchini | 0:9fca2b23d0ba | 316 | TEST_ASSERT_EQUAL(true, q.full()); |
marcozecchini | 0:9fca2b23d0ba | 317 | } |
marcozecchini | 0:9fca2b23d0ba | 318 | |
marcozecchini | 0:9fca2b23d0ba | 319 | utest::v1::status_t test_setup(const size_t number_of_cases) |
marcozecchini | 0:9fca2b23d0ba | 320 | { |
marcozecchini | 0:9fca2b23d0ba | 321 | GREENTEA_SETUP(5, "default_auto"); |
marcozecchini | 0:9fca2b23d0ba | 322 | return verbose_test_setup_handler(number_of_cases); |
marcozecchini | 0:9fca2b23d0ba | 323 | } |
marcozecchini | 0:9fca2b23d0ba | 324 | |
marcozecchini | 0:9fca2b23d0ba | 325 | Case cases[] = { |
marcozecchini | 0:9fca2b23d0ba | 326 | Case("Test pass uint msg", test_pass_uint), |
marcozecchini | 0:9fca2b23d0ba | 327 | Case("Test pass uint msg twice", test_pass_uint_twice), |
marcozecchini | 0:9fca2b23d0ba | 328 | Case("Test pass ptr msg", test_pass_ptr), |
marcozecchini | 0:9fca2b23d0ba | 329 | Case("Test get from empty queue no timeout", test_get_empty_no_timeout), |
marcozecchini | 0:9fca2b23d0ba | 330 | Case("Test get from empty queue timeout", test_get_empty_timeout), |
marcozecchini | 0:9fca2b23d0ba | 331 | Case("Test get empty wait forever", test_get_empty_wait_forever), |
marcozecchini | 0:9fca2b23d0ba | 332 | Case("Test put full no timeout", test_put_full_no_timeout), |
marcozecchini | 0:9fca2b23d0ba | 333 | Case("Test put full timeout", test_put_full_timeout), |
marcozecchini | 0:9fca2b23d0ba | 334 | Case("Test put full wait forever", test_put_full_waitforever), |
marcozecchini | 0:9fca2b23d0ba | 335 | Case("Test message ordering", test_msg_order), |
marcozecchini | 0:9fca2b23d0ba | 336 | Case("Test message priority", test_msg_prio), |
marcozecchini | 0:9fca2b23d0ba | 337 | Case("Test queue empty", test_queue_empty), |
marcozecchini | 0:9fca2b23d0ba | 338 | Case("Test queue full", test_queue_full) |
marcozecchini | 0:9fca2b23d0ba | 339 | }; |
marcozecchini | 0:9fca2b23d0ba | 340 | |
marcozecchini | 0:9fca2b23d0ba | 341 | Specification specification(test_setup, cases); |
marcozecchini | 0:9fca2b23d0ba | 342 | |
marcozecchini | 0:9fca2b23d0ba | 343 | int main() |
marcozecchini | 0:9fca2b23d0ba | 344 | { |
marcozecchini | 0:9fca2b23d0ba | 345 | return !Harness::run(specification); |
marcozecchini | 0:9fca2b23d0ba | 346 | } |