Lev Walkin | d17cf88 | 2017-10-01 22:46:23 -0700 | [diff] [blame^] | 1 | #include <assert.h> |
| 2 | #include <oer_support.c> |
| 3 | |
| 4 | static char buffer[128]; |
| 5 | |
| 6 | static int fill_buffer(const void *data, size_t size, void *app_key) { |
| 7 | size_t *offset = (size_t *)app_key; |
| 8 | assert(*offset + size < sizeof(buffer)); |
| 9 | memcpy(&buffer[*offset], data, size); |
| 10 | *offset += size; |
| 11 | return 0; |
| 12 | } |
| 13 | |
| 14 | static void |
| 15 | check_round_trip(size_t length) { |
| 16 | fprintf(stderr, "Round-trip for %zu\n", length); |
| 17 | |
| 18 | /* Serialize */ |
| 19 | size_t enc_len = 0; |
| 20 | size_t enc_len_len = oer_serialize_length(length, fill_buffer, &enc_len); |
| 21 | assert(enc_len == enc_len_len); |
| 22 | |
| 23 | /* Deserialize */ |
| 24 | size_t recovered_length = 0; |
| 25 | for(size_t part = 0; part < enc_len; part++) { |
| 26 | size_t ret = oer_fetch_length(buffer, part, &recovered_length); |
| 27 | assert(ret == 0); /* More data expected. */ |
| 28 | } |
| 29 | size_t dec_len = oer_fetch_length(buffer, enc_len, &recovered_length); |
| 30 | assert(dec_len == enc_len); |
| 31 | if(recovered_length != length) { |
| 32 | fprintf(stderr, "Round-trip failed %zu->%zu (encoded %zd, decoded %zd)\n", |
| 33 | length, recovered_length, enc_len, dec_len); |
| 34 | assert(recovered_length == length); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | int main() { |
| 39 | |
| 40 | check_round_trip(0); |
| 41 | check_round_trip(1); |
| 42 | check_round_trip(127); |
| 43 | check_round_trip(128); |
| 44 | check_round_trip(129); |
| 45 | check_round_trip(255); |
| 46 | check_round_trip(256); |
| 47 | check_round_trip(65534); |
| 48 | check_round_trip(65535); |
| 49 | check_round_trip(65536); |
| 50 | check_round_trip(65538); |
| 51 | check_round_trip(16000000); |
| 52 | check_round_trip(16777216); |
| 53 | check_round_trip(2147483648); |
| 54 | check_round_trip(4294967296); |
| 55 | |
| 56 | } |
| 57 | |