|
| 1 | +#include "ee-test.h" |
| 2 | +#include <assert.h> |
| 3 | +#include <stdio.h> |
| 4 | +#include <stdlib.h> |
| 5 | +#include <sys/stat.h> |
| 6 | +int ee_main(void); |
| 7 | +int ee_main_once(void); |
| 8 | +int ee_main_loop(void); |
| 9 | +void on_added_new_listener(void *arg); |
| 10 | + |
| 11 | +ee_t *EE; |
| 12 | +#define LOOP_LIM 10 |
| 13 | +#define L log_info |
| 14 | + |
| 15 | +void on_hello(void *arg); |
| 16 | +void on_hi(void *arg); |
| 17 | + |
| 18 | + |
| 19 | +TEST test_logh(void){ |
| 20 | + log_info("logging an info message"); |
| 21 | + log_warn("logging a warn message"); |
| 22 | + log_debug("logging a debug message"); |
| 23 | + log_error("logging an error %d", 123); |
| 24 | + L("xxxxxxxxxxxx %s", "2222222222"); |
| 25 | + PASS(); |
| 26 | +} |
| 27 | + |
| 28 | + |
| 29 | +TEST test_ee_loop(void){ |
| 30 | + ee_main_loop(); |
| 31 | + PASS(); |
| 32 | +} |
| 33 | + |
| 34 | + |
| 35 | +TEST test_ee_once(void){ |
| 36 | + ee_main_once(); |
| 37 | + PASS(); |
| 38 | +} |
| 39 | + |
| 40 | + |
| 41 | +TEST test_ee(void){ |
| 42 | + ee_main(); |
| 43 | + PASS(); |
| 44 | +} |
| 45 | + |
| 46 | +SUITE(suite_ee_once) { |
| 47 | + test_ee_once(); |
| 48 | + PASS(); |
| 49 | +} |
| 50 | +SUITE(suite_logh) { |
| 51 | + test_logh(); |
| 52 | + PASS(); |
| 53 | +} |
| 54 | +SUITE(suite_ee_loop) { |
| 55 | + test_ee_loop(); |
| 56 | + PASS(); |
| 57 | +} |
| 58 | +SUITE(suite_ee) { |
| 59 | + test_ee(); |
| 60 | + PASS(); |
| 61 | +} |
| 62 | + |
| 63 | +GREATEST_MAIN_DEFS(); |
| 64 | + |
| 65 | + |
| 66 | +int main(int argc, char **argv) { |
| 67 | + GREATEST_MAIN_BEGIN(); |
| 68 | + (void)argc; (void)argv; |
| 69 | + RUN_SUITE(suite_logh); |
| 70 | + RUN_SUITE(suite_ee); |
| 71 | + RUN_SUITE(suite_ee_once); |
| 72 | + RUN_SUITE(suite_ee_loop); |
| 73 | + |
| 74 | + GREATEST_MAIN_END(); |
| 75 | + return(0); |
| 76 | +} |
| 77 | + |
| 78 | + |
| 79 | +void on_added_new_listener(void *arg) { |
| 80 | + ee_new_listener_t *listener; |
| 81 | + |
| 82 | + listener = (ee_new_listener_t *)arg; |
| 83 | + L("New listener added for event '%s'.", listener->name); |
| 84 | +} |
| 85 | + |
| 86 | + |
| 87 | +int ee_main_once(void) { |
| 88 | + const char *event_hello = "hello"; |
| 89 | + const char *event_hi = "hi"; |
| 90 | + |
| 91 | + EE = ee_new(); |
| 92 | + ee_once(EE, event_hi, on_hi); |
| 93 | + assert(1 == ee_listener_count(EE, event_hi)); |
| 94 | + ee_emit(EE, event_hi, "world"); |
| 95 | + assert(0 == ee_listener_count(EE, event_hi)); |
| 96 | + ee_emit(EE, event_hi, "world"); /* => nothing happens */ |
| 97 | + return(0); |
| 98 | +} |
| 99 | + |
| 100 | + |
| 101 | +int ee_main_loop(void) { |
| 102 | + const char *event_hello = "hello"; |
| 103 | + const char *event_hi = "hi"; |
| 104 | + |
| 105 | + EE = ee_new(); |
| 106 | + ee_on(EE, EE_NEW_LISTENER, on_added_new_listener); |
| 107 | + ee_on(EE, event_hello, on_hello); |
| 108 | + ee_once(EE, event_hi, on_hi); |
| 109 | + for (int i = 0; i < LOOP_LIM; i++) { |
| 110 | + ee_emit(EE, event_hello, "world"); |
| 111 | + ee_emit(EE, event_hi, "world"); |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | + |
| 116 | +void on_hello(void *arg) { |
| 117 | + char *s = (char *)arg; |
| 118 | + |
| 119 | + L("Invoked `on_hello` with '%s'", s); |
| 120 | +} |
| 121 | + |
| 122 | + |
| 123 | +void on_hi(void *arg) { |
| 124 | + char *s = (char *)arg; |
| 125 | + |
| 126 | + L("Invoked `on_hi` with '%s'", s); |
| 127 | +} |
| 128 | + |
| 129 | + |
| 130 | +int ee_main(void) { |
| 131 | + const char *event_hello = "hello"; |
| 132 | + const char *event_hi = "hi"; |
| 133 | + |
| 134 | + EE = ee_new(); |
| 135 | + |
| 136 | + ee_on(EE, EE_NEW_LISTENER, on_added_new_listener); |
| 137 | + |
| 138 | + assert(0 == ee_listener_count(EE, event_hello)); |
| 139 | + |
| 140 | + /* subscribe "hello" indefinitely, "hi" only once */ |
| 141 | + ee_on(EE, event_hello, on_hello); |
| 142 | + ee_once(EE, event_hi, on_hi); |
| 143 | + |
| 144 | + L("ee_listener_count,event_hello->%d", ee_listener_count(EE, event_hello)); |
| 145 | + assert(1 == ee_listener_count(EE, event_hello)); |
| 146 | + assert(1 == ee_listener_count(EE, event_hi)); |
| 147 | + |
| 148 | + ee_emit(EE, event_hello, "world"); |
| 149 | + ee_emit(EE, event_hi, "world"); |
| 150 | + |
| 151 | + assert(1 == ee_listener_count(EE, event_hello)); |
| 152 | + assert(0 == ee_listener_count(EE, event_hi)); |
| 153 | + |
| 154 | + ee_emit(EE, event_hello, "world"); |
| 155 | + ee_emit(EE, event_hi, "world"); /* => nothing happens */ |
| 156 | + |
| 157 | + ee_remove_listener(EE, event_hello, on_hello); |
| 158 | + assert(0 == ee_listener_count(EE, event_hello)); |
| 159 | + |
| 160 | + ee_emit(EE, event_hello, "world"); /* => nothing happens */ |
| 161 | + |
| 162 | + return(0); |
| 163 | +} |
0 commit comments