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 { 16 BigintMath as BIM, 17} from './bigint_math'; 18 19describe('BigIntMath.bitCeil', () => { 20 it('rounds powers of 2 to themselves', () => { 21 expect(BIM.bitCeil(1n)).toBe(1n); 22 expect(BIM.bitCeil(2n)).toBe(2n); 23 expect(BIM.bitCeil(4n)).toBe(4n); 24 expect(BIM.bitCeil(4294967296n)).toBe(4294967296n); 25 expect(BIM.bitCeil(2305843009213693952n)).toBe(2305843009213693952n); 26 }); 27 28 it('rounds non powers of 2 up to nearest power of 2', () => { 29 expect(BIM.bitCeil(3n)).toBe(4n); 30 expect(BIM.bitCeil(11n)).toBe(16n); 31 expect(BIM.bitCeil(33n)).toBe(64n); 32 expect(BIM.bitCeil(63n)).toBe(64n); 33 expect(BIM.bitCeil(1234567890123456789n)).toBe(2305843009213693952n); 34 }); 35 36 it('rounds 0 or negative values up to 1', () => { 37 expect(BIM.bitCeil(0n)).toBe(1n); 38 expect(BIM.bitCeil(-123n)).toBe(1n); 39 }); 40}); 41 42describe('BigIntMath.bigFloor', () => { 43 it('rounds powers of 2 to themselves', () => { 44 expect(BIM.bitFloor(1n)).toBe(1n); 45 expect(BIM.bitFloor(2n)).toBe(2n); 46 expect(BIM.bitFloor(4n)).toBe(4n); 47 expect(BIM.bitFloor(4294967296n)).toBe(4294967296n); 48 expect(BIM.bitFloor(2305843009213693952n)).toBe(2305843009213693952n); 49 }); 50 51 it('rounds non powers of 2 down to nearest power of 2', () => { 52 expect(BIM.bitFloor(3n)).toBe(2n); 53 expect(BIM.bitFloor(11n)).toBe(8n); 54 expect(BIM.bitFloor(33n)).toBe(32n); 55 expect(BIM.bitFloor(63n)).toBe(32n); 56 expect(BIM.bitFloor(1234567890123456789n)).toBe(1152921504606846976n); 57 }); 58 59 it('rounds 0 or negative values up to 1', () => { 60 expect(BIM.bitFloor(0n)).toBe(1n); 61 expect(BIM.bitFloor(-123n)).toBe(1n); 62 }); 63}); 64 65describe('quantize', () => { 66 it('should quantize a number to the nearest multiple of a stepsize', () => { 67 expect(BIM.quantizeFloor(10n, 2n)).toEqual(10n); 68 expect(BIM.quantizeFloor(11n, 2n)).toEqual(10n); 69 expect(BIM.quantizeFloor(12n, 2n)).toEqual(12n); 70 expect(BIM.quantizeFloor(13n, 2n)).toEqual(12n); 71 72 expect(BIM.quantizeFloor(9n, 4n)).toEqual(8n); 73 expect(BIM.quantizeFloor(10n, 4n)).toEqual(8n); 74 expect(BIM.quantizeFloor(11n, 4n)).toEqual(8n); 75 expect(BIM.quantizeFloor(12n, 4n)).toEqual(12n); 76 expect(BIM.quantizeFloor(13n, 4n)).toEqual(12n); 77 }); 78 79 it('should return value if stepsize is smaller than 1', () => { 80 expect(BIM.quantizeFloor(123n, 0n)).toEqual(123n); 81 expect(BIM.quantizeFloor(123n, -10n)).toEqual(123n); 82 }); 83}); 84 85describe('quantizeRound', () => { 86 it('should quantize a number to the nearest multiple of a stepsize', () => { 87 expect(BIM.quantize(0n, 2n)).toEqual(0n); 88 expect(BIM.quantize(1n, 2n)).toEqual(2n); 89 expect(BIM.quantize(2n, 2n)).toEqual(2n); 90 expect(BIM.quantize(3n, 2n)).toEqual(4n); 91 expect(BIM.quantize(4n, 2n)).toEqual(4n); 92 93 expect(BIM.quantize(0n, 3n)).toEqual(0n); 94 expect(BIM.quantize(1n, 3n)).toEqual(0n); 95 expect(BIM.quantize(2n, 3n)).toEqual(3n); 96 expect(BIM.quantize(3n, 3n)).toEqual(3n); 97 expect(BIM.quantize(4n, 3n)).toEqual(3n); 98 expect(BIM.quantize(5n, 3n)).toEqual(6n); 99 expect(BIM.quantize(6n, 3n)).toEqual(6n); 100 }); 101 102 it('should return value if stepsize is smaller than 1', () => { 103 expect(BIM.quantize(123n, 0n)).toEqual(123n); 104 expect(BIM.quantize(123n, -10n)).toEqual(123n); 105 }); 106}); 107 108describe('max', () => { 109 it('should return the greater of two numbers', () => { 110 expect(BIM.max(5n, 8n)).toEqual(8n); 111 expect(BIM.max(3n, 7n)).toEqual(7n); 112 expect(BIM.max(6n, 6n)).toEqual(6n); 113 expect(BIM.max(-7n, -12n)).toEqual(-7n); 114 }); 115}); 116 117describe('min', () => { 118 it('should return the smaller of two numbers', () => { 119 expect(BIM.min(5n, 8n)).toEqual(5n); 120 expect(BIM.min(3n, 7n)).toEqual(3n); 121 expect(BIM.min(6n, 6n)).toEqual(6n); 122 expect(BIM.min(-7n, -12n)).toEqual(-12n); 123 }); 124}); 125 126describe('popcount', () => { 127 it('should return the number of set bits in an integer', () => { 128 expect(BIM.popcount(0n)).toBe(0); 129 expect(BIM.popcount(1n)).toBe(1); 130 expect(BIM.popcount(2n)).toBe(1); 131 expect(BIM.popcount(3n)).toBe(2); 132 expect(BIM.popcount(4n)).toBe(1); 133 expect(BIM.popcount(5n)).toBe(2); 134 expect(BIM.popcount(3462151285050974216n)).toBe(10); 135 }); 136 137 it('should throw when presented with a negative integer', () => { 138 expect(() => BIM.popcount(-1n)) 139 .toThrowError('Can\'t get popcount of negative number -1'); 140 }); 141}); 142