1// Copyright (C) 2023 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 {allUnique, arrayEquals, removeFalsyValues, range} from './array_utils'; 16 17describe('range', () => { 18 it('returns array of elements in range [0; n)', () => { 19 expect(range(3)).toEqual([0, 1, 2]); 20 expect(range(5)).toEqual([0, 1, 2, 3, 4]); 21 }); 22 23 it('returns empty array on n = 0', () => { 24 expect(range(0)).toEqual([]); 25 }); 26 27 it('throws an error on negative input', () => { 28 expect(() => { 29 range(-10); 30 }).toThrowError(); 31 }); 32}); 33 34describe('allUnique', () => { 35 it('returns true on array with unique elements', () => { 36 expect(allUnique(['a', 'b', 'c'])).toBeTruthy(); 37 }); 38 39 it('returns false on array with repeated elements', () => { 40 expect(allUnique(['a', 'a', 'b'])).toBeFalsy(); 41 }); 42 43 // Couple of corner cases 44 it('returns true on an empty array', () => { 45 expect(allUnique([])).toBeTruthy(); 46 }); 47 48 it('returns true on an array with one element', () => { 49 expect(allUnique(['test'])).toBeTruthy(); 50 }); 51}); 52 53describe('arrayEquals', () => { 54 it('returns true when two arrays are the same', () => { 55 expect(arrayEquals(['a', 'b', 'c'], ['a', 'b', 'c'])).toBeTruthy(); 56 }); 57 58 it('returns false when two arrays differ', () => { 59 expect(arrayEquals(['a', 'b', 'c'], ['a', 'c', 'b'])).toBeFalsy(); 60 }); 61 62 it('returns false when arrays have differing lengths', () => { 63 expect(arrayEquals(['a', 'b', 'c'], ['a'])).toBeFalsy(); 64 }); 65}); 66 67test('removeFalsyValues', () => { 68 const input = [ 69 'a', 70 false, 71 undefined, 72 null, 73 '', 74 123, 75 123n, 76 true, 77 {foo: 'bar'}, 78 ]; 79 const expected = ['a', 123, 123n, true, {foo: 'bar'}]; 80 expect(removeFalsyValues(input)).toEqual(expected); 81}); 82