// Tests for types.zig
const std = @import("std");
const types = @import("types.zig");
const TypeId = types.TypeId;
const TypeTable = types.TypeTable;
const TypeInfo = types.TypeInfo;

test "builtin types pre-populated" {
    const alloc = std.testing.allocator;
    var table = TypeTable.init(alloc);
    defer table.deinit();

    // Verify builtin slots
    try std.testing.expectEqual(TypeInfo.void, table.get(.void));
    try std.testing.expectEqual(TypeInfo.bool, table.get(.bool));
    try std.testing.expectEqual(TypeInfo{ .signed = 32 }, table.get(.s32));
    try std.testing.expectEqual(TypeInfo{ .unsigned = 8 }, table.get(.u8));
    try std.testing.expectEqual(TypeInfo.f64, table.get(.f64));
    try std.testing.expectEqual(TypeInfo.string, table.get(.string));
    try std.testing.expectEqual(TypeInfo.any, table.get(.any));
}

test "intern deduplicates structural types" {
    const alloc = std.testing.allocator;
    var table = TypeTable.init(alloc);
    defer table.deinit();

    const ptr1 = table.ptrTo(.s32);
    const ptr2 = table.ptrTo(.s32);
    try std.testing.expectEqual(ptr1, ptr2);

    const ptr3 = table.ptrTo(.f64);
    try std.testing.expect(ptr1 != ptr3);
}

test "slice and array interning" {
    const alloc = std.testing.allocator;
    var table = TypeTable.init(alloc);
    defer table.deinit();

    const slice1 = table.sliceOf(.s32);
    const slice2 = table.sliceOf(.s32);
    try std.testing.expectEqual(slice1, slice2);

    const arr1 = table.arrayOf(.u8, 10);
    const arr2 = table.arrayOf(.u8, 10);
    const arr3 = table.arrayOf(.u8, 20);
    try std.testing.expectEqual(arr1, arr2);
    try std.testing.expect(arr1 != arr3);
}

test "optional interning" {
    const alloc = std.testing.allocator;
    var table = TypeTable.init(alloc);
    defer table.deinit();

    const opt1 = table.optionalOf(.s32);
    const opt2 = table.optionalOf(.s32);
    try std.testing.expectEqual(opt1, opt2);

    const opt3 = table.optionalOf(.f64);
    try std.testing.expect(opt1 != opt3);
}

test "function type interning" {
    const alloc = std.testing.allocator;
    var table = TypeTable.init(alloc);
    defer table.deinit();

    const params = &[_]TypeId{ .s32, .s32 };
    const fn1 = table.functionType(params, .s64);
    const fn2 = table.functionType(params, .s64);
    try std.testing.expectEqual(fn1, fn2);

    const fn3 = table.functionType(params, .f64);
    try std.testing.expect(fn1 != fn3);
}

test "string pool interning" {
    const alloc = std.testing.allocator;
    var table = TypeTable.init(alloc);
    defer table.deinit();

    const id1 = table.internString("Point");
    const id2 = table.internString("Point");
    const id3 = table.internString("Rect");

    try std.testing.expectEqual(id1, id2);
    try std.testing.expect(id1 != id3);
    try std.testing.expectEqualStrings("Point", table.getString(id1));
    try std.testing.expectEqualStrings("Rect", table.getString(id3));
}

test "sizeOf builtins" {
    const alloc = std.testing.allocator;
    var table = TypeTable.init(alloc);
    defer table.deinit();

    try std.testing.expectEqual(@as(u32, 0), table.sizeOf(.void));
    try std.testing.expectEqual(@as(u32, 1), table.sizeOf(.bool));
    try std.testing.expectEqual(@as(u32, 4), table.sizeOf(.s32));
    try std.testing.expectEqual(@as(u32, 8), table.sizeOf(.s64));
    try std.testing.expectEqual(@as(u32, 1), table.sizeOf(.u8));
    try std.testing.expectEqual(@as(u32, 4), table.sizeOf(.f32));
    try std.testing.expectEqual(@as(u32, 8), table.sizeOf(.f64));
    try std.testing.expectEqual(@as(u32, 16), table.sizeOf(.string));
    try std.testing.expectEqual(@as(u32, 8), table.sizeOf(table.ptrTo(.s32)));
    try std.testing.expectEqual(@as(u32, 16), table.sizeOf(table.sliceOf(.s32)));
}

test "typeName for builtins" {
    const alloc = std.testing.allocator;
    var table = TypeTable.init(alloc);
    defer table.deinit();

    try std.testing.expectEqualStrings("s32", table.typeName(.s32));
    try std.testing.expectEqualStrings("bool", table.typeName(.bool));
    try std.testing.expectEqualStrings("string", table.typeName(.string));
    try std.testing.expectEqualStrings("void", table.typeName(.void));
    try std.testing.expectEqualStrings("Any", table.typeName(.any));
}
