From b71898ea30e5b10b7f9e96d80b5944fc14538b91 Mon Sep 17 00:00:00 2001 From: Antonio Maiorano Date: Fri, 23 Dec 2022 18:51:26 +0000 Subject: [PATCH] tint: add const eval array accessor unit tests Bug: tint:1581 Change-Id: I0cbb16936bbc1c139e17cecd07007d418c6ee0fa Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/115503 Kokoro: Kokoro Reviewed-by: David Neto Commit-Queue: Antonio Maiorano --- .../resolver/const_eval_member_access_test.cc | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/src/tint/resolver/const_eval_member_access_test.cc b/src/tint/resolver/const_eval_member_access_test.cc index 65a18f2e29..e6a66abe2b 100644 --- a/src/tint/resolver/const_eval_member_access_test.cc +++ b/src/tint/resolver/const_eval_member_access_test.cc @@ -96,5 +96,81 @@ TEST_F(ResolverConstEvalTest, Matrix_AFloat_Construct_From_AInt_Vectors) { EXPECT_EQ(c1->Index(0)->ValueAs(), 3.0); EXPECT_EQ(c1->Index(1)->ValueAs(), 4.0); } + +namespace ArrayAccess { +struct Case { + Value input; +}; +static Case C(Value input) { + return Case{std::move(input)}; +} +static std::ostream& operator<<(std::ostream& o, const Case& c) { + return o << "input: " << c.input; +} + +using ResolverConstEvalArrayAccessTest = ResolverTestWithParam; +TEST_P(ResolverConstEvalArrayAccessTest, Test) { + Enable(ast::Extension::kF16); + + auto& param = GetParam(); + auto* expr = param.input.Expr(*this); + auto* a = Const("a", expr); + + utils::Vector index_accessors; + for (size_t i = 0; i < param.input.args.Length(); ++i) { + auto* index = IndexAccessor("a", Expr(i32(i))); + index_accessors.Push(index); + } + + utils::Vector stmts; + stmts.Push(WrapInStatement(a)); + for (auto* ia : index_accessors) { + stmts.Push(WrapInStatement(ia)); + } + WrapInFunction(std::move(stmts)); + + EXPECT_TRUE(r()->Resolve()) << r()->error(); + + auto* sem = Sem().Get(expr); + ASSERT_NE(sem, nullptr); + auto* arr = sem->Type()->As(); + ASSERT_NE(arr, nullptr); + + EXPECT_TYPE(sem->ConstantValue()->Type(), sem->Type()); + for (size_t i = 0; i < index_accessors.Length(); ++i) { + auto* ia_sem = Sem().Get(index_accessors[i]); + ASSERT_NE(ia_sem, nullptr); + ASSERT_NE(ia_sem->ConstantValue(), nullptr); + EXPECT_EQ(ia_sem->ConstantValue()->ValueAs(), i); + } +} +template +std::vector ArrayAccessCases() { + if constexpr (std::is_same_v) { + return { + C(Array(false, true)), + }; + } else { + return { + C(Array(T(0))), // + C(Array(T(0), T(1))), // + C(Array(T(0), T(1), T(2))), // + C(Array(T(0), T(1), T(2), T(3))), // + C(Array(T(0), T(1), T(2), T(3), T(4))), // + }; + } +} +INSTANTIATE_TEST_SUITE_P( // + ArrayAccess, + ResolverConstEvalArrayAccessTest, + testing::ValuesIn(Concat(ArrayAccessCases(), // + ArrayAccessCases(), // + ArrayAccessCases(), // + ArrayAccessCases(), // + ArrayAccessCases(), // + ArrayAccessCases(), // + ArrayAccessCases()))); +} // namespace ArrayAccess + } // namespace } // namespace tint::resolver