]> git.proxmox.com Git - ceph.git/blob - ceph/src/arrow/js/test/unit/int-tests.ts
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / js / test / unit / int-tests.ts
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 { Int64, Uint64, Int128 } = Arrow.util;
20
21 describe(`Uint64`, () => {
22 test(`gets expected high/low bytes`, () => {
23 let i = new Uint64(new Uint32Array([5, 0]));
24 expect(i.high()).toEqual(0);
25 expect(i.low()).toEqual(5);
26 });
27 test(`adds 32-bit numbers`, () => {
28 let a = new Uint64(new Uint32Array([5, 0]));
29 let b = new Uint64(new Uint32Array([9, 0]));
30 let expected = new Uint64(new Uint32Array([14, 0]));
31 expect(a.plus(b)).toEqual(expected);
32 });
33 test(`addition overflows 32-bit numbers`, () => {
34 let a = new Uint64(new Uint32Array([0xffffffff, 0]));
35 let b = new Uint64(new Uint32Array([9, 0]));
36 let expected = new Uint64(new Uint32Array([8, 1]));
37 expect(a.plus(b)).toEqual(expected);
38 });
39 test(`multiplies 32-bit numbers`, () => {
40 let a = new Uint64(new Uint32Array([5, 0]));
41 let b = new Uint64(new Uint32Array([9, 0]));
42 let expected = new Uint64(new Uint32Array([45, 0]));
43 expect(a.times(b)).toEqual(expected);
44 });
45 test(`multiplication overflows 32-bit numbers`, () => {
46 let a = new Uint64(new Uint32Array([0x80000000, 0]));
47 let b = new Uint64(new Uint32Array([3, 0]));
48 let expected = new Uint64(new Uint32Array([0x80000000, 1]));
49 expect(a.times(b)).toEqual(expected);
50 });
51 test(`multiplication is associative`, () => {
52 let a = new Uint64(new Uint32Array([0x80000000, 0]));
53 let b = new Uint64(new Uint32Array([3, 0]));
54 expect(Uint64.multiply(a, b)).toEqual(Uint64.multiply(b,a));
55 });
56 test(`lessThan works on 32-bit numbers`, () => {
57 let a = new Uint64(new Uint32Array([0x0000abcd, 0]));
58 let b = new Uint64(new Uint32Array([0x0000abcf, 0]));
59 expect(a.lessThan(b)).toBeTruthy();
60 });
61 test(`lessThan works on 64-bit numbers`, () => {
62 let a = new Uint64(new Uint32Array([123, 32]));
63 let b = new Uint64(new Uint32Array([568, 32]));
64 expect(a.lessThan(b)).toBeTruthy();
65 });
66 test(`fromString parses string`, () => {
67 expect(Uint64.fromString('6789123456789')).toEqual(new Int64(new Uint32Array([0xb74abf15, 0x62c])));
68 });
69 test(`fromString parses big (full unsigned 64-bit) string`, () => {
70 expect(Uint64.fromString('18364758544493064720')).toEqual(new Uint64(new Uint32Array([0x76543210, 0xfedcba98])));
71 });
72 test(`fromNumber converts 53-ish bit number`, () => {
73 expect(Uint64.fromNumber(8086463330923024)).toEqual(new Uint64(new Uint32Array([0x76543210, 0x001cba98])));
74 });
75 });
76
77 describe(`Int64`, () => {
78 test(`gets expected high/low bytes`, () => {
79 let i = new Int64(new Uint32Array([5, 0]));
80 expect(i.high()).toEqual(0);
81 expect(i.low()).toEqual(5);
82 });
83 test(`adds 32-bit numbers`, () => {
84 let a = new Int64(new Uint32Array([5, 0]));
85 let b = new Int64(new Uint32Array([9, 0]));
86 let expected = new Int64(new Uint32Array([14, 0]));
87 expect(a.plus(b)).toEqual(expected);
88 });
89 test(`adds negative 32-bit numbers`, () => {
90 let a = new Int64(new Uint32Array([56789 , 0]));
91 let b = new Int64(new Uint32Array([-66789, -1]));
92 let expected = new Int64(new Uint32Array([-10000, -1]));
93 expect(a.plus(b)).toEqual(expected);
94 });
95 test(`addition overflows 32-bit numbers`, () => {
96 let a = new Int64(new Uint32Array([0xffffffff, 0]));
97 let b = new Int64(new Uint32Array([9, 0]));
98 let expected = new Int64(new Uint32Array([8, 1]));
99 expect(a.plus(b)).toEqual(expected);
100 });
101 test(`multiplies 32-bit numbers`, () => {
102 let a = new Int64(new Uint32Array([5, 0]));
103 let b = new Int64(new Uint32Array([9, 0]));
104 let expected = new Int64(new Uint32Array([45, 0]));
105 expect(a.times(b)).toEqual(expected);
106 });
107 test(`multiplication overflows 32-bit numbers`, () => {
108 let a = new Int64(new Uint32Array([0x80000000, 0]));
109 let b = new Int64(new Uint32Array([3, 0]));
110 let expected = new Int64(new Uint32Array([0x80000000, 1]));
111 expect(a.times(b)).toEqual(expected);
112 });
113 test(`multiplication works on negative numbers`, () => {
114 let a = new Int64(new Uint32Array([-5, -1]));
115 let b = new Int64(new Uint32Array([-100, -1]));
116 expect(a.times(b)).toEqual(new Int64(new Uint32Array([ 500, 0])));
117 expect(a.times(b)).toEqual(new Int64(new Uint32Array([ -50000, -1])));
118 expect(a.times(b)).toEqual(new Int64(new Uint32Array([5000000, 0])));
119 });
120 test(`multiplication is associative`, () => {
121 let a = new Int64(new Uint32Array([0x80000000, 0]));
122 let b = new Int64(new Uint32Array([3, 0]));
123 expect(Int64.multiply(a, b)).toEqual(Int64.multiply(b,a));
124 });
125 test(`lessThan works on 32-bit numbers`, () => {
126 let a = new Int64(new Uint32Array([0x0000abcd, 0]));
127 let b = new Int64(new Uint32Array([0x0000abcf, 0]));
128 expect(a.lessThan(b)).toBeTruthy();
129 });
130 test(`lessThan works on 64-bit numbers`, () => {
131 let a = new Int64(new Uint32Array([123, 32]));
132 let b = new Int64(new Uint32Array([568, 32]));
133 expect(a.lessThan(b)).toBeTruthy();
134 });
135 test(`lessThan works on negative numbers`, () => {
136 let a = new Int64(new Uint32Array([0, -158]));
137 let b = new Int64(new Uint32Array([-3, -1]));
138 expect(a.lessThan(b)).toBeTruthy();
139 });
140 test(`lessThan works on mixed numbers`, () => {
141 let a = new Int64(new Uint32Array([-3, -1]));
142 let b = new Int64(new Uint32Array([ 0, 3]));
143 expect(a.lessThan(b)).toBeTruthy();
144 });
145 test(`negate works on 32-bit number`, () => {
146 expect (new Int64(new Uint32Array([123456, 0])).negate()).toEqual(new Int64(new Uint32Array([-123456, -1])));
147 });
148 test(`double negation is noop`, () => {
149 let test = new Int64(new Uint32Array([6789, 12345]));
150 let expected = new Int64(new Uint32Array([6789, 12345]));
151 expect(test.negate().negate()).toEqual(expected);
152 });
153 test(`negate works on 64-bit number`, () => {
154 expect (new Int64(new Uint32Array([0xb74abf15, 0x62c])).negate()).toEqual(new Int64(new Uint32Array([0x48b540eb, 0xfffff9d3])));
155 });
156 test(`fromString parses string`, () => {
157 expect(Int64.fromString('6789123456789')).toEqual(new Int64(new Uint32Array([0xb74abf15, 0x62c])));
158 });
159 test(`fromString parses negative string`, () => {
160 expect(Int64.fromString('-6789123456789')).toEqual(new Int64(new Uint32Array([0x48b540eb, 0xfffff9d3])));
161 });
162 test(`fromNumber converts 53-ish bit number`, () => {
163 expect(Int64.fromNumber(8086463330923024)).toEqual(new Int64(new Uint32Array([0x76543210, 0x001cba98])));
164 expect(Int64.fromNumber(-8086463330923024)).toEqual(new Int64(new Uint32Array([0x89abcdf0, 0xffe34567])));
165 });
166 });
167
168 describe(`Int128`, () => {
169 test(`gets expected bytes`, () => {
170 let i = new Int128(new Uint32Array([4, 3, 2, 1]));
171 expect(i.high().high()).toEqual(1);
172 expect(i.high().low() ).toEqual(2);
173 expect(i.low().high() ).toEqual(3);
174 expect(i.low().low() ).toEqual(4);
175 });
176 test(`adds 32-bit numbers`, () => {
177 let a = new Int128(new Uint32Array([5, 0, 0, 0]));
178 let b = new Int128(new Uint32Array([9, 0, 0, 0]));
179 let expected = new Int128(new Uint32Array([14, 0, 0, 0]));
180 expect(a.plus(b)).toEqual(expected);
181 });
182 test(`adds negative 32-bit numbers`, () => {
183 let a = new Int128(new Uint32Array([56789 , 0, 0, 0]));
184 let b = new Int128(new Uint32Array([-66789, -1, -1, -1]));
185 let expected = new Int128(new Uint32Array([-10000, -1, -1, -1]));
186 expect(a.plus(b)).toEqual(expected);
187 });
188 test(`addition overflows 32-bit numbers`, () => {
189 let a = new Int128(new Uint32Array([0xffffffff, 0, 0, 0]));
190 let b = new Int128(new Uint32Array([9, 0, 0, 0]));
191 let expected = new Int128(new Uint32Array([8, 1, 0, 0]));
192 expect(a.plus(b)).toEqual(expected);
193 });
194 test(`multiplies 32-bit numbers`, () => {
195 let a = new Int128(new Uint32Array([5, 0, 0, 0]));
196 let b = new Int128(new Uint32Array([9, 0, 0, 0]));
197 let expected = new Int128(new Uint32Array([45, 0, 0, 0]));
198 expect(a.times(b)).toEqual(expected);
199 });
200 test(`multiplication overflows 32-bit numbers`, () => {
201 let a = new Int128(new Uint32Array([0x80000000, 0, 0, 0]));
202 let b = new Int128(new Uint32Array([3, 0, 0, 0]));
203 let expected = new Int128(new Uint32Array([0x80000000, 1, 0, 0]));
204 expect(a.times(b)).toEqual(expected);
205 });
206 test(`multiplication works on negative numbers`, () => {
207 let a = new Int128(new Uint32Array([-5, -1, -1, -1]));
208 let b = new Int128(new Uint32Array([-100, -1, -1, -1]));
209 expect(a.times(b)).toEqual(new Int128(new Uint32Array([ 500, 0, 0, 0])));
210 expect(a.times(b)).toEqual(new Int128(new Uint32Array([ -50000, -1, -1, -1])));
211 expect(a.times(b)).toEqual(new Int128(new Uint32Array([5000000, 0, 0, 0])));
212 });
213 test(`multiplication is associative`, () => {
214 let a = new Int128(new Uint32Array([4, 3, 2, 1]));
215 let b = new Int128(new Uint32Array([3, 0, 0, 0]));
216 expect(Int128.multiply(a, b)).toEqual(Int128.multiply(b,a));
217 });
218 test(`multiplication can produce 128-bit number`, () => {
219 let a = new Int128(new Uint32Array([0, 0xf0000000, 0, 0]));
220 let b = new Int128(new Uint32Array([0, 0x10000000, 0, 0]));
221 expect(a.times(b)).toEqual(new Int128(new Uint32Array([0x00000000, 0x00000000, 0x00000000, 0xf000000])));
222 });
223 test(`fromString parses string`, () => {
224 expect(Int128.fromString('1002111867823618826746863804903129070'))
225 .toEqual(new Int64(new Uint32Array([0x00c0ffee,
226 0x00c0ffee,
227 0x00c0ffee,
228 0x00c0ffee])));
229 });
230 test(`fromString parses negative string`, () => {
231 expect(Int128.fromString('-12345678901234567890123456789012345678'))
232 .toEqual(new Int64(new Uint32Array([0x21c70cb2,
233 0x3bb66faf,
234 0x0ffdccec,
235 0xf6b64f09])));
236 });
237 test(`fromNumber converts 53-ish bit number`, () => {
238 expect(Int128.fromNumber(8086463330923024)).toEqual(new Int128(new Uint32Array([0x76543210, 0x001cba98, 0, 0])));
239 expect(Int128.fromNumber(-8086463330923024)).toEqual(new Int128(new Uint32Array([0x89abcdf0, 0xffe34567, 0xffffffff, 0xffffffff])));
240 });
241 });