creole/c_test/encode_decode.c

72 lines
2.0 KiB
C
Raw Permalink Normal View History

2023-02-12 15:54:21 -05:00
/* 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. */
2023-02-25 16:01:03 -05:00
2023-02-07 00:25:30 -05:00
#include <string.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
2023-02-25 16:01:03 -05:00
#include "../creole.h"
2023-02-07 01:19:40 -05:00
void encode_decode_byte_seq(creole_word max, unsigned encode_to, unsigned high_bits) {
unsigned char buf[7];
struct creole_reader r = {0};
2023-02-25 16:01:03 -05:00
struct creole_word w;
2023-02-07 01:19:40 -05:00
creole_word i = 0;
for (;;) {
r.p = buf;
r.left = encode_to;
assert(creole_encode(i, encode_to, high_bits,
buf) == 1);
2023-02-25 16:01:03 -05:00
assert(creole_decode(&r, &w) == 1);
2023-02-07 01:19:40 -05:00
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;
2023-02-07 01:35:18 -05:00
const creole_word maxima[6] = {0x7F, 0xFFF, 0x1FFFF, 0x3FFFFF, 0x7FFFFFF, 0xFFFFFFFF};
2023-02-07 01:19:40 -05:00
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);
}
}
2023-02-07 00:25:30 -05:00
int main(void) {
2023-02-07 00:29:08 -05:00
printf("test encode\n");
2023-02-07 01:19:40 -05:00
test_encode_decode();
2023-02-07 00:29:08 -05:00
printf("finished\n");
2023-02-07 00:25:30 -05:00
return 0;
}