creole/c_test/encode_decode.c

72 lines
2.0 KiB
C

/* Copyright (c) 2023 Peter McGoron <code@mcgoron.com>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
#include <string.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include "../creole.h"
void encode_decode_byte_seq(creole_word max, unsigned encode_to, unsigned high_bits) {
unsigned char buf[7];
struct creole_reader r = {0};
struct creole_word w;
creole_word i = 0;
for (;;) {
r.p = buf;
r.left = encode_to;
assert(creole_encode(i, encode_to, high_bits,
buf) == 1);
assert(creole_decode(&r, &w) == 1);
assert(w.len == encode_to);
if (w.high_bits != high_bits) {
printf("high bits %u != %u\n", w.high_bits, high_bits);
abort();
}
if (w.word != i) {
printf("word %X != %X\n", w.word, i);
abort();
}
if (i == max)
break;
i++;
}
}
static void test_encode_decode(void) {
unsigned high_bits = 0;
int encode_len;
const creole_word maxima[6] = {0x7F, 0xFFF, 0x1FFFF, 0x3FFFFF, 0x7FFFFFF, 0xFFFFFFFF};
encode_decode_byte_seq(0x7F, 1, 0);
#pragma omp parallel for collapse(2) num_threads(8)
for (high_bits = 0; high_bits < 16; high_bits++) {
for (encode_len = 2; encode_len < 8; encode_len++)
encode_decode_byte_seq(maxima[encode_len - 2], encode_len, high_bits);
}
}
int main(void) {
printf("test encode\n");
test_encode_decode();
printf("finished\n");
return 0;
}