This is a fork of the `events` subdirectory of https://github.com/ARMmbed/mbed-os
Dependents: HelloWorld_CCA01M1 HelloWorld_CCA02M1 CI-data-logger-server HelloWorld_CCA02M1 ... more
This is a fork of the events
subdirectory of https://github.com/ARMmbed/mbed-os.
Note, you must import this library with import name: events
!!!
equeue/tests/tests.c@0:a792d4bf36c2, 2016-09-29 (annotated)
- Committer:
- Bogdan Marinescu
- Date:
- Thu Sep 29 12:38:02 2016 +0300
- Revision:
- 0:a792d4bf36c2
- Child:
- 11:1bc395c82a41
Added mbed-events library
Added mbed-events from https://github.com/ARMMbed/mbed-events. Changes
from upstream:
- the whole code is licensed under the Apache license. Sources and
headers were updates with this information.
- removed the porting layers for Windows and FreeRTOS and the references
to these porting layers in equeue_platform.h.
- moved the TESTS directory in mbed-events to the TESTS directory of
mbed-os.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Bogdan Marinescu |
0:a792d4bf36c2 | 1 | /* |
Bogdan Marinescu |
0:a792d4bf36c2 | 2 | * Testing framework for the events library |
Bogdan Marinescu |
0:a792d4bf36c2 | 3 | * |
Bogdan Marinescu |
0:a792d4bf36c2 | 4 | * Copyright (c) 2016 Christopher Haster |
Bogdan Marinescu |
0:a792d4bf36c2 | 5 | * |
Bogdan Marinescu |
0:a792d4bf36c2 | 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Bogdan Marinescu |
0:a792d4bf36c2 | 7 | * you may not use this file except in compliance with the License. |
Bogdan Marinescu |
0:a792d4bf36c2 | 8 | * You may obtain a copy of the License at |
Bogdan Marinescu |
0:a792d4bf36c2 | 9 | * |
Bogdan Marinescu |
0:a792d4bf36c2 | 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
Bogdan Marinescu |
0:a792d4bf36c2 | 11 | * |
Bogdan Marinescu |
0:a792d4bf36c2 | 12 | * Unless required by applicable law or agreed to in writing, software |
Bogdan Marinescu |
0:a792d4bf36c2 | 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
Bogdan Marinescu |
0:a792d4bf36c2 | 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Bogdan Marinescu |
0:a792d4bf36c2 | 15 | * See the License for the specific language governing permissions and |
Bogdan Marinescu |
0:a792d4bf36c2 | 16 | * limitations under the License. |
Bogdan Marinescu |
0:a792d4bf36c2 | 17 | */ |
Bogdan Marinescu |
0:a792d4bf36c2 | 18 | #include "equeue.h" |
Bogdan Marinescu |
0:a792d4bf36c2 | 19 | #include <unistd.h> |
Bogdan Marinescu |
0:a792d4bf36c2 | 20 | #include <stdio.h> |
Bogdan Marinescu |
0:a792d4bf36c2 | 21 | #include <setjmp.h> |
Bogdan Marinescu |
0:a792d4bf36c2 | 22 | #include <stdint.h> |
Bogdan Marinescu |
0:a792d4bf36c2 | 23 | #include <stdlib.h> |
Bogdan Marinescu |
0:a792d4bf36c2 | 24 | #include <pthread.h> |
Bogdan Marinescu |
0:a792d4bf36c2 | 25 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 26 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 27 | // Testing setup |
Bogdan Marinescu |
0:a792d4bf36c2 | 28 | static jmp_buf test_buf; |
Bogdan Marinescu |
0:a792d4bf36c2 | 29 | static int test_line; |
Bogdan Marinescu |
0:a792d4bf36c2 | 30 | static int test_failure; |
Bogdan Marinescu |
0:a792d4bf36c2 | 31 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 32 | #define test_assert(test) ({ \ |
Bogdan Marinescu |
0:a792d4bf36c2 | 33 | if (!(test)) { \ |
Bogdan Marinescu |
0:a792d4bf36c2 | 34 | test_line = __LINE__; \ |
Bogdan Marinescu |
0:a792d4bf36c2 | 35 | longjmp(test_buf, 1); \ |
Bogdan Marinescu |
0:a792d4bf36c2 | 36 | } \ |
Bogdan Marinescu |
0:a792d4bf36c2 | 37 | }) |
Bogdan Marinescu |
0:a792d4bf36c2 | 38 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 39 | #define test_run(func, ...) ({ \ |
Bogdan Marinescu |
0:a792d4bf36c2 | 40 | printf("%s: ...", #func); \ |
Bogdan Marinescu |
0:a792d4bf36c2 | 41 | fflush(stdout); \ |
Bogdan Marinescu |
0:a792d4bf36c2 | 42 | \ |
Bogdan Marinescu |
0:a792d4bf36c2 | 43 | if (!setjmp(test_buf)) { \ |
Bogdan Marinescu |
0:a792d4bf36c2 | 44 | func(__VA_ARGS__); \ |
Bogdan Marinescu |
0:a792d4bf36c2 | 45 | printf("\r%s: \e[32mpassed\e[0m\n", #func); \ |
Bogdan Marinescu |
0:a792d4bf36c2 | 46 | } else { \ |
Bogdan Marinescu |
0:a792d4bf36c2 | 47 | printf("\r%s: \e[31mfailed\e[0m at line %d\n", #func, test_line); \ |
Bogdan Marinescu |
0:a792d4bf36c2 | 48 | test_failure = true; \ |
Bogdan Marinescu |
0:a792d4bf36c2 | 49 | } \ |
Bogdan Marinescu |
0:a792d4bf36c2 | 50 | }) |
Bogdan Marinescu |
0:a792d4bf36c2 | 51 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 52 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 53 | // Test functions |
Bogdan Marinescu |
0:a792d4bf36c2 | 54 | void pass_func(void *eh) { |
Bogdan Marinescu |
0:a792d4bf36c2 | 55 | } |
Bogdan Marinescu |
0:a792d4bf36c2 | 56 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 57 | void simple_func(void *p) { |
Bogdan Marinescu |
0:a792d4bf36c2 | 58 | (*(int *)p)++; |
Bogdan Marinescu |
0:a792d4bf36c2 | 59 | } |
Bogdan Marinescu |
0:a792d4bf36c2 | 60 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 61 | void sloth_func(void *p) { |
Bogdan Marinescu |
0:a792d4bf36c2 | 62 | usleep(10000); |
Bogdan Marinescu |
0:a792d4bf36c2 | 63 | (*(int *)p)++; |
Bogdan Marinescu |
0:a792d4bf36c2 | 64 | } |
Bogdan Marinescu |
0:a792d4bf36c2 | 65 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 66 | struct indirect { |
Bogdan Marinescu |
0:a792d4bf36c2 | 67 | int *touched; |
Bogdan Marinescu |
0:a792d4bf36c2 | 68 | uint8_t buffer[7]; |
Bogdan Marinescu |
0:a792d4bf36c2 | 69 | }; |
Bogdan Marinescu |
0:a792d4bf36c2 | 70 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 71 | void indirect_func(void *p) { |
Bogdan Marinescu |
0:a792d4bf36c2 | 72 | struct indirect *i = (struct indirect*)p; |
Bogdan Marinescu |
0:a792d4bf36c2 | 73 | (*i->touched)++; |
Bogdan Marinescu |
0:a792d4bf36c2 | 74 | } |
Bogdan Marinescu |
0:a792d4bf36c2 | 75 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 76 | struct timing { |
Bogdan Marinescu |
0:a792d4bf36c2 | 77 | unsigned tick; |
Bogdan Marinescu |
0:a792d4bf36c2 | 78 | unsigned delay; |
Bogdan Marinescu |
0:a792d4bf36c2 | 79 | }; |
Bogdan Marinescu |
0:a792d4bf36c2 | 80 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 81 | void timing_func(void *p) { |
Bogdan Marinescu |
0:a792d4bf36c2 | 82 | struct timing *timing = (struct timing*)p; |
Bogdan Marinescu |
0:a792d4bf36c2 | 83 | unsigned tick = equeue_tick(); |
Bogdan Marinescu |
0:a792d4bf36c2 | 84 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 85 | unsigned t1 = timing->delay; |
Bogdan Marinescu |
0:a792d4bf36c2 | 86 | unsigned t2 = tick - timing->tick; |
Bogdan Marinescu |
0:a792d4bf36c2 | 87 | test_assert(t1 > t2 - 10 && t1 < t2 + 10); |
Bogdan Marinescu |
0:a792d4bf36c2 | 88 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 89 | timing->tick = tick; |
Bogdan Marinescu |
0:a792d4bf36c2 | 90 | } |
Bogdan Marinescu |
0:a792d4bf36c2 | 91 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 92 | struct fragment { |
Bogdan Marinescu |
0:a792d4bf36c2 | 93 | equeue_t *q; |
Bogdan Marinescu |
0:a792d4bf36c2 | 94 | size_t size; |
Bogdan Marinescu |
0:a792d4bf36c2 | 95 | struct timing timing; |
Bogdan Marinescu |
0:a792d4bf36c2 | 96 | }; |
Bogdan Marinescu |
0:a792d4bf36c2 | 97 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 98 | void fragment_func(void *p) { |
Bogdan Marinescu |
0:a792d4bf36c2 | 99 | struct fragment *fragment = (struct fragment*)p; |
Bogdan Marinescu |
0:a792d4bf36c2 | 100 | timing_func(&fragment->timing); |
Bogdan Marinescu |
0:a792d4bf36c2 | 101 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 102 | struct fragment *nfragment = equeue_alloc(fragment->q, fragment->size); |
Bogdan Marinescu |
0:a792d4bf36c2 | 103 | test_assert(nfragment); |
Bogdan Marinescu |
0:a792d4bf36c2 | 104 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 105 | *nfragment = *fragment; |
Bogdan Marinescu |
0:a792d4bf36c2 | 106 | equeue_event_delay(nfragment, fragment->timing.delay); |
Bogdan Marinescu |
0:a792d4bf36c2 | 107 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 108 | int id = equeue_post(nfragment->q, fragment_func, nfragment); |
Bogdan Marinescu |
0:a792d4bf36c2 | 109 | test_assert(id); |
Bogdan Marinescu |
0:a792d4bf36c2 | 110 | } |
Bogdan Marinescu |
0:a792d4bf36c2 | 111 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 112 | struct cancel { |
Bogdan Marinescu |
0:a792d4bf36c2 | 113 | equeue_t *q; |
Bogdan Marinescu |
0:a792d4bf36c2 | 114 | int id; |
Bogdan Marinescu |
0:a792d4bf36c2 | 115 | }; |
Bogdan Marinescu |
0:a792d4bf36c2 | 116 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 117 | void cancel_func(void *p) { |
Bogdan Marinescu |
0:a792d4bf36c2 | 118 | struct cancel *cancel = (struct cancel *)p; |
Bogdan Marinescu |
0:a792d4bf36c2 | 119 | equeue_cancel(cancel->q, cancel->id); |
Bogdan Marinescu |
0:a792d4bf36c2 | 120 | } |
Bogdan Marinescu |
0:a792d4bf36c2 | 121 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 122 | struct nest { |
Bogdan Marinescu |
0:a792d4bf36c2 | 123 | equeue_t *q; |
Bogdan Marinescu |
0:a792d4bf36c2 | 124 | void (*cb)(void *); |
Bogdan Marinescu |
0:a792d4bf36c2 | 125 | void *data; |
Bogdan Marinescu |
0:a792d4bf36c2 | 126 | }; |
Bogdan Marinescu |
0:a792d4bf36c2 | 127 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 128 | void nest_func(void *p) { |
Bogdan Marinescu |
0:a792d4bf36c2 | 129 | struct nest *nest = (struct nest *)p; |
Bogdan Marinescu |
0:a792d4bf36c2 | 130 | equeue_call(nest->q, nest->cb, nest->data); |
Bogdan Marinescu |
0:a792d4bf36c2 | 131 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 132 | usleep(10000); |
Bogdan Marinescu |
0:a792d4bf36c2 | 133 | } |
Bogdan Marinescu |
0:a792d4bf36c2 | 134 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 135 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 136 | // Simple call tests |
Bogdan Marinescu |
0:a792d4bf36c2 | 137 | void simple_call_test(void) { |
Bogdan Marinescu |
0:a792d4bf36c2 | 138 | equeue_t q; |
Bogdan Marinescu |
0:a792d4bf36c2 | 139 | int err = equeue_create(&q, 2048); |
Bogdan Marinescu |
0:a792d4bf36c2 | 140 | test_assert(!err); |
Bogdan Marinescu |
0:a792d4bf36c2 | 141 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 142 | bool touched = false; |
Bogdan Marinescu |
0:a792d4bf36c2 | 143 | equeue_call(&q, simple_func, &touched); |
Bogdan Marinescu |
0:a792d4bf36c2 | 144 | equeue_dispatch(&q, 0); |
Bogdan Marinescu |
0:a792d4bf36c2 | 145 | test_assert(touched); |
Bogdan Marinescu |
0:a792d4bf36c2 | 146 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 147 | equeue_destroy(&q); |
Bogdan Marinescu |
0:a792d4bf36c2 | 148 | } |
Bogdan Marinescu |
0:a792d4bf36c2 | 149 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 150 | void simple_call_in_test(void) { |
Bogdan Marinescu |
0:a792d4bf36c2 | 151 | equeue_t q; |
Bogdan Marinescu |
0:a792d4bf36c2 | 152 | int err = equeue_create(&q, 2048); |
Bogdan Marinescu |
0:a792d4bf36c2 | 153 | test_assert(!err); |
Bogdan Marinescu |
0:a792d4bf36c2 | 154 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 155 | bool touched = false; |
Bogdan Marinescu |
0:a792d4bf36c2 | 156 | int id = equeue_call_in(&q, 10, simple_func, &touched); |
Bogdan Marinescu |
0:a792d4bf36c2 | 157 | test_assert(id); |
Bogdan Marinescu |
0:a792d4bf36c2 | 158 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 159 | equeue_dispatch(&q, 15); |
Bogdan Marinescu |
0:a792d4bf36c2 | 160 | test_assert(touched); |
Bogdan Marinescu |
0:a792d4bf36c2 | 161 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 162 | equeue_destroy(&q); |
Bogdan Marinescu |
0:a792d4bf36c2 | 163 | } |
Bogdan Marinescu |
0:a792d4bf36c2 | 164 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 165 | void simple_call_every_test(void) { |
Bogdan Marinescu |
0:a792d4bf36c2 | 166 | equeue_t q; |
Bogdan Marinescu |
0:a792d4bf36c2 | 167 | int err = equeue_create(&q, 2048); |
Bogdan Marinescu |
0:a792d4bf36c2 | 168 | test_assert(!err); |
Bogdan Marinescu |
0:a792d4bf36c2 | 169 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 170 | bool touched = false; |
Bogdan Marinescu |
0:a792d4bf36c2 | 171 | int id = equeue_call_every(&q, 10, simple_func, &touched); |
Bogdan Marinescu |
0:a792d4bf36c2 | 172 | test_assert(id); |
Bogdan Marinescu |
0:a792d4bf36c2 | 173 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 174 | equeue_dispatch(&q, 15); |
Bogdan Marinescu |
0:a792d4bf36c2 | 175 | test_assert(touched); |
Bogdan Marinescu |
0:a792d4bf36c2 | 176 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 177 | equeue_destroy(&q); |
Bogdan Marinescu |
0:a792d4bf36c2 | 178 | } |
Bogdan Marinescu |
0:a792d4bf36c2 | 179 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 180 | void simple_post_test(void) { |
Bogdan Marinescu |
0:a792d4bf36c2 | 181 | equeue_t q; |
Bogdan Marinescu |
0:a792d4bf36c2 | 182 | int err = equeue_create(&q, 2048); |
Bogdan Marinescu |
0:a792d4bf36c2 | 183 | test_assert(!err); |
Bogdan Marinescu |
0:a792d4bf36c2 | 184 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 185 | int touched = false; |
Bogdan Marinescu |
0:a792d4bf36c2 | 186 | struct indirect *i = equeue_alloc(&q, sizeof(struct indirect)); |
Bogdan Marinescu |
0:a792d4bf36c2 | 187 | test_assert(i); |
Bogdan Marinescu |
0:a792d4bf36c2 | 188 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 189 | i->touched = &touched; |
Bogdan Marinescu |
0:a792d4bf36c2 | 190 | int id = equeue_post(&q, indirect_func, i); |
Bogdan Marinescu |
0:a792d4bf36c2 | 191 | test_assert(id); |
Bogdan Marinescu |
0:a792d4bf36c2 | 192 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 193 | equeue_dispatch(&q, 0); |
Bogdan Marinescu |
0:a792d4bf36c2 | 194 | test_assert(*i->touched); |
Bogdan Marinescu |
0:a792d4bf36c2 | 195 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 196 | equeue_destroy(&q); |
Bogdan Marinescu |
0:a792d4bf36c2 | 197 | } |
Bogdan Marinescu |
0:a792d4bf36c2 | 198 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 199 | // Misc tests |
Bogdan Marinescu |
0:a792d4bf36c2 | 200 | void destructor_test(void) { |
Bogdan Marinescu |
0:a792d4bf36c2 | 201 | equeue_t q; |
Bogdan Marinescu |
0:a792d4bf36c2 | 202 | int err = equeue_create(&q, 2048); |
Bogdan Marinescu |
0:a792d4bf36c2 | 203 | test_assert(!err); |
Bogdan Marinescu |
0:a792d4bf36c2 | 204 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 205 | int touched; |
Bogdan Marinescu |
0:a792d4bf36c2 | 206 | struct indirect *e; |
Bogdan Marinescu |
0:a792d4bf36c2 | 207 | int ids[3]; |
Bogdan Marinescu |
0:a792d4bf36c2 | 208 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 209 | touched = 0; |
Bogdan Marinescu |
0:a792d4bf36c2 | 210 | for (int i = 0; i < 3; i++) { |
Bogdan Marinescu |
0:a792d4bf36c2 | 211 | e = equeue_alloc(&q, sizeof(struct indirect)); |
Bogdan Marinescu |
0:a792d4bf36c2 | 212 | test_assert(e); |
Bogdan Marinescu |
0:a792d4bf36c2 | 213 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 214 | e->touched = &touched; |
Bogdan Marinescu |
0:a792d4bf36c2 | 215 | equeue_event_dtor(e, indirect_func); |
Bogdan Marinescu |
0:a792d4bf36c2 | 216 | int id = equeue_post(&q, pass_func, e); |
Bogdan Marinescu |
0:a792d4bf36c2 | 217 | test_assert(id); |
Bogdan Marinescu |
0:a792d4bf36c2 | 218 | } |
Bogdan Marinescu |
0:a792d4bf36c2 | 219 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 220 | equeue_dispatch(&q, 0); |
Bogdan Marinescu |
0:a792d4bf36c2 | 221 | test_assert(touched == 3); |
Bogdan Marinescu |
0:a792d4bf36c2 | 222 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 223 | touched = 0; |
Bogdan Marinescu |
0:a792d4bf36c2 | 224 | for (int i = 0; i < 3; i++) { |
Bogdan Marinescu |
0:a792d4bf36c2 | 225 | e = equeue_alloc(&q, sizeof(struct indirect)); |
Bogdan Marinescu |
0:a792d4bf36c2 | 226 | test_assert(e); |
Bogdan Marinescu |
0:a792d4bf36c2 | 227 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 228 | e->touched = &touched; |
Bogdan Marinescu |
0:a792d4bf36c2 | 229 | equeue_event_dtor(e, indirect_func); |
Bogdan Marinescu |
0:a792d4bf36c2 | 230 | ids[i] = equeue_post(&q, pass_func, e); |
Bogdan Marinescu |
0:a792d4bf36c2 | 231 | test_assert(ids[i]); |
Bogdan Marinescu |
0:a792d4bf36c2 | 232 | } |
Bogdan Marinescu |
0:a792d4bf36c2 | 233 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 234 | for (int i = 0; i < 3; i++) { |
Bogdan Marinescu |
0:a792d4bf36c2 | 235 | equeue_cancel(&q, ids[i]); |
Bogdan Marinescu |
0:a792d4bf36c2 | 236 | } |
Bogdan Marinescu |
0:a792d4bf36c2 | 237 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 238 | equeue_dispatch(&q, 0); |
Bogdan Marinescu |
0:a792d4bf36c2 | 239 | test_assert(touched == 3); |
Bogdan Marinescu |
0:a792d4bf36c2 | 240 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 241 | touched = 0; |
Bogdan Marinescu |
0:a792d4bf36c2 | 242 | for (int i = 0; i < 3; i++) { |
Bogdan Marinescu |
0:a792d4bf36c2 | 243 | e = equeue_alloc(&q, sizeof(struct indirect)); |
Bogdan Marinescu |
0:a792d4bf36c2 | 244 | test_assert(e); |
Bogdan Marinescu |
0:a792d4bf36c2 | 245 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 246 | e->touched = &touched; |
Bogdan Marinescu |
0:a792d4bf36c2 | 247 | equeue_event_dtor(e, indirect_func); |
Bogdan Marinescu |
0:a792d4bf36c2 | 248 | int id = equeue_post(&q, pass_func, e); |
Bogdan Marinescu |
0:a792d4bf36c2 | 249 | test_assert(id); |
Bogdan Marinescu |
0:a792d4bf36c2 | 250 | } |
Bogdan Marinescu |
0:a792d4bf36c2 | 251 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 252 | equeue_destroy(&q); |
Bogdan Marinescu |
0:a792d4bf36c2 | 253 | test_assert(touched == 3); |
Bogdan Marinescu |
0:a792d4bf36c2 | 254 | } |
Bogdan Marinescu |
0:a792d4bf36c2 | 255 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 256 | void allocation_failure_test(void) { |
Bogdan Marinescu |
0:a792d4bf36c2 | 257 | equeue_t q; |
Bogdan Marinescu |
0:a792d4bf36c2 | 258 | int err = equeue_create(&q, 2048); |
Bogdan Marinescu |
0:a792d4bf36c2 | 259 | test_assert(!err); |
Bogdan Marinescu |
0:a792d4bf36c2 | 260 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 261 | void *p = equeue_alloc(&q, 4096); |
Bogdan Marinescu |
0:a792d4bf36c2 | 262 | test_assert(!p); |
Bogdan Marinescu |
0:a792d4bf36c2 | 263 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 264 | for (int i = 0; i < 100; i++) { |
Bogdan Marinescu |
0:a792d4bf36c2 | 265 | p = equeue_alloc(&q, 0); |
Bogdan Marinescu |
0:a792d4bf36c2 | 266 | } |
Bogdan Marinescu |
0:a792d4bf36c2 | 267 | test_assert(!p); |
Bogdan Marinescu |
0:a792d4bf36c2 | 268 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 269 | equeue_destroy(&q); |
Bogdan Marinescu |
0:a792d4bf36c2 | 270 | } |
Bogdan Marinescu |
0:a792d4bf36c2 | 271 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 272 | void cancel_test(int N) { |
Bogdan Marinescu |
0:a792d4bf36c2 | 273 | equeue_t q; |
Bogdan Marinescu |
0:a792d4bf36c2 | 274 | int err = equeue_create(&q, 2048); |
Bogdan Marinescu |
0:a792d4bf36c2 | 275 | test_assert(!err); |
Bogdan Marinescu |
0:a792d4bf36c2 | 276 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 277 | bool touched = false; |
Bogdan Marinescu |
0:a792d4bf36c2 | 278 | int *ids = malloc(N*sizeof(int)); |
Bogdan Marinescu |
0:a792d4bf36c2 | 279 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 280 | for (int i = 0; i < N; i++) { |
Bogdan Marinescu |
0:a792d4bf36c2 | 281 | ids[i] = equeue_call(&q, simple_func, &touched); |
Bogdan Marinescu |
0:a792d4bf36c2 | 282 | } |
Bogdan Marinescu |
0:a792d4bf36c2 | 283 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 284 | for (int i = N-1; i >= 0; i--) { |
Bogdan Marinescu |
0:a792d4bf36c2 | 285 | equeue_cancel(&q, ids[i]); |
Bogdan Marinescu |
0:a792d4bf36c2 | 286 | } |
Bogdan Marinescu |
0:a792d4bf36c2 | 287 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 288 | free(ids); |
Bogdan Marinescu |
0:a792d4bf36c2 | 289 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 290 | equeue_dispatch(&q, 0); |
Bogdan Marinescu |
0:a792d4bf36c2 | 291 | test_assert(!touched); |
Bogdan Marinescu |
0:a792d4bf36c2 | 292 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 293 | equeue_destroy(&q); |
Bogdan Marinescu |
0:a792d4bf36c2 | 294 | } |
Bogdan Marinescu |
0:a792d4bf36c2 | 295 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 296 | void cancel_inflight_test(void) { |
Bogdan Marinescu |
0:a792d4bf36c2 | 297 | equeue_t q; |
Bogdan Marinescu |
0:a792d4bf36c2 | 298 | int err = equeue_create(&q, 2048); |
Bogdan Marinescu |
0:a792d4bf36c2 | 299 | test_assert(!err); |
Bogdan Marinescu |
0:a792d4bf36c2 | 300 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 301 | bool touched = false; |
Bogdan Marinescu |
0:a792d4bf36c2 | 302 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 303 | int id = equeue_call(&q, simple_func, &touched); |
Bogdan Marinescu |
0:a792d4bf36c2 | 304 | equeue_cancel(&q, id); |
Bogdan Marinescu |
0:a792d4bf36c2 | 305 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 306 | equeue_dispatch(&q, 0); |
Bogdan Marinescu |
0:a792d4bf36c2 | 307 | test_assert(!touched); |
Bogdan Marinescu |
0:a792d4bf36c2 | 308 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 309 | id = equeue_call(&q, simple_func, &touched); |
Bogdan Marinescu |
0:a792d4bf36c2 | 310 | equeue_cancel(&q, id); |
Bogdan Marinescu |
0:a792d4bf36c2 | 311 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 312 | equeue_dispatch(&q, 0); |
Bogdan Marinescu |
0:a792d4bf36c2 | 313 | test_assert(!touched); |
Bogdan Marinescu |
0:a792d4bf36c2 | 314 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 315 | struct cancel *cancel = equeue_alloc(&q, sizeof(struct cancel)); |
Bogdan Marinescu |
0:a792d4bf36c2 | 316 | test_assert(cancel); |
Bogdan Marinescu |
0:a792d4bf36c2 | 317 | cancel->q = &q; |
Bogdan Marinescu |
0:a792d4bf36c2 | 318 | cancel->id = 0; |
Bogdan Marinescu |
0:a792d4bf36c2 | 319 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 320 | id = equeue_post(&q, cancel_func, cancel); |
Bogdan Marinescu |
0:a792d4bf36c2 | 321 | test_assert(id); |
Bogdan Marinescu |
0:a792d4bf36c2 | 322 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 323 | cancel->id = equeue_call(&q, simple_func, &touched); |
Bogdan Marinescu |
0:a792d4bf36c2 | 324 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 325 | equeue_dispatch(&q, 0); |
Bogdan Marinescu |
0:a792d4bf36c2 | 326 | test_assert(!touched); |
Bogdan Marinescu |
0:a792d4bf36c2 | 327 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 328 | equeue_destroy(&q); |
Bogdan Marinescu |
0:a792d4bf36c2 | 329 | } |
Bogdan Marinescu |
0:a792d4bf36c2 | 330 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 331 | void cancel_unnecessarily_test(void) { |
Bogdan Marinescu |
0:a792d4bf36c2 | 332 | equeue_t q; |
Bogdan Marinescu |
0:a792d4bf36c2 | 333 | int err = equeue_create(&q, 2048); |
Bogdan Marinescu |
0:a792d4bf36c2 | 334 | test_assert(!err); |
Bogdan Marinescu |
0:a792d4bf36c2 | 335 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 336 | int id = equeue_call(&q, pass_func, 0); |
Bogdan Marinescu |
0:a792d4bf36c2 | 337 | for (int i = 0; i < 5; i++) { |
Bogdan Marinescu |
0:a792d4bf36c2 | 338 | equeue_cancel(&q, id); |
Bogdan Marinescu |
0:a792d4bf36c2 | 339 | } |
Bogdan Marinescu |
0:a792d4bf36c2 | 340 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 341 | id = equeue_call(&q, pass_func, 0); |
Bogdan Marinescu |
0:a792d4bf36c2 | 342 | equeue_dispatch(&q, 0); |
Bogdan Marinescu |
0:a792d4bf36c2 | 343 | for (int i = 0; i < 5; i++) { |
Bogdan Marinescu |
0:a792d4bf36c2 | 344 | equeue_cancel(&q, id); |
Bogdan Marinescu |
0:a792d4bf36c2 | 345 | } |
Bogdan Marinescu |
0:a792d4bf36c2 | 346 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 347 | bool touched = false; |
Bogdan Marinescu |
0:a792d4bf36c2 | 348 | equeue_call(&q, simple_func, &touched); |
Bogdan Marinescu |
0:a792d4bf36c2 | 349 | for (int i = 0; i < 5; i++) { |
Bogdan Marinescu |
0:a792d4bf36c2 | 350 | equeue_cancel(&q, id); |
Bogdan Marinescu |
0:a792d4bf36c2 | 351 | } |
Bogdan Marinescu |
0:a792d4bf36c2 | 352 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 353 | equeue_dispatch(&q, 0); |
Bogdan Marinescu |
0:a792d4bf36c2 | 354 | test_assert(touched); |
Bogdan Marinescu |
0:a792d4bf36c2 | 355 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 356 | equeue_destroy(&q); |
Bogdan Marinescu |
0:a792d4bf36c2 | 357 | } |
Bogdan Marinescu |
0:a792d4bf36c2 | 358 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 359 | void loop_protect_test(void) { |
Bogdan Marinescu |
0:a792d4bf36c2 | 360 | equeue_t q; |
Bogdan Marinescu |
0:a792d4bf36c2 | 361 | int err = equeue_create(&q, 2048); |
Bogdan Marinescu |
0:a792d4bf36c2 | 362 | test_assert(!err); |
Bogdan Marinescu |
0:a792d4bf36c2 | 363 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 364 | bool touched = false; |
Bogdan Marinescu |
0:a792d4bf36c2 | 365 | equeue_call_every(&q, 0, simple_func, &touched); |
Bogdan Marinescu |
0:a792d4bf36c2 | 366 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 367 | equeue_dispatch(&q, 0); |
Bogdan Marinescu |
0:a792d4bf36c2 | 368 | test_assert(touched); |
Bogdan Marinescu |
0:a792d4bf36c2 | 369 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 370 | touched = false; |
Bogdan Marinescu |
0:a792d4bf36c2 | 371 | equeue_call_every(&q, 1, simple_func, &touched); |
Bogdan Marinescu |
0:a792d4bf36c2 | 372 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 373 | equeue_dispatch(&q, 0); |
Bogdan Marinescu |
0:a792d4bf36c2 | 374 | test_assert(touched); |
Bogdan Marinescu |
0:a792d4bf36c2 | 375 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 376 | equeue_destroy(&q); |
Bogdan Marinescu |
0:a792d4bf36c2 | 377 | } |
Bogdan Marinescu |
0:a792d4bf36c2 | 378 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 379 | void break_test(void) { |
Bogdan Marinescu |
0:a792d4bf36c2 | 380 | equeue_t q; |
Bogdan Marinescu |
0:a792d4bf36c2 | 381 | int err = equeue_create(&q, 2048); |
Bogdan Marinescu |
0:a792d4bf36c2 | 382 | test_assert(!err); |
Bogdan Marinescu |
0:a792d4bf36c2 | 383 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 384 | bool touched = false; |
Bogdan Marinescu |
0:a792d4bf36c2 | 385 | equeue_call_every(&q, 0, simple_func, &touched); |
Bogdan Marinescu |
0:a792d4bf36c2 | 386 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 387 | equeue_break(&q); |
Bogdan Marinescu |
0:a792d4bf36c2 | 388 | equeue_dispatch(&q, -1); |
Bogdan Marinescu |
0:a792d4bf36c2 | 389 | test_assert(touched); |
Bogdan Marinescu |
0:a792d4bf36c2 | 390 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 391 | equeue_destroy(&q); |
Bogdan Marinescu |
0:a792d4bf36c2 | 392 | } |
Bogdan Marinescu |
0:a792d4bf36c2 | 393 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 394 | void period_test(void) { |
Bogdan Marinescu |
0:a792d4bf36c2 | 395 | equeue_t q; |
Bogdan Marinescu |
0:a792d4bf36c2 | 396 | int err = equeue_create(&q, 2048); |
Bogdan Marinescu |
0:a792d4bf36c2 | 397 | test_assert(!err); |
Bogdan Marinescu |
0:a792d4bf36c2 | 398 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 399 | int count = 0; |
Bogdan Marinescu |
0:a792d4bf36c2 | 400 | equeue_call_every(&q, 10, simple_func, &count); |
Bogdan Marinescu |
0:a792d4bf36c2 | 401 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 402 | equeue_dispatch(&q, 55); |
Bogdan Marinescu |
0:a792d4bf36c2 | 403 | test_assert(count == 5); |
Bogdan Marinescu |
0:a792d4bf36c2 | 404 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 405 | equeue_destroy(&q); |
Bogdan Marinescu |
0:a792d4bf36c2 | 406 | } |
Bogdan Marinescu |
0:a792d4bf36c2 | 407 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 408 | void nested_test(void) { |
Bogdan Marinescu |
0:a792d4bf36c2 | 409 | equeue_t q; |
Bogdan Marinescu |
0:a792d4bf36c2 | 410 | int err = equeue_create(&q, 2048); |
Bogdan Marinescu |
0:a792d4bf36c2 | 411 | test_assert(!err); |
Bogdan Marinescu |
0:a792d4bf36c2 | 412 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 413 | int touched = 0; |
Bogdan Marinescu |
0:a792d4bf36c2 | 414 | struct nest *nest = equeue_alloc(&q, sizeof(struct nest)); |
Bogdan Marinescu |
0:a792d4bf36c2 | 415 | test_assert(nest); |
Bogdan Marinescu |
0:a792d4bf36c2 | 416 | nest->q = &q; |
Bogdan Marinescu |
0:a792d4bf36c2 | 417 | nest->cb = simple_func; |
Bogdan Marinescu |
0:a792d4bf36c2 | 418 | nest->data = &touched; |
Bogdan Marinescu |
0:a792d4bf36c2 | 419 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 420 | int id = equeue_post(&q, nest_func, nest); |
Bogdan Marinescu |
0:a792d4bf36c2 | 421 | test_assert(id); |
Bogdan Marinescu |
0:a792d4bf36c2 | 422 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 423 | equeue_dispatch(&q, 5); |
Bogdan Marinescu |
0:a792d4bf36c2 | 424 | test_assert(touched == 0); |
Bogdan Marinescu |
0:a792d4bf36c2 | 425 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 426 | equeue_dispatch(&q, 5); |
Bogdan Marinescu |
0:a792d4bf36c2 | 427 | test_assert(touched == 1); |
Bogdan Marinescu |
0:a792d4bf36c2 | 428 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 429 | touched = 0; |
Bogdan Marinescu |
0:a792d4bf36c2 | 430 | nest = equeue_alloc(&q, sizeof(struct nest)); |
Bogdan Marinescu |
0:a792d4bf36c2 | 431 | test_assert(nest); |
Bogdan Marinescu |
0:a792d4bf36c2 | 432 | nest->q = &q; |
Bogdan Marinescu |
0:a792d4bf36c2 | 433 | nest->cb = simple_func; |
Bogdan Marinescu |
0:a792d4bf36c2 | 434 | nest->data = &touched; |
Bogdan Marinescu |
0:a792d4bf36c2 | 435 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 436 | id = equeue_post(&q, nest_func, nest); |
Bogdan Marinescu |
0:a792d4bf36c2 | 437 | test_assert(id); |
Bogdan Marinescu |
0:a792d4bf36c2 | 438 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 439 | equeue_dispatch(&q, 20); |
Bogdan Marinescu |
0:a792d4bf36c2 | 440 | test_assert(touched == 1); |
Bogdan Marinescu |
0:a792d4bf36c2 | 441 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 442 | equeue_destroy(&q); |
Bogdan Marinescu |
0:a792d4bf36c2 | 443 | } |
Bogdan Marinescu |
0:a792d4bf36c2 | 444 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 445 | void sloth_test(void) { |
Bogdan Marinescu |
0:a792d4bf36c2 | 446 | equeue_t q; |
Bogdan Marinescu |
0:a792d4bf36c2 | 447 | int err = equeue_create(&q, 2048); |
Bogdan Marinescu |
0:a792d4bf36c2 | 448 | test_assert(!err); |
Bogdan Marinescu |
0:a792d4bf36c2 | 449 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 450 | int touched = 0; |
Bogdan Marinescu |
0:a792d4bf36c2 | 451 | int id = equeue_call(&q, sloth_func, &touched); |
Bogdan Marinescu |
0:a792d4bf36c2 | 452 | test_assert(id); |
Bogdan Marinescu |
0:a792d4bf36c2 | 453 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 454 | id = equeue_call_in(&q, 5, simple_func, &touched); |
Bogdan Marinescu |
0:a792d4bf36c2 | 455 | test_assert(id); |
Bogdan Marinescu |
0:a792d4bf36c2 | 456 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 457 | id = equeue_call_in(&q, 15, simple_func, &touched); |
Bogdan Marinescu |
0:a792d4bf36c2 | 458 | test_assert(id); |
Bogdan Marinescu |
0:a792d4bf36c2 | 459 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 460 | equeue_dispatch(&q, 20); |
Bogdan Marinescu |
0:a792d4bf36c2 | 461 | test_assert(touched == 3); |
Bogdan Marinescu |
0:a792d4bf36c2 | 462 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 463 | equeue_destroy(&q); |
Bogdan Marinescu |
0:a792d4bf36c2 | 464 | } |
Bogdan Marinescu |
0:a792d4bf36c2 | 465 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 466 | void *multithread_thread(void *p) { |
Bogdan Marinescu |
0:a792d4bf36c2 | 467 | equeue_t *q = (equeue_t *)p; |
Bogdan Marinescu |
0:a792d4bf36c2 | 468 | equeue_dispatch(q, -1); |
Bogdan Marinescu |
0:a792d4bf36c2 | 469 | return 0; |
Bogdan Marinescu |
0:a792d4bf36c2 | 470 | } |
Bogdan Marinescu |
0:a792d4bf36c2 | 471 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 472 | void multithread_test(void) { |
Bogdan Marinescu |
0:a792d4bf36c2 | 473 | equeue_t q; |
Bogdan Marinescu |
0:a792d4bf36c2 | 474 | int err = equeue_create(&q, 2048); |
Bogdan Marinescu |
0:a792d4bf36c2 | 475 | test_assert(!err); |
Bogdan Marinescu |
0:a792d4bf36c2 | 476 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 477 | int touched = 0; |
Bogdan Marinescu |
0:a792d4bf36c2 | 478 | equeue_call_every(&q, 1, simple_func, &touched); |
Bogdan Marinescu |
0:a792d4bf36c2 | 479 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 480 | pthread_t thread; |
Bogdan Marinescu |
0:a792d4bf36c2 | 481 | err = pthread_create(&thread, 0, multithread_thread, &q); |
Bogdan Marinescu |
0:a792d4bf36c2 | 482 | test_assert(!err); |
Bogdan Marinescu |
0:a792d4bf36c2 | 483 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 484 | usleep(10000); |
Bogdan Marinescu |
0:a792d4bf36c2 | 485 | equeue_break(&q); |
Bogdan Marinescu |
0:a792d4bf36c2 | 486 | err = pthread_join(thread, 0); |
Bogdan Marinescu |
0:a792d4bf36c2 | 487 | test_assert(!err); |
Bogdan Marinescu |
0:a792d4bf36c2 | 488 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 489 | test_assert(touched); |
Bogdan Marinescu |
0:a792d4bf36c2 | 490 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 491 | equeue_destroy(&q); |
Bogdan Marinescu |
0:a792d4bf36c2 | 492 | } |
Bogdan Marinescu |
0:a792d4bf36c2 | 493 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 494 | void background_func(void *p, int ms) { |
Bogdan Marinescu |
0:a792d4bf36c2 | 495 | *(unsigned *)p = ms; |
Bogdan Marinescu |
0:a792d4bf36c2 | 496 | } |
Bogdan Marinescu |
0:a792d4bf36c2 | 497 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 498 | void background_test(void) { |
Bogdan Marinescu |
0:a792d4bf36c2 | 499 | equeue_t q; |
Bogdan Marinescu |
0:a792d4bf36c2 | 500 | int err = equeue_create(&q, 2048); |
Bogdan Marinescu |
0:a792d4bf36c2 | 501 | test_assert(!err); |
Bogdan Marinescu |
0:a792d4bf36c2 | 502 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 503 | int id = equeue_call_in(&q, 20, pass_func, 0); |
Bogdan Marinescu |
0:a792d4bf36c2 | 504 | test_assert(id); |
Bogdan Marinescu |
0:a792d4bf36c2 | 505 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 506 | unsigned ms; |
Bogdan Marinescu |
0:a792d4bf36c2 | 507 | equeue_background(&q, background_func, &ms); |
Bogdan Marinescu |
0:a792d4bf36c2 | 508 | test_assert(ms == 20); |
Bogdan Marinescu |
0:a792d4bf36c2 | 509 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 510 | id = equeue_call_in(&q, 10, pass_func, 0); |
Bogdan Marinescu |
0:a792d4bf36c2 | 511 | test_assert(id); |
Bogdan Marinescu |
0:a792d4bf36c2 | 512 | test_assert(ms == 10); |
Bogdan Marinescu |
0:a792d4bf36c2 | 513 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 514 | id = equeue_call(&q, pass_func, 0); |
Bogdan Marinescu |
0:a792d4bf36c2 | 515 | test_assert(id); |
Bogdan Marinescu |
0:a792d4bf36c2 | 516 | test_assert(ms == 0); |
Bogdan Marinescu |
0:a792d4bf36c2 | 517 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 518 | equeue_dispatch(&q, 0); |
Bogdan Marinescu |
0:a792d4bf36c2 | 519 | test_assert(ms == 10); |
Bogdan Marinescu |
0:a792d4bf36c2 | 520 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 521 | equeue_destroy(&q); |
Bogdan Marinescu |
0:a792d4bf36c2 | 522 | test_assert(ms == -1); |
Bogdan Marinescu |
0:a792d4bf36c2 | 523 | } |
Bogdan Marinescu |
0:a792d4bf36c2 | 524 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 525 | void chain_test(void) { |
Bogdan Marinescu |
0:a792d4bf36c2 | 526 | equeue_t q1; |
Bogdan Marinescu |
0:a792d4bf36c2 | 527 | int err = equeue_create(&q1, 2048); |
Bogdan Marinescu |
0:a792d4bf36c2 | 528 | test_assert(!err); |
Bogdan Marinescu |
0:a792d4bf36c2 | 529 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 530 | equeue_t q2; |
Bogdan Marinescu |
0:a792d4bf36c2 | 531 | err = equeue_create(&q2, 2048); |
Bogdan Marinescu |
0:a792d4bf36c2 | 532 | test_assert(!err); |
Bogdan Marinescu |
0:a792d4bf36c2 | 533 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 534 | equeue_chain(&q2, &q1); |
Bogdan Marinescu |
0:a792d4bf36c2 | 535 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 536 | int touched = 0; |
Bogdan Marinescu |
0:a792d4bf36c2 | 537 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 538 | int id1 = equeue_call_in(&q1, 20, simple_func, &touched); |
Bogdan Marinescu |
0:a792d4bf36c2 | 539 | int id2 = equeue_call_in(&q2, 20, simple_func, &touched); |
Bogdan Marinescu |
0:a792d4bf36c2 | 540 | test_assert(id1 && id2); |
Bogdan Marinescu |
0:a792d4bf36c2 | 541 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 542 | id1 = equeue_call(&q1, simple_func, &touched); |
Bogdan Marinescu |
0:a792d4bf36c2 | 543 | id2 = equeue_call(&q2, simple_func, &touched); |
Bogdan Marinescu |
0:a792d4bf36c2 | 544 | test_assert(id1 && id2); |
Bogdan Marinescu |
0:a792d4bf36c2 | 545 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 546 | id1 = equeue_call_in(&q1, 5, simple_func, &touched); |
Bogdan Marinescu |
0:a792d4bf36c2 | 547 | id2 = equeue_call_in(&q2, 5, simple_func, &touched); |
Bogdan Marinescu |
0:a792d4bf36c2 | 548 | test_assert(id1 && id2); |
Bogdan Marinescu |
0:a792d4bf36c2 | 549 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 550 | equeue_cancel(&q1, id1); |
Bogdan Marinescu |
0:a792d4bf36c2 | 551 | equeue_cancel(&q2, id2); |
Bogdan Marinescu |
0:a792d4bf36c2 | 552 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 553 | id1 = equeue_call_in(&q1, 10, simple_func, &touched); |
Bogdan Marinescu |
0:a792d4bf36c2 | 554 | id2 = equeue_call_in(&q2, 10, simple_func, &touched); |
Bogdan Marinescu |
0:a792d4bf36c2 | 555 | test_assert(id1 && id2); |
Bogdan Marinescu |
0:a792d4bf36c2 | 556 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 557 | equeue_dispatch(&q1, 30); |
Bogdan Marinescu |
0:a792d4bf36c2 | 558 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 559 | test_assert(touched == 6); |
Bogdan Marinescu |
0:a792d4bf36c2 | 560 | } |
Bogdan Marinescu |
0:a792d4bf36c2 | 561 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 562 | // Barrage tests |
Bogdan Marinescu |
0:a792d4bf36c2 | 563 | void simple_barrage_test(int N) { |
Bogdan Marinescu |
0:a792d4bf36c2 | 564 | equeue_t q; |
Bogdan Marinescu |
0:a792d4bf36c2 | 565 | int err = equeue_create(&q, N*(EQUEUE_EVENT_SIZE+sizeof(struct timing))); |
Bogdan Marinescu |
0:a792d4bf36c2 | 566 | test_assert(!err); |
Bogdan Marinescu |
0:a792d4bf36c2 | 567 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 568 | for (int i = 0; i < N; i++) { |
Bogdan Marinescu |
0:a792d4bf36c2 | 569 | struct timing *timing = equeue_alloc(&q, sizeof(struct timing)); |
Bogdan Marinescu |
0:a792d4bf36c2 | 570 | test_assert(timing); |
Bogdan Marinescu |
0:a792d4bf36c2 | 571 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 572 | timing->tick = equeue_tick(); |
Bogdan Marinescu |
0:a792d4bf36c2 | 573 | timing->delay = (i+1)*100; |
Bogdan Marinescu |
0:a792d4bf36c2 | 574 | equeue_event_delay(timing, timing->delay); |
Bogdan Marinescu |
0:a792d4bf36c2 | 575 | equeue_event_period(timing, timing->delay); |
Bogdan Marinescu |
0:a792d4bf36c2 | 576 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 577 | int id = equeue_post(&q, timing_func, timing); |
Bogdan Marinescu |
0:a792d4bf36c2 | 578 | test_assert(id); |
Bogdan Marinescu |
0:a792d4bf36c2 | 579 | } |
Bogdan Marinescu |
0:a792d4bf36c2 | 580 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 581 | equeue_dispatch(&q, N*100); |
Bogdan Marinescu |
0:a792d4bf36c2 | 582 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 583 | equeue_destroy(&q); |
Bogdan Marinescu |
0:a792d4bf36c2 | 584 | } |
Bogdan Marinescu |
0:a792d4bf36c2 | 585 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 586 | void fragmenting_barrage_test(int N) { |
Bogdan Marinescu |
0:a792d4bf36c2 | 587 | equeue_t q; |
Bogdan Marinescu |
0:a792d4bf36c2 | 588 | int err = equeue_create(&q, |
Bogdan Marinescu |
0:a792d4bf36c2 | 589 | 2*N*(EQUEUE_EVENT_SIZE+sizeof(struct fragment)+N*sizeof(int))); |
Bogdan Marinescu |
0:a792d4bf36c2 | 590 | test_assert(!err); |
Bogdan Marinescu |
0:a792d4bf36c2 | 591 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 592 | for (int i = 0; i < N; i++) { |
Bogdan Marinescu |
0:a792d4bf36c2 | 593 | size_t size = sizeof(struct fragment) + i*sizeof(int); |
Bogdan Marinescu |
0:a792d4bf36c2 | 594 | struct fragment *fragment = equeue_alloc(&q, size); |
Bogdan Marinescu |
0:a792d4bf36c2 | 595 | test_assert(fragment); |
Bogdan Marinescu |
0:a792d4bf36c2 | 596 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 597 | fragment->q = &q; |
Bogdan Marinescu |
0:a792d4bf36c2 | 598 | fragment->size = size; |
Bogdan Marinescu |
0:a792d4bf36c2 | 599 | fragment->timing.tick = equeue_tick(); |
Bogdan Marinescu |
0:a792d4bf36c2 | 600 | fragment->timing.delay = (i+1)*100; |
Bogdan Marinescu |
0:a792d4bf36c2 | 601 | equeue_event_delay(fragment, fragment->timing.delay); |
Bogdan Marinescu |
0:a792d4bf36c2 | 602 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 603 | int id = equeue_post(&q, fragment_func, fragment); |
Bogdan Marinescu |
0:a792d4bf36c2 | 604 | test_assert(id); |
Bogdan Marinescu |
0:a792d4bf36c2 | 605 | } |
Bogdan Marinescu |
0:a792d4bf36c2 | 606 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 607 | equeue_dispatch(&q, N*100); |
Bogdan Marinescu |
0:a792d4bf36c2 | 608 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 609 | equeue_destroy(&q); |
Bogdan Marinescu |
0:a792d4bf36c2 | 610 | } |
Bogdan Marinescu |
0:a792d4bf36c2 | 611 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 612 | struct ethread { |
Bogdan Marinescu |
0:a792d4bf36c2 | 613 | pthread_t thread; |
Bogdan Marinescu |
0:a792d4bf36c2 | 614 | equeue_t *q; |
Bogdan Marinescu |
0:a792d4bf36c2 | 615 | int ms; |
Bogdan Marinescu |
0:a792d4bf36c2 | 616 | }; |
Bogdan Marinescu |
0:a792d4bf36c2 | 617 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 618 | static void *ethread_dispatch(void *p) { |
Bogdan Marinescu |
0:a792d4bf36c2 | 619 | struct ethread *t = (struct ethread*)p; |
Bogdan Marinescu |
0:a792d4bf36c2 | 620 | equeue_dispatch(t->q, t->ms); |
Bogdan Marinescu |
0:a792d4bf36c2 | 621 | return 0; |
Bogdan Marinescu |
0:a792d4bf36c2 | 622 | } |
Bogdan Marinescu |
0:a792d4bf36c2 | 623 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 624 | void multithreaded_barrage_test(int N) { |
Bogdan Marinescu |
0:a792d4bf36c2 | 625 | equeue_t q; |
Bogdan Marinescu |
0:a792d4bf36c2 | 626 | int err = equeue_create(&q, N*(EQUEUE_EVENT_SIZE+sizeof(struct timing))); |
Bogdan Marinescu |
0:a792d4bf36c2 | 627 | test_assert(!err); |
Bogdan Marinescu |
0:a792d4bf36c2 | 628 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 629 | struct ethread t; |
Bogdan Marinescu |
0:a792d4bf36c2 | 630 | t.q = &q; |
Bogdan Marinescu |
0:a792d4bf36c2 | 631 | t.ms = N*100; |
Bogdan Marinescu |
0:a792d4bf36c2 | 632 | err = pthread_create(&t.thread, 0, ethread_dispatch, &t); |
Bogdan Marinescu |
0:a792d4bf36c2 | 633 | test_assert(!err); |
Bogdan Marinescu |
0:a792d4bf36c2 | 634 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 635 | for (int i = 0; i < N; i++) { |
Bogdan Marinescu |
0:a792d4bf36c2 | 636 | struct timing *timing = equeue_alloc(&q, sizeof(struct timing)); |
Bogdan Marinescu |
0:a792d4bf36c2 | 637 | test_assert(timing); |
Bogdan Marinescu |
0:a792d4bf36c2 | 638 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 639 | timing->tick = equeue_tick(); |
Bogdan Marinescu |
0:a792d4bf36c2 | 640 | timing->delay = (i+1)*100; |
Bogdan Marinescu |
0:a792d4bf36c2 | 641 | equeue_event_delay(timing, timing->delay); |
Bogdan Marinescu |
0:a792d4bf36c2 | 642 | equeue_event_period(timing, timing->delay); |
Bogdan Marinescu |
0:a792d4bf36c2 | 643 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 644 | int id = equeue_post(&q, timing_func, timing); |
Bogdan Marinescu |
0:a792d4bf36c2 | 645 | test_assert(id); |
Bogdan Marinescu |
0:a792d4bf36c2 | 646 | } |
Bogdan Marinescu |
0:a792d4bf36c2 | 647 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 648 | err = pthread_join(t.thread, 0); |
Bogdan Marinescu |
0:a792d4bf36c2 | 649 | test_assert(!err); |
Bogdan Marinescu |
0:a792d4bf36c2 | 650 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 651 | equeue_destroy(&q); |
Bogdan Marinescu |
0:a792d4bf36c2 | 652 | } |
Bogdan Marinescu |
0:a792d4bf36c2 | 653 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 654 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 655 | int main() { |
Bogdan Marinescu |
0:a792d4bf36c2 | 656 | printf("beginning tests...\n"); |
Bogdan Marinescu |
0:a792d4bf36c2 | 657 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 658 | test_run(simple_call_test); |
Bogdan Marinescu |
0:a792d4bf36c2 | 659 | test_run(simple_call_in_test); |
Bogdan Marinescu |
0:a792d4bf36c2 | 660 | test_run(simple_call_every_test); |
Bogdan Marinescu |
0:a792d4bf36c2 | 661 | test_run(simple_post_test); |
Bogdan Marinescu |
0:a792d4bf36c2 | 662 | test_run(destructor_test); |
Bogdan Marinescu |
0:a792d4bf36c2 | 663 | test_run(allocation_failure_test); |
Bogdan Marinescu |
0:a792d4bf36c2 | 664 | test_run(cancel_test, 20); |
Bogdan Marinescu |
0:a792d4bf36c2 | 665 | test_run(cancel_inflight_test); |
Bogdan Marinescu |
0:a792d4bf36c2 | 666 | test_run(cancel_unnecessarily_test); |
Bogdan Marinescu |
0:a792d4bf36c2 | 667 | test_run(loop_protect_test); |
Bogdan Marinescu |
0:a792d4bf36c2 | 668 | test_run(break_test); |
Bogdan Marinescu |
0:a792d4bf36c2 | 669 | test_run(period_test); |
Bogdan Marinescu |
0:a792d4bf36c2 | 670 | test_run(nested_test); |
Bogdan Marinescu |
0:a792d4bf36c2 | 671 | test_run(sloth_test); |
Bogdan Marinescu |
0:a792d4bf36c2 | 672 | test_run(background_test); |
Bogdan Marinescu |
0:a792d4bf36c2 | 673 | test_run(chain_test); |
Bogdan Marinescu |
0:a792d4bf36c2 | 674 | test_run(multithread_test); |
Bogdan Marinescu |
0:a792d4bf36c2 | 675 | test_run(simple_barrage_test, 20); |
Bogdan Marinescu |
0:a792d4bf36c2 | 676 | test_run(fragmenting_barrage_test, 20); |
Bogdan Marinescu |
0:a792d4bf36c2 | 677 | test_run(multithreaded_barrage_test, 20); |
Bogdan Marinescu |
0:a792d4bf36c2 | 678 | |
Bogdan Marinescu |
0:a792d4bf36c2 | 679 | printf("done!\n"); |
Bogdan Marinescu |
0:a792d4bf36c2 | 680 | return test_failure; |
Bogdan Marinescu |
0:a792d4bf36c2 | 681 | } |