blob: 450599c69fcb2c3bdfbd424b09564dd378ae628e [file] [log] [blame]
// Copyright (C) 2018 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import {search, searchEq, searchRange, searchSegment} from './binary_search';
test('binarySearch', () => {
expect(search([], 100)).toEqual(-1);
expect(search([42], 42)).toEqual(0);
expect(search([42], 43)).toEqual(0);
expect(search([42], 41)).toEqual(-1);
expect(search([42, 43], 42)).toEqual(0);
expect(search([42, 43], 43)).toEqual(1);
expect(search([42, 43], 44)).toEqual(1);
expect(search([42, 43, 44], 41)).toEqual(-1);
expect(search([42, 43, 44], 42)).toEqual(0);
expect(search([42, 43, 44], 43)).toEqual(1);
expect(search([42, 43, 44], 44)).toEqual(2);
expect(search([42, 43, 44], 45)).toEqual(2);
});
test('searchEq', () => {
expect(searchEq([], 100)).toEqual([0, 0]);
expect(searchEq([42], 41)).toEqual([0, 0]);
expect(searchEq([42], 42)).toEqual([0, 1]);
expect(searchEq([42], 43)).toEqual([1, 1]);
expect(searchEq([42, 44], 42)).toEqual([0, 1]);
expect(searchEq([42, 42], 42)).toEqual([0, 2]);
expect(searchEq([41, 43], 42)).toEqual([1, 1]);
});
test('searchRange', () => {
expect(searchRange([], 100)).toEqual([0, 0]);
expect(searchRange([42], 41)).toEqual([0, 0]);
expect(searchRange([42], 42)).toEqual([0, 1]);
expect(searchRange([42], 43)).toEqual([0, 1]);
expect(searchRange([39, 41], 38)).toEqual([0, 0]);
expect(searchRange([39, 41], 39)).toEqual([0, 1]);
expect(searchRange([39, 41], 40)).toEqual([0, 1]);
expect(searchRange([39, 41], 41)).toEqual([1, 2]);
expect(searchRange([39, 41], 42)).toEqual([1, 2]);
expect(searchRange([38, 40, 40, 40, 42], 39)).toEqual([0, 1]);
expect(searchRange([38, 40, 40, 40, 42], 40)).toEqual([1, 4]);
expect(searchRange([38, 40, 40, 40, 42], 41)).toEqual([3, 4]);
});
test('searchSegment', () => {
expect(searchSegment(Float64Array.of(), 100)).toEqual([-1, -1]);
expect(searchSegment([42], 41)).toEqual([-1, 0]);
expect(searchSegment([42], 42)).toEqual([0, -1]);
expect(searchSegment([42], 43)).toEqual([0, -1]);
expect(searchSegment([42, 44], 42)).toEqual([0, 1]);
expect(searchSegment([1, 2, 2, 3], 2)).toEqual([2, 3]);
});