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
|
%tNUMERICORSTRING Unit tests for
% arrow.internal.validate.index.numericOrString.
% 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 tNumericOrString < matlab.unittest.TestCase
methods (Test)
function ValidNumericIndex(testCase)
% Verify numericOrString() returns the expected index array
% when given a valid numeric index array.
import arrow.internal.validate.index.numericOrString
original = 1;
expected = int32(1);
actual = numericOrString(original, "int32");
testCase.verifyEqual(actual, expected);
original = [1 2 5];
expected = int32([1 2 5])';
actual = numericOrString(original, "int32");
testCase.verifyEqual(actual, expected);
end
function InvalidNumericIndexError(testCase)
% Verify numericOrString() errors if given an invalid numeric
% index array.
import arrow.internal.validate.index.numericOrString
fcn = @() numericOrString(-1.1, "int8");
testCase.verifyError(fcn, "arrow:badsubscript:NonPositive");
fcn = @() numericOrString([2 -1.1], "int8");
testCase.verifyError(fcn, "arrow:badsubscript:NonPositive");
end
function ValidStringArray(testCase)
% Verify numericOrString() returns the expected index array if
% given a valid string index array.
import arrow.internal.validate.index.numericOrString
testCase.verifyEqual(numericOrString("A", "int32"), "A");
testCase.verifyEqual(numericOrString(["B" "A"], "int32"), ["B", "A"]');
end
function AllowNonScalarTrue(testCase)
% Verify numericOrString() behaves as expected provided
% AllowNonScalar=true.
import arrow.internal.validate.index.numericOrString
% Provide a nonscalar double array
original = [1 2 3]';
expected = int32([1 2 3])';
actual = numericOrString(original, "int32", AllowNonScalar=true);
testCase.verifyEqual(actual, expected);
% Provide a scalar double array
original = 1;
expected = int32(1);
actual = numericOrString(original, "int32", AllowNonScalar=true);
testCase.verifyEqual(actual, expected);
% Provide a nonscalar string array
original = ["A", "B", "C"];
expected = ["A", "B", "C"]';
actual = numericOrString(original, "int32", AllowNonScalar=true);
testCase.verifyEqual(actual, expected);
% Provide a scalar string array
original = "A";
expected = "A";
actual = numericOrString(original, "int32", AllowNonScalar=true);
testCase.verifyEqual(actual, expected);
end
function AllowNonScalarFalse(testCase)
% Verify numericOrString() behaves as expected when provided
% AllowNonScalar=false.
import arrow.internal.validate.index.numericOrString
% Should throw an error when provided a nonscalar double array
original = [1 2 3]';
fcn = @() numericOrString(original, "int32", AllowNonScalar=false);
testCase.verifyError(fcn, "arrow:badsubscript:NonScalar");
% Should not throw an error when provided a scalar double array
original = 1;
expected = int32(1);
actual = numericOrString(original, "int32", AllowNonScalar=true);
testCase.verifyEqual(actual, expected);
% Should throw an error if provided a nonscalar string array
original = ["A", "B", "C"];
fcn = @() numericOrString(original, "int32", AllowNonScalar=false);
testCase.verifyError(fcn, "arrow:badsubscript:NonScalar");
% Should not throw an error if provided a scalar string array
original = "A";
expected = "A";
actual = numericOrString(original, "int32", AllowNonScalar=false);
testCase.verifyEqual(actual, expected);
% Should not throw an error if provided a character row vector
original = 'ABC';
expected = "ABC";
actual = numericOrString(original, "int32", AllowNonScalar=false);
testCase.verifyEqual(actual, expected);
end
end
end
|