Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1 | // 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 sinclair | 34323ac | 2022-04-07 18:39:35 +0000 | [diff] [blame] | 20 | namespace tint::ast { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 21 | namespace { |
| 22 | |
| 23 | using BitcastExpressionTest = TestHelper; |
| 24 | |
| 25 | TEST_F(BitcastExpressionTest, Create) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 26 | auto* expr = Expr("expr"); |
Ben Clayton | 971318f | 2023-02-14 13:52:43 +0000 | [diff] [blame] | 27 | auto* exp = Bitcast(ty.f32(), expr); |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 28 | CheckIdentifier(exp->type, "f32"); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 29 | ASSERT_EQ(exp->expr, expr); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | TEST_F(BitcastExpressionTest, CreateWithSource) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 33 | auto* expr = Expr("expr"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 34 | |
Ben Clayton | 971318f | 2023-02-14 13:52:43 +0000 | [diff] [blame] | 35 | auto* exp = Bitcast(Source{Source::Location{20, 2}}, ty.f32(), expr); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 36 | auto src = exp->source; |
| 37 | EXPECT_EQ(src.range.begin.line, 20u); |
| 38 | EXPECT_EQ(src.range.begin.column, 2u); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | TEST_F(BitcastExpressionTest, IsBitcast) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 42 | auto* expr = Expr("expr"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 43 | |
Ben Clayton | 971318f | 2023-02-14 13:52:43 +0000 | [diff] [blame] | 44 | auto* exp = Bitcast(ty.f32(), expr); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 45 | EXPECT_TRUE(exp->Is<BitcastExpression>()); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | TEST_F(BitcastExpressionTest, Assert_Null_Type) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 49 | EXPECT_FATAL_FAILURE( |
| 50 | { |
| 51 | ProgramBuilder b; |
Ben Clayton | 971318f | 2023-02-14 13:52:43 +0000 | [diff] [blame] | 52 | b.Bitcast(ast::Type(), "idx"); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 53 | }, |
| 54 | "internal compiler error"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | TEST_F(BitcastExpressionTest, Assert_Null_Expr) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 58 | EXPECT_FATAL_FAILURE( |
| 59 | { |
| 60 | ProgramBuilder b; |
Ben Clayton | 971318f | 2023-02-14 13:52:43 +0000 | [diff] [blame] | 61 | b.Bitcast(b.ty.f32(), nullptr); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 62 | }, |
| 63 | "internal compiler error"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | TEST_F(BitcastExpressionTest, Assert_DifferentProgramID_Expr) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 67 | EXPECT_FATAL_FAILURE( |
| 68 | { |
| 69 | ProgramBuilder b1; |
| 70 | ProgramBuilder b2; |
Ben Clayton | 971318f | 2023-02-14 13:52:43 +0000 | [diff] [blame] | 71 | b1.Bitcast(b1.ty.f32(), b2.Expr("idx")); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 72 | }, |
| 73 | "internal compiler error"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | } // namespace |
dan sinclair | 34323ac | 2022-04-07 18:39:35 +0000 | [diff] [blame] | 77 | } // namespace tint::ast |