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 { Data, Bool, Vector, BoolVector } from '../../Arrow';
19
20const newBoolVector = (length: number, data: Uint8Array) => Vector.new(Data.Bool(new Bool(), 0, length, 0, null, data));
21
22describe(`BoolVector`, () => {
23    const values = [true, true, false, true, true, false, false, false];
24    const n = values.length;
25    const vector = newBoolVector(n, new Uint8Array([27, 0, 0, 0, 0, 0, 0, 0]));
26    test(`gets expected values`, () => {
27        let i = -1;
28        while (++i < n) {
29            expect(vector.get(i)).toEqual(values[i]);
30        }
31    });
32    test(`iterates expected values`, () => {
33        let i = -1;
34        for (let v of vector) {
35            expect(++i).toBeLessThan(n);
36            expect(v).toEqual(values[i]);
37        }
38    });
39    test(`indexOf returns expected values`, () => {
40        for (let test_value of [true, false]) {
41            const expected = values.indexOf(test_value);
42            expect(vector.indexOf(test_value)).toEqual(expected);
43        }
44    });
45    test(`indexOf returns -1 when value not found`, () => {
46        const v = newBoolVector(3, new Uint8Array([0xFF]));
47        expect(v.indexOf(false)).toEqual(-1);
48    });
49    test(`can set values to true and false`, () => {
50        const v = newBoolVector(n, new Uint8Array([27, 0, 0, 0, 0, 0, 0, 0]));
51        const expected1 = [true, true, false, true, true, false, false, false];
52        const expected2 = [true, true,  true, true, true, false, false, false];
53        const expected3 = [true, true, false, false, false, false, true, true];
54        function validate(expected: boolean[]) {
55            for (let i = -1; ++i < n;) {
56                expect(v.get(i)).toEqual(expected[i]);
57            }
58        }
59        validate(expected1);
60        v.set(2, true);
61        validate(expected2);
62        v.set(2, false);
63        validate(expected1);
64        v.set(3, false);
65        v.set(4, false);
66        v.set(6, true);
67        v.set(7, true);
68        validate(expected3);
69        v.set(3, true);
70        v.set(4, true);
71        v.set(6, false);
72        v.set(7, false);
73        validate(expected1);
74    });
75    test(`packs 0 values`, () => {
76        const expected = new Uint8Array(64);
77        expect(BoolVector.from([]).values).toEqual(expected);
78    });
79    test(`packs 3 values`, () => {
80        const expected = new Uint8Array(64);
81        expected[0] = 5;
82        expect(BoolVector.from([
83            true, false, true
84        ]).values).toEqual(expected);
85    });
86    test(`packs 8 values`, () => {
87        const expected = new Uint8Array(64);
88        expected[0] = 27;
89        expect(BoolVector.from([
90            true, true, false, true, true, false, false, false
91        ]).values).toEqual(expected);
92    });
93    test(`packs 25 values`, () => {
94        const expected = new Uint8Array(64);
95        expected[0] = 27;
96        expected[1] = 216;
97        expect(BoolVector.from([
98            true, true, false, true, true, false, false, false,
99            false, false, false, true, true, false, true, true,
100            false
101        ]).values).toEqual(expected);
102    });
103    test(`from with boolean Array packs values`, () => {
104        const expected = new Uint8Array(64);
105        expected[0] = 5;
106        expect(BoolVector
107            .from([true, false, true])
108            .slice().values
109        ).toEqual(expected);
110    });
111});
112