var utf8 = use("utf8");

// Test character counting vs byte counting
var test1 = "Hello";
log.console("ASCII length test:");
log.console("  Characters:", utf8.length(test1));
log.console("  Bytes:", utf8.byte_length(test1));
log.console("  Match:", utf8.length(test1) == utf8.byte_length(test1) ? "PASS" : "FAIL");

var test2 = "Hello 世界";
log.console("\nMixed ASCII/Unicode length test:");
log.console("  Characters:", utf8.length(test2));
log.console("  Bytes:", utf8.byte_length(test2));
log.console("  Bytes > Characters:", utf8.byte_length(test2) > utf8.length(test2) ? "PASS" : "FAIL");

// Test codepoints
var test3 = "A😀B";
var codepoints = utf8.codepoints(test3);
log.console("\nCodepoints test:");
log.console("  String:", test3);
log.console("  Codepoints:", codepoints);
log.console("  A=65:", codepoints[0] == 65 ? "PASS" : "FAIL");
log.console("  😀=128512:", codepoints[1] == 128512 ? "PASS" : "FAIL");
log.console("  B=66:", codepoints[2] == 66 ? "PASS" : "FAIL");

// Test from_codepoints
var reconstructed = utf8.from_codepoints(codepoints);
log.console("  Reconstructed:", reconstructed);
log.console("  Match:", test3 == reconstructed ? "PASS" : "FAIL");

// Test encode/decode
var test4 = "UTF-8 encoding: 你好世界 🌍";
var encoded = utf8.encode(test4);
var decoded = utf8.decode(encoded);
log.console("\nEncode/decode test:");
log.console("  Original:", test4);
log.console("  Decoded:", decoded);
log.console("  Match:", test4 == decoded ? "PASS" : "FAIL");

// Test validation
log.console("\nValidation tests:");
log.console("  Valid UTF-8:", utf8.validate("Hello 世界") ? "PASS" : "FAIL");

// Test slicing
var test5 = "Hello 世界!";
log.console("\nSlice tests:");
log.console("  Original:", test5);
log.console("  slice(0, 5):", utf8.slice(test5, 0, 5));
log.console("  slice(6, 8):", utf8.slice(test5, 6, 8));
log.console("  slice(-3):", utf8.slice(test5, -3));
log.console("  slice(0, -1):", utf8.slice(test5, 0, -1));

// Test char_at
log.console("\nchar_at tests:");
log.console("  char_at(0):", utf8.char_at(test5, 0));
log.console("  char_at(6):", utf8.char_at(test5, 6));
log.console("  char_at(7):", utf8.char_at(test5, 7));
log.console("  char_at(100):", utf8.char_at(test5, 100));

// Test with emoji sequences
var test6 = "👨‍👩‍👧‍👦";
log.console("\nComplex emoji test:");
log.console("  String:", test6);
log.console("  Length:", utf8.length(test6));
log.console("  Byte length:", utf8.byte_length(test6));
log.console("  Codepoints:", utf8.codepoints(test6).length);

log.console("\nAll tests completed!");

$_.stop()