core: irrt improve test_ndarray_subscript

- rewrote some comments
- add test_ndsubscript_index_subscript_out_of_bounds
This commit is contained in:
lyken 2024-07-15 11:54:21 +08:00
parent 628965e519
commit b12d7fcb2d

View File

@ -27,6 +27,7 @@ namespace test { namespace ndarray_subscript {
*/
BEGIN_TEST();
// Prepare src_ndarray
double src_data[12] = { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0 };
int32_t src_itemsize = sizeof(double);
const int32_t src_ndims = 2;
@ -41,8 +42,7 @@ namespace test { namespace ndarray_subscript {
};
ndarray::basic::set_strides_by_shape(&src_ndarray);
// Destination ndarray
// As documented, ndims and shape & strides must be allocated and determined by the caller.
// Prepare dst_ndarray
const int32_t dst_ndims = 2;
int32_t dst_shape[dst_ndims] = {999, 999}; // Empty values
int32_t dst_strides[dst_ndims] = {999, 999}; // Empty values
@ -53,7 +53,7 @@ namespace test { namespace ndarray_subscript {
.strides = dst_strides
};
// Create the slice in `ndarray[-2::, 1::2]`
// Create the subscripts in `ndarray[-2::, 1::2]`
UserSlice subscript_1;
subscript_1.set_start(-2);
@ -118,6 +118,7 @@ namespace test { namespace ndarray_subscript {
*/
BEGIN_TEST();
// Prepare src_ndarray
double src_data[12] = { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0 };
int32_t src_itemsize = sizeof(double);
const int32_t src_ndims = 2;
@ -132,8 +133,7 @@ namespace test { namespace ndarray_subscript {
};
ndarray::basic::set_strides_by_shape(&src_ndarray);
// Destination ndarray
// As documented, ndims and shape & strides must be allocated and determined by the caller.
// Prepare dst_ndarray
const int32_t dst_ndims = 1;
int32_t dst_shape[dst_ndims] = {999}; // Empty values
int32_t dst_strides[dst_ndims] = {999}; // Empty values
@ -144,7 +144,7 @@ namespace test { namespace ndarray_subscript {
.strides = dst_strides
};
// Create the slice in `ndarray[2, ::-2]`
// Create the subscripts in `ndarray[2, ::-2]`
int32_t subscript_1 = 2;
UserSlice subscript_2;
@ -175,8 +175,59 @@ namespace test { namespace ndarray_subscript {
);
}
void test_ndsubscript_index_subscript_out_of_bounds() {
/*
# Consider `my_array`
print(my_array.shape)
# (4, 5, 6)
my_array[2, 100] # error, index subscript at axis 1 is out of bounds
*/
BEGIN_TEST();
// Prepare src_ndarray
const int32_t src_ndims = 2;
int32_t src_shape[src_ndims] = { 3, 4 };
int32_t src_strides[src_ndims] = {};
NDArray<int32_t> src_ndarray = {
.data = (uint8_t*) nullptr, // placeholder, we wouldn't access it
.itemsize = sizeof(double), // placeholder
.ndims = src_ndims,
.shape = src_shape,
.strides = src_strides
};
ndarray::basic::set_strides_by_shape(&src_ndarray);
// Create the subscripts in `my_array[2, 100]`
int32_t subscript_1 = 2;
int32_t subscript_2 = 100;
const int32_t num_ndsubscripts = 2;
NDSubscript ndsubscripts[num_ndsubscripts] = {
{ .type = INPUT_SUBSCRIPT_TYPE_INDEX, .data = (uint8_t*) &subscript_1 },
{ .type = INPUT_SUBSCRIPT_TYPE_INDEX, .data = (uint8_t*) &subscript_2 }
};
// Prepare dst_ndarray
const int32_t dst_ndims = 0;
int32_t dst_shape[dst_ndims] = {};
int32_t dst_strides[dst_ndims] = {};
NDArray<int32_t> dst_ndarray = {
.data = nullptr, // placehloder
.ndims = dst_ndims,
.shape = dst_shape,
.strides = dst_strides
};
ErrorContext errctx = create_testing_errctx();
ndarray::subscript::subscript(&errctx, num_ndsubscripts, ndsubscripts, &src_ndarray, &dst_ndarray);
assert_errctx_has_error(&errctx, errctx.error_ids->index_error);
}
void run() {
test_ndsubscript_normal_1();
test_ndsubscript_normal_2();
test_ndsubscript_index_subscript_out_of_bounds();
}
} }