• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/writer/hlsl/test_helper.h"
16 
17 namespace tint {
18 namespace writer {
19 namespace hlsl {
20 namespace {
21 
22 using HlslGeneratorImplTest_Switch = TestHelper;
23 
TEST_F(HlslGeneratorImplTest_Switch,Emit_Switch)24 TEST_F(HlslGeneratorImplTest_Switch, Emit_Switch) {
25   Global("cond", ty.i32(), ast::StorageClass::kPrivate);
26   auto* s = Switch(                   //
27       Expr("cond"),                   //
28       Case(Expr(5), Block(Break())),  //
29       DefaultCase());
30   WrapInFunction(s);
31 
32   GeneratorImpl& gen = Build();
33 
34   gen.increment_indent();
35 
36   ASSERT_TRUE(gen.EmitStatement(s)) << gen.error();
37   EXPECT_EQ(gen.result(), R"(  switch(cond) {
38     case 5: {
39       break;
40     }
41     default: {
42       break;
43     }
44   }
45 )");
46 }
47 
TEST_F(HlslGeneratorImplTest_Switch,Emit_Switch_OnlyDefaultCase)48 TEST_F(HlslGeneratorImplTest_Switch, Emit_Switch_OnlyDefaultCase) {
49   Global("cond", ty.i32(), ast::StorageClass::kPrivate);
50   Global("a", ty.i32(), ast::StorageClass::kPrivate);
51   auto* s = Switch(  //
52       Expr("cond"),  //
53       DefaultCase(Block(Assign(Expr("a"), Expr(42)))));
54   WrapInFunction(s);
55 
56   GeneratorImpl& gen = Build();
57 
58   gen.increment_indent();
59 
60   ASSERT_TRUE(gen.EmitStatement(s)) << gen.error();
61   EXPECT_EQ(gen.result(), R"(  cond;
62   do {
63     a = 42;
64   } while (false);
65 )");
66 }
67 
68 }  // namespace
69 }  // namespace hlsl
70 }  // namespace writer
71 }  // namespace tint
72