1 //
2 // Copyright 2012 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 
7 #include "compiler/preprocessor/Input.h"
8 #include "PreprocessorTest.h"
9 #include "compiler/preprocessor/Token.h"
10 
11 namespace angle
12 {
13 
14 class InitTest : public PreprocessorTest
15 {
16   public:
InitTest()17     InitTest() : PreprocessorTest(SH_GLES2_SPEC) {}
18 };
19 
TEST_F(InitTest,ZeroCount)20 TEST_F(InitTest, ZeroCount)
21 {
22     EXPECT_TRUE(mPreprocessor.init(0, nullptr, nullptr));
23 
24     pp::Token token;
25     mPreprocessor.lex(&token);
26     EXPECT_EQ(pp::Token::LAST, token.type);
27 }
28 
TEST_F(InitTest,NullString)29 TEST_F(InitTest, NullString)
30 {
31     EXPECT_FALSE(mPreprocessor.init(1, nullptr, nullptr));
32 }
33 
TEST(InputTest,DefaultConstructor)34 TEST(InputTest, DefaultConstructor)
35 {
36     pp::Input input;
37     EXPECT_EQ(0u, input.count());
38     int lineNo = 0;
39     EXPECT_EQ(0u, input.read(nullptr, 1, &lineNo));
40 }
41 
TEST(InputTest,NullLength)42 TEST(InputTest, NullLength)
43 {
44     const char *str[] = {"foo"};
45     pp::Input input(1, str, nullptr);
46     EXPECT_EQ(3u, input.length(0));
47 }
48 
TEST(InputTest,NegativeLength)49 TEST(InputTest, NegativeLength)
50 {
51     const char *str[] = {"foo"};
52     int length[]      = {-1};
53     pp::Input input(1, str, length);
54     EXPECT_EQ(3u, input.length(0));
55 }
56 
TEST(InputTest,ActualLength)57 TEST(InputTest, ActualLength)
58 {
59     const char *str[] = {"foobar"};
60     int length[]      = {3};
61     pp::Input input(1, str, length);
62     // Note that strlen(str[0]) != length[0].
63     // Even then Input should just accept any non-negative number.
64     EXPECT_EQ(static_cast<size_t>(length[0]), input.length(0));
65 }
66 
TEST(InputTest,String)67 TEST(InputTest, String)
68 {
69     const char *str[] = {"foo"};
70     pp::Input input(1, str, nullptr);
71     EXPECT_STREQ(str[0], input.string(0));
72 }
73 
TEST(InputTest,ReadSingleString)74 TEST(InputTest, ReadSingleString)
75 {
76     int count         = 1;
77     const char *str[] = {"foo"};
78     char buf[4]       = {'\0', '\0', '\0', '\0'};
79 
80     int maxSize = 1;
81     int lineNo  = 0;
82     pp::Input input1(count, str, nullptr);
83     EXPECT_EQ(1u, input1.read(buf, maxSize, &lineNo));
84     EXPECT_EQ('f', buf[0]);
85     EXPECT_EQ(1u, input1.read(buf, maxSize, &lineNo));
86     EXPECT_EQ('o', buf[0]);
87     EXPECT_EQ(1u, input1.read(buf, maxSize, &lineNo));
88     EXPECT_EQ('o', buf[0]);
89     EXPECT_EQ(0u, input1.read(buf, maxSize, &lineNo));
90 
91     maxSize = 2;
92     pp::Input input2(count, str, nullptr);
93     EXPECT_EQ(2u, input2.read(buf, maxSize, &lineNo));
94     EXPECT_STREQ("fo", buf);
95     EXPECT_EQ(1u, input2.read(buf, maxSize, &lineNo));
96     EXPECT_EQ('o', buf[0]);
97     EXPECT_EQ(0u, input2.read(buf, maxSize, &lineNo));
98 
99     maxSize = 3;
100     pp::Input input3(count, str, nullptr);
101     EXPECT_EQ(3u, input3.read(buf, maxSize, &lineNo));
102     EXPECT_STREQ("foo", buf);
103     EXPECT_EQ(0u, input3.read(buf, maxSize, &lineNo));
104 
105     maxSize = 4;
106     pp::Input input4(count, str, nullptr);
107     EXPECT_EQ(3u, input4.read(buf, maxSize, &lineNo));
108     EXPECT_STREQ("foo", buf);
109     EXPECT_EQ(0u, input4.read(buf, maxSize, &lineNo));
110 }
111 
TEST(InputTest,ReadMultipleStrings)112 TEST(InputTest, ReadMultipleStrings)
113 {
114     int count         = 3;
115     const char *str[] = {"f", "o", "o"};
116     char buf[4]       = {'\0', '\0', '\0', '\0'};
117 
118     int maxSize = 1;
119     int lineNo  = 0;
120     pp::Input input1(count, str, nullptr);
121     EXPECT_EQ(1u, input1.read(buf, maxSize, &lineNo));
122     EXPECT_EQ('f', buf[0]);
123     EXPECT_EQ(1u, input1.read(buf, maxSize, &lineNo));
124     EXPECT_EQ('o', buf[0]);
125     EXPECT_EQ(1u, input1.read(buf, maxSize, &lineNo));
126     EXPECT_EQ('o', buf[0]);
127     EXPECT_EQ(0u, input1.read(buf, maxSize, &lineNo));
128 
129     maxSize = 2;
130     pp::Input input2(count, str, nullptr);
131     EXPECT_EQ(2u, input2.read(buf, maxSize, &lineNo));
132     EXPECT_STREQ("fo", buf);
133     EXPECT_EQ(1u, input2.read(buf, maxSize, &lineNo));
134     EXPECT_EQ('o', buf[0]);
135     EXPECT_EQ(0u, input2.read(buf, maxSize, &lineNo));
136 
137     maxSize = 3;
138     pp::Input input3(count, str, nullptr);
139     EXPECT_EQ(3u, input3.read(buf, maxSize, &lineNo));
140     EXPECT_STREQ("foo", buf);
141     EXPECT_EQ(0u, input3.read(buf, maxSize, &lineNo));
142 
143     maxSize = 4;
144     pp::Input input4(count, str, nullptr);
145     EXPECT_EQ(3u, input4.read(buf, maxSize, &lineNo));
146     EXPECT_STREQ("foo", buf);
147     EXPECT_EQ(0u, input4.read(buf, maxSize, &lineNo));
148 }
149 
TEST(InputTest,ReadStringsWithLength)150 TEST(InputTest, ReadStringsWithLength)
151 {
152     int count         = 2;
153     const char *str[] = {"foo", "bar"};
154     // Note that the length for the first string is 2 which is less than
155     // strlen(str[0]. We want to make sure that the last character is ignored.
156     int length[]   = {2, 3};
157     char buf[6]    = {'\0', '\0', '\0', '\0', '\0', '\0'};
158     size_t maxSize = 5;
159     int lineNo     = 0;
160 
161     pp::Input input(count, str, length);
162     EXPECT_EQ(maxSize, input.read(buf, maxSize, &lineNo));
163     EXPECT_STREQ("fobar", buf);
164 }
165 
TEST(InputTest,ReadStringsWithLineContinuation)166 TEST(InputTest, ReadStringsWithLineContinuation)
167 {
168     int count         = 2;
169     const char *str[] = {"foo\\", "\nba\\\r\nr"};
170     int length[]      = {4, 7};
171     char buf[11]      = {'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0'};
172     size_t maxSize    = 11;
173     int lineNo        = 0;
174 
175     pp::Input input(count, str, length);
176     EXPECT_EQ(3u, input.read(buf, maxSize, &lineNo));
177     EXPECT_EQ(0, lineNo);
178     EXPECT_EQ(2u, input.read(buf + 3, maxSize - 3, &lineNo));
179     EXPECT_EQ(1, lineNo);
180     EXPECT_EQ(1u, input.read(buf + 5, maxSize - 5, &lineNo));
181     EXPECT_EQ(2, lineNo);
182     EXPECT_STREQ("foobar", buf);
183 }
184 
185 }  // namespace angle
186