]> git.saurik.com Git - apple/libdispatch.git/blob - testing/dispatch_timer_bit31.c
libdispatch-84.5.5.tar.gz
[apple/libdispatch.git] / testing / dispatch_timer_bit31.c
1 #include <assert.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <sys/time.h>
5
6 #include <dispatch/dispatch.h>
7
8 #include "dispatch_test.h"
9
10 int main(void)
11 {
12 test_start("Dispatch Source Timer, bit 31");
13
14 dispatch_queue_t main_q = dispatch_get_main_queue();
15 test_ptr("dispatch_get_main_queue", main_q, dispatch_get_current_queue());
16
17 __block int i = 0;
18 struct timeval start_time;
19
20 gettimeofday(&start_time, NULL);
21 dispatch_source_attr_t attr = dispatch_source_attr_create();
22 dispatch_source_attr_set_finalizer(attr, ^(dispatch_source_t ds) {
23 struct timeval end_time;
24 gettimeofday(&end_time, NULL);
25 test_ptr_notnull("finalizer ran", ds);
26 // XXX: check, s/b 2.0799... seconds, which is <4 seconds
27 // when it could end on a bad boundry.
28 test_long_less_than("needs to finish faster than 4 seconds", end_time.tv_sec - start_time.tv_sec, 4);
29 // And it has to take at least two seconds...
30 test_long_less_than("can't finish faster than 2 seconds", 1, end_time.tv_sec - start_time.tv_sec);
31 test_stop();
32 });
33
34 dispatch_source_t s = dispatch_source_timer_create(DISPATCH_TIMER_INTERVAL,
35 0x80000000ull,
36 0,
37 attr,
38 main_q,
39 ^(dispatch_event_t ev) {
40 long err;
41 if (dispatch_event_get_error(ev, &err)) {
42 test_errno("dispatch_event_get_error", err, ECANCELED);
43 dispatch_release(dispatch_event_get_source(ev));
44 } else {
45 fprintf(stderr, "%d\n", ++i);
46 dispatch_cancel(dispatch_event_get_source(ev));
47 }
48 });
49 test_ptr_notnull("dispatch_source_timer_create", s);
50
51 dispatch_release(attr);
52
53 dispatch_main();
54
55 return 0;
56 }