// Auto-generated by scripts/gen_parity_tests.py from docs/runtime-parity.md.
// Module: node:buffer
// This file is a byte-for-byte parity test against `node --experimental-strip-types`.
// Edit the TODO lines below to exercise each API; rerun
// `./run_parity_tests.sh --filter test_parity_buffer` to verify.

import { Buffer } from "node:buffer";
import * as buffer from "node:buffer";


// ── Static Methods (Buffer) ──
try {
  const aAlloc = Buffer.alloc(8);
  console.log("Buffer.alloc(8) len:", aAlloc.length, "hex:", aAlloc.toString("hex"));
  const aAllocFill = Buffer.alloc(8, 0xab);
  console.log("Buffer.alloc(8,0xab) hex:", aAllocFill.toString("hex"));
  const aAllocFillStr = Buffer.alloc(6, "ab", "hex");
  console.log("Buffer.alloc(6,'ab','hex') hex:", aAllocFillStr.toString("hex"));
} catch (e: any) { console.log("Buffer.alloc ERR:", e && e.message); }

try {
  const u = Buffer.allocUnsafe(4);
  u.fill(0);
  console.log("Buffer.allocUnsafe(4).fill(0) hex:", u.toString("hex"));
} catch (e: any) { console.log("Buffer.allocUnsafe ERR:", e && e.message); }

try {
  const us = Buffer.allocUnsafeSlow(4);
  us.fill(0);
  console.log("Buffer.allocUnsafeSlow(4).fill(0) hex:", us.toString("hex"));
} catch (e: any) { console.log("Buffer.allocUnsafeSlow ERR:", e && e.message); }

try {
  console.log("Buffer.byteLength('hello','utf8'):", Buffer.byteLength("hello", "utf8"));
  console.log("Buffer.byteLength('héllo','utf8'):", Buffer.byteLength("héllo", "utf8"));
  console.log("Buffer.byteLength('616263','hex'):", Buffer.byteLength("616263", "hex"));
} catch (e: any) { console.log("Buffer.byteLength ERR:", e && e.message); }

try {
  const c1 = Buffer.from("abc");
  const c2 = Buffer.from("abd");
  console.log("Buffer.compare(a,b):", Buffer.compare(c1, c2));
  console.log("Buffer.compare(a,a):", Buffer.compare(c1, c1));
  console.log("Buffer.compare(b,a):", Buffer.compare(c2, c1));
} catch (e: any) { console.log("Buffer.compare ERR:", e && e.message); }

try {
  const concated = Buffer.concat([Buffer.from("ab"), Buffer.from("cd")]);
  console.log("Buffer.concat hex:", concated.toString("hex"));
  const concatedTotal = Buffer.concat([Buffer.from("ab"), Buffer.from("cd")], 3);
  console.log("Buffer.concat totalLen=3 hex:", concatedTotal.toString("hex"));
} catch (e: any) { console.log("Buffer.concat ERR:", e && e.message); }

try {
  const src = new Uint8Array([1, 2, 3, 4]);
  const cb = Buffer.copyBytesFrom(src);
  console.log("Buffer.copyBytesFrom hex:", cb.toString("hex"));
  const cb2 = Buffer.copyBytesFrom(src, 1, 2);
  console.log("Buffer.copyBytesFrom(off,len) hex:", cb2.toString("hex"));
} catch (e: any) { console.log("Buffer.copyBytesFrom ERR:", e && e.message); }

try {
  const fa = Buffer.from([0x68, 0x65, 0x6c, 0x6c, 0x6f]);
  console.log("Buffer.from(array) utf8:", fa.toString("utf8"));
} catch (e: any) { console.log("Buffer.from(array) ERR:", e && e.message); }

try {
  const ab = new ArrayBuffer(4);
  const view = new Uint8Array(ab);
  view[0] = 0xde; view[1] = 0xad; view[2] = 0xbe; view[3] = 0xef;
  const fab = Buffer.from(ab);
  console.log("Buffer.from(arrayBuffer) hex:", fab.toString("hex"));
  const fab2 = Buffer.from(ab, 1, 2);
  console.log("Buffer.from(ab,1,2) hex:", fab2.toString("hex"));
} catch (e: any) { console.log("Buffer.from(arrayBuffer) ERR:", e && e.message); }

try {
  const src2 = Buffer.from("hello");
  const fb = Buffer.from(src2);
  console.log("Buffer.from(buffer) utf8:", fb.toString("utf8"));
} catch (e: any) { console.log("Buffer.from(buffer) ERR:", e && e.message); }

try {
  // object with toString
  const fo = Buffer.from({ length: 3, 0: 0x61, 1: 0x62, 2: 0x63 } as any);
  console.log("Buffer.from(object) hex:", fo.toString("hex"));
} catch (e: any) { console.log("Buffer.from(object) ERR:", e && e.message); }

try {
  console.log("Buffer.from('hello','utf8'):", Buffer.from("hello", "utf8").toString("utf8"));
  console.log("Buffer.from('68656c6c6f','hex'):", Buffer.from("68656c6c6f", "hex").toString("utf8"));
  console.log("Buffer.from('aGVsbG8=','base64'):", Buffer.from("aGVsbG8=", "base64").toString("utf8"));
} catch (e: any) { console.log("Buffer.from(string,enc) ERR:", e && e.message); }

try {
  console.log("Buffer.isBuffer(Buffer.from('x')):", Buffer.isBuffer(Buffer.from("x")));
  console.log("Buffer.isBuffer('x'):", Buffer.isBuffer("x"));
  console.log("Buffer.isBuffer(null):", Buffer.isBuffer(null));
} catch (e: any) { console.log("Buffer.isBuffer ERR:", e && e.message); }

try {
  console.log("Buffer.isEncoding('utf8'):", Buffer.isEncoding("utf8"));
  console.log("Buffer.isEncoding('hex'):", Buffer.isEncoding("hex"));
  console.log("Buffer.isEncoding('bogus'):", Buffer.isEncoding("bogus"));
} catch (e: any) { console.log("Buffer.isEncoding ERR:", e && e.message); }

// ── Static Properties (Buffer) ──
try {
  console.log("Buffer.poolSize:", Buffer.poolSize);
} catch (e: any) { console.log("Buffer.poolSize ERR:", e && e.message); }

// ── Instance Methods (Buffer) — Read ──
try {
  const r = Buffer.from("0102030405060708", "hex");
  console.log("readBigInt64BE:", r.readBigInt64BE(0).toString());
  console.log("readBigInt64LE:", r.readBigInt64LE(0).toString());
  console.log("readBigUInt64BE:", r.readBigUInt64BE(0).toString());
  console.log("readBigUInt64LE:", r.readBigUInt64LE(0).toString());
  console.log("readDoubleBE:", r.readDoubleBE(0));
  console.log("readDoubleLE:", r.readDoubleLE(0));
  console.log("readFloatBE:", r.readFloatBE(0));
  console.log("readFloatLE:", r.readFloatLE(0));
  console.log("readInt8:", r.readInt8(0));
  console.log("readInt16BE:", r.readInt16BE(0));
  console.log("readInt16LE:", r.readInt16LE(0));
  console.log("readInt32BE:", r.readInt32BE(0));
  console.log("readInt32LE:", r.readInt32LE(0));
  console.log("readIntBE(0,3):", r.readIntBE(0, 3));
  console.log("readIntLE(0,3):", r.readIntLE(0, 3));
  console.log("readUInt8:", r.readUInt8(0));
  console.log("readUInt16BE:", r.readUInt16BE(0));
  console.log("readUInt16LE:", r.readUInt16LE(0));
  console.log("readUInt32BE:", r.readUInt32BE(0));
  console.log("readUInt32LE:", r.readUInt32LE(0));
  console.log("readUIntBE(0,3):", r.readUIntBE(0, 3));
  console.log("readUIntLE(0,3):", r.readUIntLE(0, 3));
} catch (e: any) { console.log("read* ERR:", e && e.message); }

// ── Instance Methods (Buffer) — Write ──
try {
  const w = Buffer.alloc(8);
  w.writeBigInt64BE(BigInt(1), 0);
  console.log("writeBigInt64BE hex:", w.toString("hex"));
  w.fill(0);
  w.writeBigInt64LE(BigInt(1), 0);
  console.log("writeBigInt64LE hex:", w.toString("hex"));
  w.fill(0);
  w.writeBigUInt64BE(BigInt(2), 0);
  console.log("writeBigUInt64BE hex:", w.toString("hex"));
  w.fill(0);
  w.writeBigUInt64LE(BigInt(2), 0);
  console.log("writeBigUInt64LE hex:", w.toString("hex"));
  w.fill(0);
  w.writeDoubleBE(1.5, 0);
  console.log("writeDoubleBE hex:", w.toString("hex"));
  w.fill(0);
  w.writeDoubleLE(1.5, 0);
  console.log("writeDoubleLE hex:", w.toString("hex"));
} catch (e: any) { console.log("write big/double ERR:", e && e.message); }

try {
  const w2 = Buffer.alloc(8);
  w2.writeFloatBE(1.5, 0);
  console.log("writeFloatBE hex:", w2.toString("hex"));
  w2.fill(0);
  w2.writeFloatLE(1.5, 0);
  console.log("writeFloatLE hex:", w2.toString("hex"));
  w2.fill(0);
  w2.writeInt8(-1, 0);
  console.log("writeInt8 hex:", w2.toString("hex"));
  w2.fill(0);
  w2.writeInt16BE(-2, 0);
  console.log("writeInt16BE hex:", w2.toString("hex"));
  w2.fill(0);
  w2.writeInt16LE(-2, 0);
  console.log("writeInt16LE hex:", w2.toString("hex"));
  w2.fill(0);
  w2.writeInt32BE(-3, 0);
  console.log("writeInt32BE hex:", w2.toString("hex"));
  w2.fill(0);
  w2.writeInt32LE(-3, 0);
  console.log("writeInt32LE hex:", w2.toString("hex"));
} catch (e: any) { console.log("write float/int ERR:", e && e.message); }

try {
  const w3 = Buffer.alloc(8);
  w3.writeIntBE(0x123456, 0, 3);
  console.log("writeIntBE hex:", w3.toString("hex"));
  w3.fill(0);
  w3.writeIntLE(0x123456, 0, 3);
  console.log("writeIntLE hex:", w3.toString("hex"));
  w3.fill(0);
  w3.writeUInt8(0xab, 0);
  console.log("writeUInt8 hex:", w3.toString("hex"));
  w3.fill(0);
  w3.writeUInt16BE(0xabcd, 0);
  console.log("writeUInt16BE hex:", w3.toString("hex"));
  w3.fill(0);
  w3.writeUInt16LE(0xabcd, 0);
  console.log("writeUInt16LE hex:", w3.toString("hex"));
  w3.fill(0);
  w3.writeUInt32BE(0xabcdef01, 0);
  console.log("writeUInt32BE hex:", w3.toString("hex"));
  w3.fill(0);
  w3.writeUInt32LE(0xabcdef01, 0);
  console.log("writeUInt32LE hex:", w3.toString("hex"));
  w3.fill(0);
  w3.writeUIntBE(0x123456, 0, 3);
  console.log("writeUIntBE hex:", w3.toString("hex"));
  w3.fill(0);
  w3.writeUIntLE(0x123456, 0, 3);
  console.log("writeUIntLE hex:", w3.toString("hex"));
} catch (e: any) { console.log("write uint/intvar ERR:", e && e.message); }

try {
  const w4 = Buffer.alloc(8);
  const n = w4.write("hello", 0);
  console.log("write('hello',0) n:", n, "hex:", w4.toString("hex"));
} catch (e: any) { console.log("buf.write ERR:", e && e.message); }

// ── Instance Methods (Buffer) — Utility ──
try {
  const a = Buffer.from("abc");
  const b = Buffer.from("abd");
  console.log("buf.compare(target):", a.compare(b));
  console.log("buf.compare(target,1,2):", a.compare(b, 1, 2));
} catch (e: any) { console.log("buf.compare ERR:", e && e.message); }

try {
  const src = Buffer.from("abcdef");
  const dst = Buffer.alloc(6);
  const n = src.copy(dst);
  console.log("buf.copy n:", n, "hex:", dst.toString("hex"));
  const dst2 = Buffer.alloc(6);
  src.copy(dst2, 1, 2, 5);
  console.log("buf.copy(t,1,2,5) hex:", dst2.toString("hex"));
} catch (e: any) { console.log("buf.copy ERR:", e && e.message); }

try {
  const eb = Buffer.from("ab");
  const entries: string[] = [];
  for (const [i, v] of eb.entries()) {
    entries.push(i + "=" + v);
  }
  console.log("buf.entries():", entries.join(","));
} catch (e: any) { console.log("buf.entries ERR:", e && e.message); }

try {
  console.log("buf.equals(a,a):", Buffer.from("abc").equals(Buffer.from("abc")));
  console.log("buf.equals(a,b):", Buffer.from("abc").equals(Buffer.from("abd")));
} catch (e: any) { console.log("buf.equals ERR:", e && e.message); }

try {
  const f = Buffer.alloc(4).fill(0xaa);
  console.log("buf.fill(0xaa) hex:", f.toString("hex"));
  const f2 = Buffer.alloc(6).fill("ab", 0, 6, "utf8");
  console.log("buf.fill('ab',0,6,'utf8') hex:", f2.toString("hex"));
} catch (e: any) { console.log("buf.fill ERR:", e && e.message); }

try {
  const inc = Buffer.from("hello world");
  console.log("buf.includes('world'):", inc.includes("world"));
  console.log("buf.includes('xyz'):", inc.includes("xyz"));
} catch (e: any) { console.log("buf.includes ERR:", e && e.message); }

try {
  const idx = Buffer.from("hello world");
  console.log("buf.indexOf('world'):", idx.indexOf("world"));
  console.log("buf.indexOf('xyz'):", idx.indexOf("xyz"));
} catch (e: any) { console.log("buf.indexOf ERR:", e && e.message); }

try {
  const kb = Buffer.from("ab");
  const keys: number[] = [];
  for (const k of kb.keys()) {
    keys.push(k);
  }
  console.log("buf.keys():", keys.join(","));
} catch (e: any) { console.log("buf.keys ERR:", e && e.message); }

try {
  const lib = Buffer.from("hello world hello");
  console.log("buf.lastIndexOf('hello'):", lib.lastIndexOf("hello"));
  console.log("buf.lastIndexOf('xyz'):", lib.lastIndexOf("xyz"));
} catch (e: any) { console.log("buf.lastIndexOf ERR:", e && e.message); }

try {
  const sl = Buffer.from("hello world");
  console.log("buf.slice(0,5) utf8:", sl.slice(0, 5).toString("utf8"));
  console.log("buf.slice(6) utf8:", sl.slice(6).toString("utf8"));
} catch (e: any) { console.log("buf.slice ERR:", e && e.message); }

try {
  const sa = Buffer.from("hello world");
  console.log("buf.subarray(0,5) utf8:", sa.subarray(0, 5).toString("utf8"));
  console.log("buf.subarray(6) utf8:", sa.subarray(6).toString("utf8"));
} catch (e: any) { console.log("buf.subarray ERR:", e && e.message); }

try {
  const s16 = Buffer.from("0102030405060708", "hex");
  s16.swap16();
  console.log("buf.swap16() hex:", s16.toString("hex"));
} catch (e: any) { console.log("buf.swap16 ERR:", e && e.message); }

try {
  const s32 = Buffer.from("0102030405060708", "hex");
  s32.swap32();
  console.log("buf.swap32() hex:", s32.toString("hex"));
} catch (e: any) { console.log("buf.swap32 ERR:", e && e.message); }

try {
  const s64 = Buffer.from("0102030405060708", "hex");
  s64.swap64();
  console.log("buf.swap64() hex:", s64.toString("hex"));
} catch (e: any) { console.log("buf.swap64 ERR:", e && e.message); }

try {
  const j = Buffer.from([0x61, 0x62, 0x63]);
  console.log("buf.toJSON() type:", j.toJSON().type);
  console.log("buf.toJSON() data:", j.toJSON().data.join(","));
} catch (e: any) { console.log("buf.toJSON ERR:", e && e.message); }

try {
  const t = Buffer.from("hello");
  console.log("buf.toString():", t.toString());
  console.log("buf.toString('hex'):", t.toString("hex"));
  console.log("buf.toString('base64'):", t.toString("base64"));
  console.log("buf.toString('utf8',0,3):", t.toString("utf8", 0, 3));
} catch (e: any) { console.log("buf.toString ERR:", e && e.message); }

try {
  const vb = Buffer.from("ab");
  const vals: number[] = [];
  for (const v of vb.values()) {
    vals.push(v);
  }
  console.log("buf.values():", vals.join(","));
} catch (e: any) { console.log("buf.values ERR:", e && e.message); }

// ── Instance Properties (Buffer) ──
try {
  const ip = Buffer.from([0x10, 0x20, 0x30]);
  console.log("buf[0]:", ip[0]);
  console.log("buf[1]:", ip[1]);
  console.log("buf[2]:", ip[2]);
  console.log("buf.length:", ip.length);
  console.log("buf.buffer instanceof ArrayBuffer:", ip.buffer instanceof ArrayBuffer);
  // Use allocUnsafeSlow to get a non-pooled buffer with deterministic byteOffset=0.
  const ipSlow = Buffer.allocUnsafeSlow(4);
  ipSlow.fill(0);
  console.log("buf.byteOffset (slow):", ipSlow.byteOffset);
  console.log("buf.parent typeof:", typeof ipSlow.parent);
} catch (e: any) { console.log("buf properties ERR:", e && e.message); }

// ── Class: Blob ──
// Indirect ctor lookup so an unsupported Blob doesn't break linking.
const BlobCtor: any = (buffer as any).Blob;
async function blobTestsAsync() {
  try {
    const blob = new BlobCtor(["hello"]);
    console.log("Blob size:", blob.size);
    console.log("Blob type:", blob.type);
    const txt = await blob.text();
    console.log("Blob text:", txt);
    const ab = await blob.arrayBuffer();
    console.log("Blob arrayBuffer byteLength:", ab.byteLength);
    const sl = blob.slice(0, 3);
    console.log("Blob slice size:", sl.size);
    const sliceTxt = await sl.text();
    console.log("Blob slice text:", sliceTxt);
    const blobTyped = new BlobCtor(["{\"a\":1}"], { type: "application/json" });
    console.log("Blob with type:", blobTyped.type);
  } catch (e: any) { console.log("Blob ERR:", e && e.message); }

  try {
    const blob2 = new BlobCtor(["bytes"]);
    const bb = await blob2.bytes();
    console.log("Blob bytes length:", bb.length);
    console.log("Blob bytes[0]:", bb[0]);
  } catch (e: any) { console.log("Blob.bytes ERR:", e && e.message); }

  try {
    const blob3 = new BlobCtor(["stream"]);
    const s = blob3.stream();
    console.log("Blob.stream() typeof:", typeof s);
  } catch (e: any) { console.log("Blob.stream ERR:", e && e.message); }
}

function blobTestsUnsupported() {
  console.log("Blob size: <unsupported>");
  console.log("Blob type: <unsupported>");
  console.log("Blob text: <unsupported>");
  console.log("Blob arrayBuffer byteLength: <unsupported>");
  console.log("Blob slice size: <unsupported>");
  console.log("Blob slice text: <unsupported>");
  console.log("Blob with type: <unsupported>");
  console.log("Blob bytes length: <unsupported>");
  console.log("Blob bytes[0]: <unsupported>");
  console.log("Blob.stream() typeof: <unsupported>");
}


// ── Class: File ──
const FileCtor: any = (buffer as any).File;
function fileTests() {
  try {
    if (typeof FileCtor === "function") {
      const f = new FileCtor(["hello"], "greeting.txt", { lastModified: 1700000000000 });
      console.log("File.name:", f.name);
      console.log("File.lastModified:", f.lastModified);
    } else {
      console.log("File.name: <unsupported>");
      console.log("File.lastModified: <unsupported>");
    }
  } catch (e: any) { console.log("File ERR:", e && e.message); }
}

// ── Module-Level Functions ──
try {
  console.log("buffer.atob('aGVsbG8='):", buffer.atob("aGVsbG8="));
} catch (e: any) { console.log("buffer.atob ERR:", e && e.message); }

try {
  console.log("buffer.btoa('hello'):", buffer.btoa("hello"));
} catch (e: any) { console.log("buffer.btoa ERR:", e && e.message); }

try {
  console.log("buffer.isAscii(Buffer.from('hi')):", buffer.isAscii(Buffer.from("hi")));
  console.log("buffer.isAscii(Buffer.from([0xff])):", buffer.isAscii(Buffer.from([0xff])));
} catch (e: any) { console.log("buffer.isAscii ERR:", e && e.message); }

try {
  console.log("buffer.isUtf8(Buffer.from('hi')):", buffer.isUtf8(Buffer.from("hi")));
  console.log("buffer.isUtf8(Buffer.from([0xff,0xfe])):", buffer.isUtf8(Buffer.from([0xff, 0xfe])));
} catch (e: any) { console.log("buffer.isUtf8 ERR:", e && e.message); }

try {
  // resolveObjectURL on a never-registered id returns undefined.
  const r = (buffer as any).resolveObjectURL("blob:nodedata:00000000-0000-0000-0000-000000000000");
  console.log("buffer.resolveObjectURL:", r);
} catch (e: any) { console.log("buffer.resolveObjectURL ERR:", e && e.message); }

try {
  const tc = (buffer as any).transcode(Buffer.from("hello"), "utf8", "latin1");
  console.log("buffer.transcode hex:", tc.toString("hex"));
} catch (e: any) { console.log("buffer.transcode ERR:", e && e.message); }

// Drive the helpers.
if (typeof BlobCtor === "function") {
  blobTestsAsync();
} else {
  blobTestsUnsupported();
}
fileTests();

// ── Module-Level Constants ──
try {
  console.log("buffer.INSPECT_MAX_BYTES:", buffer.INSPECT_MAX_BYTES);
} catch (e: any) { console.log("buffer.INSPECT_MAX_BYTES ERR:", e && e.message); }
try {
  console.log("buffer.kMaxLength:", buffer.kMaxLength);
} catch (e: any) { console.log("buffer.kMaxLength ERR:", e && e.message); }
try {
  console.log("buffer.kStringMaxLength:", buffer.kStringMaxLength);
} catch (e: any) { console.log("buffer.kStringMaxLength ERR:", e && e.message); }
try {
  console.log("buffer.constants.MAX_LENGTH:", buffer.constants.MAX_LENGTH);
} catch (e: any) { console.log("buffer.constants.MAX_LENGTH ERR:", e && e.message); }
try {
  console.log("buffer.constants.MAX_STRING_LENGTH:", buffer.constants.MAX_STRING_LENGTH);
} catch (e: any) { console.log("buffer.constants.MAX_STRING_LENGTH ERR:", e && e.message); }

// Coverage: 5 auto-emitted, 101 TODO, 0 skip-listed.
