// tests/chicago_cli_cover.c
// Chicago TDD: Cover Noun Tests
// Tests cover(#{select, shard}) - Cover definition over O

#include <assert.h>
#include <stdint.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "knhk.h"

#if defined(__GNUC__)
#define ALN __attribute__((aligned(64)))
#else
#define ALN
#endif

static uint64_t ALN S[NROWS];
static uint64_t ALN P[NROWS];
static uint64_t ALN O[NROWS];
static knhk_context_t ctx;

static void reset_test_data(void) {
    memset(S, 0, sizeof(S));
    memset(P, 0, sizeof(P));
    memset(O, 0, sizeof(O));
    knhk_init_ctx(&ctx, S, P, O);
}

// Test: Cover defines select specification
static int test_cover_defines_select(void) {
    printf("[TEST] Cover - Define Select Specification\n");
    reset_test_data();
    
    // Cover selects S ⊂ O (subset of ontology)
    // Select spec: predicates to cover
    uint64_t predicates[] = {0xC0FFEE, 0xDEADBEEF};
    size_t num_predicates = 2;
    
    assert(num_predicates > 0);
    assert(predicates[0] != 0);
    
    printf("  ✓ Select specification defined (%zu predicates)\n", num_predicates);
    return 1;
}

// Test: Cover defines shard specification
static int test_cover_defines_shard(void) {
    printf("[TEST] Cover - Define Shard Specification\n");
    reset_test_data();
    
    // Shard spec: run length constraints (len ≤ 8)
    uint64_t max_run_len = 8;
    uint64_t align = 64;
    
    assert(max_run_len <= 8);
    assert(align == 64);
    
    printf("  ✓ Shard specification defined (max_run_len=%lu, align=%lu)\n", max_run_len, align);
    return 1;
}

// Test: Cover enforces glue constraint
static int test_cover_enforces_glue(void) {
    printf("[TEST] Cover - Glue Constraint\n");
    reset_test_data();
    
    // glue(Cover(O)) = Γ(O) - local views glue to global
    // Verify runs can be merged
    
    knhk_pred_run_t run1 = { .pred = 0xC0FFEE, .off = 0, .len = 4 };
    knhk_pred_run_t run2 = { .pred = 0xDEADBEEF, .off = 4, .len = 4 };
    
    assert(run1.len <= 8);
    assert(run2.len <= 8);
    assert(run1.len + run2.len <= 8); // Can merge if same predicate
    
    printf("  ✓ Glue constraint enforced\n");
    return 1;
}

// Test: Cover validates run length
static int test_cover_validates_run_length(void) {
    printf("[TEST] Cover - Run Length Validation\n");
    reset_test_data();
    
    // All runs in cover must have len ≤ 8
    uint64_t run_lens[] = {1, 2, 4, 8};
    size_t num_runs = 4;
    
    for (size_t i = 0; i < num_runs; i++) {
        assert(run_lens[i] <= 8);
    }
    
    printf("  ✓ Run length validation passed\n");
    return 1;
}

int main(void) {
    printf("========================================\n");
    printf("Chicago TDD: Cover Noun Tests\n");
    printf("========================================\n\n");
    
    int passed = 0;
    int total = 0;
    
    total++; if (test_cover_defines_select()) passed++;
    total++; if (test_cover_defines_shard()) passed++;
    total++; if (test_cover_enforces_glue()) passed++;
    total++; if (test_cover_validates_run_length()) passed++;
    
    printf("\n========================================\n");
    printf("Results: %d/%d tests passed\n", passed, total);
    printf("========================================\n");
    
    return (passed == total) ? 0 : 1;
}

