|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2017 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 ascEncoding: Uint8Array, |
| 28 | + readonly descEncoding: Uint8Array |
| 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 | + new Uint8Array([0x07, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]), |
| 37 | + new Uint8Array([0xf8, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]) |
| 38 | + ), |
| 39 | + new ValueTestCase( |
| 40 | + Number.MIN_SAFE_INTEGER, |
| 41 | + new Uint8Array([0x08, 0x3c, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), |
| 42 | + new Uint8Array([0xf7, 0xc3, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]) |
| 43 | + ), |
| 44 | + new ValueTestCase( |
| 45 | + -2, |
| 46 | + new Uint8Array([0x08, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]), |
| 47 | + new Uint8Array([0xf7, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]) |
| 48 | + ), |
| 49 | + new ValueTestCase( |
| 50 | + -1, |
| 51 | + new Uint8Array([0x08, 0x40, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]), |
| 52 | + new Uint8Array([0xf7, 0xbf, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]) |
| 53 | + ), |
| 54 | + new ValueTestCase( |
| 55 | + -0.1, |
| 56 | + new Uint8Array([0x08, 0x40, 0x46, 0x66, 0x66, 0x66, 0x66, 0x66, 0x65]), |
| 57 | + new Uint8Array([0xf7, 0xbf, 0xb9, 0x99, 0x99, 0x99, 0x99, 0x99, 0x9a]) |
| 58 | + ), |
| 59 | + new ValueTestCase( |
| 60 | + 0, |
| 61 | + new Uint8Array([0x08, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), |
| 62 | + new Uint8Array([0xf7, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]) |
| 63 | + ), |
| 64 | + new ValueTestCase( |
| 65 | + Number.MIN_VALUE, |
| 66 | + new Uint8Array([0x08, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01]), |
| 67 | + new Uint8Array([0xf7, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe]) |
| 68 | + ), |
| 69 | + new ValueTestCase( |
| 70 | + 0.1, |
| 71 | + new Uint8Array([0x08, 0xbf, 0xb9, 0x99, 0x99, 0x99, 0x99, 0x99, 0x9a]), |
| 72 | + new Uint8Array([0xf7, 0x40, 0x46, 0x66, 0x66, 0x66, 0x66, 0x66, 0x65]) |
| 73 | + ), |
| 74 | + new ValueTestCase( |
| 75 | + 1, |
| 76 | + new Uint8Array([0x08, 0xbf, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), |
| 77 | + new Uint8Array([0xf7, 0x40, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]) |
| 78 | + ), |
| 79 | + new ValueTestCase( |
| 80 | + 2, |
| 81 | + new Uint8Array([0x08, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), |
| 82 | + new Uint8Array([0xf7, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]) |
| 83 | + ), |
| 84 | + new ValueTestCase( |
| 85 | + 4, |
| 86 | + new Uint8Array([0x08, 0xc0, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), |
| 87 | + new Uint8Array([0xf7, 0x3f, 0xef, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]) |
| 88 | + ), |
| 89 | + new ValueTestCase( |
| 90 | + 0x08, |
| 91 | + new Uint8Array([0x08, 0xc0, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), |
| 92 | + new Uint8Array([0xf7, 0x3f, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]) |
| 93 | + ), |
| 94 | + new ValueTestCase( |
| 95 | + 0x10, |
| 96 | + new Uint8Array([0x08, 0xc0, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), |
| 97 | + new Uint8Array([0xf7, 0x3f, 0xcf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]) |
| 98 | + ), |
| 99 | + new ValueTestCase( |
| 100 | + 0x20, |
| 101 | + new Uint8Array([0x08, 0xc0, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), |
| 102 | + new Uint8Array([0xf7, 0x3f, 0xbf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]) |
| 103 | + ), |
| 104 | + new ValueTestCase( |
| 105 | + 0x40, |
| 106 | + new Uint8Array([0x08, 0xc0, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), |
| 107 | + new Uint8Array([0xf7, 0x3f, 0xaf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]) |
| 108 | + ), |
| 109 | + new ValueTestCase( |
| 110 | + 0x80, |
| 111 | + new Uint8Array([0x08, 0xc0, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), |
| 112 | + new Uint8Array([0xf7, 0x3f, 0x9f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]) |
| 113 | + ), |
| 114 | + new ValueTestCase( |
| 115 | + 0xff, |
| 116 | + new Uint8Array([0x08, 0xc0, 0x6f, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00]), |
| 117 | + new Uint8Array([0xf7, 0x3f, 0x90, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff]) |
| 118 | + ), |
| 119 | + new ValueTestCase( |
| 120 | + 256, |
| 121 | + new Uint8Array([0x08, 0xc0, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), |
| 122 | + new Uint8Array([0xf7, 0x3f, 0x8f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]) |
| 123 | + ), |
| 124 | + new ValueTestCase( |
| 125 | + 257, |
| 126 | + new Uint8Array([0x08, 0xc0, 0x70, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00]), |
| 127 | + new Uint8Array([0xf7, 0x3f, 0x8f, 0xef, 0xff, 0xff, 0xff, 0xff, 0xff]) |
| 128 | + ), |
| 129 | + new ValueTestCase( |
| 130 | + Number.MAX_SAFE_INTEGER, |
| 131 | + new Uint8Array([0x08, 0xc3, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]), |
| 132 | + new Uint8Array([0xf7, 0x3c, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]) |
| 133 | + ), |
| 134 | + new ValueTestCase( |
| 135 | + Number.POSITIVE_INFINITY, |
| 136 | + new Uint8Array([0x08, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), |
| 137 | + new Uint8Array([0xf7, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]) |
| 138 | + ), |
| 139 | + new ValueTestCase( |
| 140 | + Number.NaN, |
| 141 | + new Uint8Array([0x08, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), |
| 142 | + new Uint8Array([0xf7, 0x00, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]) |
| 143 | + ) |
| 144 | +]; |
| 145 | + |
| 146 | +describe('Ordered Code Writer', () => { |
| 147 | + it('computes number of leading zeros', () => { |
| 148 | + for (let i = 0; i < 0xff; ++i) { |
| 149 | + let zeros = 0; |
| 150 | + for (let bit = 7; bit >= 0; --bit) { |
| 151 | + if ((i & (1 << bit)) === 0) { |
| 152 | + ++zeros; |
| 153 | + } else { |
| 154 | + break; |
| 155 | + } |
| 156 | + } |
| 157 | + expect(numberOfLeadingZerosInByte(i)).to.equal(zeros, `for number ${i}`); |
| 158 | + } |
| 159 | + }); |
| 160 | + |
| 161 | + it('converts numbers to bits', () => { |
| 162 | + for (let i = 0; i < NUMBER_TEST_CASES.length; ++i) { |
| 163 | + const bytes = getBytes(NUMBER_TEST_CASES[i].val); |
| 164 | + expect(bytes.asc).to.deep.equal( |
| 165 | + NUMBER_TEST_CASES[i].ascEncoding, |
| 166 | + 'Ascending for ' + NUMBER_TEST_CASES[i].val |
| 167 | + ); |
| 168 | + expect(bytes.desc).to.deep.equal( |
| 169 | + NUMBER_TEST_CASES[i].descEncoding, |
| 170 | + 'Descending for ' + NUMBER_TEST_CASES[i].val |
| 171 | + ); |
| 172 | + } |
| 173 | + }); |
| 174 | + |
| 175 | + it('orders numbers correctly', () => { |
| 176 | + for (let i = 0; i < NUMBER_TEST_CASES.length; ++i) { |
| 177 | + for (let j = i; j < NUMBER_TEST_CASES.length; ++j) { |
| 178 | + const left = NUMBER_TEST_CASES[i].val; |
| 179 | + const leftBytes = getBytes(left); |
| 180 | + const right = NUMBER_TEST_CASES[j].val; |
| 181 | + const rightBytes = getBytes(right); |
| 182 | + expect(compare(leftBytes.asc, rightBytes.asc)).to.equal( |
| 183 | + i === j ? 0 : -1, |
| 184 | + `Ascending order: ${left} vs ${right}` |
| 185 | + ); |
| 186 | + expect(compare(leftBytes.desc, rightBytes.desc)).to.equal( |
| 187 | + i === j ? 0 : 1, |
| 188 | + `Descending order: ${left} vs ${right}` |
| 189 | + ); |
| 190 | + } |
| 191 | + } |
| 192 | + }); |
| 193 | +}); |
| 194 | + |
| 195 | +function compare(left: Uint8Array, right: Uint8Array): number { |
| 196 | + for (let i = 0; i < Math.min(left.length, right.length); ++i) { |
| 197 | + if (left[i] < right[i]) { |
| 198 | + return -1; |
| 199 | + } |
| 200 | + if (left[i] > right[i]) { |
| 201 | + return 1; |
| 202 | + } |
| 203 | + } |
| 204 | + return left.length - right.length; |
| 205 | +} |
| 206 | + |
| 207 | +function getBytes(val: unknown): { asc: Uint8Array; desc: Uint8Array } { |
| 208 | + const ascWriter = new OrderedCodeWriter(); |
| 209 | + const descWriter = new OrderedCodeWriter(); |
| 210 | + if (typeof val === 'number') { |
| 211 | + ascWriter.writeNumberAscending(val); |
| 212 | + descWriter.writeNumberDescending(val); |
| 213 | + } else { |
| 214 | + throw new Error('Encoding not yet supported for ' + val); |
| 215 | + } |
| 216 | + return { asc: ascWriter.encodedBytes(), desc: descWriter.encodedBytes() }; |
| 217 | +} |
0 commit comments