1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
|
#include "stream_expectations.h"
struct test_assertion assertions_queue[MAX_QUEUE_ITEMS];
int queue_size = 0;
int current_expectation = 0;
int clean_up_stream_assertions(void **state) {
if (queue_size != current_expectation) {
return 1; // We have not matched all expectations correctly
}
queue_size = current_expectation = 0;
free(*state);
return 0;
}
/* Callbacks */
struct test_assertion current(void) {
return assertions_queue[current_expectation];
}
/* Assertions builders and matcher callbacks */
void assert_uint8_eq(uint8_t actual) {
assertions_queue[queue_size++] = (struct test_assertion){
UINT8_EQ, (union test_expectation_data){.int8 = actual}};
}
void uint8_callback(void *_CBOR_UNUSED(_context), uint8_t actual) {
assert_true(current().expectation == UINT8_EQ);
assert_true(current().data.int8 == actual);
current_expectation++;
}
void assert_uint16_eq(uint16_t actual) {
assertions_queue[queue_size++] = (struct test_assertion){
UINT16_EQ, (union test_expectation_data){.int16 = actual}};
}
void uint16_callback(void *_CBOR_UNUSED(_context), uint16_t actual) {
assert_true(current().expectation == UINT16_EQ);
assert_true(current().data.int16 == actual);
current_expectation++;
}
void assert_uint32_eq(uint32_t actual) {
assertions_queue[queue_size++] = (struct test_assertion){
UINT32_EQ, (union test_expectation_data){.int32 = actual}};
}
void uint32_callback(void *_CBOR_UNUSED(_context), uint32_t actual) {
assert_true(current().expectation == UINT32_EQ);
assert_true(current().data.int32 == actual);
current_expectation++;
}
void assert_uint64_eq(uint64_t actual) {
assertions_queue[queue_size++] = (struct test_assertion){
UINT64_EQ, (union test_expectation_data){.int64 = actual}};
}
void uint64_callback(void *_CBOR_UNUSED(_context), uint64_t actual) {
assert_true(current().expectation == UINT64_EQ);
assert_true(current().data.int64 == actual);
current_expectation++;
}
void assert_negint8_eq(uint8_t actual) {
assertions_queue[queue_size++] = (struct test_assertion){
NEGINT8_EQ, (union test_expectation_data){.int8 = actual}};
}
void negint8_callback(void *_CBOR_UNUSED(_context), uint8_t actual) {
assert_true(current().expectation == NEGINT8_EQ);
assert_true(current().data.int8 == actual);
current_expectation++;
}
void assert_negint16_eq(uint16_t actual) {
assertions_queue[queue_size++] = (struct test_assertion){
NEGINT16_EQ, (union test_expectation_data){.int16 = actual}};
}
void negint16_callback(void *_CBOR_UNUSED(_context), uint16_t actual) {
assert_true(current().expectation == NEGINT16_EQ);
assert_true(current().data.int16 == actual);
current_expectation++;
}
void assert_negint32_eq(uint32_t actual) {
assertions_queue[queue_size++] = (struct test_assertion){
NEGINT32_EQ, (union test_expectation_data){.int32 = actual}};
}
void negint32_callback(void *_CBOR_UNUSED(_context), uint32_t actual) {
assert_true(current().expectation == NEGINT32_EQ);
assert_true(current().data.int32 == actual);
current_expectation++;
}
void assert_negint64_eq(uint64_t actual) {
assertions_queue[queue_size++] = (struct test_assertion){
NEGINT64_EQ, (union test_expectation_data){.int64 = actual}};
}
void negint64_callback(void *_CBOR_UNUSED(_context), uint64_t actual) {
assert_true(current().expectation == NEGINT64_EQ);
assert_true(current().data.int64 == actual);
current_expectation++;
}
void assert_bstring_mem_eq(cbor_data address, size_t length) {
assertions_queue[queue_size++] = (struct test_assertion){
BSTRING_MEM_EQ,
(union test_expectation_data){.string = {address, length}}};
}
void byte_string_callback(void *_CBOR_UNUSED(_context), cbor_data address,
uint64_t length) {
assert_true(current().expectation == BSTRING_MEM_EQ);
assert_true(current().data.string.address == address);
assert_true(current().data.string.length == length);
current_expectation++;
}
void assert_bstring_indef_start(void) {
assertions_queue[queue_size++] =
(struct test_assertion){.expectation = BSTRING_INDEF_START};
}
void byte_string_start_callback(void *_CBOR_UNUSED(_context)) {
assert_true(current().expectation == BSTRING_INDEF_START);
current_expectation++;
}
void assert_string_mem_eq(cbor_data address, size_t length) {
assertions_queue[queue_size++] = (struct test_assertion){
STRING_MEM_EQ,
(union test_expectation_data){.string = {address, length}}};
}
void string_callback(void *_CBOR_UNUSED(_context), cbor_data address,
uint64_t length) {
assert_true(current().expectation == STRING_MEM_EQ);
assert_true(current().data.string.address == address);
assert_true(current().data.string.length == length);
current_expectation++;
}
void assert_string_indef_start(void) {
assertions_queue[queue_size++] =
(struct test_assertion){.expectation = STRING_INDEF_START};
}
void string_start_callback(void *_CBOR_UNUSED(_context)) {
assert_true(current().expectation == STRING_INDEF_START);
current_expectation++;
}
void assert_indef_break(void) {
assertions_queue[queue_size++] =
(struct test_assertion){.expectation = INDEF_BREAK};
}
void indef_break_callback(void *_CBOR_UNUSED(_context)) {
assert_true(current().expectation == INDEF_BREAK);
current_expectation++;
}
void assert_array_start(size_t length) {
assertions_queue[queue_size++] =
(struct test_assertion){ARRAY_START, {.length = length}};
}
void array_start_callback(void *_CBOR_UNUSED(_context), uint64_t length) {
assert_true(current().expectation == ARRAY_START);
assert_true(current().data.length == length);
current_expectation++;
}
void assert_indef_array_start(void) {
assertions_queue[queue_size++] =
(struct test_assertion){.expectation = ARRAY_INDEF_START};
}
void indef_array_start_callback(void *_CBOR_UNUSED(_context)) {
assert_true(current().expectation == ARRAY_INDEF_START);
current_expectation++;
}
void assert_map_start(size_t length) {
assertions_queue[queue_size++] =
(struct test_assertion){MAP_START, {.length = length}};
}
void map_start_callback(void *_CBOR_UNUSED(_context), uint64_t length) {
assert_true(current().expectation == MAP_START);
assert_true(current().data.length == length);
current_expectation++;
}
void assert_indef_map_start(void) {
assertions_queue[queue_size++] =
(struct test_assertion){.expectation = MAP_INDEF_START};
}
void indef_map_start_callback(void *_CBOR_UNUSED(_context)) {
assert_true(current().expectation == MAP_INDEF_START);
current_expectation++;
}
void assert_tag_eq(uint64_t value) {
assertions_queue[queue_size++] =
(struct test_assertion){TAG_EQ, {.int64 = value}};
}
void tag_callback(void *_CBOR_UNUSED(_context), uint64_t value) {
assert_true(current().expectation == TAG_EQ);
assert_true(current().data.int64 == value);
current_expectation++;
}
void assert_half(float value) {
assertions_queue[queue_size++] =
(struct test_assertion){HALF_EQ, {.float2 = value}};
}
void half_callback(void *_CBOR_UNUSED(_context), float actual) {
assert_true(current().expectation == HALF_EQ);
assert_true(current().data.float2 == actual);
current_expectation++;
}
void assert_float(float value) {
assertions_queue[queue_size++] =
(struct test_assertion){FLOAT_EQ, {.float4 = value}};
}
void float_callback(void *_CBOR_UNUSED(_context), float actual) {
assert_true(current().expectation == FLOAT_EQ);
assert_true(current().data.float4 == actual);
current_expectation++;
}
void assert_double(double value) {
assertions_queue[queue_size++] =
(struct test_assertion){DOUBLE_EQ, {.float8 = value}};
}
void double_callback(void *_CBOR_UNUSED(_context), double actual) {
assert_true(current().expectation == DOUBLE_EQ);
assert_true(current().data.float8 == actual);
current_expectation++;
}
void assert_bool(bool value) {
assertions_queue[queue_size++] =
(struct test_assertion){BOOL_EQ, {.boolean = value}};
}
void assert_nil(void) {
assertions_queue[queue_size++] = (struct test_assertion){.expectation = NIL};
}
void assert_undef(void) {
assertions_queue[queue_size++] =
(struct test_assertion){.expectation = UNDEF};
}
void bool_callback(void *_CBOR_UNUSED(_context), bool actual) {
assert_true(current().expectation == BOOL_EQ);
assert_true(current().data.boolean == actual);
current_expectation++;
}
void null_callback(void *_CBOR_UNUSED(_context)) {
assert_true(current().expectation == NIL);
current_expectation++;
}
void undef_callback(void *_CBOR_UNUSED(_context)) {
assert_true(current().expectation == UNDEF);
current_expectation++;
}
const struct cbor_callbacks asserting_callbacks = {
.uint8 = &uint8_callback,
.uint16 = &uint16_callback,
.uint32 = &uint32_callback,
.uint64 = &uint64_callback,
.negint8 = &negint8_callback,
.negint16 = &negint16_callback,
.negint32 = &negint32_callback,
.negint64 = &negint64_callback,
.byte_string = &byte_string_callback,
.byte_string_start = &byte_string_start_callback,
.string = &string_callback,
.string_start = &string_start_callback,
.array_start = &array_start_callback,
.indef_array_start = &indef_array_start_callback,
.map_start = &map_start_callback,
.indef_map_start = &indef_map_start_callback,
.tag = &tag_callback,
.float2 = &half_callback,
.float4 = &float_callback,
.float8 = &double_callback,
.undefined = &undef_callback,
.boolean = &bool_callback,
.null = &null_callback,
.indef_break = &indef_break_callback};
struct cbor_decoder_result decode(cbor_data source, size_t source_size) {
int last_expectation = current_expectation;
struct cbor_decoder_result result =
cbor_stream_decode(source, source_size, &asserting_callbacks, NULL);
if (result.status == CBOR_DECODER_FINISHED) {
// Check that we have matched an expectation from the queue
assert_true(last_expectation + 1 == current_expectation);
}
return result;
}
|