var fit = use("fit");

var tests_run = 0;
var tests_passed = 0;
var tests_failed = 0;

function test(description, actual, expected) {
    tests_run++;
    if (actual == expected) {
        tests_passed++;
        log.console("✓", description, "=", actual);
    } else {
        tests_failed++;
        log.console("✗", description, "expected", expected, "but got", actual);
    }
}

log.console("Running fit module tests...\n");

// Test fit.and
test("fit.and(12, 10)", fit.and(12, 10), 8);
test("fit.and(16, 2)", fit.and(16, 2), 0);
test("fit.and(15, 3)", fit.and(15, 3), 3);
test("fit.and(13, 3)", fit.and(13, 3), 1);
test("fit.and('10', 3)", fit.and("10", 3), null);

// Test fit.or
test("fit.or(12, 10)", fit.or(12, 10), 14);
test("fit.or(16, 2)", fit.or(16, 2), 18);
test("fit.or(15, 3)", fit.or(15, 3), 15);
test("fit.or(13, 3)", fit.or(13, 3), 15);

// Test fit.xor
test("fit.xor(12, 10)", fit.xor(12, 10), 6);
test("fit.xor(16, 2)", fit.xor(16, 2), 18);
test("fit.xor(15, 3)", fit.xor(15, 3), 12);
test("fit.xor(13, 3)", fit.xor(13, 3), 14);
test("fit.xor(13.01, 3)", fit.xor(13.01, 3), null);

// Test fit.left
test("fit.left(12, 10)", fit.left(12, 10), 12288);
test("fit.left(16, 2)", fit.left(16, 2), 64);
test("fit.left(15, 53)", fit.left(15, 53), -9007199254740992);

// Test fit.right
test("fit.right(12, 10)", fit.right(12, 10), 0);
test("fit.right(19, 2)", fit.right(19, 2), 4);
test("fit.right(-9007199254740992, 53)", fit.right(-9007199254740992, 53), 7);

// Test fit.right_signed
test("fit.right_signed(-2, 1)", fit.right_signed(-2, 1), -1);

// Test fit.mask
test("fit.mask(0)", fit.mask(0), 0);
test("fit.mask(1)", fit.mask(1), 1);
test("fit.mask(3)", fit.mask(3), 7);
test("fit.mask(8)", fit.mask(8), 255);
test("fit.mask(16)", fit.mask(16), 65535);
test("fit.mask(32)", fit.mask(32), 4294967295);
test("fit.mask(55)", fit.mask(55), 36028797018963967);
test("fit.mask(56)", fit.mask(56), -1);
test("fit.mask(57)", fit.mask(57), null);
test("fit.mask(-1)", fit.mask(-1), -2);
test("fit.mask(-3)", fit.mask(-3), -8);
test("fit.mask(-8)", fit.mask(-8), -256);
test("fit.mask(-16)", fit.mask(-16), -65536);
test("fit.mask(-32)", fit.mask(-32), -4294967296);
test("fit.mask(-55)", fit.mask(-55), -36028797018963968);
test("fit.mask(-56)", fit.mask(-56), 0);

// Test fit.not
test("fit.not(0)", fit.not(0), -1);
test("fit.not(1)", fit.not(1), -2);
test("fit.not(-1)", fit.not(-1), 0);

// Test fit.ones
test("fit.ones(-1)", fit.ones(-1), 56);
test("fit.ones(0)", fit.ones(0), 0);
test("fit.ones(8)", fit.ones(8), 1);
test("fit.ones(18)", fit.ones(18), 2);
test("fit.ones(255)", fit.ones(255), 8);

// Test fit.zeros
test("fit.zeros(-1)", fit.zeros(-1), 0);
test("fit.zeros(0)", fit.zeros(0), 56);
test("fit.zeros(1)", fit.zeros(1), 55);
test("fit.zeros(2)", fit.zeros(2), 54);
test("fit.zeros(1024)", fit.zeros(1024), 45);

// Test fit.rotate
test("fit.rotate(1, 1)", fit.rotate(1, 1), 2);
test("fit.rotate(-2, 1)", fit.rotate(-2, 1), -3);
test("fit.rotate(1, 56)", fit.rotate(1, 56), 1);  // Full rotation
test("fit.rotate(1, -1)", fit.rotate(1, -1), 1 << 55);  // Rotate right by 1

// Test fit.reverse
test("fit.reverse(-36028797018963968)", fit.reverse(-36028797018963968), 1);
test("fit.reverse(3141592653589793)", fit.reverse(3141592653589793), 2334719610726733);

// Test edge cases and invalid inputs
test("fit.and with out-of-range", fit.and(1 << 56, 1), null);
test("fit.left with negative shift", fit.left(1, -1), null);
test("fit.left with large shift", fit.left(1, 100), null);
test("fit.right with negative shift", fit.right(1, -1), null);
test("fit.mask with float", fit.mask(3.5), null);

// Print test summary
log.console("\n" + "=".repeat(50));
log.console("Test Summary:");
log.console("  Total tests run:", tests_run);
log.console("  Tests passed:   ", tests_passed);
log.console("  Tests failed:   ", tests_failed);
log.console("  Success rate:   ", Math.round((tests_passed / tests_run) * 100) + "%");
log.console("=".repeat(50));

if (tests_failed > 0) {
    log.console("\nSome tests failed!");
} else {
    log.console("\nAll tests passed!");
}

$_.stop()