blob: ce4ab1d04857f825acf9aad99e00bbf79dc749ea [file] [log] [blame]
Ryan Harrisondbc13af2022-02-21 15:19:07 +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/tint/ast/bitcast_expression.h"
16
17#include "gtest/gtest-spi.h"
18#include "src/tint/ast/test_helper.h"
19
dan sinclair34323ac2022-04-07 18:39:35 +000020namespace tint::ast {
Ryan Harrisondbc13af2022-02-21 15:19:07 +000021namespace {
22
23using BitcastExpressionTest = TestHelper;
24
25TEST_F(BitcastExpressionTest, Create) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000026 auto* expr = Expr("expr");
Ben Clayton971318f2023-02-14 13:52:43 +000027 auto* exp = Bitcast(ty.f32(), expr);
dan sinclaird026e132023-04-18 19:38:25 +000028 CheckIdentifier(exp->type, "f32");
dan sinclair41e4d9a2022-05-01 14:40:55 +000029 ASSERT_EQ(exp->expr, expr);
Ryan Harrisondbc13af2022-02-21 15:19:07 +000030}
31
32TEST_F(BitcastExpressionTest, CreateWithSource) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000033 auto* expr = Expr("expr");
Ryan Harrisondbc13af2022-02-21 15:19:07 +000034
Ben Clayton971318f2023-02-14 13:52:43 +000035 auto* exp = Bitcast(Source{Source::Location{20, 2}}, ty.f32(), expr);
dan sinclair41e4d9a2022-05-01 14:40:55 +000036 auto src = exp->source;
37 EXPECT_EQ(src.range.begin.line, 20u);
38 EXPECT_EQ(src.range.begin.column, 2u);
Ryan Harrisondbc13af2022-02-21 15:19:07 +000039}
40
41TEST_F(BitcastExpressionTest, IsBitcast) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000042 auto* expr = Expr("expr");
Ryan Harrisondbc13af2022-02-21 15:19:07 +000043
Ben Clayton971318f2023-02-14 13:52:43 +000044 auto* exp = Bitcast(ty.f32(), expr);
dan sinclair41e4d9a2022-05-01 14:40:55 +000045 EXPECT_TRUE(exp->Is<BitcastExpression>());
Ryan Harrisondbc13af2022-02-21 15:19:07 +000046}
47
48TEST_F(BitcastExpressionTest, Assert_Null_Type) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000049 EXPECT_FATAL_FAILURE(
50 {
51 ProgramBuilder b;
Ben Clayton971318f2023-02-14 13:52:43 +000052 b.Bitcast(ast::Type(), "idx");
dan sinclair41e4d9a2022-05-01 14:40:55 +000053 },
54 "internal compiler error");
Ryan Harrisondbc13af2022-02-21 15:19:07 +000055}
56
57TEST_F(BitcastExpressionTest, Assert_Null_Expr) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000058 EXPECT_FATAL_FAILURE(
59 {
60 ProgramBuilder b;
Ben Clayton971318f2023-02-14 13:52:43 +000061 b.Bitcast(b.ty.f32(), nullptr);
dan sinclair41e4d9a2022-05-01 14:40:55 +000062 },
63 "internal compiler error");
Ryan Harrisondbc13af2022-02-21 15:19:07 +000064}
65
66TEST_F(BitcastExpressionTest, Assert_DifferentProgramID_Expr) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000067 EXPECT_FATAL_FAILURE(
68 {
69 ProgramBuilder b1;
70 ProgramBuilder b2;
Ben Clayton971318f2023-02-14 13:52:43 +000071 b1.Bitcast(b1.ty.f32(), b2.Expr("idx"));
dan sinclair41e4d9a2022-05-01 14:40:55 +000072 },
73 "internal compiler error");
Ryan Harrisondbc13af2022-02-21 15:19:07 +000074}
75
76} // namespace
dan sinclair34323ac2022-04-07 18:39:35 +000077} // namespace tint::ast