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 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
|
% 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 tStringArray < matlab.unittest.TestCase
% Test class for arrow.array.StringArray
properties
ArrowArrayClassName(1, 1) string = "arrow.array.StringArray"
ArrowArrayConstructorFcn = @arrow.array.StringArray.fromMATLAB
MatlabArrayFcn = @string
MatlabConversionFcn = @string
NullSubstitutionValue = string(missing)
ArrowType = arrow.string
end
methods(TestClassSetup)
function verifyOnMatlabPath(tc)
% Verify the arrow array class is on the MATLAB Search Path.
tc.assertTrue(~isempty(which(tc.ArrowArrayClassName)), ...
"""" + tc.ArrowArrayClassName + """must be on the MATLAB path. " + ...
"Use ""addpath"" to add folders to the MATLAB path.");
end
end
methods(Test)
function BasicTest(tc)
A = tc.ArrowArrayConstructorFcn(tc.MatlabArrayFcn(["A", "B", "C"]));
className = string(class(A));
tc.verifyEqual(className, tc.ArrowArrayClassName);
end
function ToMATLAB(tc)
% Create array from a scalar
A1 = tc.ArrowArrayConstructorFcn(tc.MatlabArrayFcn("A"));
data = toMATLAB(A1);
tc.verifyEqual(data, tc.MatlabArrayFcn("A"));
% Create array from a vector
A2 = tc.ArrowArrayConstructorFcn(tc.MatlabArrayFcn(["A", "B", "C"]));
data = toMATLAB(A2);
tc.verifyEqual(data, tc.MatlabArrayFcn(["A", "B", "C"]'));
% Create a StringArray from an empty 0x0 string vector
A3 = tc.ArrowArrayConstructorFcn(tc.MatlabArrayFcn(string.empty(0, 0)));
data = toMATLAB(A3);
tc.verifyEqual(data, tc.MatlabArrayFcn(reshape([], 0, 1)));
% Create a StringArray from an empty 0x1 string vector
A4= tc.ArrowArrayConstructorFcn(tc.MatlabArrayFcn(string.empty(0, 1)));
data = toMATLAB(A4);
tc.verifyEqual(data, tc.MatlabArrayFcn(reshape([], 0, 1)));
% Create a StringArray from an empty 1x0 string vector
A5= tc.ArrowArrayConstructorFcn(tc.MatlabArrayFcn(string.empty(0, 1)));
data = toMATLAB(A5);
tc.verifyEqual(data, tc.MatlabArrayFcn(reshape([], 0, 1)));
end
function MatlabConversion(tc)
% Tests the type-specific conversion method (i.e. string)
% Create array from a scalar
A1 = tc.ArrowArrayConstructorFcn(tc.MatlabArrayFcn("A"));
data = tc.MatlabConversionFcn(A1);
tc.verifyEqual(data, tc.MatlabArrayFcn("A"));
% Create array from a vector
A2 = tc.ArrowArrayConstructorFcn(tc.MatlabArrayFcn(["A", "B", "C"]));
data = tc.MatlabConversionFcn(A2);
tc.verifyEqual(data, tc.MatlabArrayFcn(["A", "B", "C"]'));
% Create a StringArray from an empty 0x0 string vector
A3 = tc.ArrowArrayConstructorFcn(tc.MatlabArrayFcn(string.empty(0, 0)));
data = tc.MatlabConversionFcn(A3);
tc.verifyEqual(data, tc.MatlabArrayFcn(reshape([], 0, 1)));
% Create a StringArray from an empty 0x1 string vector
A4= tc.ArrowArrayConstructorFcn(tc.MatlabArrayFcn(string.empty(0, 1)));
data = tc.MatlabConversionFcn(A4);
tc.verifyEqual(data, tc.MatlabArrayFcn(reshape([], 0, 1)));
% Create a StringArray from an empty 1x0 string vector
A5= tc.ArrowArrayConstructorFcn(tc.MatlabArrayFcn(string.empty(0, 1)));
data = tc.MatlabConversionFcn(A5);
tc.verifyEqual(data, tc.MatlabArrayFcn(reshape([], 0, 1)));
end
function LogicalValidNVPair(tc)
% Verify the expected elements are treated as null when Valid
% is provided as a logical array
data = tc.MatlabArrayFcn(["A", "B", "C"]');
arrowArray = tc.ArrowArrayConstructorFcn(data, Valid=[false true true]);
expectedData = data;
expectedData(1) = tc.NullSubstitutionValue;
tc.verifyEqual(tc.MatlabConversionFcn(arrowArray), expectedData);
tc.verifyEqual(toMATLAB(arrowArray), expectedData);
tc.verifyEqual(arrowArray.Valid, [false; true; true]);
end
function NumericValidNVPair(tc)
% Verify the expected elements are treated as null when Valid
% is provided as a array of indices
data = tc.MatlabArrayFcn(["A", "B", "C"]');
arrowArray = tc.ArrowArrayConstructorFcn(data, Valid=[1, 2]);
expectedData = data;
expectedData(3) = tc.NullSubstitutionValue;
tc.verifyEqual(tc.MatlabConversionFcn(arrowArray), expectedData);
tc.verifyEqual(toMATLAB(arrowArray), expectedData);
tc.verifyEqual(arrowArray.Valid, [true; true; false]);
% Make sure the optimization where the valid-bitmap is stored as
% a nullptr works as expected.
expectedData = data;
arrowArray = tc.ArrowArrayConstructorFcn(data, Valid=[1, 2, 3]);
tc.verifyEqual(tc.MatlabConversionFcn(arrowArray), expectedData);
tc.verifyEqual(toMATLAB(arrowArray), expectedData);
tc.verifyEqual(arrowArray.Valid, [true; true; true]);
end
function ErrorIfNonVector(tc)
data = tc.MatlabArrayFcn(["A", "B", "A", "B", "A", "B", "A", "B", "A"]);
data = reshape(data, 3, 1, 3);
fcn = @() tc.ArrowArrayConstructorFcn(tc.MatlabArrayFcn(data));
tc.verifyError(fcn, "arrow:array:InvalidShape");
end
function AllowNDimensionalEmptyArray(tc)
data = tc.MatlabArrayFcn(reshape(string.empty(0, 0), [1 0 0]));
arrowArray = tc.ArrowArrayConstructorFcn(data);
tc.verifyEqual(arrowArray.NumElements, int64(0));
tc.verifyEqual(toMATLAB(arrowArray), string.empty(0, 1));
end
function TestArrowType(tc)
% Verify the array has the expected arrow.type.Type object
data = tc.MatlabArrayFcn(["A", "B"]);
arrowArray = tc.ArrowArrayConstructorFcn(data);
tc.verifyEqual(arrowArray.Type.ID, tc.ArrowType.ID);
end
function Unicode(tc)
% Verify that Unicode characters are preserved during round-trip
% conversion.
smiley = "😀";
tree = "🌲";
mango = "ðŸ¥";
matlabArray = tc.MatlabArrayFcn([smiley; tree; mango]);
arrowArray = tc.ArrowArrayConstructorFcn(matlabArray);
matlabArrayConverted = toMATLAB(arrowArray);
tc.verifyEqual(matlabArrayConverted, matlabArray);
end
function Missing(tc)
% Verify that string(missing) values get mapped to the empty
% string value when InferNulls=false.
matlabArray = tc.MatlabArrayFcn(["A"; string(missing); string(missing)]);
arrowArray = tc.ArrowArrayConstructorFcn(matlabArray, InferNulls=false);
matlabArrayConverted = toMATLAB(arrowArray);
tc.verifyEqual(matlabArrayConverted, ["A"; ""; ""]);
end
function CellStr(tc)
% Verify that a StringArray can be constructed from
% a cell array of character vectors (i.e. cellstr).
% Row vector
matlabArray = {'A', 'B', 'C'};
arrowArray = tc.ArrowArrayConstructorFcn(matlabArray);
matlabArrayConverted = toMATLAB(arrowArray);
tc.verifyEqual(matlabArrayConverted, string(matlabArray'));
% Column vector
matlabArray = {'A'; 'B'; 'C'};
arrowArray = tc.ArrowArrayConstructorFcn(matlabArray);
matlabArrayConverted = toMATLAB(arrowArray);
tc.verifyEqual(matlabArrayConverted, string(matlabArray));
% One element cellstr
matlabArray = {''};
arrowArray = tc.ArrowArrayConstructorFcn(matlabArray);
matlabArrayConverted = toMATLAB(arrowArray);
tc.verifyEqual(matlabArrayConverted, string(matlabArray));
% Empty cell
matlabArray = {};
arrowArray = tc.ArrowArrayConstructorFcn(matlabArray);
matlabArrayConverted = toMATLAB(arrowArray);
tc.verifyEqual(matlabArrayConverted, string.empty(0, 1));
end
function ErrorIfChar(tc)
% Verify that an error is thrown when a char array
% is passed to the StringArray constructor.
% Row vector
matlabArray = 'abc';
tc.verifyError(@() tc.ArrowArrayConstructorFcn(matlabArray), "arrow:array:InvalidType");
% Column vector
matlabArray = ['a';'b';'c'];
tc.verifyError(@() tc.ArrowArrayConstructorFcn(matlabArray), "arrow:array:InvalidType");
% Empty char (0x0)
matlabArray = '';
tc.verifyError(@() tc.ArrowArrayConstructorFcn(matlabArray), "arrow:array:InvalidType");
% Empty char (0x1)
matlabArray = char.empty(0, 1);
tc.verifyError(@() tc.ArrowArrayConstructorFcn(matlabArray), "arrow:array:InvalidType");
% Empty char (1x0)
matlabArray = char.empty(1, 0);
tc.verifyError(@() tc.ArrowArrayConstructorFcn(matlabArray), "arrow:array:InvalidType");
end
function TestIsEqualTrue(tc)
% Verifies arrays are considered equal if:
%
% 1. Their Type properties are equal
% 2. They have the same number of elements (i.e. their NumElements properties are equal)
% 3. They have the same validity bitmap (i.e. their Valid properties are equal)
% 4. All corresponding valid elements have the same values
data1 = tc.MatlabArrayFcn(["1" "2" "3" "4"]);
data2 = tc.MatlabArrayFcn(["1" "2" "5" "4"]);
array1 = tc.ArrowArrayConstructorFcn(data1, Valid=[1 2 4]);
array2 = tc.ArrowArrayConstructorFcn(data1, Valid=[1 2 4]);
array3 = tc.ArrowArrayConstructorFcn(data2, Valid=[1 2 4]);
tc.verifyTrue(isequal(array1, array2));
tc.verifyTrue(isequal(array1, array3));
% Test supplying more than two arrays to isequal
tc.verifyTrue(isequal(array1, array2, array3));
end
function TestIsEqualFalse(tc)
% Verify isequal returns false when expected.
data1 = tc.MatlabArrayFcn(["1" "2" "3" "4"]);
data2 = tc.MatlabArrayFcn(["5" "2" "3" "4"]);
data3 = tc.MatlabArrayFcn(["1" "2" "3" "4" "5"]);
array1 = tc.ArrowArrayConstructorFcn(data1, Valid=[1 2 4]);
array2 = tc.ArrowArrayConstructorFcn(data1, Valid=[1 4]);
array3 = tc.ArrowArrayConstructorFcn(data2, Valid=[1 2 4]);
array4 = arrow.array([true false true false]);
array5 = tc.ArrowArrayConstructorFcn(data3, Valid=[1 2 4]);
% Their validity bitmaps are not equal
tc.verifyFalse(isequal(array1, array2));
% Not all corresponding valid elements are equal
tc.verifyFalse(isequal(array1, array3));
% Their Type properties are not equal
tc.verifyFalse(isequal(array1, array4));
% Their NumElements properties are not equal
tc.verifyFalse(isequal(array1, array5));
% Comparing an arrow.array.Array to a MATLAB double
tc.verifyFalse(isequal(array1, 1));
% Test supplying more than two arrays to isequal
tc.verifyFalse(isequal(array1, array1, array3, array4, array5));
end
function TestNumNulls(testCase)
% Verify the NumNulls property returns correct value.
% array1 has 0 null values.
data1 = ["A"; "B"; "C"; "D"; "E"; "F"];
array1 = testCase.ArrowArrayConstructorFcn(data1);
testCase.verifyEqual(array1.NumNulls, int64(0));
% array2 has 2 null values.
array2 = testCase.ArrowArrayConstructorFcn(data1, Valid=[1 2 3 4]);
testCase.verifyEqual(array2.NumNulls, int64(2));
end
function TestNumNullsNoSetter(testCase)
% Verify the NumNulls property is read-only.
data = ["A"; "B"; "C"; missing; "D"; "E"; "F"];
array = testCase.ArrowArrayConstructorFcn(data);
fcn = @() setfield(array, "NumNulls", 1);
testCase.verifyError(fcn, "MATLAB:class:SetProhibited");
end
end
end
|