]> git.proxmox.com Git - ceph.git/blame - ceph/src/arrow/js/test/unit/vector/vector-tests.ts
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / js / test / unit / vector / vector-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 Int32, Dictionary, DateUnit, util,
20 Data, Vector, Utf8Vector, DateVector, DictionaryVector,
21} from 'apache-arrow';
22
23describe(`DateVector`, () => {
24 const extras = [
25 new Date(2000, 0, 1),
26 new Date(1991, 5, 28, 12, 11, 10)
27 ];
28 describe(`unit = MILLISECOND`, () => {
29 const values = [
30 new Date(1989, 5, 22, 1, 2, 3),
31 new Date(1988, 3, 25, 4, 5, 6),
32 new Date(1987, 2, 24, 7, 8, 9),
33 new Date(2018, 4, 12, 17, 30, 0)
34 ];
35 const vector = DateVector.from(values);
36 basicVectorTests(vector, values, extras);
37 });
38 describe(`unit = DAY`, () => {
39 // Use UTC to ensure that dates are always at midnight
40 const values = [
41 new Date(Date.UTC(1989, 5, 22)),
42 new Date(Date.UTC(1988, 3, 25)),
43 new Date(Date.UTC(1987, 2, 24)),
44 new Date(Date.UTC(2018, 4, 12))
45 ];
46 const vector = DateVector.from(values, DateUnit.DAY);
47 basicVectorTests(vector, values, extras);
48 });
49});
50
51describe(`DictionaryVector`, () => {
52
53 const dictionary = ['foo', 'bar', 'baz'];
54 const extras = ['abc', '123']; // values to search for that should NOT be found
55 const dictionary_vec = Utf8Vector.from(dictionary);
56
57 const indices = Array.from({length: 50}, () => Math.random() * 3 | 0);
58 const validity = Array.from({ length: indices.length }, () => Math.random() > 0.2 ? true : false);
59
60 describe(`index with nullCount == 0`, () => {
61
62 const values = Array.from(indices).map((d) => dictionary[d]);
63 const vector = DictionaryVector.from(dictionary_vec, new Int32(), indices);
64
65 basicVectorTests(vector, values, extras);
66
67 describe(`sliced`, () => {
68 basicVectorTests(vector.slice(10, 20), values.slice(10,20), extras);
69 });
70 });
71
72 describe(`index with nullCount > 0`, () => {
73
74 const nullBitmap = util.packBools(validity);
75 const nullCount = validity.reduce((acc, d) => acc + (d ? 0 : 1), 0);
76 const values = Array.from(indices).map((d, i) => validity[i] ? dictionary[d] : null);
77 const type = new Dictionary(dictionary_vec.type, new Int32(), null, null);
78 const vector = Vector.new(Data.Dictionary(type, 0, indices.length, nullCount, nullBitmap, indices, dictionary_vec));
79
80 basicVectorTests(vector, values, ['abc', '123']);
81 describe(`sliced`, () => {
82 basicVectorTests(vector.slice(10, 20), values.slice(10,20), extras);
83 });
84 });
85});
86
87describe(`Utf8Vector`, () => {
88 const values = ['foo', 'bar', 'baz', 'foo bar', 'bar'];
89 const vector = Utf8Vector.from(values);
90 basicVectorTests(vector, values, ['abc', '123']);
91 describe(`sliced`, () => {
92 basicVectorTests(vector.slice(1,3), values.slice(1,3), ['foo', 'abc']);
93 });
94});
95
96// Creates some basic tests for the given vector.
97// Verifies that:
98// - `get` and the native iterator return the same data as `values`
99// - `indexOf` returns the same indices as `values`
100function basicVectorTests(vector: Vector, values: any[], extras: any[]) {
101
102 const n = values.length;
103
104 test(`gets expected values`, () => {
105 let i = -1;
106 while (++i < n) {
107 expect(vector.get(i)).toEqual(values[i]);
108 }
109 });
110 test(`iterates expected values`, () => {
111 expect.hasAssertions();
112 let i = -1;
113 for (let v of vector) {
114 expect(++i).toBeLessThan(n);
115 expect(v).toEqual(values[i]);
116 }
117 });
118 test(`indexOf returns expected values`, () => {
119 let testValues = values.concat(extras);
120
121 for (const value of testValues) {
122 const actual = vector.indexOf(value);
123 const expected = values.indexOf(value);
124 expect(actual).toEqual(expected);
125 }
126 });
127}