From 0e1061d60a219dbf81b37b79bb71303ebf6b0058 Mon Sep 17 00:00:00 2001 From: Lei Hou Date: Thu, 9 Nov 2023 10:58:28 -0500 Subject: [PATCH] Add tests about time precision preservation when converting MATLAB duration to arrow time32 and time64 --- matlab/test/arrow/array/tTime32Array.m | 24 +++++++++ matlab/test/arrow/array/tTime64Array.m | 67 ++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) diff --git a/matlab/test/arrow/array/tTime32Array.m b/matlab/test/arrow/array/tTime32Array.m index cc2fad64b2a28..06ce585fef85a 100644 --- a/matlab/test/arrow/array/tTime32Array.m +++ b/matlab/test/arrow/array/tTime32Array.m @@ -30,6 +30,14 @@ function Basic(tc) times = seconds(1:4); array = tc.ArrowArrayConstructorFcn(times); tc.verifyInstanceOf(array, "arrow.array.Time32Array"); + tc.verifyEqual(array.toMATLAB, times'); + end + + function TimeUnitDefaultValue(tc) + % Verify that the default value of "TimeUnit" is "second". + times = seconds([1.2 1.3 1.4 1.5 1.7]); + array = tc.ArrowArrayConstructorFcn(times); + tc.verifyEqual(array.toMATLAB, seconds([1;1;1;2;2])); end function TypeIsTime32(tc) @@ -274,6 +282,22 @@ function TestIsEqualFalseTimeUnitMistmatch(tc) % arrays are not equal tc.verifyFalse(isequal(array1, array2)); end + + function RoundTimeBySpecifyTimeUnit(tc) + % Verify that the input parameter "TimeUnit" is used to specify + % the time resolution. The value is rounded off based on the + % specified "TimeUnit". + + % TimeUnit="Second" + matlabTimes = seconds([1.1, 1.4, 1.5, 1.9, 2.001]); + arrowTimes = tc.ArrowArrayConstructorFcn(matlabTimes, TimeUnit="Second"); + tc.verifyEqual(arrowTimes.toMATLAB(),seconds([1, 1, 2, 2, 2])'); + + % TimeUnit="Millisecond" + matlabTimes = seconds([1.1, 1.99, 1.001, 1.0004, 1.0005, 2.001]); + arrowTimes = tc.ArrowArrayConstructorFcn(matlabTimes, TimeUnit="Millisecond"); + tc.verifyEqual(arrowTimes.toMATLAB(),seconds([1.1, 1.99, 1.001, 1, 1.001, 2.001])','AbsTol',seconds(1e-15)); + end end methods diff --git a/matlab/test/arrow/array/tTime64Array.m b/matlab/test/arrow/array/tTime64Array.m index a078c5e2173f3..86c7273c121b3 100644 --- a/matlab/test/arrow/array/tTime64Array.m +++ b/matlab/test/arrow/array/tTime64Array.m @@ -30,6 +30,25 @@ function Basic(tc) times = seconds(1:4); array = tc.ArrowArrayConstructorFcn(times); tc.verifyInstanceOf(array, "arrow.array.Time64Array"); + tc.verifyEqual(array.toMATLAB, times'); + end + + function TimeUnitDefaultValue(tc) + % Verify that the default value of "TimeUnit" is "Microsecond". + matlabTimes = seconds([1; ... + 0.001; ... + 2.004521; ... + 3.1234564; ... + 4.1234566; ... + 5.000000123]); + arrowArray = tc.ArrowArrayConstructorFcn(matlabTimes); + tc.verifyEqual(arrowArray.toMATLAB(), ... + seconds([1;... + 0.001; ... + 2.004521; ... + 3.123456; ... + 4.123457; ... + 5])); end function TypeIsTime64(tc) @@ -290,6 +309,54 @@ function TestIsEqualFalseTimeUnitMistmatch(tc) % arrays are not equal tc.verifyFalse(isequal(array1, array2)); end + + function RoundTimeBySpecifyTimeUnit(tc) + % Verify that the input parameter "TimeUnit" is used to specify + % the time resolution. The value is rounded off based on the + % specified "TimeUnit". + + % TimeUnit="Microsecond" + matlabTimes = seconds([1.000001, ... + 2.999999, ... + 0.0002004, ... + 0.0000035, ... + 10.123456499, ... + 9.999999543]); + arrowTimes = tc.ArrowArrayConstructorFcn(matlabTimes, TimeUnit="Microsecond"); + tc.verifyEqual(arrowTimes.toMATLAB(), ... + seconds([1.000001, ... + 2.999999, ... + 0.0002, ... + 0.000004, ... + 10.123456, ... + 10])', ... + 'AbsTol',seconds(1e-14)); + + % TimeUnit="Nanosecond" + matlabTimes = seconds([1, ... + 1.123, ... + 1.12345, ... + 1.123456, ... + 1.1234567, ... + 1.12345678, ... + 1.123456789, ... + 1.1234567894, ... + 1.1234567895, ... + 1.123456789009]); + arrowTimes = tc.ArrowArrayConstructorFcn(matlabTimes, TimeUnit="Nanosecond"); + tc.verifyEqual(arrowTimes.toMATLAB(),... + seconds([1, ... + 1.123, ... + 1.12345, ... + 1.123456, ... + 1.1234567, ... + 1.12345678, ... + 1.123456789, ... + 1.123456789, ... + 1.123456790, ... + 1.123456789])',... + 'AbsTol',seconds(1e-15)); + end end methods