Files
dynamic-badges-action/node_modules/binary-search/test.js
Rúnar Berg a2d3829b14 Add save svg-badge directly to gist
This adds the posibility of saving an SVG badge generated by the same
shields.io dirictly to the gist. Instead of prepering a JSON file to
be sent to their service, we use their library directly, which outputs
an SVG file that we can save to the user’s gist.

Filenames ending in `.svg` will use this library automatically.

Additionally there is a major refactoring where the older `node:http`
library has been swapped out for `fetch`.

Also swap from node 16 to node 20

fixes #24
2023-10-05 08:29:45 -07:00

47 lines
1.4 KiB
JavaScript

var expect = require("chai").expect;
describe("binarysearch", function() {
var bs = require("./"),
arr = [1, 2, 2, 2, 3, 5, 9],
cmp = function(a, b) { return a - b; };
it("should bail if not passed an array", function() {
expect(function() { bs(undefined, 3, cmp); }).to.throw(TypeError);
});
it("should bail if not passed a comparator", function() {
expect(function() { bs(arr, 3, undefined); }).to.throw(TypeError);
});
it("should return the index of an item in a sorted array", function() {
expect(bs(arr, 3, cmp)).to.equal(4);
});
it("should return the index of where the item would go plus one, negated, if the item is not found", function() {
expect(bs(arr, 4, cmp)).to.equal(-6);
});
it("should return any valid index if an item exists multiple times in the array", function() {
expect(bs(arr, 2, cmp)).to.equal(3);
});
it("should work even on empty arrays", function() {
expect(bs([], 42, cmp)).to.equal(-1);
});
it("should work even on arrays of doubles", function() {
expect(bs([0.0, 0.1, 0.2, 0.3, 0.4], 0.25, cmp)).to.equal(-4);
});
it("should pass the index and array parameters to the comparator", function() {
var indexes = [],
indexCmp = function(a, b, i, array) {
expect(array).to.equal(arr);
indexes.push(i);
return cmp(a, b);
};
bs(arr, 3, indexCmp);
expect(indexes).to.deep.equal([3, 5, 4])
});
});