creole/c_test/creole.c

105 lines
2.0 KiB
C

#include "greatest.h"
GREATEST_MAIN_DEFS();
#include "../creole.c"
/**************************************************************************
* Reader
*************************************************************************/
#define reader_lit(r, s) do { \
r.p = (unsigned char *)s; \
r.left = sizeof(s) - 1; \
} while(0)
TEST reader_test_basic(struct creole_reader *r) {
size_t i = 0;
unsigned char *s = r->p;
size_t len = r->left;
for (i = 0; i < len; i++) {
ASSERT_EQ(read_eof(r), 0);
ASSERT_EQ(read(r), s[i]);
}
for (i = 0; i < 5; i++) {
ASSERT_EQ(read_eof(r), 1);
ASSERT_EQ(read(r), -1);
}
PASS();
}
SUITE(reader) {
struct creole_reader r = {0};
reader_lit(r, "abcdefg");
RUN_TEST1(reader_test_basic, &r);
reader_lit(r, "");
RUN_TEST1(reader_test_basic, &r);
}
#if 0
TEST encode_decode_byte_seq(struct seq *s) {
unsigned char buf[7];
struct creole_reader r = {0};
struct word w;
creole_word i = 0;
for (;;) {
ASSERT_EQ(creole_encode(i, s->encode_to, s->high_bits,
buf), 1);
r.p = buf;
r.left = s->encode_to;
ASSERT_EQ(decode_seq(&r, &w), 1);
ASSERT_EQ(w.len, s->encode_to);
ASSERT_EQ(w.high_bits, s->high_bits);
ASSERT_EQ(w.word, i);
if (i == s->max)
break;
i++;
}
PASS();
}
SUITE(pseudo_utf8_encode_all) {
struct seq s;
s.max = 0x7F;
s.encode_to = 1;
s.high_bits = 0;
RUN_TEST1(encode_decode_byte_seq, &s);
for (s.high_bits = 0; s.high_bits < 16; s.high_bits++) {
s.max = 0x7F;
s.encode_to = 2;
RUN_TEST1(encode_decode_byte_seq, &s);
s.max = 0xFFF;
s.encode_to = 3;
RUN_TEST1(encode_decode_byte_seq, &s);
s.max = 0x1FFFF;
s.encode_to = 4;
RUN_TEST1(encode_decode_byte_seq, &s);
s.max = 0x3FFFFF;
s.encode_to = 5;
RUN_TEST1(encode_decode_byte_seq, &s);
s.max = 0x7FFFFFF;
s.encode_to = 6;
RUN_TEST1(encode_decode_byte_seq, &s);
s.max = 0xFFFFFFFF;
s.encode_to = 7;
RUN_TEST1(encode_decode_byte_seq, &s);
}
}
#endif
int main(int argc, char *argv[]) {
GREATEST_MAIN_BEGIN();
RUN_SUITE(reader);
GREATEST_MAIN_END();
}