| 1 |
/* |
| 2 |
* Test / Data Verification with Enumerated Types |
| 3 |
* |
| 4 |
* Use the dbus-typenum library to enumerate D-Bus types systematically. Then |
| 5 |
* verify we can deal with them correctly. All of them. |
| 6 |
*/ |
| 7 |
|
| 8 |
#undef NDEBUG |
| 9 |
#include <assert.h> |
| 10 |
#include <c-stdaux.h> |
| 11 |
#include <dbus-typenum.h> |
| 12 |
#include <stdio.h> |
| 13 |
#include <stdlib.h> |
| 14 |
#include <string.h> |
| 15 |
#include "c-dvar.h" |
| 16 |
#include "c-dvar-private.h" |
| 17 |
|
| 18 |
static void test_valid_type(const char *typestr, size_t n_typestr) { |
| 19 |
CDVarType *type = NULL; |
| 20 |
int r; |
| 21 |
|
| 22 |
/* verify @typestr is a valid type */ |
| 23 |
|
| 24 |
r = c_dvar_type_new_from_signature(&type, typestr, n_typestr); |
| 25 |
c_assert(!r); |
| 26 |
c_assert(type->length == n_typestr); |
| 27 |
c_dvar_type_free(type); |
| 28 |
} |
| 29 |
|
| 30 |
static void test_enumerated_types(void) { |
| 31 |
DBusTypenum *e; |
| 32 |
char *str = NULL, *pos = NULL; |
| 33 |
size_t i, n_str = 0; |
| 34 |
char c; |
| 35 |
int r; |
| 36 |
|
| 37 |
/* |
| 38 |
* Enumerate valid types via DBusTypenum and check that all generated |
| 39 |
* types are accepted by c-dvar. |
| 40 |
*/ |
| 41 |
|
| 42 |
r = dbus_typenum_new(&e, 0); |
| 43 |
c_assert(!r); |
| 44 |
|
| 45 |
n_str = 2; |
| 46 |
str = malloc(n_str); |
| 47 |
c_assert(str); |
| 48 |
pos = str; |
| 49 |
|
| 50 |
for (i = 0; i < 1 << 16; ++i) { |
| 51 |
dbus_typenum_seed_u32(e, i); |
| 52 |
|
| 53 |
do { |
| 54 |
c = dbus_typenum_step(e); |
| 55 |
if (pos - str >= (ssize_t)n_str) { |
| 56 |
str = realloc(str, (pos - str) * 2); |
| 57 |
c_assert(str); |
| 58 |
pos = str + n_str; |
| 59 |
n_str *= 2; |
| 60 |
} |
| 61 |
|
| 62 |
*pos++ = c; |
| 63 |
} while (c); |
| 64 |
|
| 65 |
test_valid_type(str, pos - str - 1); |
| 66 |
pos = str; |
| 67 |
dbus_typenum_reset(e); |
| 68 |
} |
| 69 |
|
| 70 |
free(str); |
| 71 |
dbus_typenum_free(e); |
| 72 |
} |
| 73 |
|
| 74 |
int main(int argc, char **argv) { |
| 75 |
test_enumerated_types(); |
| 76 |
return 0; |
| 77 |
} |