allcing/test.c

170 lines
5.2 KiB
C

#include "allcing.h"
#ifdef NDEBUG
# undef NDEBUG // this test uses assert() macro
#endif
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
int main(int, char**) {
# define STREAM(size, offset, ...) \
(struct acg_stream) { \
.buffer = (uint8_t[size]) {__VA_ARGS__ __VA_OPT__(,) 0U,}, \
.size_bytes = (size), \
.cursor_bits = (offset), \
}
/* acg_stream_write_pad / without offset */ {
struct acg_stream s = STREAM(1024U, 0U);
assert(acg_stream_write_pad(&s));
assert(s.cursor_bits == 0U);
}
/* acg_stream_write_pad / with offset */ {
struct acg_stream s = STREAM(1024U, 1U);
assert(acg_stream_write_pad(&s));
assert(s.cursor_bits == 8U);
}
/* acg_stream_write_bool / value=true */ {
struct acg_stream s = STREAM(1024U, 0U);
assert(acg_stream_write_bool(&s, true));
assert(s.buffer[0U] == 0x1U);
assert(s.cursor_bits == 1U);
}
/* acg_stream_write_bool / value=false */ {
struct acg_stream s = STREAM(1024U, 0U);
assert(acg_stream_write_bool(&s, false));
assert(s.buffer[0U] == 0x0U);
assert(s.cursor_bits == 1U);
}
/* acg_stream_write_uint / ends in a single byte */ {
struct acg_stream s = STREAM(1024U, 0U);
assert(acg_stream_write_uint(&s, 7U, 4U));
assert(s.buffer[0U] == 0x07U);
assert(s.cursor_bits == 4U);
}
/* acg_stream_write_uint / ends in a single byte with offset */ {
struct acg_stream s = STREAM(1024U, 1U);
assert(acg_stream_write_uint(&s, 7U, 4U));
assert(s.buffer[0U] == 0x0EU);
assert(s.cursor_bits == 5U);
}
/* acg_stream_write_uint / separated data bits */ {
struct acg_stream s = STREAM(1024U, 6U);
assert(acg_stream_write_uint(&s, 7U, 4U));
assert(s.buffer[0U] == 0xC0U);
assert(s.buffer[1U] == 0x01U);
assert(s.cursor_bits == 10U);
}
/* acg_stream_write_uint / separated cont flag */ {
struct acg_stream s = STREAM(1024U, 7U);
assert(acg_stream_write_uint(&s, 7U, 4U));
assert(s.buffer[0U] == 0x80U);
assert(s.buffer[1U] == 0x03U);
assert(s.cursor_bits == 11U);
}
/* acg_stream_write_chunk_pad / without offset */ {
struct acg_stream s = STREAM(1024U, 0U);
assert(acg_stream_write_chunk_pad(&s));
assert(s.cursor_bits == 0U);
}
/* acg_stream_write_chunk_pad / with offset */ {
struct acg_stream s = STREAM(1024U, 1U);
assert(acg_stream_write_chunk_pad(&s));
assert(s.buffer[0U] == 0U);
assert(s.cursor_bits == 8U);
}
/* acg_stream_write_chunk_blob / without offset */ {
struct acg_stream s = STREAM(1024U, 0U);
assert(acg_stream_write_chunk_blob(
&s, 4U, (uint8_t[]) {0x00, 0x01, 0x02, 0x03}));
assert(s.buffer[0U] == 0x41);
assert(s.buffer[1U] == 0x00);
assert(s.buffer[2U] == 0x00);
assert(s.buffer[3U] == 0x01);
assert(s.buffer[4U] == 0x02);
assert(s.buffer[5U] == 0x03);
assert(s.cursor_bits == 48U);
}
/* acg_stream_write_chunk_blob / with offset=1 */ {
struct acg_stream s = STREAM(1024U, 1U);
assert(acg_stream_write_chunk_blob(
&s, 4U, (uint8_t[]) {0x00, 0x01, 0x02, 0x03}));
assert(s.buffer[0U] == 0x82);
assert(s.buffer[1U] == 0x00);
assert(s.buffer[2U] == 0x00);
assert(s.buffer[3U] == 0x01);
assert(s.buffer[4U] == 0x02);
assert(s.buffer[5U] == 0x03);
assert(s.cursor_bits == 48U);
}
/* acg_stream_write_chunk_blob / with offset=4 */ {
struct acg_stream s = STREAM(1024U, 4U);
assert(acg_stream_write_chunk_blob(
&s, 4U, (uint8_t[]) {0x00, 0x01, 0x02, 0x03}));
assert(s.buffer[0U] == 0x10);
assert(s.buffer[1U] == 0x04);
assert(s.buffer[2U] == 0x00);
assert(s.buffer[3U] == 0x01);
assert(s.buffer[4U] == 0x02);
assert(s.buffer[5U] == 0x03);
assert(s.cursor_bits == 48U);
}
/* acg_stream_read_pad / without offset */ {
struct acg_stream s = STREAM(1024U, 0U);
assert(acg_stream_read_pad(&s));
assert(s.cursor_bits == 0U);
}
/* acg_stream_read_pad / with offset=1 */ {
struct acg_stream s = STREAM(1024U, 1U);
assert(acg_stream_read_pad(&s));
assert(s.cursor_bits == 8U);
}
/* acg_stream_read_bool / with value=false */ {
struct acg_stream s = STREAM(1024U, 0U);
bool v;
assert(acg_stream_read_bool(&s, &v));
assert(v == false);
assert(s.cursor_bits == 1U);
}
/* acg_stream_read_bool / with value=true */ {
struct acg_stream s = STREAM(1024U, 0U, 0x01U);
bool v;
assert(acg_stream_read_bool(&s, &v));
assert(v == true);
assert(s.cursor_bits == 1U);
}
/* acg_stream_read_uint / with a single unit */ {
struct acg_stream s = STREAM(1024U, 0U, 0x04U);
uint64_t v;
assert(acg_stream_read_uint(&s, &v, 4U));
assert(v == 4U);
assert(s.cursor_bits == 4U);
}
/* acg_stream_read_uint / with multi units */ {
struct acg_stream s = STREAM(1024U, 0U, 0x48U);
uint64_t v;
assert(acg_stream_read_uint(&s, &v, 4U));
assert(v == 32U);
assert(s.cursor_bits == 8U);
}
/* acg_stream_read_uint / with multi bytes */ {
struct acg_stream s = STREAM(1024U, 0U, 0x88U, 0x04U);
uint64_t v;
assert(acg_stream_read_uint(&s, &v, 4U));
assert(v == 256U);
assert(s.cursor_bits == 12U);
}
return EXIT_SUCCESS;
}