Lev Walkin | 5d947a8 | 2017-10-03 01:04:03 -0700 | [diff] [blame] | 1 | #include <assert.h> |
| 2 | #include <per_support.h> |
| 3 | |
| 4 | static void put(asn_per_outp_t *po, size_t length) { |
| 5 | fprintf(stderr, "put(%zd)\n", length); |
| 6 | do { |
| 7 | int need_eom = 123; |
| 8 | ssize_t may_write = uper_put_length(po, length, &need_eom); |
| 9 | fprintf(stderr, " put %zu\n", may_write); |
| 10 | assert(may_write >= 0); |
| 11 | assert(may_write <= length); |
| 12 | assert(need_eom != 123); |
| 13 | length -= may_write; |
| 14 | if(need_eom) { |
| 15 | assert(length == 0); |
| 16 | if(uper_put_length(po, 0, 0)) { |
| 17 | assert(!"Unreachable"); |
| 18 | } |
| 19 | fprintf(stderr, " put EOM 0\n"); |
| 20 | } |
| 21 | } while(length); |
| 22 | fprintf(stderr, "put(...) in %zu bits\n", po->nboff); |
| 23 | assert(po->nboff != 0); |
| 24 | assert(po->flushed_bytes == 0); |
| 25 | } |
| 26 | |
| 27 | static size_t get(asn_per_outp_t *po) { |
| 28 | asn_bit_data_t data; |
| 29 | memset(&data, 0, sizeof(data)); |
| 30 | data.buffer = po->tmpspace; |
| 31 | data.nboff = 0; |
| 32 | data.nbits = 8 * (po->buffer - po->tmpspace) + po->nboff; |
| 33 | |
| 34 | fprintf(stderr, "get(): %s\n", asn_bit_data_string(&data)); |
| 35 | |
| 36 | size_t length = 0; |
| 37 | int repeat = 0; |
| 38 | do { |
| 39 | ssize_t n = uper_get_length(&data, -1, 0, &repeat); |
| 40 | fprintf(stderr, " get = %zu +%zd\n", length, n); |
| 41 | assert(n >= 0); |
| 42 | length += n; |
| 43 | } while(repeat); |
| 44 | fprintf(stderr, "get() = %zu\n", length); |
| 45 | |
| 46 | return length; |
| 47 | } |
| 48 | |
| 49 | static void |
| 50 | check_round_trip(size_t length) { |
| 51 | fprintf(stderr, "\nRound-trip for %zu\n", length); |
| 52 | asn_per_outp_t po; |
| 53 | |
| 54 | memset(&po, 0, sizeof(po)); |
| 55 | po.buffer = po.tmpspace; |
| 56 | po.nbits = 8 * sizeof(po.tmpspace); |
| 57 | |
| 58 | put(&po, length); |
| 59 | size_t recovered = get(&po); |
| 60 | |
| 61 | assert(recovered == length); |
| 62 | } |
| 63 | |
| 64 | int main() { |
| 65 | |
| 66 | check_round_trip(0); |
| 67 | check_round_trip(1); |
| 68 | check_round_trip(127); |
| 69 | check_round_trip(128); |
| 70 | check_round_trip(129); |
| 71 | check_round_trip(255); |
| 72 | check_round_trip(256); |
| 73 | check_round_trip(65534); |
| 74 | check_round_trip(65535); |
| 75 | check_round_trip(65536); |
| 76 | check_round_trip(65538); |
| 77 | check_round_trip(128000); |
| 78 | for(size_t i = 1; i < 10; i++) { |
| 79 | check_round_trip(i*16384 - 1); |
| 80 | check_round_trip(i*16384); |
| 81 | check_round_trip(i*16384 + 1); |
| 82 | } |
| 83 | |
| 84 | } |
| 85 | |