]> git.proxmox.com Git - ceph.git/blame - ceph/src/arrow/js/test/unit/builders/primitive-tests.ts
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / js / test / unit / builders / primitive-tests.ts
CommitLineData
1d09f67e
TL
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
18import {
19 Vector, DataType,
20 Bool, Int8, Int16, Int32, Uint8, Uint16, Uint32, Float16, Float32, Float64
21} from 'apache-arrow';
22
23import {
24 validateVector,
25 encodeAll, encodeEach, encodeEachDOM, encodeEachNode,
26 boolsNoNulls, boolsWithNulls,
27 int8sNoNulls, int8sWithNulls, int8sWithMaxInts,
28 int16sNoNulls, int16sWithNulls, int16sWithMaxInts,
29 int32sNoNulls, int32sWithNulls, int32sWithMaxInts,
30 uint8sNoNulls, uint8sWithNulls, uint8sWithMaxInts,
31 uint16sNoNulls, uint16sWithNulls, uint16sWithMaxInts,
32 uint32sNoNulls, uint32sWithNulls, uint32sWithMaxInts,
33 float16sNoNulls, float16sWithNulls, float16sWithNaNs,
34 float32sNoNulls, float32sWithNulls, float64sWithNaNs,
35 float64sNoNulls, float64sWithNulls, float32sWithNaNs,
36} from './utils';
37
38const testDOMStreams = process.env.TEST_DOM_STREAMS === 'true';
39const testNodeStreams = process.env.TEST_NODE_STREAMS === 'true';
40
41describe('BoolBuilder', () => {
42
43 runTestsWithEncoder('encodeAll: 5', encodeAll(() => new Bool()));
44 runTestsWithEncoder('encodeEach: 5', encodeEach(() => new Bool(), 5));
45 runTestsWithEncoder('encodeEach: 25', encodeEach(() => new Bool(), 25));
46 runTestsWithEncoder('encodeEach: undefined', encodeEach(() => new Bool()));
47 testDOMStreams && runTestsWithEncoder('encodeEachDOM: 25', encodeEachDOM(() => new Bool(), 25));
48 testNodeStreams && runTestsWithEncoder('encodeEachNode: 25', encodeEachNode(() => new Bool(), 25));
49
50 function runTestsWithEncoder<T extends DataType>(name: string, encode: (vals: (T['TValue'] | null)[], nullVals?: any[]) => Promise<Vector<T>>) {
51 describe(`${encode.name} ${name}`, () => {
52 it(`encodes bools no nulls`, async () => {
53 const vals = boolsNoNulls(20);
54 validateVector(vals, await encode(vals, []), []);
55 });
56 it(`encodes bools with nulls`, async () => {
57 const vals = boolsWithNulls(20);
58 validateVector(vals, await encode(vals, [null]), [null]);
59 });
60 });
61 }
62});
63
64type PrimitiveTypeOpts<T extends DataType> = [
65 new (...args: any[]) => T,
66 (count: number) => (T['TValue'] | null)[],
67 (count: number) => (T['TValue'] | null)[],
68 (count: number) => (T['TValue'] | null)[]
69];
70
71[
72 [Int8, int8sNoNulls, int8sWithNulls, int8sWithMaxInts] as PrimitiveTypeOpts<Int8>,
73 [Int16, int16sNoNulls, int16sWithNulls, int16sWithMaxInts] as PrimitiveTypeOpts<Int16>,
74 [Int32, int32sNoNulls, int32sWithNulls, int32sWithMaxInts] as PrimitiveTypeOpts<Int32>,
75 [Uint8, uint8sNoNulls, uint8sWithNulls, uint8sWithMaxInts] as PrimitiveTypeOpts<Uint8>,
76 [Uint16, uint16sNoNulls, uint16sWithNulls, uint16sWithMaxInts] as PrimitiveTypeOpts<Uint16>,
77 [Uint32, uint32sNoNulls, uint32sWithNulls, uint32sWithMaxInts] as PrimitiveTypeOpts<Uint32>,
78].forEach(([TypeCtor, noNulls, withNulls, withNaNs]) => {
79
80 describe(`${TypeCtor.name}Builder`, () => {
81
82 const typeFactory = () => new TypeCtor();
83 const valueName = TypeCtor.name.toLowerCase();
84
85 runTestsWithEncoder('encodeAll', encodeAll(typeFactory));
86 runTestsWithEncoder('encodeEach: 5', encodeEach(typeFactory, 5));
87 runTestsWithEncoder('encodeEach: 25', encodeEach(typeFactory, 25));
88 runTestsWithEncoder('encodeEach: undefined', encodeEach(typeFactory));
89 testDOMStreams && runTestsWithEncoder('encodeEachDOM: 25', encodeEachDOM(typeFactory, 25));
90 testNodeStreams && runTestsWithEncoder('encodeEachNode: 25', encodeEachNode(typeFactory, 25));
91
92 function runTestsWithEncoder<T extends DataType>(name: string, encode: (vals: (T['TValue'] | null)[], nullVals?: any[]) => Promise<Vector<T>>) {
93 describe(`${name}`, () => {
94 it(`encodes ${valueName} no nulls`, async () => {
95 const vals = noNulls(20);
96 validateVector(vals, await encode(vals, []), []);
97 });
98 it(`encodes ${valueName} with nulls`, async () => {
99 const vals = withNulls(20);
100 validateVector(vals, await encode(vals, [null]), [null]);
101 });
102 it(`encodes ${valueName} with MAX_INT`, async () => {
103 const vals = withNaNs(20);
104 validateVector(vals, await encode(vals, [0x7fffffff]), [0x7fffffff]);
105 });
106 });
107 }
108 });
109});
110
111[
112 [Float16, float16sNoNulls, float16sWithNulls, float16sWithNaNs] as PrimitiveTypeOpts<Float16>,
113 [Float32, float32sNoNulls, float32sWithNulls, float32sWithNaNs] as PrimitiveTypeOpts<Float32>,
114 [Float64, float64sNoNulls, float64sWithNulls, float64sWithNaNs] as PrimitiveTypeOpts<Float64>,
115].forEach(([TypeCtor, noNulls, withNulls, withNaNs]) => {
116
117 describe(`${TypeCtor.name}Builder`, () => {
118
119 const typeFactory = () => new TypeCtor();
120 const valueName = TypeCtor.name.toLowerCase();
121
122 runTestsWithEncoder('encodeAll', encodeAll(typeFactory));
123 runTestsWithEncoder('encodeEach: 5', encodeEach(typeFactory, 5));
124 runTestsWithEncoder('encodeEach: 25', encodeEach(typeFactory, 25));
125 runTestsWithEncoder('encodeEach: undefined', encodeEach(typeFactory));
126 testDOMStreams && runTestsWithEncoder('encodeEachDOM: 25', encodeEachDOM(typeFactory, 25));
127 testNodeStreams && runTestsWithEncoder('encodeEachNode: 25', encodeEachNode(typeFactory, 25));
128
129 function runTestsWithEncoder<T extends DataType>(name: string, encode: (vals: (T['TValue'] | null)[], nullVals?: any[]) => Promise<Vector<T>>) {
130 describe(`${name}`, () => {
131 it(`encodes ${valueName} no nulls`, async () => {
132 const vals = noNulls(20);
133 validateVector(vals, await encode(vals, []), []);
134 });
135 it(`encodes ${valueName} with nulls`, async () => {
136 const vals = withNulls(20);
137 validateVector(vals, await encode(vals, [null]), [null]);
138 });
139 it(`encodes ${valueName} with NaNs`, async () => {
140 const vals = withNaNs(20);
141 validateVector(vals, await encode(vals, [NaN]), [NaN]);
142 });
143 });
144 }
145 });
146});
147
148describe('Float16Builder', () => {
149 const encode = encodeAll(() => new Float16());
150 it(`encodes the weird values`, async () => {
151 const vals = [0, 5.960464477539063e-8, NaN, 65504, 2, -0];
152 validateVector(vals, await encode(vals, []), []);
153 });
154});