#include <stdlib.h>

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wreserved-macro-identifier"
#include <tap.h>
#pragma clang diagnostic pop

#include <keks/atom.h>
#include <keks/enc.h>
#include <keks/err.h>
#include <keks/items.h>

int
main(void)
{
    plan(NO_PLAN);

    size_t len = 0;
    unsigned char buf[] = {0, 0};
    size_t cap = 0;
    ok(!KEKSAtomNILEncode(&len, buf, cap), "atom small buf");
    ok(len == 1, "atom small buf len");

    cap = 1;
    len = 0;
    ok(KEKSAtomNILEncode(&len, buf, cap), "atom encode");
    ok(len == 1, "atom len");
    ok(buf[0] == 0x01, "atom val");

    struct KEKSItems items;
    KEKSItemsInit(&items, 1 + 1);
    size_t off = 0;
    enum KEKSErr err = KEKSItemsParse(&items, &off, buf, cap);
    ok(err == KEKSErrNo, "parse");
    ok(off == 1, "parse off");
    ok(items.reallocs == 0, "parse no reallocs");
    ok(items.len == 1, "parse len");
    ok(items.offsets[0] == 0, "parse offset");
    struct KEKSItem item = items.list[0];
    ok(item.next == 0, "atom next");
    ok(item.atom.typ == KEKSItemNIL, "atom typ");

    cap = 0;
    off = 0;
    ok(!KEKSItemsEncode(&items, 0, &off, buf, cap), "items encode small buf");
    ok(off == 0, "items encode off");
    cap = sizeof buf;
    ok(KEKSItemsEncode(&items, 0, &off, buf, cap), "items encode");
    ok(off == 1, "items encode off");
    ok(buf[0] == 0x01, "items encode val");

    return exit_status();
}
