]> git.lizzy.rs Git - dragonstd.git/blob - test/test_flag.c
refactoring + documentation + testing + added Map and Refcount
[dragonstd.git] / test / test_flag.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <time.h>
5 #include "../flag.h"
6
7 #define NUM_FLAGS 5
8 #define ITER_FLAGS for (int i = 0; i < NUM_FLAGS; i++)
9
10 Flag flags[NUM_FLAGS];
11
12 void *start_thread(__attribute__((unused)) void *val)
13 {
14         ITER_FLAGS {
15                 printf("waiting for flag %d\n", i);
16                 flag_slp(&flags[i]);
17         }
18
19         return NULL;
20 }
21
22 bool is_finished()
23 {
24         ITER_FLAGS if (! flags[i].set)
25                 return false;
26
27         return true;
28 }
29
30 int main()
31 {
32         printf("------------\n");
33         printf("Testing Flag\n");
34         printf("------------\n");
35
36         srand(time(NULL));
37         ITER_FLAGS flag_ini(&flags[i]);
38
39         pthread_t thread;
40         pthread_create(&thread, NULL, &start_thread, NULL);
41
42         while (! is_finished()) {
43                 int i = rand() % NUM_FLAGS;
44
45                 printf("setting flag %d\n", i);
46                 flag_set(&flags[i]);
47
48                 nanosleep(&(struct timespec) {
49                         .tv_sec = 0,
50                         .tv_nsec = 1e7,
51                 }, NULL);
52         }
53
54         pthread_join(thread, NULL);
55
56         ITER_FLAGS flag_dst(&flags[i]);
57 }