diff options
Diffstat (limited to 'testing')
-rw-r--r-- | testing/misc_tools.h | 26 |
1 files changed, 20 insertions, 6 deletions
diff --git a/testing/misc_tools.h b/testing/misc_tools.h index 0ffd51e7..705f4aab 100644 --- a/testing/misc_tools.h +++ b/testing/misc_tools.h | |||
@@ -117,7 +117,7 @@ static inline void tox_list_remove(tox_list *lst) | |||
117 | ************************************************************/ | 117 | ************************************************************/ |
118 | 118 | ||
119 | typedef struct tox_array { | 119 | typedef struct tox_array { |
120 | void *data; | 120 | void *data; /* last elem is data[len-1] */ |
121 | uint32_t len; | 121 | uint32_t len; |
122 | size_t elem_size; /* in bytes */ | 122 | size_t elem_size; /* in bytes */ |
123 | } tox_array; | 123 | } tox_array; |
@@ -135,24 +135,38 @@ static inline void tox_array_delete(tox_array *arr) | |||
135 | arr->len = arr->elem_size = 0; | 135 | arr->len = arr->elem_size = 0; |
136 | } | 136 | } |
137 | 137 | ||
138 | static inline void _tox_array_push(tox_array *arr, uint8_t *item) | 138 | static inline uint8_t tox_array_push_ptr(tox_array *arr, uint8_t *item) |
139 | { | 139 | { |
140 | arr->data = realloc(arr->data, arr->elem_size * (arr->len+1)); | 140 | arr->data = realloc(arr->data, arr->elem_size * (arr->len+1)); |
141 | 141 | ||
142 | memcpy(arr->data + arr->elem_size*arr->len, item, arr->elem_size); | 142 | if (arr->data == NULL) /* didn't call tox_array_init() */ |
143 | return 0; | ||
144 | |||
145 | if (item != NULL) | ||
146 | memcpy(arr->data + arr->elem_size*arr->len, item, arr->elem_size); | ||
143 | arr->len++; | 147 | arr->len++; |
148 | |||
149 | return 1; | ||
144 | } | 150 | } |
145 | #define tox_array_push(arr, item) _tox_array_push(arr, (void*)(&(item))) | 151 | #define tox_array_push(arr, item) tox_array_push_ptr(arr, (uint8_t*)(&(item))) |
146 | 152 | ||
147 | /* Deletes num items from array. | 153 | /* Deletes num items from array. |
148 | * Not same as pop in stacks, because to access elements you use data. | 154 | * Not same as pop in stacks, because to access elements you use data. |
149 | */ | 155 | */ |
150 | static inline void tox_array_pop(tox_array *arr, uint32_t num) | 156 | static inline void tox_array_pop(tox_array *arr, uint32_t num) |
151 | { | 157 | { |
152 | arr->len--; | 158 | if (num == 0) |
159 | num = 1; | ||
160 | arr->len -= num; | ||
153 | arr->data = realloc(arr->data, arr->elem_size*arr->len); | 161 | arr->data = realloc(arr->data, arr->elem_size*arr->len); |
154 | } | 162 | } |
155 | 163 | ||
156 | #define tox_array_get(arr, i, type) ((type*)(arr)->data)[i] | 164 | /* TODO: return ptr and do not take type */ |
165 | #define tox_array_get(arr, i, type) (((type*)(arr)->data)[i]) | ||
166 | |||
167 | |||
168 | #define tox_array_for_each(arr, type, tmp_name) \ | ||
169 | type tmp_name; uint32_t tmp_name ## _i = 0; \ | ||
170 | for (; tmp_name ## _i != (arr)->len; tmp_name = tox_array_get(arr, ++ tmp_name ## _i, type)) | ||
157 | 171 | ||
158 | #endif // MISC_TOOLS_H | 172 | #endif // MISC_TOOLS_H |