-
Notifications
You must be signed in to change notification settings - Fork 3
/
simple.c
94 lines (82 loc) · 3.68 KB
/
simple.c
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
#undef NDEBUG
#include <assert.h>
#include <cursor/cursor.h>
#include <stdint.h>
#include <stdlib.h>
int
main(int argc, char * argv[]) {
(void)argc;
(void)argv;
/* Create a buffer to write packed primitives into */
uint8_t buf[42];
/* Construct a new cursor with the buffer we just defined */
struct cursor_wtr wtr = cursor_wtr_new(buf, sizeof(buf));
/* Primitives we'll pack into the buffer */
uint8_t packed_u8 = rand();
int8_t packed_i8 = rand();
uint16_t packed_u16 = rand();
int16_t packed_i16 = rand();
uint32_t packed_u32 = rand();
int32_t packed_i32 = rand();
uint64_t packed_u64 = rand();
int64_t packed_i64 = rand();
float packed_f = 8.8;
double packed_d = 9.9;
/* A mixture of little/big endian packing
* Note that same function call is used for all primitive types.
* `cursor_pack_be` is actually macro using c11's generic selector feature.
* It dispatches to the correct cursor function call based on the primitive
* type */
assert(cursor_pack_be_u8(&wtr, packed_u8) == cursor_res_ok);
assert(cursor_pack_le_i8(&wtr, packed_i8) == cursor_res_ok);
assert(cursor_pack_be_u16(&wtr, packed_u16) == cursor_res_ok);
assert(cursor_pack_le_i16(&wtr, packed_i16) == cursor_res_ok);
assert(cursor_pack_be_u32(&wtr, packed_u32) == cursor_res_ok);
assert(cursor_pack_le_i32(&wtr, packed_i32) == cursor_res_ok);
assert(cursor_pack_be_u64(&wtr, packed_u64) == cursor_res_ok);
assert(cursor_pack_le_i64(&wtr, packed_i64) == cursor_res_ok);
assert(cursor_pack_be_f(&wtr, packed_f) == cursor_res_ok);
assert(cursor_pack_le_d(&wtr, packed_d) == cursor_res_ok);
/* Our buffer was sized to have no leftover space.
* Let's verify that attempting another pack operation returns an error.
* Note that we're explicitly calling `cursor_pack_le_u8` here, and not
* using the type-generic `cursor_pack_le` macro */
assert(cursor_pack_le_u16(&wtr, rand()) == cursor_res_err_buf_exhausted);
/* We'll unpack our packed variables into these variables */
uint8_t unpacked_u8;
int8_t unpacked_i8;
uint16_t unpacked_u16;
int16_t unpacked_i16;
uint32_t unpacked_u32;
int32_t unpacked_i32;
uint64_t unpacked_u64;
int64_t unpacked_i64;
float unpacked_f;
double unpacked_d;
/* Reset the cursor.
* `cursor_new` does not allocate so this is safe */
struct cursor_rdr rdr = cursor_rdr_new(buf, sizeof(buf));
/* Now unpack out of the buffer we packed into earlier */
assert(cursor_unpack_be_u8(&rdr, &unpacked_u8) == cursor_res_ok);
assert(cursor_unpack_le_i8(&rdr, &unpacked_i8) == cursor_res_ok);
assert(cursor_unpack_be_u16(&rdr, &unpacked_u16) == cursor_res_ok);
assert(cursor_unpack_le_i16(&rdr, &unpacked_i16) == cursor_res_ok);
assert(cursor_unpack_be_u32(&rdr, &unpacked_u32) == cursor_res_ok);
assert(cursor_unpack_le_i32(&rdr, &unpacked_i32) == cursor_res_ok);
assert(cursor_unpack_be_u64(&rdr, &unpacked_u64) == cursor_res_ok);
assert(cursor_unpack_le_i64(&rdr, &unpacked_i64) == cursor_res_ok);
assert(cursor_unpack_be_f(&rdr, &unpacked_f) == cursor_res_ok);
assert(cursor_unpack_le_d(&rdr, &unpacked_d) == cursor_res_ok);
/* Check the packed and unpacked variables for equality */
assert(packed_u8 == unpacked_u8);
assert(packed_i8 == unpacked_i8);
assert(packed_u16 == unpacked_u16);
assert(packed_i16 == unpacked_i16);
assert(packed_u32 == unpacked_u32);
assert(packed_i32 == unpacked_i32);
assert(packed_u64 == unpacked_u64);
assert(packed_i64 == unpacked_i64);
assert(packed_f == unpacked_f);
assert(packed_d == unpacked_d);
return 0;
}