mbed os with nrf51 internal bandgap enabled to read battery level

Dependents:   BLE_file_test BLE_Blink ExternalEncoder

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

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