#include <stdio.h>
#include <string.h>

#include "bio/bio.h"
#include "bio/bio_opcode.h"

static int require_opcode(const char *token, bio_opcode expected) {
    bio_opcode got = BIO_OPCODE_COUNT;
    if (!bio_opcode_from_token(token, &got)) {
        fprintf(stderr, "missing opcode: %s\n", token);
        return 1;
    }
    if (got != expected) {
        fprintf(stderr, "wrong opcode id: %s\n", token);
        return 1;
    }
    if (strcmp(bio_opcode_name(got), token) != 0) {
        fprintf(stderr, "wrong opcode name: %s\n", token);
        return 1;
    }
    return 0;
}

int main(void) {
    int failures = 0;
    failures += require_opcode("ABSORB_GGUF", BIO_OPCODE_ABSORB_GGUF);
    failures += require_opcode("MANIFOLD_ADDR", BIO_OPCODE_MANIFOLD_ADDR);
    failures += require_opcode("PULSE", BIO_OPCODE_PULSE);
    failures += require_opcode("PRUNE", BIO_OPCODE_PRUNE);
    failures += require_opcode("ZPM_SKIP", BIO_OPCODE_ZPM_SKIP);
    failures += require_opcode("OMEGA_ROUTE", BIO_OPCODE_OMEGA_ROUTE);
    failures += require_opcode("LEARN_CYCLE", BIO_OPCODE_LEARN_CYCLE);
    failures += require_opcode("INFER_CHAT", BIO_OPCODE_INFER_CHAT);
    failures += require_opcode("DELTA_COMMIT", BIO_OPCODE_DELTA_COMMIT);
    failures += require_opcode("CACHE_BIND", BIO_OPCODE_CACHE_BIND);
    failures += require_opcode("SYNC_FENCE", BIO_OPCODE_SYNC_FENCE);
    failures += require_opcode("HALT", BIO_OPCODE_HALT);

    if (failures != 0) {
        return 1;
    }
    puts("bio-tests: opcode table ok");
    return 0;
}
