|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2021 Google LLC |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0x00 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0x00 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +import { expect } from 'chai'; |
| 18 | + |
| 19 | +import { |
| 20 | + numberOfLeadingZerosInByte, |
| 21 | + OrderedCodeWriter |
| 22 | +} from '../../../src/index/ordered_code_writer'; |
| 23 | + |
| 24 | +class ValueTestCase<T> { |
| 25 | + constructor( |
| 26 | + readonly val: T, |
| 27 | + readonly ascString: string, |
| 28 | + readonly descString: string |
| 29 | + ) {} |
| 30 | +} |
| 31 | + |
| 32 | +const NUMBER_TEST_CASES: Array<ValueTestCase<number>> = [ |
| 33 | + new ValueTestCase( |
| 34 | + Number.NEGATIVE_INFINITY, |
| 35 | + // Note: This values are taken from the Android reference implementation |
| 36 | + '070fffffffffffff', |
| 37 | + 'f8f0000000000000' |
| 38 | + ), |
| 39 | + new ValueTestCase( |
| 40 | + Number.MIN_SAFE_INTEGER, |
| 41 | + '083cc0000000000000', |
| 42 | + 'f7c33fffffffffffff' |
| 43 | + ), |
| 44 | + new ValueTestCase(-2, '083fffffffffffffff', 'f7c000000000000000'), |
| 45 | + new ValueTestCase(-1, '08400fffffffffffff', 'f7bff0000000000000'), |
| 46 | + new ValueTestCase(-0.1, '084046666666666665', 'f7bfb999999999999a'), |
| 47 | + new ValueTestCase(-0.0, '087fffffffffffffff', 'f78000000000000000'), |
| 48 | + new ValueTestCase(0, '088000000000000000', 'f77fffffffffffffff'), |
| 49 | + new ValueTestCase( |
| 50 | + Number.MIN_VALUE, |
| 51 | + '088000000000000001', |
| 52 | + 'f77ffffffffffffffe' |
| 53 | + ), |
| 54 | + new ValueTestCase(0.1, '08bfb999999999999a', 'f74046666666666665'), |
| 55 | + new ValueTestCase(1, '08bff0000000000000', 'f7400fffffffffffff'), |
| 56 | + new ValueTestCase(2, '08c000000000000000', 'f73fffffffffffffff'), |
| 57 | + new ValueTestCase(4, '08c010000000000000', 'f73fefffffffffffff'), |
| 58 | + new ValueTestCase(8, '08c020000000000000', 'f73fdfffffffffffff'), |
| 59 | + new ValueTestCase(16, '08c030000000000000', 'f73fcfffffffffffff'), |
| 60 | + new ValueTestCase(32, '08c040000000000000', 'f73fbfffffffffffff'), |
| 61 | + new ValueTestCase(64, '08c050000000000000', 'f73fafffffffffffff'), |
| 62 | + new ValueTestCase(128, '08c060000000000000', 'f73f9fffffffffffff'), |
| 63 | + new ValueTestCase(255, '08c06fe00000000000', 'f73f901fffffffffff'), |
| 64 | + new ValueTestCase(256, '08c070000000000000', 'f73f8fffffffffffff'), |
| 65 | + new ValueTestCase(257, '08c070100000000000', 'f73f8fefffffffffff'), |
| 66 | + new ValueTestCase( |
| 67 | + Number.MAX_SAFE_INTEGER, |
| 68 | + '08c33fffffffffffff', |
| 69 | + 'f73cc0000000000000' |
| 70 | + ), |
| 71 | + new ValueTestCase( |
| 72 | + Number.POSITIVE_INFINITY, |
| 73 | + '08fff0000000000000', |
| 74 | + 'f7000fffffffffffff' |
| 75 | + ), |
| 76 | + new ValueTestCase(Number.NaN, '08fff8000000000000', 'f70007ffffffffffff') |
| 77 | +]; |
| 78 | + |
| 79 | +describe('Ordered Code Writer', () => { |
| 80 | + it('computes number of leading zeros', () => { |
| 81 | + for (let i = 0; i < 0xff; ++i) { |
| 82 | + let zeros = 0; |
| 83 | + for (let bit = 7; bit >= 0; --bit) { |
| 84 | + if ((i & (1 << bit)) === 0) { |
| 85 | + ++zeros; |
| 86 | + } else { |
| 87 | + break; |
| 88 | + } |
| 89 | + } |
| 90 | + expect(numberOfLeadingZerosInByte(i)).to.equal(zeros, `for number ${i}`); |
| 91 | + } |
| 92 | + }); |
| 93 | + |
| 94 | + it('converts numbers to bits', () => { |
| 95 | + for (let i = 0; i < NUMBER_TEST_CASES.length; ++i) { |
| 96 | + const bytes = getBytes(NUMBER_TEST_CASES[i].val); |
| 97 | + expect(bytes.asc).to.deep.equal( |
| 98 | + fromHex(NUMBER_TEST_CASES[i].ascString), |
| 99 | + 'Ascending for ' + NUMBER_TEST_CASES[i].val |
| 100 | + ); |
| 101 | + expect(bytes.desc).to.deep.equal( |
| 102 | + fromHex(NUMBER_TEST_CASES[i].descString), |
| 103 | + 'Descending for ' + NUMBER_TEST_CASES[i].val |
| 104 | + ); |
| 105 | + } |
| 106 | + }); |
| 107 | + |
| 108 | + it('orders numbers correctly', () => { |
| 109 | + for (let i = 0; i < NUMBER_TEST_CASES.length; ++i) { |
| 110 | + for (let j = i; j < NUMBER_TEST_CASES.length; ++j) { |
| 111 | + const left = NUMBER_TEST_CASES[i].val; |
| 112 | + const leftBytes = getBytes(left); |
| 113 | + const right = NUMBER_TEST_CASES[j].val; |
| 114 | + const rightBytes = getBytes(right); |
| 115 | + expect(compare(leftBytes.asc, rightBytes.asc)).to.equal( |
| 116 | + i === j ? 0 : -1, |
| 117 | + `Ascending order: ${left} vs ${right}` |
| 118 | + ); |
| 119 | + expect(compare(leftBytes.desc, rightBytes.desc)).to.equal( |
| 120 | + i === j ? 0 : 1, |
| 121 | + `Descending order: ${left} vs ${right}` |
| 122 | + ); |
| 123 | + } |
| 124 | + } |
| 125 | + }); |
| 126 | +}); |
| 127 | + |
| 128 | +function fromHex(hexString: string): Uint8Array { |
| 129 | + const bytes = new Uint8Array(hexString.length / 2); |
| 130 | + for (let c = 0; c < hexString.length; c += 2) { |
| 131 | + bytes[c / 2] = parseInt(hexString.substr(c, 2), 16); |
| 132 | + } |
| 133 | + return bytes; |
| 134 | +} |
| 135 | + |
| 136 | +function compare(left: Uint8Array, right: Uint8Array): number { |
| 137 | + for (let i = 0; i < Math.min(left.length, right.length); ++i) { |
| 138 | + if (left[i] < right[i]) { |
| 139 | + return -1; |
| 140 | + } |
| 141 | + if (left[i] > right[i]) { |
| 142 | + return 1; |
| 143 | + } |
| 144 | + } |
| 145 | + return left.length - right.length; |
| 146 | +} |
| 147 | + |
| 148 | +function getBytes(val: unknown): { asc: Uint8Array; desc: Uint8Array } { |
| 149 | + const ascWriter = new OrderedCodeWriter(); |
| 150 | + const descWriter = new OrderedCodeWriter(); |
| 151 | + if (typeof val === 'number') { |
| 152 | + ascWriter.writeNumberAscending(val); |
| 153 | + descWriter.writeNumberDescending(val); |
| 154 | + } else { |
| 155 | + throw new Error('Encoding not yet supported for ' + val); |
| 156 | + } |
| 157 | + return { asc: ascWriter.encodedBytes(), desc: descWriter.encodedBytes() }; |
| 158 | +} |
0 commit comments