Path: blob/main/tests/unit/binary-search.test.ts
12924 views
/*1* binary-search.test.ts2*3* Copyright (C) 2021-2022 Posit Software, PBC4*5*/67import { glb } from "../../src/core/lib/binary-search.ts";8import bounds from "binary-search-bounds";910import { unitTest } from "../test.ts";11import { assert } from "testing/asserts";1213// deno-lint-ignore require-await14unitTest("binary-search-test - glb property tests", async () => {15// test by randomization that glb() behaves like le()1617const nTests = 1000;18// const nTests = 10000000; // this passed locally19for (let i = 0; i < nTests; ++i) {20const sz = ~~(Math.random() * 10);21const a: number[] = [];22for (let j = 0; j < sz; ++j) {23a.push(~~(Math.random() * 10));24}25a.sort();26const n = ~~(Math.random() * 10);27const ours = glb(a, n);28const theirs = bounds.le(a, n);29if (ours !== theirs) {30console.log("randomization failed!", { a, n });31}32assert(ours === theirs);33}34});3536// deno-lint-ignore require-await37unitTest("binary-search-test - previous failures", async () => {38assert(glb([1, 2, 4, 5, 5, 6, 6, 7, 7], 9) === 8);39assert(glb([5, 8, 9], 1) === -1);40assert(glb([5], 4) === -1);41assert(glb([5], 5) === 0);42assert(glb([5], 6) === 0);43});444546