Skip to content

Commit 4f2d2f6

Browse files
committed
Add tests for slicing
Includes a test for multi-dim indexing with varargs Signed-off-by: Anna Rift <[email protected]>
1 parent d7ce299 commit 4f2d2f6

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

frontend/test/resolution/testArrays.cpp

+40
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,41 @@ static void testArrayLiteral(std::string arrayLiteral, std::string domainType,
169169
assert(guard.realizeErrors() == 0);
170170
}
171171

172+
static void testArraySlicing(std::string arrayLiteral, std::string sliceArgs,
173+
std::string resultType) {
174+
printf("Testing slicing array %s with %s\n", arrayLiteral.c_str(),
175+
sliceArgs.c_str());
176+
177+
Context* context = buildStdContext();
178+
ErrorGuard guard(context);
179+
180+
auto vars = resolveTypesOfVariables(context,
181+
R"""(
182+
module M {
183+
var A = )""" + arrayLiteral + R"""(;
184+
185+
var mySlice = A[)""" + sliceArgs + R"""(];
186+
187+
type expectedSliceTy = )""" + resultType + R"""(;
188+
param correctSliceType = mySlice.type == expectedSliceTy;
189+
}
190+
)""",
191+
{"A", "mySlice", "correctSliceType"});
192+
auto arrType = vars.at("A");
193+
assert(arrType.type());
194+
assert(!arrType.type()->isUnknownType());
195+
assert(arrType.type()->isArrayType());
196+
197+
auto sliceType = vars.at("mySlice");
198+
assert(sliceType.type());
199+
assert(!sliceType.type()->isUnknownType());
200+
assert(sliceType.type()->isArrayType());
201+
202+
ensureParamBool(vars.at("correctSliceType"), true);
203+
204+
assert(guard.realizeErrors() == 0);
205+
}
206+
172207
int main() {
173208
// rectangular
174209
testArray("domain(1)", "int");
@@ -189,6 +224,11 @@ int main() {
189224
testArrayLiteral("[1;]", "domain(2)", "int");
190225
testArrayLiteral("[1, 2; 3, 4;; 5, 6; 7, 8]", "domain(3)", "int");
191226

227+
// slices
228+
testArraySlicing("[1, 2, 3]", "0..1", "[1..2] int");
229+
testArraySlicing("[1, 2; 3, 4]", "0, 1", "int"); // multi-dim indexing, not really a slice
230+
testArraySlicing("[1, 2; 3, 4;; 5, 6; 7, 8]", "0..1, 0..1, 1", "[0..1, 0..1] int");
231+
192232
// associative
193233
testArray("domain(int)", "int");
194234
testArray("domain(int, true)", "int");

0 commit comments

Comments
 (0)