]> git.lizzy.rs Git - dragonstd.git/blob - test/test_refcount_map.c
Add transformer to del
[dragonstd.git] / test / test_refcount_map.c
1 #include <assert.h>
2 #include <stdatomic.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <time.h>
6 #include <unistd.h>
7 #include <limits.h>
8 #include "../map.h"
9 #include "../refcount.h"
10
11 Map map;
12 atomic_bool cancel;
13
14 typedef struct {
15         int id;
16         Refcount rc;
17 } DataObject;
18
19 void data_object_delete(DataObject *obj)
20 {
21         refcount_dst(&obj->rc);
22         free(obj);
23 }
24
25 int rand_id()
26 {
27         return rand() % 1000;
28 }
29
30 int data_object_compare(const void *pa, const void *pb)
31 {
32         return
33                 ((DataObject *) ((const Refcount *) pa)->obj)->id -
34                 ((DataObject *) ((const Refcount *) pb)->obj)->id;
35 }
36
37 int data_object_compare_id(const void *pa, const void *pb)
38 {
39         return
40                 ((DataObject *) ((const Refcount *) pa)->obj)->id -
41                 *(const int *) pb;
42 }
43
44 static void *thread_create(unsigned int *result)
45 {
46         while (!cancel) {
47                 DataObject *obj = malloc(sizeof *obj);
48                 obj->id = rand_id();
49
50                 refcount_ini(&obj->rc, obj, (void *) &data_object_delete);
51
52                 if (map_add(&map, &obj->rc, &data_object_compare, (void *) &refcount_inc))
53                         (*result)++;
54
55                 refcount_drp(&obj->rc);
56         }
57
58         return NULL;
59 }
60
61 #define NUM_OBJS 100
62
63 static void *thread_access(unsigned int *result)
64 {
65         DataObject *objs[NUM_OBJS] = {NULL};
66
67         while (!cancel) {
68                 int i = rand() % NUM_OBJS;
69
70                 if (objs[i]) {
71                         refcount_drp(&objs[i]->rc);
72                         objs[i] = NULL;
73                 }
74
75                 while (!objs[i] && !cancel) {
76                         int id = rand_id();
77                         objs[i] = map_get(&map, &id, &data_object_compare_id, (void *) &refcount_grb);
78                 }
79
80                 if (objs[i])
81                         (*result)++;
82         }
83
84         for (int i = 0; i < NUM_OBJS; i++)
85                 if (objs[i])
86                         refcount_drp(&objs[i]->rc);
87
88         return NULL;
89 }
90
91 #undef NUM_OBJS
92
93 static void *thread_delete(unsigned int *result)
94 {
95         while (!cancel) {
96                 unsigned int id = rand_id();
97
98                 if (map_del(&map, &id, &data_object_compare_id, (void *) &refcount_drp, NULL, NULL))
99                         (*result)++;
100         }
101
102         return NULL;
103 }
104
105 int main()
106 {
107         srand(time(NULL));
108
109         printf("------------------------\n");
110         printf("Testing Map and Refcount\n");
111         printf("------------------------\n");
112
113         map_ini(&map);
114
115         void *(*funcs[3])(void *) = {
116                 (void *) &thread_create,
117                 (void *) &thread_access,
118                 (void *) &thread_delete,
119         };
120
121         unsigned int results[3][5] = {0};
122         pthread_t threads[3][5] = {0};
123
124         for (int i = 0; i < 3; i++)
125                 for (int j = 0; j < 5; j++)
126                         pthread_create(&threads[i][j], NULL, funcs[i], &results[i][j]);
127
128         sleep(1);
129
130         cancel = true;
131         for (int i = 0; i < 3; i++)
132                 for (int j = 0; j < 5; j++) {
133                         pthread_join(threads[i][j], NULL);
134
135                         if (j)
136                                 results[i][0] += results[i][j];
137                 }
138
139         map_cnl(&map, (void *) &refcount_drp, NULL, NULL, 0);
140         map_dst(&map);
141
142         printf("Time: 1 second\n");
143         printf("Created objects: %u\n", results[0][0]);
144         printf("Accessed objects: %u\n", results[1][0]);
145         printf("Deleted objects: %u\n", results[2][0]);
146 }