Skip to content

Commit 26a53a4

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 26a53a4

File tree

1 file changed

+40
-3
lines changed

1 file changed

+40
-3
lines changed

frontend/test/resolution/testArrays.cpp

+40-3
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@
2828
#include "chpl/uast/Record.h"
2929
#include "chpl/uast/Variable.h"
3030

31-
// TODO:
32-
// - Slices
33-
3431
static void testArray(std::string domainType,
3532
std::string eltType) {
3633
std::string arrayText;
@@ -169,6 +166,41 @@ static void testArrayLiteral(std::string arrayLiteral, std::string domainType,
169166
assert(guard.realizeErrors() == 0);
170167
}
171168

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

224+
// slices
225+
testArraySlicing("[1, 2, 3]", "0..1", "[1..2] int");
226+
testArraySlicing("[1, 2; 3, 4]", "0, 1", "int"); // multi-dim indexing, not really a slice
227+
testArraySlicing("[1, 2; 3, 4;; 5, 6; 7, 8]", "0..1, 0..1, 1", "[0..1, 0..1] int");
228+
192229
// associative
193230
testArray("domain(int)", "int");
194231
testArray("domain(int, true)", "int");

0 commit comments

Comments
 (0)