BBR 1 Ebene

Committer:
borlanic
Date:
Mon May 14 11:29:06 2018 +0000
Revision:
0:fbdae7e6d805
BBR

Who changed what in which revision?

UserRevisionLine numberNew contents of line
borlanic 0:fbdae7e6d805 1 /* mbed Microcontroller Library
borlanic 0:fbdae7e6d805 2 * Copyright (c) 2017 ARM Limited
borlanic 0:fbdae7e6d805 3 *
borlanic 0:fbdae7e6d805 4 * Licensed under the Apache License, Version 2.0 (the "License");
borlanic 0:fbdae7e6d805 5 * you may not use this file except in compliance with the License.
borlanic 0:fbdae7e6d805 6 * You may obtain a copy of the License at
borlanic 0:fbdae7e6d805 7 *
borlanic 0:fbdae7e6d805 8 * http://www.apache.org/licenses/LICENSE-2.0
borlanic 0:fbdae7e6d805 9 *
borlanic 0:fbdae7e6d805 10 * Unless required by applicable law or agreed to in writing, software
borlanic 0:fbdae7e6d805 11 * distributed under the License is distributed on an "AS IS" BASIS,
borlanic 0:fbdae7e6d805 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
borlanic 0:fbdae7e6d805 13 * See the License for the specific language governing permissions and
borlanic 0:fbdae7e6d805 14 * limitations under the License.
borlanic 0:fbdae7e6d805 15 */
borlanic 0:fbdae7e6d805 16 #include "mbed_events.h"
borlanic 0:fbdae7e6d805 17 #include "mbed.h"
borlanic 0:fbdae7e6d805 18 #include "rtos.h"
borlanic 0:fbdae7e6d805 19 #include "greentea-client/test_env.h"
borlanic 0:fbdae7e6d805 20 #include "unity.h"
borlanic 0:fbdae7e6d805 21 #include "utest.h"
borlanic 0:fbdae7e6d805 22
borlanic 0:fbdae7e6d805 23 using namespace utest::v1;
borlanic 0:fbdae7e6d805 24
borlanic 0:fbdae7e6d805 25 // TEST_EQUEUE_SIZE was reduced below 1024B to fit this test to devices with small RAM (RAM <= 16kB)
borlanic 0:fbdae7e6d805 26 // additionally TEST_EQUEUE_SIZE was expressed in EVENTS_EVENT_SIZE to increase readability
borlanic 0:fbdae7e6d805 27 // (for more details about EVENTS_EVENT_SIZE see EventQueue constructor)
borlanic 0:fbdae7e6d805 28 #define TEST_EQUEUE_SIZE (18*EVENTS_EVENT_SIZE)
borlanic 0:fbdae7e6d805 29
borlanic 0:fbdae7e6d805 30 // flag for called
borlanic 0:fbdae7e6d805 31 volatile bool touched = false;
borlanic 0:fbdae7e6d805 32
borlanic 0:fbdae7e6d805 33 // static functions
borlanic 0:fbdae7e6d805 34 void func5(int a0, int a1, int a2, int a3, int a4) {
borlanic 0:fbdae7e6d805 35 touched = true;
borlanic 0:fbdae7e6d805 36 TEST_ASSERT_EQUAL(a0 | a1 | a2 | a3 | a4, 0x1f);
borlanic 0:fbdae7e6d805 37 }
borlanic 0:fbdae7e6d805 38
borlanic 0:fbdae7e6d805 39 void func4(int a0, int a1, int a2, int a3) {
borlanic 0:fbdae7e6d805 40 touched = true;
borlanic 0:fbdae7e6d805 41 TEST_ASSERT_EQUAL(a0 | a1 | a2 | a3, 0xf);
borlanic 0:fbdae7e6d805 42 }
borlanic 0:fbdae7e6d805 43
borlanic 0:fbdae7e6d805 44 void func3(int a0, int a1, int a2) {
borlanic 0:fbdae7e6d805 45 touched = true;
borlanic 0:fbdae7e6d805 46 TEST_ASSERT_EQUAL(a0 | a1 | a2, 0x7);
borlanic 0:fbdae7e6d805 47 }
borlanic 0:fbdae7e6d805 48
borlanic 0:fbdae7e6d805 49 void func2(int a0, int a1) {
borlanic 0:fbdae7e6d805 50 touched = true;
borlanic 0:fbdae7e6d805 51 TEST_ASSERT_EQUAL(a0 | a1, 0x3);
borlanic 0:fbdae7e6d805 52 }
borlanic 0:fbdae7e6d805 53
borlanic 0:fbdae7e6d805 54 void func1(int a0) {
borlanic 0:fbdae7e6d805 55 touched = true;
borlanic 0:fbdae7e6d805 56 TEST_ASSERT_EQUAL(a0, 0x1);
borlanic 0:fbdae7e6d805 57 }
borlanic 0:fbdae7e6d805 58
borlanic 0:fbdae7e6d805 59 void func0() {
borlanic 0:fbdae7e6d805 60 touched = true;
borlanic 0:fbdae7e6d805 61 }
borlanic 0:fbdae7e6d805 62
borlanic 0:fbdae7e6d805 63 #define SIMPLE_POSTS_TEST(i, ...) \
borlanic 0:fbdae7e6d805 64 void simple_posts_test##i() { \
borlanic 0:fbdae7e6d805 65 EventQueue queue(TEST_EQUEUE_SIZE); \
borlanic 0:fbdae7e6d805 66 \
borlanic 0:fbdae7e6d805 67 touched = false; \
borlanic 0:fbdae7e6d805 68 queue.call(func##i,##__VA_ARGS__); \
borlanic 0:fbdae7e6d805 69 queue.dispatch(0); \
borlanic 0:fbdae7e6d805 70 TEST_ASSERT(touched); \
borlanic 0:fbdae7e6d805 71 \
borlanic 0:fbdae7e6d805 72 touched = false; \
borlanic 0:fbdae7e6d805 73 queue.call_in(1, func##i,##__VA_ARGS__); \
borlanic 0:fbdae7e6d805 74 queue.dispatch(2); \
borlanic 0:fbdae7e6d805 75 TEST_ASSERT(touched); \
borlanic 0:fbdae7e6d805 76 \
borlanic 0:fbdae7e6d805 77 touched = false; \
borlanic 0:fbdae7e6d805 78 queue.call_every(1, func##i,##__VA_ARGS__); \
borlanic 0:fbdae7e6d805 79 queue.dispatch(2); \
borlanic 0:fbdae7e6d805 80 TEST_ASSERT(touched); \
borlanic 0:fbdae7e6d805 81 }
borlanic 0:fbdae7e6d805 82
borlanic 0:fbdae7e6d805 83 SIMPLE_POSTS_TEST(5, 0x01, 0x02, 0x04, 0x08, 0x010)
borlanic 0:fbdae7e6d805 84 SIMPLE_POSTS_TEST(4, 0x01, 0x02, 0x04, 0x08)
borlanic 0:fbdae7e6d805 85 SIMPLE_POSTS_TEST(3, 0x01, 0x02, 0x04)
borlanic 0:fbdae7e6d805 86 SIMPLE_POSTS_TEST(2, 0x01, 0x02)
borlanic 0:fbdae7e6d805 87 SIMPLE_POSTS_TEST(1, 0x01)
borlanic 0:fbdae7e6d805 88 SIMPLE_POSTS_TEST(0)
borlanic 0:fbdae7e6d805 89
borlanic 0:fbdae7e6d805 90
borlanic 0:fbdae7e6d805 91 void time_func(Timer *t, int ms) {
borlanic 0:fbdae7e6d805 92 TEST_ASSERT_INT_WITHIN(5, ms, t->read_ms());
borlanic 0:fbdae7e6d805 93 t->reset();
borlanic 0:fbdae7e6d805 94 }
borlanic 0:fbdae7e6d805 95
borlanic 0:fbdae7e6d805 96 template <int N>
borlanic 0:fbdae7e6d805 97 void call_in_test() {
borlanic 0:fbdae7e6d805 98 Timer tickers[N];
borlanic 0:fbdae7e6d805 99
borlanic 0:fbdae7e6d805 100 EventQueue queue(TEST_EQUEUE_SIZE);
borlanic 0:fbdae7e6d805 101
borlanic 0:fbdae7e6d805 102 for (int i = 0; i < N; i++) {
borlanic 0:fbdae7e6d805 103 tickers[i].start();
borlanic 0:fbdae7e6d805 104 queue.call_in((i+1)*100, time_func, &tickers[i], (i+1)*100);
borlanic 0:fbdae7e6d805 105 }
borlanic 0:fbdae7e6d805 106
borlanic 0:fbdae7e6d805 107 queue.dispatch(N*100);
borlanic 0:fbdae7e6d805 108 }
borlanic 0:fbdae7e6d805 109
borlanic 0:fbdae7e6d805 110 template <int N>
borlanic 0:fbdae7e6d805 111 void call_every_test() {
borlanic 0:fbdae7e6d805 112 Timer tickers[N];
borlanic 0:fbdae7e6d805 113
borlanic 0:fbdae7e6d805 114 EventQueue queue(TEST_EQUEUE_SIZE);
borlanic 0:fbdae7e6d805 115
borlanic 0:fbdae7e6d805 116 for (int i = 0; i < N; i++) {
borlanic 0:fbdae7e6d805 117 tickers[i].start();
borlanic 0:fbdae7e6d805 118 queue.call_every((i+1)*100, time_func, &tickers[i], (i+1)*100);
borlanic 0:fbdae7e6d805 119 }
borlanic 0:fbdae7e6d805 120
borlanic 0:fbdae7e6d805 121 queue.dispatch(N*100);
borlanic 0:fbdae7e6d805 122 }
borlanic 0:fbdae7e6d805 123
borlanic 0:fbdae7e6d805 124 void allocate_failure_test() {
borlanic 0:fbdae7e6d805 125 EventQueue queue(TEST_EQUEUE_SIZE);
borlanic 0:fbdae7e6d805 126 int id;
borlanic 0:fbdae7e6d805 127
borlanic 0:fbdae7e6d805 128 for (int i = 0; i < 100; i++) {
borlanic 0:fbdae7e6d805 129 id = queue.call((void (*)())0);
borlanic 0:fbdae7e6d805 130 }
borlanic 0:fbdae7e6d805 131
borlanic 0:fbdae7e6d805 132 TEST_ASSERT(!id);
borlanic 0:fbdae7e6d805 133 }
borlanic 0:fbdae7e6d805 134
borlanic 0:fbdae7e6d805 135 void no() {
borlanic 0:fbdae7e6d805 136 TEST_ASSERT(false);
borlanic 0:fbdae7e6d805 137 }
borlanic 0:fbdae7e6d805 138
borlanic 0:fbdae7e6d805 139 template <int N>
borlanic 0:fbdae7e6d805 140 void cancel_test1() {
borlanic 0:fbdae7e6d805 141 EventQueue queue(TEST_EQUEUE_SIZE);
borlanic 0:fbdae7e6d805 142
borlanic 0:fbdae7e6d805 143 int ids[N];
borlanic 0:fbdae7e6d805 144
borlanic 0:fbdae7e6d805 145 for (int i = 0; i < N; i++) {
borlanic 0:fbdae7e6d805 146 ids[i] = queue.call_in(1000, no);
borlanic 0:fbdae7e6d805 147 }
borlanic 0:fbdae7e6d805 148
borlanic 0:fbdae7e6d805 149 for (int i = N-1; i >= 0; i--) {
borlanic 0:fbdae7e6d805 150 queue.cancel(ids[i]);
borlanic 0:fbdae7e6d805 151 }
borlanic 0:fbdae7e6d805 152
borlanic 0:fbdae7e6d805 153 queue.dispatch(0);
borlanic 0:fbdae7e6d805 154 }
borlanic 0:fbdae7e6d805 155
borlanic 0:fbdae7e6d805 156
borlanic 0:fbdae7e6d805 157 // Testing the dynamic arguments to the event class
borlanic 0:fbdae7e6d805 158 unsigned counter = 0;
borlanic 0:fbdae7e6d805 159
borlanic 0:fbdae7e6d805 160 void count5(unsigned a0, unsigned a1, unsigned a2, unsigned a3, unsigned a5) {
borlanic 0:fbdae7e6d805 161 counter += a0 + a1 + a2 + a3 + a5;
borlanic 0:fbdae7e6d805 162 }
borlanic 0:fbdae7e6d805 163
borlanic 0:fbdae7e6d805 164 void count4(unsigned a0, unsigned a1, unsigned a2, unsigned a3) {
borlanic 0:fbdae7e6d805 165 counter += a0 + a1 + a2 + a3;
borlanic 0:fbdae7e6d805 166 }
borlanic 0:fbdae7e6d805 167
borlanic 0:fbdae7e6d805 168 void count3(unsigned a0, unsigned a1, unsigned a2) {
borlanic 0:fbdae7e6d805 169 counter += a0 + a1 + a2;
borlanic 0:fbdae7e6d805 170 }
borlanic 0:fbdae7e6d805 171
borlanic 0:fbdae7e6d805 172 void count2(unsigned a0, unsigned a1) {
borlanic 0:fbdae7e6d805 173 counter += a0 + a1;
borlanic 0:fbdae7e6d805 174 }
borlanic 0:fbdae7e6d805 175
borlanic 0:fbdae7e6d805 176 void count1(unsigned a0) {
borlanic 0:fbdae7e6d805 177 counter += a0;
borlanic 0:fbdae7e6d805 178 }
borlanic 0:fbdae7e6d805 179
borlanic 0:fbdae7e6d805 180 void count0() {
borlanic 0:fbdae7e6d805 181 counter += 0;
borlanic 0:fbdae7e6d805 182 }
borlanic 0:fbdae7e6d805 183
borlanic 0:fbdae7e6d805 184 void event_class_test() {
borlanic 0:fbdae7e6d805 185 counter = 0;
borlanic 0:fbdae7e6d805 186 EventQueue queue(TEST_EQUEUE_SIZE);
borlanic 0:fbdae7e6d805 187
borlanic 0:fbdae7e6d805 188 Event<void(int, int, int, int, int)> e5(&queue, count5);
borlanic 0:fbdae7e6d805 189 Event<void(int, int, int, int)> e4(&queue, count5, 1);
borlanic 0:fbdae7e6d805 190 Event<void(int, int, int)> e3(&queue, count5, 1, 1);
borlanic 0:fbdae7e6d805 191 Event<void(int, int)> e2(&queue, count5, 1, 1, 1);
borlanic 0:fbdae7e6d805 192 Event<void(int)> e1(&queue, count5, 1, 1, 1, 1);
borlanic 0:fbdae7e6d805 193 Event<void()> e0(&queue, count5, 1, 1, 1, 1, 1);
borlanic 0:fbdae7e6d805 194
borlanic 0:fbdae7e6d805 195 e5.post(1, 1, 1, 1, 1);
borlanic 0:fbdae7e6d805 196 e4.post(1, 1, 1, 1);
borlanic 0:fbdae7e6d805 197 e3.post(1, 1, 1);
borlanic 0:fbdae7e6d805 198 e2.post(1, 1);
borlanic 0:fbdae7e6d805 199 e1.post(1);
borlanic 0:fbdae7e6d805 200 e0.post();
borlanic 0:fbdae7e6d805 201
borlanic 0:fbdae7e6d805 202 queue.dispatch(0);
borlanic 0:fbdae7e6d805 203
borlanic 0:fbdae7e6d805 204 TEST_ASSERT_EQUAL(counter, 30);
borlanic 0:fbdae7e6d805 205 }
borlanic 0:fbdae7e6d805 206
borlanic 0:fbdae7e6d805 207 void event_class_helper_test() {
borlanic 0:fbdae7e6d805 208 counter = 0;
borlanic 0:fbdae7e6d805 209 EventQueue queue(TEST_EQUEUE_SIZE);
borlanic 0:fbdae7e6d805 210
borlanic 0:fbdae7e6d805 211 Event<void()> e5 = queue.event(count5, 1, 1, 1, 1, 1);
borlanic 0:fbdae7e6d805 212 Event<void()> e4 = queue.event(count4, 1, 1, 1, 1);
borlanic 0:fbdae7e6d805 213 Event<void()> e3 = queue.event(count3, 1, 1, 1);
borlanic 0:fbdae7e6d805 214 Event<void()> e2 = queue.event(count2, 1, 1);
borlanic 0:fbdae7e6d805 215 Event<void()> e1 = queue.event(count1, 1);
borlanic 0:fbdae7e6d805 216 Event<void()> e0 = queue.event(count0);
borlanic 0:fbdae7e6d805 217
borlanic 0:fbdae7e6d805 218 e5.post();
borlanic 0:fbdae7e6d805 219 e4.post();
borlanic 0:fbdae7e6d805 220 e3.post();
borlanic 0:fbdae7e6d805 221 e2.post();
borlanic 0:fbdae7e6d805 222 e1.post();
borlanic 0:fbdae7e6d805 223 e0.post();
borlanic 0:fbdae7e6d805 224
borlanic 0:fbdae7e6d805 225 queue.dispatch(0);
borlanic 0:fbdae7e6d805 226
borlanic 0:fbdae7e6d805 227 TEST_ASSERT_EQUAL(counter, 15);
borlanic 0:fbdae7e6d805 228 }
borlanic 0:fbdae7e6d805 229
borlanic 0:fbdae7e6d805 230 void event_inference_test() {
borlanic 0:fbdae7e6d805 231 counter = 0;
borlanic 0:fbdae7e6d805 232 EventQueue queue(TEST_EQUEUE_SIZE);
borlanic 0:fbdae7e6d805 233
borlanic 0:fbdae7e6d805 234 queue.event(count5, 1, 1, 1, 1, 1).post();
borlanic 0:fbdae7e6d805 235 queue.event(count5, 1, 1, 1, 1).post(1);
borlanic 0:fbdae7e6d805 236 queue.event(count5, 1, 1, 1).post(1, 1);
borlanic 0:fbdae7e6d805 237 queue.event(count5, 1, 1).post(1, 1, 1);
borlanic 0:fbdae7e6d805 238 queue.event(count5, 1).post(1, 1, 1, 1);
borlanic 0:fbdae7e6d805 239 queue.event(count5).post(1, 1, 1, 1, 1);
borlanic 0:fbdae7e6d805 240
borlanic 0:fbdae7e6d805 241 queue.event(callback(count5), 1, 1, 1, 1, 1).post();
borlanic 0:fbdae7e6d805 242 queue.event(callback(count5), 1, 1, 1, 1).post(1);
borlanic 0:fbdae7e6d805 243 queue.event(callback(count5), 1, 1, 1).post(1, 1);
borlanic 0:fbdae7e6d805 244 queue.event(callback(count5), 1, 1).post(1, 1, 1);
borlanic 0:fbdae7e6d805 245 queue.event(callback(count5), 1).post(1, 1, 1, 1);
borlanic 0:fbdae7e6d805 246 queue.event(callback(count5)).post(1, 1, 1, 1, 1);
borlanic 0:fbdae7e6d805 247
borlanic 0:fbdae7e6d805 248 queue.dispatch(0);
borlanic 0:fbdae7e6d805 249
borlanic 0:fbdae7e6d805 250 TEST_ASSERT_EQUAL(counter, 60);
borlanic 0:fbdae7e6d805 251 }
borlanic 0:fbdae7e6d805 252
borlanic 0:fbdae7e6d805 253
borlanic 0:fbdae7e6d805 254 // Test setup
borlanic 0:fbdae7e6d805 255 utest::v1::status_t test_setup(const size_t number_of_cases) {
borlanic 0:fbdae7e6d805 256 GREENTEA_SETUP(20, "default_auto");
borlanic 0:fbdae7e6d805 257 return verbose_test_setup_handler(number_of_cases);
borlanic 0:fbdae7e6d805 258 }
borlanic 0:fbdae7e6d805 259
borlanic 0:fbdae7e6d805 260 const Case cases[] = {
borlanic 0:fbdae7e6d805 261 Case("Testing calls with 5 args", simple_posts_test5),
borlanic 0:fbdae7e6d805 262 Case("Testing calls with 4 args", simple_posts_test4),
borlanic 0:fbdae7e6d805 263 Case("Testing calls with 3 args", simple_posts_test3),
borlanic 0:fbdae7e6d805 264 Case("Testing calls with 2 args", simple_posts_test2),
borlanic 0:fbdae7e6d805 265 Case("Testing calls with 1 args", simple_posts_test1),
borlanic 0:fbdae7e6d805 266 Case("Testing calls with 0 args", simple_posts_test0),
borlanic 0:fbdae7e6d805 267
borlanic 0:fbdae7e6d805 268 Case("Testing call_in", call_in_test<20>),
borlanic 0:fbdae7e6d805 269 Case("Testing call_every", call_every_test<20>),
borlanic 0:fbdae7e6d805 270
borlanic 0:fbdae7e6d805 271 Case("Testing allocate failure", allocate_failure_test),
borlanic 0:fbdae7e6d805 272
borlanic 0:fbdae7e6d805 273 Case("Testing event cancel 1", cancel_test1<20>),
borlanic 0:fbdae7e6d805 274 Case("Testing the event class", event_class_test),
borlanic 0:fbdae7e6d805 275 Case("Testing the event class helpers", event_class_helper_test),
borlanic 0:fbdae7e6d805 276 Case("Testing the event inference", event_inference_test),
borlanic 0:fbdae7e6d805 277 };
borlanic 0:fbdae7e6d805 278
borlanic 0:fbdae7e6d805 279 Specification specification(test_setup, cases);
borlanic 0:fbdae7e6d805 280
borlanic 0:fbdae7e6d805 281 int main() {
borlanic 0:fbdae7e6d805 282 return !Harness::run(specification);
borlanic 0:fbdae7e6d805 283 }
borlanic 0:fbdae7e6d805 284