| 1 |
/* |
| 2 |
* Tests Type Parser |
| 3 |
* |
| 4 |
* Test the type-parser for correctness and completeness. This includes static |
| 5 |
* tests for known values, as well as automated tests for random types. |
| 6 |
*/ |
| 7 |
|
| 8 |
#undef NDEBUG |
| 9 |
#include <assert.h> |
| 10 |
#include <c-stdaux.h> |
| 11 |
#include <stdio.h> |
| 12 |
#include <stdlib.h> |
| 13 |
#include <string.h> |
| 14 |
#include "c-dvar.h" |
| 15 |
#include "c-dvar-private.h" |
| 16 |
#include "c-dvar-type.h" |
| 17 |
|
| 18 |
static const char test_array_signature[] = { |
| 19 |
"u" |
| 20 |
"()" |
| 21 |
"(nq)" |
| 22 |
"a{sa(vt)}" |
| 23 |
"({uu}{uu})" |
| 24 |
"(yqut)" |
| 25 |
}; |
| 26 |
|
| 27 |
static const CDVarType test_array[] = { |
| 28 |
/* "u" */ |
| 29 |
C_DVAR_T_INIT( |
| 30 |
C_DVAR_T_u |
| 31 |
), |
| 32 |
/* "()" */ |
| 33 |
C_DVAR_T_INIT( |
| 34 |
C_DVAR_T_TUPLE0 |
| 35 |
), |
| 36 |
/* "(nq)" */ |
| 37 |
C_DVAR_T_INIT( |
| 38 |
C_DVAR_T_TUPLE2( |
| 39 |
C_DVAR_T_n, |
| 40 |
C_DVAR_T_q |
| 41 |
) |
| 42 |
), |
| 43 |
/* "a{sa(vt)}" */ |
| 44 |
C_DVAR_T_INIT( |
| 45 |
C_DVAR_T_ARRAY( |
| 46 |
C_DVAR_T_PAIR( |
| 47 |
C_DVAR_T_s, |
| 48 |
C_DVAR_T_ARRAY( |
| 49 |
C_DVAR_T_TUPLE2( |
| 50 |
C_DVAR_T_v, |
| 51 |
C_DVAR_T_t |
| 52 |
) |
| 53 |
) |
| 54 |
) |
| 55 |
) |
| 56 |
), |
| 57 |
/* "({uu}{uu})" */ |
| 58 |
C_DVAR_T_INIT( |
| 59 |
C_DVAR_T_TUPLE2( |
| 60 |
C_DVAR_T_PAIR( |
| 61 |
C_DVAR_T_u, |
| 62 |
C_DVAR_T_u |
| 63 |
), |
| 64 |
C_DVAR_T_PAIR( |
| 65 |
C_DVAR_T_u, |
| 66 |
C_DVAR_T_u |
| 67 |
) |
| 68 |
) |
| 69 |
), |
| 70 |
/* "(yqut)" */ |
| 71 |
C_DVAR_T_INIT( |
| 72 |
C_DVAR_T_TUPLE4( |
| 73 |
C_DVAR_T_y, |
| 74 |
C_DVAR_T_q, |
| 75 |
C_DVAR_T_u, |
| 76 |
C_DVAR_T_t |
| 77 |
) |
| 78 |
), |
| 79 |
}; |
| 80 |
|
| 81 |
static void test_base(void) { |
| 82 |
c_assert(sizeof(CDVarType) == 4); |
| 83 |
} |
| 84 |
|
| 85 |
static void test_common(void) { |
| 86 |
CDVarType *type = (CDVarType[2]){}; |
| 87 |
unsigned int i; |
| 88 |
int r; |
| 89 |
|
| 90 |
r = c_dvar_type_new_from_string(&type, "()"); |
| 91 |
c_assert(!r); |
| 92 |
|
| 93 |
/* test open coded comparison for better debugging */ |
| 94 |
c_assert(type->length == 2); |
| 95 |
for (i = 0; i < 2; ++i) { |
| 96 |
c_assert(c_dvar_type_unit[i].size == type[i].size); |
| 97 |
c_assert(c_dvar_type_unit[i].alignment == type[i].alignment); |
| 98 |
c_assert(c_dvar_type_unit[i].element == type[i].element); |
| 99 |
c_assert(c_dvar_type_unit[i].length == type[i].length); |
| 100 |
c_assert(c_dvar_type_unit[i].basic == type[i].basic); |
| 101 |
c_assert(c_dvar_type_unit[i].__padding == type[i].__padding); |
| 102 |
} |
| 103 |
|
| 104 |
/* test memcmp-comparison for API guarantees */ |
| 105 |
c_assert(!memcmp(c_dvar_type_unit, type, type->length * sizeof(*type))); |
| 106 |
c_assert(!memcmp(c_dvar_type_unit, |
| 107 |
(const CDVarType[]){ C_DVAR_T_INIT(C_DVAR_T_TUPLE0) }, |
| 108 |
c_dvar_type_unit->length * sizeof(CDVarType))); |
| 109 |
} |
| 110 |
|
| 111 |
static void test_known_types(void) { |
| 112 |
_c_cleanup_(c_dvar_type_freep) CDVarType *type = NULL; |
| 113 |
const CDVarType *expect; |
| 114 |
const char *signature; |
| 115 |
size_t i, n_signature; |
| 116 |
int r; |
| 117 |
|
| 118 |
/* |
| 119 |
* This runs c_dvar_type_new_from_signature() across |
| 120 |
* @test_array_signature until its end. It verifies the output is the |
| 121 |
* exact sequence provided by @test_array. |
| 122 |
*/ |
| 123 |
|
| 124 |
signature = test_array_signature; |
| 125 |
n_signature = strlen(signature); |
| 126 |
expect = test_array; |
| 127 |
|
| 128 |
while (n_signature) { |
| 129 |
r = c_dvar_type_new_from_signature(&type, signature, n_signature); |
| 130 |
c_assert(!r); |
| 131 |
|
| 132 |
/* this is redunant, but tests the compare operator */ |
| 133 |
c_assert(!c_dvar_type_compare_string(type, signature, type->length)); |
| 134 |
|
| 135 |
for (i = 0; i < type->length; ++i) { |
| 136 |
c_assert(expect[i].size == type[i].size); |
| 137 |
c_assert(expect[i].alignment == type[i].alignment); |
| 138 |
c_assert(expect[i].element == type[i].element); |
| 139 |
c_assert(expect[i].length == type[i].length); |
| 140 |
c_assert(expect[i].basic == type[i].basic); |
| 141 |
} |
| 142 |
|
| 143 |
n_signature -= type->length; |
| 144 |
signature += type->length; |
| 145 |
expect += type->length; |
| 146 |
type = c_dvar_type_free(type); |
| 147 |
} |
| 148 |
|
| 149 |
c_assert(expect == test_array + sizeof(test_array) / sizeof(*test_array)); |
| 150 |
} |
| 151 |
|
| 152 |
static void test_valid_types(void) { |
| 153 |
const char *signatures[] = { |
| 154 |
"()", |
| 155 |
"{tv}", |
| 156 |
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaau", |
| 157 |
"((((((((((((((((((((((((((((((((u))))))))))))))))))))))))))))))))", |
| 158 |
"((((((((((((((((((((((((((((((((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaau))))))))))))))))))))))))))))))))", |
| 159 |
"a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(u))))))))))))))))))))))))))))))))", |
| 160 |
}; |
| 161 |
CDVarType *type = NULL; |
| 162 |
size_t i; |
| 163 |
int r; |
| 164 |
|
| 165 |
/* |
| 166 |
* This parses the signatures provided by @signatures one by one, each |
| 167 |
* of them must be valid. No further validation is done. |
| 168 |
*/ |
| 169 |
|
| 170 |
for (i = 0; i < sizeof(signatures) / sizeof(*signatures); ++i) { |
| 171 |
r = c_dvar_type_new_from_string(&type, signatures[i]); |
| 172 |
c_assert(!r); |
| 173 |
type = c_dvar_type_free(type); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
static void test_invalid_types(void) { |
| 178 |
const char *signatures[] = { |
| 179 |
"A", /* invalid element */ |
| 180 |
"$", /* invalid element */ |
| 181 |
"{}", /* invalid pair */ |
| 182 |
"{)", /* non-matching brackets */ |
| 183 |
"(}", /* non-matching brackets */ |
| 184 |
"{()y}", /* invalid pair */ |
| 185 |
"{yyy}", /* invalid pair */ |
| 186 |
"(yy{))", /* non-matching brackets */ |
| 187 |
"(yy{}}", /* invalid pair */ |
| 188 |
"(yy{)}", /* non-matching brackets */ |
| 189 |
"(", /* unclosed container */ |
| 190 |
")", /* closing wrong container */ |
| 191 |
"((", /* unclosed container */ |
| 192 |
")(", /* closing wrong container */ |
| 193 |
"a", /* unclosed container */ |
| 194 |
"aaa", /* unclosed container */ |
| 195 |
"{aau}", /* invalid pair */ |
| 196 |
"{vu}", /* invalid pair*/ |
| 197 |
"(uu(u())uu{vu}uu)", /* invalid pair */ |
| 198 |
"(uu(u())uu(vu}uu)", /* non-matching brackets */ |
| 199 |
"(uu(u())uu{uu)uu)", /* non-matching brackets */ |
| 200 |
"(uu(u())uuuuuuuu}", /* non-matching brackets */ |
| 201 |
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaau", /* depth exceeded */ |
| 202 |
"(((((((((((((((((((((((((((((((((u)))))))))))))))))))))))))))))))))", /* depth exceeded */ |
| 203 |
"a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(au))))))))))))))))))))))))))))))))", /* depth exceeded */ |
| 204 |
}; |
| 205 |
CDVarType *type = NULL; |
| 206 |
size_t i; |
| 207 |
int r; |
| 208 |
|
| 209 |
/* |
| 210 |
* This parses the signatures provided by @signatures one by one, each |
| 211 |
* of them must be rejected. |
| 212 |
*/ |
| 213 |
|
| 214 |
for (i = 0; i < sizeof(signatures) / sizeof(*signatures); ++i) { |
| 215 |
r = c_dvar_type_new_from_string(&type, signatures[i]); |
| 216 |
c_assert(r != 0); |
| 217 |
c_assert(r > 0); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
int main(int argc, char **argv) { |
| 222 |
test_base(); |
| 223 |
test_common(); |
| 224 |
test_known_types(); |
| 225 |
test_valid_types(); |
| 226 |
test_invalid_types(); |
| 227 |
return 0; |
| 228 |
} |