1+ % TROUNDTRIP Tests for roundtripping using the C Data Interface format.
2+
3+ % Licensed to the Apache Software Foundation (ASF) under one or more
4+ % contributor license agreements. See the NOTICE file distributed with
5+ % this work for additional information regarding copyright ownership.
6+ % The ASF licenses this file to you under the Apache License, Version
7+ % 2.0 (the "License"); you may not use this file except in compliance
8+ % with the License. You may obtain a copy of the License at
9+ %
10+ % http://www.apache.org/licenses/LICENSE-2.0
11+ %
12+ % Unless required by applicable law or agreed to in writing, software
13+ % distributed under the License is distributed on an "AS IS" BASIS,
14+ % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
15+ % implied. See the License for the specific language governing
16+ % permissions and limitations under the License.
17+ classdef tRoundTrip < matlab .unittest .TestCase
18+
19+ methods (Test )
20+
21+ function EmptyArray(testCase )
22+ % Empty 0x0
23+ expected = arrow .array(double .empty(0 , 0 ));
24+ cArray = arrow .c .Array();
25+ cSchema = arrow .c .Schema();
26+
27+ expected .export(cArray .Address , cSchema .Address );
28+ actual = arrow .array .Array .import(cArray , cSchema );
29+
30+ testCase .verifyEqual(actual , expected );
31+
32+ % Empty 0x1
33+ expected = arrow .array(double .empty(0 , 1 ));
34+ cArray = arrow .c .Array();
35+ cSchema = arrow .c .Schema();
36+
37+ expected .export(cArray .Address , cSchema .Address );
38+ actual = arrow .array .Array .import(cArray , cSchema );
39+
40+ testCase .verifyEqual(actual , expected );
41+
42+ % Empty 1x0
43+ expected = arrow .array(double .empty(0 , 1 ));
44+ cArray = arrow .c .Array();
45+ cSchema = arrow .c .Schema();
46+
47+ expected .export(cArray .Address , cSchema .Address );
48+ actual = arrow .array .Array .import(cArray , cSchema );
49+
50+ testCase .verifyEqual(actual , expected );
51+ end
52+
53+ function ArrayWithNulls(testCase )
54+ % Scalar null
55+ expected = arrow .array(double(NaN ));
56+ cArray = arrow .c .Array();
57+ cSchema = arrow .c .Schema();
58+
59+ expected .export(cArray .Address , cSchema .Address );
60+ actual = arrow .array .Array .import(cArray , cSchema );
61+
62+ testCase .verifyEqual(actual , expected );
63+
64+ % Vector with nulls
65+ expected = arrow .array([1 , NaN , 3 , NaN , 5 ]);
66+ cArray = arrow .c .Array();
67+ cSchema = arrow .c .Schema();
68+
69+ expected .export(cArray .Address , cSchema .Address );
70+ actual = arrow .array .Array .import(cArray , cSchema );
71+
72+ testCase .verifyEqual(actual , expected );
73+
74+ % Vector all nulls
75+ expected = arrow .array([NaN , NaN , NaN ]);
76+ cArray = arrow .c .Array();
77+ cSchema = arrow .c .Schema();
78+
79+ expected .export(cArray .Address , cSchema .Address );
80+ actual = arrow .array .Array .import(cArray , cSchema );
81+
82+ testCase .verifyEqual(actual , expected );
83+ end
84+
85+ function Float64Array(testCase )
86+ % Scalar
87+ expected = arrow .array(double(1 ));
88+ cArray = arrow .c .Array();
89+ cSchema = arrow .c .Schema();
90+
91+ expected .export(cArray .Address , cSchema .Address );
92+ actual = arrow .array .Array .import(cArray , cSchema );
93+
94+ testCase .verifyEqual(actual , expected );
95+
96+ % Vector
97+ expected = arrow .array([1 , 2 , 3 ]);
98+ cArray = arrow .c .Array();
99+ cSchema = arrow .c .Schema();
100+
101+ expected .export(cArray .Address , cSchema .Address );
102+ actual = arrow .array .Array .import(cArray , cSchema );
103+
104+ testCase .verifyEqual(actual , expected );
105+ end
106+
107+ function StringArray(testCase )
108+ % Scalar
109+ expected = arrow .array(" A" );
110+ cArray = arrow .c .Array();
111+ cSchema = arrow .c .Schema();
112+
113+ expected .export(cArray .Address , cSchema .Address );
114+ actual = arrow .array .Array .import(cArray , cSchema );
115+
116+ testCase .verifyEqual(actual , expected );
117+
118+ % Vector
119+ expected = arrow .array([" A" , " B" , " C" ]);
120+ cArray = arrow .c .Array();
121+ cSchema = arrow .c .Schema();
122+
123+ expected .export(cArray .Address , cSchema .Address );
124+ actual = arrow .array .Array .import(cArray , cSchema );
125+
126+ testCase .verifyEqual(actual , expected );
127+ end
128+
129+ function TimestampArray(testCase )
130+ % Scalar
131+ expected = arrow .array(datetime(2024 , 1 , 1 ));
132+ cArray = arrow .c .Array();
133+ cSchema = arrow .c .Schema();
134+
135+ expected .export(cArray .Address , cSchema .Address );
136+ actual = arrow .array .Array .import(cArray , cSchema );
137+
138+ testCase .verifyEqual(actual , expected );
139+
140+ % Vector
141+ expected = arrow .array([...
142+ datetime(2024 , 1 , 1 ),...
143+ datetime(2024 , 1 , 2 ),...
144+ datetime(2024 , 1 , 3 )...
145+ ]);
146+ cArray = arrow .c .Array();
147+ cSchema = arrow .c .Schema();
148+
149+ expected .export(cArray .Address , cSchema .Address );
150+ actual = arrow .array .Array .import(cArray , cSchema );
151+
152+ testCase .verifyEqual(actual , expected );
153+ end
154+
155+ function ExportErrorWrongInputTypes(testCase )
156+ A = arrow .array([1 , 2 , 3 ]);
157+ fcn = @() A .export(" cArray.Address" , " cSchema.Address" );
158+ testCase .verifyError(fcn , " MATLAB:validation:UnableToConvert" );
159+ end
160+
161+ function ExportTooFewInputs(testCase )
162+ A = arrow .array([1 , 2 , 3 ]);
163+ fcn = @() A .export();
164+ testCase .verifyError(fcn , " MATLAB:minrhs" );
165+ end
166+
167+ function ExportTooManyInputs(testCase )
168+ A = arrow .array([1 , 2 , 3 ]);
169+ fcn = @() A .export(" A" , " B" , " C" );
170+ testCase .verifyError(fcn , " MATLAB:TooManyInputs" );
171+ end
172+
173+ function ImportErrorWrongInputTypes(testCase )
174+ cArray = " arrow.c.Array" ;
175+ cSchema = " arrow.c.Schema" ;
176+ fcn = @() arrow .array .Array .import(cArray , cSchema );
177+ testCase .verifyError(fcn , " MATLAB:validation:UnableToConvert" );
178+ end
179+
180+ function ImportTooFewInputs(testCase )
181+ fcn = @() arrow .array .Array .import();
182+ testCase .verifyError(fcn , " MATLAB:minrhs" );
183+ end
184+
185+ function ImportTooManyInputs(testCase )
186+ A = arrow .array([1 , 2 , 3 ]);
187+ fcn = @() arrow .array .Array .import(" A" , " B" , " C" );
188+ testCase .verifyError(fcn , " MATLAB:TooManyInputs" );
189+ end
190+
191+ function ImportErrorImportFailed(testCase )
192+ cArray = arrow .c .Array();
193+ cSchema = arrow .c .Schema();
194+ % An arrow:c:import:ImportFailed error should be thrown
195+ % if the supplied arrow.c.Array and arrow.c.Schema were
196+ % never populated previously from an exported Array.
197+ fcn = @() arrow .array .Array .import(cArray , cSchema );
198+ testCase .verifyError(fcn , " arrow:c:import:ImportFailed" );
199+ end
200+
201+ end
202+
203+ end
0 commit comments