blob: a4c8087b720f4a3f4ea8ce48a67ed34c10372b9e [file] [log] [blame]
dan sinclairb7edc4c2020-04-07 12:46:30 +00001// Copyright 2020 The Tint Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#include "src/type_determiner.h"
16
17#include <memory>
18#include <utility>
19
20#include "gtest/gtest.h"
dan sinclair6c498fc2020-04-07 12:47:23 +000021#include "src/ast/assignment_statement.h"
dan sinclairb7ea6e22020-04-07 12:54:10 +000022#include "src/ast/break_statement.h"
dan sinclairb7edc4c2020-04-07 12:46:30 +000023#include "src/ast/float_literal.h"
24#include "src/ast/int_literal.h"
25#include "src/ast/scalar_constructor_expression.h"
26#include "src/ast/type/f32_type.h"
dan sinclair6c498fc2020-04-07 12:47:23 +000027#include "src/ast/type/i32_type.h"
dan sinclairb7edc4c2020-04-07 12:46:30 +000028#include "src/ast/type/vector_type.h"
29#include "src/ast/type_constructor_expression.h"
30
31namespace tint {
32namespace {
33
34class TypeDeterminerTest : public testing::Test {
35 public:
36 void SetUp() { td_ = std::make_unique<TypeDeterminer>(&ctx_); }
37
38 TypeDeterminer* td() const { return td_.get(); }
39
40 private:
41 Context ctx_;
42 std::unique_ptr<TypeDeterminer> td_;
43};
44
dan sinclair6c498fc2020-04-07 12:47:23 +000045TEST_F(TypeDeterminerTest, Stmt_Assign) {
46 ast::type::F32Type f32;
47 ast::type::I32Type i32;
48
49 auto lhs = std::make_unique<ast::ScalarConstructorExpression>(
50 std::make_unique<ast::IntLiteral>(&i32, 2));
51 auto lhs_ptr = lhs.get();
52
53 auto rhs = std::make_unique<ast::ScalarConstructorExpression>(
54 std::make_unique<ast::FloatLiteral>(&f32, 2.3f));
55 auto rhs_ptr = rhs.get();
56
57 ast::AssignmentStatement assign(std::move(lhs), std::move(rhs));
58
59 EXPECT_TRUE(td()->DetermineResultType(&assign));
60 ASSERT_NE(lhs_ptr->result_type(), nullptr);
61 ASSERT_NE(rhs_ptr->result_type(), nullptr);
62
63 EXPECT_TRUE(lhs_ptr->result_type()->IsI32());
64 EXPECT_TRUE(rhs_ptr->result_type()->IsF32());
65}
66
dan sinclairb7ea6e22020-04-07 12:54:10 +000067TEST_F(TypeDeterminerTest, Stmt_Break) {
68 ast::type::I32Type i32;
69
70 auto cond = std::make_unique<ast::ScalarConstructorExpression>(
71 std::make_unique<ast::IntLiteral>(&i32, 2));
72 auto cond_ptr = cond.get();
73
74 ast::BreakStatement brk(ast::StatementCondition::kIf, std::move(cond));
75
76 EXPECT_TRUE(td()->DetermineResultType(&brk));
77 ASSERT_NE(cond_ptr->result_type(), nullptr);
78 EXPECT_TRUE(cond_ptr->result_type()->IsI32());
79}
80
dan sinclairb7edc4c2020-04-07 12:46:30 +000081TEST_F(TypeDeterminerTest, Expr_Constructor_Scalar) {
82 ast::type::F32Type f32;
83 ast::ScalarConstructorExpression s(
84 std::make_unique<ast::FloatLiteral>(&f32, 1.0f));
85
86 EXPECT_TRUE(td()->DetermineResultType(&s));
87 ASSERT_NE(s.result_type(), nullptr);
88 EXPECT_TRUE(s.result_type()->IsF32());
89}
90
91TEST_F(TypeDeterminerTest, Expr_Constructor_Type) {
92 ast::type::F32Type f32;
93 ast::type::VectorType vec(&f32, 3);
94
95 ast::ExpressionList vals;
96 vals.push_back(std::make_unique<ast::ScalarConstructorExpression>(
97 std::make_unique<ast::FloatLiteral>(&f32, 1.0f)));
98 vals.push_back(std::make_unique<ast::ScalarConstructorExpression>(
99 std::make_unique<ast::FloatLiteral>(&f32, 1.0f)));
100 vals.push_back(std::make_unique<ast::ScalarConstructorExpression>(
101 std::make_unique<ast::FloatLiteral>(&f32, 3.0f)));
102
103 ast::TypeConstructorExpression tc(&vec, std::move(vals));
104
105 EXPECT_TRUE(td()->DetermineResultType(&tc));
106 ASSERT_NE(tc.result_type(), nullptr);
107 ASSERT_TRUE(tc.result_type()->IsVector());
108 EXPECT_TRUE(tc.result_type()->AsVector()->type()->IsF32());
109 EXPECT_EQ(tc.result_type()->AsVector()->size(), 3);
110}
111
112} // namespace
113} // namespace tint