aboutsummaryrefslogtreecommitdiff
path: root/test/bun.js/third-party/napi_create_external/napi-create-external.test.ts
blob: 9084b525648be6e5bd5f6f0ce80de17dbebe3381 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import { test, it, describe, expect } from "bun:test";
import { withoutAggressiveGC } from "gc";
import * as _ from "lodash";

function rebase(str, inBase, outBase) {
  const mapBase = (b) => (b === 2 ? 32 : b === 16 ? 8 : null);
  const stride = mapBase(inBase);
  const pad = mapBase(outBase);
  if (!stride) throw new Error(`Bad inBase ${inBase}`);
  if (!pad) throw new Error(`Bad outBase ${outBase}`);
  if (str.length % stride) throw new Error(`Bad string length ${str.length}`);
  const out = [];
  for (let i = 0; i < str.length; i += stride)
    out.push(
      parseInt(str.slice(i, i + stride), inBase)
        .toString(outBase)
        .padStart(pad, "0"),
    );
  return out.join("");
}

function expectDeepEqual(a, b) {
  expect(a).toEqual(b);
}
class HashMaker {
  constructor(length) {
    this.length = length;
    this._dist = {};
  }
  length: number;
  _dist: any;

  binToHex(binHash) {
    if (binHash.length !== this.length)
      throw new Error(
        `Hash length mismatch ${this.length} != ${binHash.length}`,
      );
    return rebase(binHash, 2, 16);
  }

  makeBits() {
    const bits = [];
    for (let i = 0; i < this.length; i++) bits.push(i);
    return _.shuffle(bits);
  }

  makeRandom() {
    const bits = [];
    for (let i = 0; i < this.length; i++)
      bits.push(Math.random() < 0.5 ? 1 : 0);
    return bits;
  }

  get keySet() {
    return (this._set = this._set || new Set(this.data));
  }

  randomKey() {
    while (true) {
      const hash = this.binToHex(this.makeRandom().join(""));
      if (!this.keySet.has(hash)) return hash;
    }
  }

  get data() {
    return (this._data =
      this._data ||
      (() => {
        const bits = this.makeBits();
        const base = this.makeRandom();
        const data = [];
        for (let stride = 0; bits.length; stride++) {
          const flip = bits.splice(0, stride);
          for (const bit of flip) base[bit] = 1 - base[bit];
          data.push(this.binToHex(base.join("")));
        }
        return data;
      })());
  }

  get random() {
    const d = this.data;
    return d[Math.floor(Math.random() * d.length)];
  }

  distance(a, b) {
    const bitCount = (n) => {
      n = n - ((n >> 1) & 0x55555555);
      n = (n & 0x33333333) + ((n >> 2) & 0x33333333);
      return (((n + (n >> 4)) & 0xf0f0f0f) * 0x1010101) >> 24;
    };

    if (a === b) return 0;
    if (a > b) return this.distance(b, a);
    const hash = a + "-" + b;
    return (this._dist[hash] =
      this._dist[hash] ||
      (() => {
        let dist = 0;
        for (let i = 0; i < a.length; i += 8) {
          const va = parseInt(a.slice(i, i + 8), 16);
          const vb = parseInt(b.slice(i, i + 8), 16);
          dist += bitCount(va ^ vb);
        }
        return dist;
      })());
  }

  query(baseKey, maxDist) {
    const out = [];
    for (const key of this.data) {
      const distance = this.distance(key, baseKey);
      if (distance <= maxDist) out.push({ key, distance });
    }
    return out.sort((a, b) => a.distance - b.distance);
  }
}

const treeClass = require("bktree-fast/native");

withoutAggressiveGC(() => {
  // this test is too slow
  for (let keyLen = 64; keyLen <= 64; keyLen += 64) {
    // for (let keyLen = 64; keyLen <= 512; keyLen += 64) {
    const hm = new HashMaker(keyLen);
    describe(`Key length: ${keyLen}`, () => {
      it("should compute distance", () => {
        const tree = new treeClass(keyLen);
        for (const a of hm.data)
          for (const b of hm.data)
            expect(tree.distance(a, b)).toBe(hm.distance(a, b));
      });

      it("should know which keys it has", () => {
        const tree = new treeClass(keyLen).add(hm.data);
        expectDeepEqual(
          hm.data.map((hash) => tree.has(hash)),
          hm.data.map(() => true),
        );
        // Not interested in the hash
        for (const hash of hm.data)
          expect(tree.has(hm.randomKey())).toBe(false);
      });

      it("should know the tree size", () => {
        const tree = new treeClass(keyLen, { foo: 1 });
        expect(tree.size).toBe(0);
        tree.add(hm.data);
        expect(tree.size).toBe(hm.data.length);
        tree.add(hm.data);
        expect(tree.size).toBe(hm.data.length);
      });

      it("should walk the tree", () => {
        const tree = new treeClass(keyLen).add(hm.data);
        const got = [];
        tree.walk((hash, depth) => got.push(hash));
        expectDeepEqual(got.sort(), hm.data.slice(0).sort());
      });

      it("should query", () => {
        ((treeClass, expectDeepEqual) => {
          const tree = new treeClass(keyLen).add(hm.data);

          for (let dist = 0; dist <= hm.length; dist++) {
            for (const baseKey of [hm.random, hm.data[0]]) {
              const baseKey = hm.random;
              const got = [];
              tree.query(baseKey, dist, (key, distance) =>
                got.push({ key, distance }),
              );
              const want = hm.query(baseKey, dist);
              expectDeepEqual(
                got.sort((a, b) => a.distance - b.distance),
                want,
              );
              expectDeepEqual(tree.find(baseKey, dist), want);
            }
          }
        })(treeClass, expectDeepEqual);
      });
    });
  }

  describe("Misc functions", () => {
    it("should pad keys", () => {
      const tree = new treeClass(64);
      expect(tree.padKey("1")).toBe("0000000000000001");
      tree.add(["1", "2", "3"]);

      const got = [];
      tree.query("2", 3, (hash, distance) => got.push({ hash, distance }));
      const res = got.sort((a, b) => a.distance - b.distance);
      const want = [
        { hash: "0000000000000002", distance: 0 },
        { hash: "0000000000000003", distance: 1 },
        { hash: "0000000000000001", distance: 2 },
      ];

      expectDeepEqual(res, want);
    });
  });
});