|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +import * as Arrow from 'apache-arrow'; |
| 19 | +const { BN } = Arrow.util; |
| 20 | + |
| 21 | +describe(`BN`, () => { |
| 22 | + test(`to detect signed numbers, unsigned numbers and decimals`, () => { |
| 23 | + // SignedBigNum |
| 24 | + const i = new BN(new Int32Array([5, 0])); |
| 25 | + expect(i.signed).toBe(true); |
| 26 | + |
| 27 | + // UnsignedBigNum |
| 28 | + const u = new BN(new Uint32Array([5, 0])); |
| 29 | + expect(u.signed).toBe(false); |
| 30 | + |
| 31 | + // DecimalBigNum |
| 32 | + const d = new BN(new Uint16Array([1, 2, 3, 4, 5, 6, 7, 8])); |
| 33 | + expect(d.signed).toBe(true); |
| 34 | + }); |
| 35 | + |
| 36 | + test(`toString for signed numbers`, () => { |
| 37 | + const i1 = new BN(new Int32Array([5, 33]), true); |
| 38 | + expect(i1.toString()).toBe('141733920773'); |
| 39 | + |
| 40 | + const i2 = new BN(new Int32Array([0xFFFFFFFF, 0xFFFFFFFF]), true); |
| 41 | + expect(i2.toString()).toBe('-1'); |
| 42 | + |
| 43 | + const i3 = new BN(new Int32Array([0x11111111, 0x11111111, 0x11111111]), true); |
| 44 | + expect(i3.toString()).toBe('5281877500950955839569596689'); |
| 45 | + |
| 46 | + const i4 = new BN(new Int16Array([0xFFFF, 0xFFFF]), true); |
| 47 | + expect(i4.toString()).toBe('-1'); |
| 48 | + |
| 49 | + const i5 = new BN(new Int32Array([0xFFFFFFFE, 0xFFFFFFFF, 0xFFFFFFFF]), true); |
| 50 | + expect(i5.toString()).toBe('-2'); |
| 51 | + }); |
| 52 | + |
| 53 | + test(`toString for unsigned numbers`, () => { |
| 54 | + const u1 = new BN(new Uint32Array([5, 33]), false); |
| 55 | + expect(u1.toString()).toBe('141733920773'); |
| 56 | + |
| 57 | + const u2 = new BN(new Uint32Array([0xFFFFFFFF, 0xFFFFFFFF]), false); |
| 58 | + expect(u2.toString()).toBe('18446744073709551615'); |
| 59 | + |
| 60 | + const u3 = new BN(new Uint32Array([0x11111111, 0x11111111, 0x11111111]), false); |
| 61 | + expect(u3.toString()).toBe('5281877500950955839569596689'); |
| 62 | + |
| 63 | + const u4 = new BN(new Uint16Array([0xFFFF, 0xFFFF]), false); |
| 64 | + expect(u4.toString()).toBe('4294967295'); |
| 65 | + }); |
| 66 | + |
| 67 | + test(`toString for decimals`, () => { |
| 68 | + const toDecimal = (val: Uint32Array) => { |
| 69 | + const builder = Arrow.makeBuilder({ |
| 70 | + type: new Arrow.Decimal(128, 0), |
| 71 | + nullValues: [] |
| 72 | + }); |
| 73 | + builder.append(val); |
| 74 | + return <Arrow.Type.Decimal><any>builder.finish().toVector().get(0); |
| 75 | + }; |
| 76 | + |
| 77 | + const d2 = toDecimal(new Uint32Array([0xFFFFFFFE, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF])); |
| 78 | + expect(d2.toString()).toBe('-2'); |
| 79 | + |
| 80 | + const d3 = toDecimal(new Uint32Array([0x00000000, 0x00000000, 0x00000000, 0x80000000])); |
| 81 | + expect(d3.toString()).toBe('-170141183460469231731687303715884105728'); |
| 82 | + |
| 83 | + const d4 = toDecimal(new Uint32Array([0x9D91E773, 0x4BB90CED, 0xAB2354CC, 0x54278E9B])); |
| 84 | + expect(d4.toString()).toBe('111860543658909349380118287427608635251'); |
| 85 | + }); |
| 86 | +}); |
0 commit comments