|
| 1 | +/* Defect hunt: run every candidate equality lib against the signature cases |
| 2 | + * and a differential fuzz adjudicated by isoequal + the brute-force |
| 3 | + * reference. Output: per-lib defect table for the PR campaign. |
| 4 | + * Run: node hunt/differential.mjs (from repo root, after npm run build) */ |
| 5 | +import { isDeepStrictEqual } from 'node:util' |
| 6 | +import { createRequire } from 'node:module' |
| 7 | +import { isoEqual, isoEqualReference } from '../dist/index.js' |
| 8 | + |
| 9 | +const require = createRequire(import.meta.url) |
| 10 | + |
| 11 | +const libs = {} |
| 12 | +libs['node:isDeepStrictEqual'] = isDeepStrictEqual |
| 13 | +try { libs['deep-eql (chai/vitest)'] = require('deep-eql') } catch (e) { console.error('deep-eql load:', e.message) } |
| 14 | +try { libs['es-toolkit isEqual'] = require('es-toolkit').isEqual } catch (e) { console.error('es-toolkit load:', e.message) } |
| 15 | +try { libs['remeda isDeepEqual'] = require('remeda').isDeepEqual } catch (e) { console.error('remeda load:', e.message) } |
| 16 | +try { libs['@wry/equality (apollo)'] = require('@wry/equality').equal } catch (e) { console.error('wry load:', e.message) } |
| 17 | +try { libs['react-fast-compare'] = require('react-fast-compare') } catch (e) { console.error('rfc load:', e.message) } |
| 18 | + |
| 19 | +/* ------------------------------ signature cases --------------------------- */ |
| 20 | +const ring = (k) => { |
| 21 | + const nodes = Array.from({ length: k }, () => ({})) |
| 22 | + for (let i = 0; i < k; i++) nodes[i].next = nodes[(i + 1) % k] |
| 23 | + return nodes |
| 24 | +} |
| 25 | +const shuffledRingSet = (k, order) => { |
| 26 | + const b = ring(k) |
| 27 | + return new Set(order.map((i) => b[i])) |
| 28 | +} |
| 29 | + |
| 30 | +const CASES = [] |
| 31 | +{ |
| 32 | + // 1. self-cycle (must be true) |
| 33 | + const a = {}; a.self = a |
| 34 | + const b = {}; b.self = b |
| 35 | + CASES.push(['self-cycle', a, b, true]) |
| 36 | +} |
| 37 | +{ |
| 38 | + // 2. cyclic shuffled rings in Sets (must be true) |
| 39 | + CASES.push(['C6 vs shuffled C6 in Set', new Set(ring(6)), shuffledRingSet(6, [3, 1, 5, 0, 4, 2]), true]) |
| 40 | +} |
| 41 | +{ |
| 42 | + // 3. C3+C3 vs C6 (must be false) |
| 43 | + CASES.push(['C3+C3 vs C6', new Set([...ring(3), ...ring(3)]), new Set(ring(6)), false]) |
| 44 | +} |
| 45 | +{ |
| 46 | + // 4. multiset trap (must be false) |
| 47 | + CASES.push(['set multiset trap', new Set([[1, 2], [1, 2], [3, 4]]), new Set([[1, 2], [3, 4], [3, 4]]), false]) |
| 48 | +} |
| 49 | +{ |
| 50 | + // 5. Map duplicate-shaped keys (must be true) |
| 51 | + CASES.push(['Map dup-shaped keys', new Map([[{}, 1], [{}, 2]]), new Map([[{}, 2], [{}, 1]]), true]) |
| 52 | +} |
| 53 | +{ |
| 54 | + // 6. nested-set false positive (dequal#31 shape; must be false) |
| 55 | + CASES.push(['nested-set overcount', new Set([new Set([1, 2]), new Set([1, 2])]), new Set([new Set([1, 2]), new Set([1, 4])]), false]) |
| 56 | +} |
| 57 | +{ |
| 58 | + // 7. boxed primitives (must be false) |
| 59 | + // eslint-disable-next-line no-new-wrappers |
| 60 | + CASES.push(['boxed Number 1 vs 2', new Number(1), new Number(2), false]) |
| 61 | +} |
| 62 | +{ |
| 63 | + // 8. plain agreement sanity (must be true) |
| 64 | + CASES.push(['plain nested equal', { a: [1, { b: 2 }], s: new Set([1, 2]) }, { a: [1, { b: 2 }], s: new Set([2, 1]) }, true]) |
| 65 | +} |
| 66 | + |
| 67 | +console.log('=== SIGNATURE CASES ===') |
| 68 | +const defects = {} |
| 69 | +for (const [name, fn] of Object.entries(libs)) { |
| 70 | + defects[name] = [] |
| 71 | + for (const [label, a, b, want] of CASES) { |
| 72 | + let got |
| 73 | + try { |
| 74 | + got = fn(a, b) |
| 75 | + } catch (e) { |
| 76 | + got = `THROW(${e.constructor.name})` |
| 77 | + } |
| 78 | + const ok = got === want |
| 79 | + if (!ok) defects[name].push(`${label}: got ${got}, want ${want}`) |
| 80 | + } |
| 81 | +} |
| 82 | +for (const [name, list] of Object.entries(defects)) { |
| 83 | + console.log(`\n--- ${name}: ${list.length} defects`) |
| 84 | + for (const d of list) console.log(' ✗', d) |
| 85 | +} |
| 86 | + |
| 87 | +/* ------------------------------ acyclic fuzz ------------------------------ */ |
| 88 | +let s = 0xdecafbad |
| 89 | +const rnd = () => { |
| 90 | + s ^= s << 13; s ^= s >>> 17; s ^= s << 5 |
| 91 | + return (s >>> 0) / 0x100000000 |
| 92 | +} |
| 93 | +const ri = (n) => (rnd() * n) | 0 |
| 94 | +const PRIMS = [0, 1, 'a', 'b', true, null, undefined] |
| 95 | +function gen(depth) { |
| 96 | + if (depth <= 0 || rnd() < 0.35) return PRIMS[ri(PRIMS.length)] |
| 97 | + const kind = ri(4) |
| 98 | + const n = 1 + ri(3) |
| 99 | + if (kind === 0) return Array.from({ length: n }, () => gen(depth - 1)) |
| 100 | + if (kind === 1) { |
| 101 | + const o = {} |
| 102 | + for (let i = 0; i < n; i++) o[`k${ri(4)}`] = gen(depth - 1) |
| 103 | + return o |
| 104 | + } |
| 105 | + if (kind === 2) return new Set(Array.from({ length: n }, () => gen(depth - 1))) |
| 106 | + return new Map(Array.from({ length: n }, () => [gen(depth - 1), gen(depth - 1)])) |
| 107 | +} |
| 108 | + |
| 109 | +console.log('\n=== ACYCLIC DIFFERENTIAL FUZZ (3000 pairs/lib) ===') |
| 110 | +for (const [name, fn] of Object.entries(libs)) { |
| 111 | + let falseNeg = 0 |
| 112 | + let falsePos = 0 |
| 113 | + let crash = 0 |
| 114 | + let example = null |
| 115 | + for (let i = 0; i < 3000; i++) { |
| 116 | + const a = gen(3) |
| 117 | + const b = rnd() < 0.4 ? structuredClone(a) : gen(3) |
| 118 | + const truth = isoEqualReference(a, b) // exhaustive oracle |
| 119 | + if (isoEqual(a, b) !== truth) { console.log('!!! ENGINE/ORACLE SPLIT', i); break } |
| 120 | + let got |
| 121 | + try { |
| 122 | + got = fn(a, b) |
| 123 | + } catch { |
| 124 | + crash++ |
| 125 | + continue |
| 126 | + } |
| 127 | + if (got !== truth) { |
| 128 | + if (truth && !got) falseNeg++ |
| 129 | + else falsePos++ |
| 130 | + if (!example) example = i |
| 131 | + } |
| 132 | + } |
| 133 | + console.log(`${name}: falseNeg=${falseNeg} falsePos=${falsePos} crash=${crash}${example !== null ? ` (first at i=${example})` : ''}`) |
| 134 | +} |
0 commit comments