From bfc562dacab1460a4c2c6a7a4d3965539736d187 Mon Sep 17 00:00:00 2001 From: Dayoung Lee Date: Mon, 20 Jan 2025 18:06:47 +0900 Subject: [PATCH] Fix ArrayIndex test --- compiler/luci/pass/src/helpers/ArrayIndex.cpp | 10 ++++++++-- compiler/luci/pass/src/helpers/ArrayIndex.test.cpp | 4 +--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/compiler/luci/pass/src/helpers/ArrayIndex.cpp b/compiler/luci/pass/src/helpers/ArrayIndex.cpp index dcdc1fda091..63ee68b14ad 100644 --- a/compiler/luci/pass/src/helpers/ArrayIndex.cpp +++ b/compiler/luci/pass/src/helpers/ArrayIndex.cpp @@ -17,15 +17,19 @@ #include "ArrayIndex.h" #include +#include namespace luci { +#define UNLESS_INVALID_ARGUMENT(COND) \ + if (not(COND)) \ + throw std::invalid_argument(""); Array4DIndex::Array4DIndex(uint32_t D0, uint32_t D1, uint32_t D2, uint32_t D3) : _dim{D0, D1, D2, D3} { assert(D0 > 0 && D1 > 0 && D2 > 0 && D3 > 0); - + _strides[3] = 1; _strides[2] = D3; _strides[1] = D3 * D2; @@ -39,7 +43,9 @@ Array4DIndex::Array4DIndex(uint32_t D0, uint32_t D1, uint32_t D2, uint32_t D3) uint32_t Array4DIndex::operator()(uint32_t i0, uint32_t i1, uint32_t i2, uint32_t i3) const { - assert(i0 < _dim[0] && i1 < _dim[1] && i2 < _dim[2] && i3 < _dim[3]); + UNLESS_INVALID_ARGUMENT(i0 < _dim[0] && i1 < _dim[1] && i2 < _dim[2] && i3 < _dim[3]); + UNLESS_INVALID_ARGUMENT(0 <= i0 && 0 <= i1 && 0 <= i2 && 0 <= i3); + return i0 * _strides[0] + i1 * _strides[1] + i2 * _strides[2] + i3 * _strides[3]; } diff --git a/compiler/luci/pass/src/helpers/ArrayIndex.test.cpp b/compiler/luci/pass/src/helpers/ArrayIndex.test.cpp index 3e0bb39fdd0..4e2322a0582 100644 --- a/compiler/luci/pass/src/helpers/ArrayIndex.test.cpp +++ b/compiler/luci/pass/src/helpers/ArrayIndex.test.cpp @@ -22,8 +22,6 @@ TEST(LuciPassHelpersArrayIndex, array_index_4d) { luci::Array4DIndex idx(5, 4, 3, 2); - EXPECT_EQ(idx.rank(), 4); - EXPECT_EQ(idx(0, 0, 0, 0), 0); EXPECT_EQ(idx(1, 0, 0, 0), idx.stride(0)); @@ -36,5 +34,5 @@ TEST(LuciPassHelpersArrayIndex, array_index_4d) TEST(LuciPassHelpersArrayIndex, array_index_4d_NEG) { - ASSERT_THROW(luci::Array4DIndex idx(5, 4, 3, -1)); + EXPECT_ANY_THROW(luci::Array4DIndex idx(-1000, 4, 3, -1)); }