|
| 1 | +// Copyright 2022 Google Inc. |
| 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 | +// <https://apache.org/licenses/LICENSE-2.0>. |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an “AS IS” BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +import JSBI from '../jsbi'; |
| 15 | + |
| 16 | +const ab = new ArrayBuffer(16); |
| 17 | +const dv = new DataView(ab, 0, 8); |
| 18 | +const dv2 = new DataView(ab, 8, 8); |
| 19 | + |
| 20 | +function hexParse(s) { |
| 21 | + if (s.charCodeAt(0) === 0x2D) { |
| 22 | + const result = JSBI.BigInt(s.slice(1)); |
| 23 | + result.sign = true; |
| 24 | + return result; |
| 25 | + } |
| 26 | + return JSBI.BigInt(s); |
| 27 | +} |
| 28 | +function hexParse2(s) { |
| 29 | + if (s.charCodeAt(0) === 0x2D) return -BigInt(s.slice(1)); |
| 30 | + return BigInt(s); |
| 31 | +} |
| 32 | + |
| 33 | +function dataViewEqual(a, b) { |
| 34 | + for (let i = 0; i < a.length; i++) { |
| 35 | + if (a.getUint8(i) !== b.getUint8(i)) return false; |
| 36 | + } |
| 37 | + return true; |
| 38 | +} |
| 39 | + |
| 40 | +function dump(dataview) { |
| 41 | + const chunks = []; |
| 42 | + for (let i = 0; i < 8; i++) { |
| 43 | + chunks.push('0x' + dataview.getUint8(i).toString(16)); |
| 44 | + } |
| 45 | + return chunks.join(', '); |
| 46 | +} |
| 47 | + |
| 48 | +function error(what, value, littleEndian, expected, actual) { |
| 49 | + throw new Error(`Incorrect ${what} for ${value}, littleEndian=${ |
| 50 | + littleEndian}: expected ${expected} but got ${actual}`); |
| 51 | +} |
| 52 | + |
| 53 | +function test(value) { |
| 54 | + const jsbi = hexParse(value); |
| 55 | + const bigint = hexParse2(value); |
| 56 | + for (const littleEndian of [true, false]) { |
| 57 | + // Signed |
| 58 | + JSBI.DataViewSetBigInt64(dv, 0, jsbi, littleEndian); |
| 59 | + dv2.setBigInt64(0, bigint, littleEndian); |
| 60 | + if (!dataViewEqual(dv, dv2)) { |
| 61 | + error('signed DV contents', value, littleEndian, dump(dv2), dump(dv)); |
| 62 | + } |
| 63 | + let out = JSBI.DataViewGetBigInt64(dv, 0, littleEndian); |
| 64 | + let out2 = JSBI.BigInt(dv2.getBigInt64(0, littleEndian).toString()); |
| 65 | + if (!JSBI.equal(out, out2)) { |
| 66 | + error( |
| 67 | + 'signed readout value', value, littleEndian, jsbi.toString(16), |
| 68 | + out.toString(16)); |
| 69 | + } |
| 70 | + // Unsigned |
| 71 | + JSBI.DataViewSetBigUint64(dv, 0, jsbi, littleEndian); |
| 72 | + dv2.setBigUint64(0, bigint, littleEndian); |
| 73 | + if (!dataViewEqual(dv, dv2)) { |
| 74 | + error('unsigned DV contents', value, littleEndian, dump(dv2), dump(dv)); |
| 75 | + } |
| 76 | + out = JSBI.DataViewGetBigUint64(dv, 0, littleEndian); |
| 77 | + out2 = JSBI.BigInt(dv2.getBigUint64(0, littleEndian).toString()); |
| 78 | + if (!JSBI.equal(out, out2)) { |
| 79 | + error( |
| 80 | + 'unsigned readout value', value, littleEndian, jsbi.toString(16), |
| 81 | + out.toString(16)); |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +const tests = [ |
| 87 | + '0', |
| 88 | + '1', |
| 89 | + '-1', |
| 90 | + '2', |
| 91 | + '-2', |
| 92 | + '0xFFFFFFFF', |
| 93 | + '-0xFFFFFFFF', |
| 94 | + '0xFFFFFFFFFFFFFFFF', |
| 95 | + '0xFFFFFFFF00000000', |
| 96 | + '-0x8000000000000000', |
| 97 | + // Verify byte order. |
| 98 | + '0x1234567890abcdef', |
| 99 | + // ...00110011... bit patterns to flush out 30-bit digits issues. |
| 100 | + '0x3333333333333333', |
| 101 | + '0xcccccccccccccccc', |
| 102 | +]; |
| 103 | + |
| 104 | +for (const t of tests) test(t); |
0 commit comments