1 // Copyright 2019 The Amber 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 parseried.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "gtest/gtest.h"
16 #include "src/amberscript/parser.h"
17
18 namespace amber {
19 namespace amberscript {
20
21 using AmberScriptParserTest = testing::Test;
22
TEST_F(AmberScriptParserTest,Repeat)23 TEST_F(AmberScriptParserTest, Repeat) {
24 std::string in = R"(
25 SHADER compute shader GLSL
26 # shader
27 END
28
29 PIPELINE compute my_pipeline
30 ATTACH shader
31 END
32
33 REPEAT 4
34 RUN my_pipeline 1 2 3
35 RUN my_pipeline 4 5 6
36 RUN my_pipeline 7 8 9
37 END
38 )";
39
40 Parser parser;
41 Result r = parser.Parse(in);
42 ASSERT_TRUE(r.IsSuccess()) << r.Error();
43
44 auto script = parser.GetScript();
45 const auto& commands = script->GetCommands();
46 ASSERT_EQ(1U, commands.size());
47
48 auto* cmd = commands[0].get();
49 ASSERT_TRUE(cmd->IsRepeat());
50
51 auto* repeat = cmd->AsRepeat();
52 EXPECT_EQ(4U, repeat->GetCount());
53
54 const auto& repeat_cmds = repeat->GetCommands();
55 ASSERT_EQ(3U, repeat_cmds.size());
56 ASSERT_TRUE(repeat_cmds[0]->IsCompute());
57 EXPECT_EQ(1U, repeat_cmds[0]->AsCompute()->GetX());
58 ASSERT_TRUE(repeat_cmds[1]->IsCompute());
59 EXPECT_EQ(4U, repeat_cmds[1]->AsCompute()->GetX());
60 ASSERT_TRUE(repeat_cmds[2]->IsCompute());
61 EXPECT_EQ(7U, repeat_cmds[2]->AsCompute()->GetX());
62 }
63
TEST_F(AmberScriptParserTest,RepeatMissingNum)64 TEST_F(AmberScriptParserTest, RepeatMissingNum) {
65 std::string in = R"(
66 REPEAT
67 RUN my_pipeline 1 1 1
68 END
69 )";
70
71 Parser parser;
72 Result r = parser.Parse(in);
73 ASSERT_FALSE(r.IsSuccess());
74 EXPECT_EQ("3: missing count parameter for REPEAT command", r.Error());
75 }
76
TEST_F(AmberScriptParserTest,RepeatInvalidNum)77 TEST_F(AmberScriptParserTest, RepeatInvalidNum) {
78 std::string in = R"(
79 REPEAT INVALID
80 RUN my_pipeline 1 1 1
81 END
82 )";
83
84 Parser parser;
85 Result r = parser.Parse(in);
86 ASSERT_FALSE(r.IsSuccess());
87 EXPECT_EQ("2: invalid count parameter for REPEAT command: INVALID",
88 r.Error());
89 }
90
TEST_F(AmberScriptParserTest,RepeatFloatNum)91 TEST_F(AmberScriptParserTest, RepeatFloatNum) {
92 std::string in = R"(
93 REPEAT 3.4
94 RUN my_pipeline 1 1 1
95 END
96 )";
97
98 Parser parser;
99 Result r = parser.Parse(in);
100 ASSERT_FALSE(r.IsSuccess());
101 EXPECT_EQ("2: invalid count parameter for REPEAT command: 3.4", r.Error());
102 }
103
TEST_F(AmberScriptParserTest,RepeatMissingEnd)104 TEST_F(AmberScriptParserTest, RepeatMissingEnd) {
105 std::string in = R"(
106 SHADER compute shader GLSL
107 # shader
108 END
109
110 PIPELINE compute my_pipeline
111 ATTACH shader
112 END
113 REPEAT 3
114 RUN my_pipeline 1 1 1
115 )";
116
117 Parser parser;
118 Result r = parser.Parse(in);
119 ASSERT_FALSE(r.IsSuccess());
120 EXPECT_EQ("11: missing END for REPEAT command", r.Error());
121 }
122
TEST_F(AmberScriptParserTest,RepeatExtraParams)123 TEST_F(AmberScriptParserTest, RepeatExtraParams) {
124 std::string in = R"(
125 REPEAT 3 EXTRA
126 RUN my_pipeline 1 1 1
127 END
128 )";
129
130 Parser parser;
131 Result r = parser.Parse(in);
132 ASSERT_FALSE(r.IsSuccess());
133 EXPECT_EQ("2: unknown token: EXTRA", r.Error());
134 }
135
TEST_F(AmberScriptParserTest,RepeatNegativeCount)136 TEST_F(AmberScriptParserTest, RepeatNegativeCount) {
137 std::string in = R"(
138 REPEAT -3
139 RUN my_pipeline 1 1 1
140 END
141 )";
142
143 Parser parser;
144 Result r = parser.Parse(in);
145 ASSERT_FALSE(r.IsSuccess());
146 EXPECT_EQ("2: count parameter must be > 0 for REPEAT command", r.Error());
147 }
148
TEST_F(AmberScriptParserTest,RepeatZeroCount)149 TEST_F(AmberScriptParserTest, RepeatZeroCount) {
150 std::string in = R"(
151 REPEAT 0
152 RUN my_pipeline 1 1 1
153 END
154 )";
155
156 Parser parser;
157 Result r = parser.Parse(in);
158 ASSERT_FALSE(r.IsSuccess());
159 EXPECT_EQ("2: count parameter must be > 0 for REPEAT command", r.Error());
160 }
161
162 } // namespace amberscript
163 } // namespace amber
164