File: tListArray.m

package info (click to toggle)
apache-arrow 23.0.1-9
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 76,056 kB
  • sloc: cpp: 654,608; python: 70,522; ruby: 45,964; ansic: 18,742; sh: 7,371; makefile: 635; javascript: 125; xml: 41
file content (278 lines) | stat: -rw-r--r-- 12,060 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
%TLISTARRAY Tests for arrow.array.ListArray

% Licensed to the Apache Software Foundation (ASF) under one or more
% contributor license agreements.  See the NOTICE file distributed with
% this work for additional information regarding copyright ownership.
% The ASF licenses this file to you under the Apache License, Version
% 2.0 (the "License"); you may not use this file except in compliance
% with the License.  You may obtain a copy of the License at
%
%   http://www.apache.org/licenses/LICENSE-2.0
%
% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS,
% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
% implied.  See the License for the specific language governing
% permissions and limitations under the License.

classdef tListArray < matlab.unittest.TestCase

    properties (Constant)
        Traits = arrow.type.traits.traits(arrow.type.ID.List)
    end

    properties (TestParameter)
        TestArrowArray
        TestValidationModeArray
    end

    methods (TestParameterDefinition, Static)

        function TestArrowArray = initializeTestArrowArray()
            %% Empty (zero-element) list (List<Float64>)
            Type = arrow.list(arrow.float64());
            NumElements = int64(0);
            NumNulls = int64(0);
            Valid = logical.empty(0, 1);
            Offsets = arrow.array(int32(0));
            Values = arrow.array([]);
            ArrowArray = arrow.array.ListArray.fromArrays(Offsets, Values, Valid=Valid);
            MatlabArray = {cell.empty(0, 1)};

            TestArrowArray.EmptyList = struct( ...
                ArrowArray=ArrowArray, ...
                MatlabArray=MatlabArray, ...
                Properties=struct(...
                    Type=Type, ...
                    NumElements=NumElements, ...
                    NumNulls=NumNulls, ...
                    Valid=Valid, ...
                    Offsets=Offsets, ...
                    Values=Values ...
                ) ...
            );

            %% List with NULLs (List<String>)
            Type = arrow.list(arrow.string());
            NumElements = int64(4);
            NumNulls = int64(2);
            Valid = [true, false, true, false];
            Offsets = arrow.array(int32([0, 1, 4, 6, 7]));
            Values = arrow.array(["A", missing, "C", "D", "E", missing, "G"]);
            ArrowArray = arrow.array.ListArray.fromArrays(Offsets, Values, Valid=Valid);
            MatlabArray = {{"A"; missing; ["E"; missing]; missing}};

            TestArrowArray.NullList = struct( ...
                ArrowArray=ArrowArray, ...
                MatlabArray=MatlabArray, ...
                Properties=struct(...
                    Type=Type, ...
                    NumElements=NumElements, ...
                    NumNulls=NumNulls, ...
                    Valid=Valid, ...
                    Offsets=Offsets, ...
                    Values=Values ...
                ) ...
            );

            %% Single-level list (List<Float64>)
            Type = arrow.list(arrow.float64());
            NumElements = int64(3);
            NumNulls = int64(0);
            Valid = true(1, NumElements);
            Offsets = arrow.array(int32([0, 2, 5, 9]));
            Values = arrow.array([1, 2, 3, 4, 5, 6, 7, 8, 9]);
            ArrowArray = arrow.array.ListArray.fromArrays(Offsets, Values, Valid=Valid);
            MatlabArray = {{[1; 2]; [3; 4; 5]; [6; 7; 8; 9]}};

            TestArrowArray.SingleLevelList = struct( ...
                ArrowArray=ArrowArray, ...
                MatlabArray=MatlabArray, ...
                Properties=struct(...
                    Type=Type, ...
                    NumElements=NumElements, ...
                    NumNulls=NumNulls, ...
                    Valid=Valid, ...
                    Offsets=Offsets, ...
                    Values=Values ...
                ) ...
            );

            %% Multi-level list (List<List<Float64>>)
            Type = arrow.list(arrow.list(arrow.float64()));
            NumElements = int64(2);
            NumNulls = int64(0);
            Valid = true(1, NumElements);
            Offsets = arrow.array(int32([0, 1, 3]));
            Values = TestArrowArray.SingleLevelList.ArrowArray;
            ArrowArray = arrow.array.ListArray.fromArrays(Offsets, Values, Valid=Valid);
            MatlabArray = {{{[1; 2]}; {[3; 4; 5]; [6; 7; 8; 9]}}};

            TestArrowArray.MultiLevelList = struct( ...
                ArrowArray=ArrowArray, ...
                MatlabArray=MatlabArray, ...
                Properties=struct(...
                    Type=Type, ...
                    NumElements=NumElements, ...
                    NumNulls=NumNulls, ...
                    Valid=Valid, ...
                    Offsets=Offsets, ...
                    Values=Values ...
                ) ...
            );
        end

        function TestValidationModeArray = initializeTestValidationModeArray()
            %% Valid ListArray
            Offsets = arrow.array(int32([0, 1, 2, 3]));
            Values = arrow.array([1, 2, 3]);

            TestValidationModeArray.ValidList = struct( ...
                Offsets=Offsets, ...
                Values=Values, ...
                Valid=true ...
            );

            %% Invalid ListArray
            % Incorrect number of offsets (length should be 1 more than the number of Values).
            Offsets = arrow.array(int32([0, 1, 2, 3, 4, 5]));
            Values = arrow.array([1, 2, 3]);

            TestValidationModeArray.InvalidList = struct( ...
                Offsets=Offsets, ...
                Values=Values, ...
                Valid=false ...
            );
        end

    end

    methods (Test)

        function TestClass(testCase, TestArrowArray)
            % Verify that the arrow.array.Array has the expected class.
            testCase.verifyInstanceOf(TestArrowArray.ArrowArray, testCase.Traits.ArrayClassName);
        end

        function TestProperties(testCase, TestArrowArray)
            % Verify that all properties of the arrow.array.Array:
            %
            % 1. Return the expected value
            % 2. Cannot be modified (i.e. are read-only).
            %
            properties = string(fieldnames(TestArrowArray.Properties));
            for ii = numel(properties)
                property = properties(ii);
                expected = TestArrowArray.Properties.(property);
                actual = getfield(TestArrowArray.ArrowArray, property);
                % Verify that the property returns the expected value.
                testCase.verifyEqual(actual, expected);
                fcn = @() setfield(TestArrowArray.ArrowArray, property, "NewValue");
                % Verify that the property cannot be modified (i.e. that it
                % is read-only).
                testCase.verifyError(fcn, "MATLAB:class:SetProhibited");
            end
        end

        function TestToMatlab(testCase, TestArrowArray)
            % Verify that the toMATLAB method returns the
            % expected MATLAB array.
            actual = TestArrowArray.ArrowArray.toMATLAB();
            expected = TestArrowArray.MatlabArray;
            testCase.verifyEqual(actual, expected);
        end

        function TestErrorIfEmptyOffsets(testCase)
            % Verify that an arrow:array:ListArrayFromArraysFailed error
            % is thrown if an empty Offsets array is provided to the
            % arrow.array.ListArray.fromArrays.
            offsets = arrow.array(int32.empty(0, 0));
            values = arrow.array([1, 2, 3]);
            fcn = @() arrow.array.ListArray.fromArrays(offsets, values);
            testCase.verifyError(fcn, "arrow:array:ListArrayFromArraysFailed");
        end

        function TestValidationModeDefault(testCase, TestValidationModeArray)
            % Verify that the default ValidationMode value for the
            % arrow.array.ListArray.fromArrays method is
            % arrow.array.ValidationMode.Minimal.
            offsets = TestValidationModeArray.Offsets;
            values = TestValidationModeArray.Values;
            valid = TestValidationModeArray.Valid;
            fcn = @() arrow.array.ListArray.fromArrays(offsets, values);
            if valid
                testCase.verifyWarningFree(fcn);
            else
                testCase.verifyError(fcn, "arrow:array:ValidateMinimalFailed");
            end
        end

        function TestValidationModeNone(testCase, TestValidationModeArray)
            % Verify that no error is thrown when supplying the
            % ValidationMode name-value pair, with a value of
            % arrow.array.ValidationMode.None, to the
            % arrow.array.ListArray.fromArrays method.
            offsets = TestValidationModeArray.Offsets;
            values = TestValidationModeArray.Values;
            validationMode = arrow.array.ValidationMode.None;
            fcn = @() arrow.array.ListArray.fromArrays(offsets, values, ValidationMode=validationMode);
            testCase.verifyWarningFree(fcn);
        end

        function TestValidationModeMinimal(testCase, TestValidationModeArray)
            % Verify that an error of type arrow:array:ValidateMinimalFailed
            % is thrown when supplying the ValidationMode name-value pair,
            % with a value of arrow.array.ValidationMode.Minimal, to the
            % arrow.array.ListArray.fromArrays method, if the provided offsets
            % and values arrays are invalid.
            offsets = TestValidationModeArray.Offsets;
            values = TestValidationModeArray.Values;
            valid = TestValidationModeArray.Valid;
            validationMode = arrow.array.ValidationMode.Minimal;
            fcn = @() arrow.array.ListArray.fromArrays(offsets, values, ValidationMode=validationMode);
            if valid
                testCase.verifyWarningFree(fcn);
            else
                testCase.verifyError(fcn, "arrow:array:ValidateMinimalFailed");
            end
        end

        function TestValidationModeFull(testCase, TestValidationModeArray)
            % Verify that an error of type arrow:array:ValidateFullFailed
            % is thrown when supplying the ValidationMode name-value pair,
            % with a value of arrow.array.ValidationMode.Full, to the
            % arrow.array.ListArray.fromArrays method, if the provided offsets
            % and values arrays are invalid.
            offsets = TestValidationModeArray.Offsets;
            values = TestValidationModeArray.Values;
            validationMode = arrow.array.ValidationMode.Full;
            valid = TestValidationModeArray.Valid;
            fcn = @() arrow.array.ListArray.fromArrays(offsets, values, ValidationMode=validationMode);
            if valid
                testCase.verifyWarningFree(fcn);
            else
                testCase.verifyError(fcn, "arrow:array:ValidateFullFailed");
            end
        end

        function TestValidationModeUnsupportedEnum(testCase)
            % Verify that an error of type arrow:array:ValidateUnsupportedEnum
            % is thrown when an unsupported integer enumeration value is
            % supplied for the ValidationMode parameter to the internal
            % C++ ListArray Proxy validate method.
            offsets = arrow.array.Int32Array.fromMATLAB(int32([0, 1, 2]));
            values = arrow.array.Float64Array.fromMATLAB([1, 2, 3]);
            array = arrow.array.ListArray.fromArrays(offsets, values);
            % Get the underlying Proxy instance from the ListArray.
            proxy = array.Proxy;
            % Call the internal Proxy method "validate" with an unsupported
            % integer ValidationMode value.
            validationMode = uint8(3);
            args = struct(ValidationMode=validationMode);
            fcn = @() proxy.validate(args);
            testCase.verifyError(fcn, "arrow:array:ValidateUnsupportedEnum");
        end

    end

end