1// Copyright (C) 2018 The Android Open Source Project 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15import {search, searchEq, searchRange, searchSegment} from './binary_search'; 16 17test('binarySearch', () => { 18 expect(search(Float64Array.of(), 100)).toEqual(-1); 19 expect(search(Float64Array.of(42), 42)).toEqual(0); 20 expect(search(Float64Array.of(42), 43)).toEqual(0); 21 expect(search(Float64Array.of(42), 41)).toEqual(-1); 22 expect(search(Float64Array.of(42, 43), 42)).toEqual(0); 23 expect(search(Float64Array.of(42, 43), 43)).toEqual(1); 24 expect(search(Float64Array.of(42, 43), 44)).toEqual(1); 25 expect(search(Float64Array.of(42, 43, 44), 41)).toEqual(-1); 26 expect(search(Float64Array.of(42, 43, 44), 42)).toEqual(0); 27 expect(search(Float64Array.of(42, 43, 44), 43)).toEqual(1); 28 expect(search(Float64Array.of(42, 43, 44), 44)).toEqual(2); 29 expect(search(Float64Array.of(42, 43, 44), 45)).toEqual(2); 30}); 31 32test('searchEq', () => { 33 expect(searchEq([], 100)).toEqual([0, 0]); 34 expect(searchEq([42], 41)).toEqual([0, 0]); 35 expect(searchEq([42], 42)).toEqual([0, 1]); 36 expect(searchEq([42], 43)).toEqual([1, 1]); 37 expect(searchEq([42, 44], 42)).toEqual([0, 1]); 38 expect(searchEq([42, 42], 42)).toEqual([0, 2]); 39 expect(searchEq([41, 43], 42)).toEqual([1, 1]); 40}); 41 42test('searchRange', () => { 43 expect(searchRange([], 100)).toEqual([0, 0]); 44 expect(searchRange([42], 41)).toEqual([0, 0]); 45 expect(searchRange([42], 42)).toEqual([0, 1]); 46 expect(searchRange([42], 43)).toEqual([0, 1]); 47 48 expect(searchRange([39, 41], 38)).toEqual([0, 0]); 49 expect(searchRange([39, 41], 39)).toEqual([0, 1]); 50 expect(searchRange([39, 41], 40)).toEqual([0, 1]); 51 expect(searchRange([39, 41], 41)).toEqual([1, 2]); 52 expect(searchRange([39, 41], 42)).toEqual([1, 2]); 53 54 expect(searchRange([38, 40, 40, 40, 42], 39)).toEqual([0, 1]); 55 expect(searchRange([38, 40, 40, 40, 42], 40)).toEqual([1, 4]); 56 expect(searchRange([38, 40, 40, 40, 42], 41)).toEqual([3, 4]); 57}); 58 59test('searchSegment', () => { 60 expect(searchSegment(Float64Array.of(), 100)).toEqual([-1, -1]); 61 expect(searchSegment(Float64Array.of(42), 41)).toEqual([-1, 0]); 62 expect(searchSegment(Float64Array.of(42), 42)).toEqual([0, -1]); 63 expect(searchSegment(Float64Array.of(42), 43)).toEqual([0, -1]); 64 expect(searchSegment(Float64Array.of(42, 44), 42)).toEqual([0, 1]); 65}); 66