110ff414cSEd Maste #ifndef STREAM_EXPECTATIONS_H_ 210ff414cSEd Maste #define STREAM_EXPECTATIONS_H_ 310ff414cSEd Maste 4*5d3e7166SEd Maste #include "assertions.h" 510ff414cSEd Maste #include "cbor.h" 610ff414cSEd Maste 710ff414cSEd Maste #define MAX_QUEUE_ITEMS 30 810ff414cSEd Maste 9*5d3e7166SEd Maste // Utilities to test `cbor_stream_decode`. See `cbor_stream_decode_test.cc`. 10*5d3e7166SEd Maste // 11*5d3e7166SEd Maste // Usage: 12*5d3e7166SEd Maste // - The `assert_` helpers build a queue of `test_assertion`s 13*5d3e7166SEd Maste // (`assertions_queue` in the implementation file), specifying 14*5d3e7166SEd Maste // - Which callback is expected (`test_expectation`) 15*5d3e7166SEd Maste // - And what is the expected argument value (if applicable, 16*5d3e7166SEd Maste // `test_expectation_data`) 17*5d3e7166SEd Maste // - `decode` will invoke `cbor_stream_decode` (test subject) 18*5d3e7166SEd Maste // - `cbor_stream_decode` will invoke one of the `_callback` functions, which 19*5d3e7166SEd Maste // will check the passed data against the `assertions_queue` 20*5d3e7166SEd Maste 2110ff414cSEd Maste enum test_expectation { 2210ff414cSEd Maste UINT8_EQ, 2310ff414cSEd Maste UINT16_EQ, 2410ff414cSEd Maste UINT32_EQ, 2510ff414cSEd Maste UINT64_EQ, 2610ff414cSEd Maste 2710ff414cSEd Maste NEGINT8_EQ, 2810ff414cSEd Maste NEGINT16_EQ, 2910ff414cSEd Maste NEGINT32_EQ, 3010ff414cSEd Maste NEGINT64_EQ, 3110ff414cSEd Maste 32*5d3e7166SEd Maste // Matches length and memory address for definite strings 33*5d3e7166SEd Maste BSTRING_MEM_EQ, 3410ff414cSEd Maste BSTRING_INDEF_START, 3510ff414cSEd Maste 36*5d3e7166SEd Maste STRING_MEM_EQ, 37*5d3e7166SEd Maste STRING_INDEF_START, 38*5d3e7166SEd Maste 3910ff414cSEd Maste ARRAY_START, /* Definite arrays only */ 4010ff414cSEd Maste ARRAY_INDEF_START, 4110ff414cSEd Maste 4210ff414cSEd Maste MAP_START, /* Definite maps only */ 4310ff414cSEd Maste MAP_INDEF_START, 4410ff414cSEd Maste 4510ff414cSEd Maste TAG_EQ, 4610ff414cSEd Maste 4710ff414cSEd Maste HALF_EQ, 4810ff414cSEd Maste FLOAT_EQ, 4910ff414cSEd Maste DOUBLE_EQ, 5010ff414cSEd Maste BOOL_EQ, 5110ff414cSEd Maste NIL, 5210ff414cSEd Maste UNDEF, 5310ff414cSEd Maste INDEF_BREAK /* Expect "Break" */ 5410ff414cSEd Maste }; 5510ff414cSEd Maste 5610ff414cSEd Maste union test_expectation_data { 5710ff414cSEd Maste uint8_t int8; 5810ff414cSEd Maste uint16_t int16; 5910ff414cSEd Maste uint32_t int32; 6010ff414cSEd Maste uint64_t int64; 6110ff414cSEd Maste struct string { 6210ff414cSEd Maste cbor_data address; 6310ff414cSEd Maste size_t length; 6410ff414cSEd Maste } string; 6510ff414cSEd Maste size_t length; 6610ff414cSEd Maste float float2; 6710ff414cSEd Maste float float4; 6810ff414cSEd Maste double float8; 6910ff414cSEd Maste bool boolean; 7010ff414cSEd Maste }; 7110ff414cSEd Maste 7210ff414cSEd Maste struct test_assertion { 7310ff414cSEd Maste enum test_expectation expectation; 7410ff414cSEd Maste union test_expectation_data data; 7510ff414cSEd Maste }; 7610ff414cSEd Maste 77*5d3e7166SEd Maste /* Test harness -- calls `cbor_stream_decode` and checks assertions */ 7810ff414cSEd Maste struct cbor_decoder_result decode(cbor_data, size_t); 7910ff414cSEd Maste 80*5d3e7166SEd Maste /* Verify all assertions were applied and clean up */ 81*5d3e7166SEd Maste int clean_up_stream_assertions(void **); 8210ff414cSEd Maste 8310ff414cSEd Maste /* Assertions builders */ 8410ff414cSEd Maste void assert_uint8_eq(uint8_t); 8510ff414cSEd Maste void assert_uint16_eq(uint16_t); 8610ff414cSEd Maste void assert_uint32_eq(uint32_t); 8710ff414cSEd Maste void assert_uint64_eq(uint64_t); 8810ff414cSEd Maste 8910ff414cSEd Maste void assert_negint8_eq(uint8_t); 9010ff414cSEd Maste void assert_negint16_eq(uint16_t); 9110ff414cSEd Maste void assert_negint32_eq(uint32_t); 9210ff414cSEd Maste void assert_negint64_eq(uint64_t); 9310ff414cSEd Maste 9410ff414cSEd Maste void assert_bstring_mem_eq(cbor_data, size_t); 95*5d3e7166SEd Maste void assert_bstring_indef_start(void); 96*5d3e7166SEd Maste 97*5d3e7166SEd Maste void assert_string_mem_eq(cbor_data, size_t); 98*5d3e7166SEd Maste void assert_string_indef_start(void); 9910ff414cSEd Maste 10010ff414cSEd Maste void assert_array_start(size_t); 101*5d3e7166SEd Maste void assert_indef_array_start(void); 10210ff414cSEd Maste 10310ff414cSEd Maste void assert_map_start(size_t); 104*5d3e7166SEd Maste void assert_indef_map_start(void); 10510ff414cSEd Maste 10610ff414cSEd Maste void assert_tag_eq(uint64_t); 10710ff414cSEd Maste 10810ff414cSEd Maste void assert_half(float); 10910ff414cSEd Maste void assert_float(float); 11010ff414cSEd Maste void assert_double(double); 11110ff414cSEd Maste 11210ff414cSEd Maste void assert_bool(bool); 113*5d3e7166SEd Maste void assert_nil(void); /* assert_null already exists */ 114*5d3e7166SEd Maste void assert_undef(void); 11510ff414cSEd Maste 116*5d3e7166SEd Maste void assert_indef_break(void); 11710ff414cSEd Maste 11810ff414cSEd Maste /* Assertions verifying callbacks */ 11910ff414cSEd Maste void uint8_callback(void *, uint8_t); 12010ff414cSEd Maste void uint16_callback(void *, uint16_t); 12110ff414cSEd Maste void uint32_callback(void *, uint32_t); 12210ff414cSEd Maste void uint64_callback(void *, uint64_t); 12310ff414cSEd Maste 12410ff414cSEd Maste void negint8_callback(void *, uint8_t); 12510ff414cSEd Maste void negint16_callback(void *, uint16_t); 12610ff414cSEd Maste void negint32_callback(void *, uint32_t); 12710ff414cSEd Maste void negint64_callback(void *, uint64_t); 12810ff414cSEd Maste 129*5d3e7166SEd Maste void byte_string_callback(void *, cbor_data, uint64_t); 13010ff414cSEd Maste void byte_string_start_callback(void *); 13110ff414cSEd Maste 132*5d3e7166SEd Maste void string_callback(void *, cbor_data, uint64_t); 133*5d3e7166SEd Maste void string_start_callback(void *); 134*5d3e7166SEd Maste 135*5d3e7166SEd Maste void array_start_callback(void *, uint64_t); 13610ff414cSEd Maste void indef_array_start_callback(void *); 13710ff414cSEd Maste 138*5d3e7166SEd Maste void map_start_callback(void *, uint64_t); 13910ff414cSEd Maste void indef_map_start_callback(void *); 14010ff414cSEd Maste 14110ff414cSEd Maste void tag_callback(void *, uint64_t); 14210ff414cSEd Maste 14310ff414cSEd Maste void half_callback(void *, float); 14410ff414cSEd Maste void float_callback(void *, float); 14510ff414cSEd Maste void double_callback(void *, double); 14610ff414cSEd Maste void indef_break_callback(void *); 14710ff414cSEd Maste 14810ff414cSEd Maste void bool_callback(void *, bool); 14910ff414cSEd Maste void null_callback(void *); 15010ff414cSEd Maste void undef_callback(void *); 15110ff414cSEd Maste 15210ff414cSEd Maste #endif 153