|
| 1 | +#include "mbed_events.h" |
| 2 | +#include "mbed.h" |
| 3 | +#include "rtos.h" |
| 4 | +#include "greentea-client/test_env.h" |
| 5 | +#include "unity.h" |
| 6 | +#include "utest.h" |
| 7 | + |
| 8 | +using namespace utest::v1; |
| 9 | + |
| 10 | + |
| 11 | +// flag for called |
| 12 | +volatile bool touched = false; |
| 13 | + |
| 14 | +// static functions |
| 15 | +void func5(int a0, int a1, int a2, int a3, int a4) { |
| 16 | + touched = true; |
| 17 | + TEST_ASSERT_EQUAL(a0 | a1 | a2 | a3 | a4, 0x1f); |
| 18 | +} |
| 19 | + |
| 20 | +void func4(int a0, int a1, int a2, int a3) { |
| 21 | + touched = true; |
| 22 | + TEST_ASSERT_EQUAL(a0 | a1 | a2 | a3, 0xf); |
| 23 | +} |
| 24 | + |
| 25 | +void func3(int a0, int a1, int a2) { |
| 26 | + touched = true; |
| 27 | + TEST_ASSERT_EQUAL(a0 | a1 | a2, 0x7); |
| 28 | +} |
| 29 | + |
| 30 | +void func2(int a0, int a1) { |
| 31 | + touched = true; |
| 32 | + TEST_ASSERT_EQUAL(a0 | a1, 0x3); |
| 33 | +} |
| 34 | + |
| 35 | +void func1(int a0) { |
| 36 | + touched = true; |
| 37 | + TEST_ASSERT_EQUAL(a0, 0x1); |
| 38 | +} |
| 39 | + |
| 40 | +void func0() { |
| 41 | + touched = true; |
| 42 | +} |
| 43 | + |
| 44 | +#define SIMPLE_POSTS_TEST(i, ...) \ |
| 45 | +void simple_posts_test##i() { \ |
| 46 | + EventQueue queue; \ |
| 47 | + \ |
| 48 | + touched = false; \ |
| 49 | + queue.call(func##i,##__VA_ARGS__); \ |
| 50 | + queue.dispatch(0); \ |
| 51 | + TEST_ASSERT(touched); \ |
| 52 | + \ |
| 53 | + touched = false; \ |
| 54 | + queue.call_in(1, func##i,##__VA_ARGS__); \ |
| 55 | + queue.dispatch(2); \ |
| 56 | + TEST_ASSERT(touched); \ |
| 57 | + \ |
| 58 | + touched = false; \ |
| 59 | + queue.call_every(1, func##i,##__VA_ARGS__); \ |
| 60 | + queue.dispatch(2); \ |
| 61 | + TEST_ASSERT(touched); \ |
| 62 | +} |
| 63 | + |
| 64 | +SIMPLE_POSTS_TEST(5, 0x01, 0x02, 0x04, 0x08, 0x010) |
| 65 | +SIMPLE_POSTS_TEST(4, 0x01, 0x02, 0x04, 0x08) |
| 66 | +SIMPLE_POSTS_TEST(3, 0x01, 0x02, 0x04) |
| 67 | +SIMPLE_POSTS_TEST(2, 0x01, 0x02) |
| 68 | +SIMPLE_POSTS_TEST(1, 0x01) |
| 69 | +SIMPLE_POSTS_TEST(0) |
| 70 | + |
| 71 | + |
| 72 | +void time_func(Timer *t, int ms) { |
| 73 | + TEST_ASSERT_INT_WITHIN(2, ms, t->read_ms()); |
| 74 | + t->reset(); |
| 75 | +} |
| 76 | + |
| 77 | +template <int N> |
| 78 | +void call_in_test() { |
| 79 | + Timer tickers[N]; |
| 80 | + |
| 81 | + EventQueue queue; |
| 82 | + |
| 83 | + for (int i = 0; i < N; i++) { |
| 84 | + tickers[i].start(); |
| 85 | + queue.call_in((i+1)*100, time_func, &tickers[i], (i+1)*100); |
| 86 | + } |
| 87 | + |
| 88 | + queue.dispatch(N*100); |
| 89 | +} |
| 90 | + |
| 91 | +template <int N> |
| 92 | +void call_every_test() { |
| 93 | + Timer tickers[N]; |
| 94 | + |
| 95 | + EventQueue queue; |
| 96 | + |
| 97 | + for (int i = 0; i < N; i++) { |
| 98 | + tickers[i].start(); |
| 99 | + queue.call_every((i+1)*100, time_func, &tickers[i], (i+1)*100); |
| 100 | + } |
| 101 | + |
| 102 | + queue.dispatch(N*100); |
| 103 | +} |
| 104 | + |
| 105 | +struct big { char data[1024]; } big; |
| 106 | + |
| 107 | +void allocate_failure_test1() { |
| 108 | + EventQueue queue(32); |
| 109 | + int id = queue.call((void (*)(struct big))0, big); |
| 110 | + TEST_ASSERT(!id); |
| 111 | +} |
| 112 | + |
| 113 | +void allocate_failure_test2() { |
| 114 | + EventQueue queue; |
| 115 | + int id; |
| 116 | + |
| 117 | + for (int i = 0; i < 100; i++) { |
| 118 | + id = queue.call((void (*)())0); |
| 119 | + } |
| 120 | + |
| 121 | + TEST_ASSERT(!id); |
| 122 | +} |
| 123 | + |
| 124 | +void no() { |
| 125 | + TEST_ASSERT(false); |
| 126 | +} |
| 127 | + |
| 128 | +template <int N> |
| 129 | +void cancel_test1() { |
| 130 | + EventQueue queue; |
| 131 | + |
| 132 | + int ids[N]; |
| 133 | + |
| 134 | + for (int i = 0; i < N; i++) { |
| 135 | + ids[i] = queue.call_in(1000, no); |
| 136 | + } |
| 137 | + |
| 138 | + for (int i = N-1; i >= 0; i--) { |
| 139 | + queue.cancel(ids[i]); |
| 140 | + } |
| 141 | + |
| 142 | + queue.dispatch(0); |
| 143 | +} |
| 144 | + |
| 145 | + |
| 146 | +// Testing the dynamic arguments to the event class |
| 147 | +unsigned counter = 0; |
| 148 | + |
| 149 | +void count5(unsigned a0, unsigned a1, unsigned a2, unsigned a3, unsigned a5) { |
| 150 | + counter += a0 + a1 + a2 + a3 + a5; |
| 151 | +} |
| 152 | + |
| 153 | +void count4(unsigned a0, unsigned a1, unsigned a2, unsigned a3) { |
| 154 | + counter += a0 + a1 + a2 + a3; |
| 155 | +} |
| 156 | + |
| 157 | +void count3(unsigned a0, unsigned a1, unsigned a2) { |
| 158 | + counter += a0 + a1 + a2; |
| 159 | +} |
| 160 | + |
| 161 | +void count2(unsigned a0, unsigned a1) { |
| 162 | + counter += a0 + a1; |
| 163 | +} |
| 164 | + |
| 165 | +void count1(unsigned a0) { |
| 166 | + counter += a0; |
| 167 | +} |
| 168 | + |
| 169 | +void count0() { |
| 170 | + counter += 0; |
| 171 | +} |
| 172 | + |
| 173 | +void event_class_test() { |
| 174 | + counter = 0; |
| 175 | + EventQueue queue(2048); |
| 176 | + |
| 177 | + Event<void(int, int, int, int, int)> e5(&queue, count5); |
| 178 | + Event<void(int, int, int, int)> e4(&queue, count5, 1); |
| 179 | + Event<void(int, int, int)> e3(&queue, count5, 1, 1); |
| 180 | + Event<void(int, int)> e2(&queue, count5, 1, 1, 1); |
| 181 | + Event<void(int)> e1(&queue, count5, 1, 1, 1, 1); |
| 182 | + Event<void()> e0(&queue, count5, 1, 1, 1, 1, 1); |
| 183 | + |
| 184 | + e5.post(1, 1, 1, 1, 1); |
| 185 | + e4.post(1, 1, 1, 1); |
| 186 | + e3.post(1, 1, 1); |
| 187 | + e2.post(1, 1); |
| 188 | + e1.post(1); |
| 189 | + e0.post(); |
| 190 | + |
| 191 | + queue.dispatch(0); |
| 192 | + |
| 193 | + TEST_ASSERT_EQUAL(counter, 30); |
| 194 | +} |
| 195 | + |
| 196 | +void event_class_helper_test() { |
| 197 | + counter = 0; |
| 198 | + EventQueue queue(2048); |
| 199 | + |
| 200 | + Event<void()> e5 = queue.event(count5, 1, 1, 1, 1, 1); |
| 201 | + Event<void()> e4 = queue.event(count4, 1, 1, 1, 1); |
| 202 | + Event<void()> e3 = queue.event(count3, 1, 1, 1); |
| 203 | + Event<void()> e2 = queue.event(count2, 1, 1); |
| 204 | + Event<void()> e1 = queue.event(count1, 1); |
| 205 | + Event<void()> e0 = queue.event(count0); |
| 206 | + |
| 207 | + e5.post(); |
| 208 | + e4.post(); |
| 209 | + e3.post(); |
| 210 | + e2.post(); |
| 211 | + e1.post(); |
| 212 | + e0.post(); |
| 213 | + |
| 214 | + queue.dispatch(0); |
| 215 | + |
| 216 | + TEST_ASSERT_EQUAL(counter, 15); |
| 217 | +} |
| 218 | + |
| 219 | +void event_inference_test() { |
| 220 | + counter = 0; |
| 221 | + EventQueue queue (2048); |
| 222 | + |
| 223 | + queue.event(count5, 1, 1, 1, 1, 1).post(); |
| 224 | + queue.event(count5, 1, 1, 1, 1).post(1); |
| 225 | + queue.event(count5, 1, 1, 1).post(1, 1); |
| 226 | + queue.event(count5, 1, 1).post(1, 1, 1); |
| 227 | + queue.event(count5, 1).post(1, 1, 1, 1); |
| 228 | + queue.event(count5).post(1, 1, 1, 1, 1); |
| 229 | + |
| 230 | + queue.dispatch(0); |
| 231 | + |
| 232 | + TEST_ASSERT_EQUAL(counter, 30); |
| 233 | +} |
| 234 | + |
| 235 | + |
| 236 | +// Test setup |
| 237 | +utest::v1::status_t test_setup(const size_t number_of_cases) { |
| 238 | + GREENTEA_SETUP(20, "default_auto"); |
| 239 | + return verbose_test_setup_handler(number_of_cases); |
| 240 | +} |
| 241 | + |
| 242 | +const Case cases[] = { |
| 243 | + Case("Testing calls with 5 args", simple_posts_test5), |
| 244 | + Case("Testing calls with 4 args", simple_posts_test4), |
| 245 | + Case("Testing calls with 3 args", simple_posts_test3), |
| 246 | + Case("Testing calls with 2 args", simple_posts_test2), |
| 247 | + Case("Testing calls with 1 args", simple_posts_test1), |
| 248 | + Case("Testing calls with 0 args", simple_posts_test0), |
| 249 | + |
| 250 | + Case("Testing call_in", call_in_test<20>), |
| 251 | + Case("Testing call_every", call_every_test<20>), |
| 252 | + |
| 253 | + Case("Testing allocate failure 1", allocate_failure_test1), |
| 254 | + Case("Testing allocate failure 2", allocate_failure_test2), |
| 255 | + |
| 256 | + Case("Testing event cancel 1", cancel_test1<20>), |
| 257 | + Case("Testing the event class", event_class_test), |
| 258 | + Case("Testing the event class helpers", event_class_helper_test), |
| 259 | + Case("Testing the event inference", event_inference_test), |
| 260 | +}; |
| 261 | + |
| 262 | +Specification specification(test_setup, cases); |
| 263 | + |
| 264 | +int main() { |
| 265 | + return !Harness::run(specification); |
| 266 | +} |
| 267 | + |
0 commit comments