1 //
2 // Copyright 2015 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 // utilities_unittest.cpp: Unit tests for ANGLE's GL utility functions
7
8 #include "gmock/gmock.h"
9 #include "gtest/gtest.h"
10
11 #include "common/utilities.h"
12
13 namespace
14 {
15
16 // Test parsing valid single array indices
TEST(ParseResourceName,ArrayIndex)17 TEST(ParseResourceName, ArrayIndex)
18 {
19 std::vector<unsigned int> indices;
20 EXPECT_EQ("foo", gl::ParseResourceName("foo[123]", &indices));
21 ASSERT_EQ(1u, indices.size());
22 EXPECT_EQ(123u, indices[0]);
23
24 EXPECT_EQ("bar", gl::ParseResourceName("bar[0]", &indices));
25 ASSERT_EQ(1u, indices.size());
26 EXPECT_EQ(0u, indices[0]);
27 }
28
29 // Parsing a negative array index should result in INVALID_INDEX.
TEST(ParseResourceName,NegativeArrayIndex)30 TEST(ParseResourceName, NegativeArrayIndex)
31 {
32 std::vector<unsigned int> indices;
33 EXPECT_EQ("foo", gl::ParseResourceName("foo[-1]", &indices));
34 ASSERT_EQ(1u, indices.size());
35 EXPECT_EQ(GL_INVALID_INDEX, indices.back());
36 }
37
38 // Parsing no array indices should result in an empty array.
TEST(ParseResourceName,NoArrayIndex)39 TEST(ParseResourceName, NoArrayIndex)
40 {
41 std::vector<unsigned int> indices;
42 EXPECT_EQ("foo", gl::ParseResourceName("foo", &indices));
43 EXPECT_TRUE(indices.empty());
44 }
45
46 // The ParseResourceName function should work when a nullptr is passed as the indices output vector.
TEST(ParseResourceName,NULLArrayIndices)47 TEST(ParseResourceName, NULLArrayIndices)
48 {
49 EXPECT_EQ("foo", gl::ParseResourceName("foo[10]", nullptr));
50 }
51
52 // Parsing multiple array indices should result in outermost array indices being last in the vector.
TEST(ParseResourceName,MultipleArrayIndices)53 TEST(ParseResourceName, MultipleArrayIndices)
54 {
55 std::vector<unsigned int> indices;
56 EXPECT_EQ("foo", gl::ParseResourceName("foo[12][34][56]", &indices));
57 ASSERT_EQ(3u, indices.size());
58 // Indices are sorted with outermost array index last.
59 EXPECT_EQ(56u, indices[0]);
60 EXPECT_EQ(34u, indices[1]);
61 EXPECT_EQ(12u, indices[2]);
62 }
63
64 // Trailing whitespace should not be accepted by ParseResourceName.
TEST(ParseResourceName,TrailingWhitespace)65 TEST(ParseResourceName, TrailingWhitespace)
66 {
67 std::vector<unsigned int> indices;
68 EXPECT_EQ("foo ", gl::ParseResourceName("foo ", &indices));
69 EXPECT_TRUE(indices.empty());
70
71 EXPECT_EQ("foo[10] ", gl::ParseResourceName("foo[10] ", &indices));
72 EXPECT_TRUE(indices.empty());
73
74 EXPECT_EQ("foo[10][20] ", gl::ParseResourceName("foo[10][20] ", &indices));
75 EXPECT_TRUE(indices.empty());
76 }
77
78 // Parse a string without any index.
TEST(ParseArrayIndex,NoArrayIndex)79 TEST(ParseArrayIndex, NoArrayIndex)
80 {
81 size_t nameLengthWithoutArrayIndex;
82 EXPECT_EQ(GL_INVALID_INDEX, gl::ParseArrayIndex("foo", &nameLengthWithoutArrayIndex));
83 EXPECT_EQ(3u, nameLengthWithoutArrayIndex);
84 }
85
86 // Parse an empty string for an array index.
TEST(ParseArrayIndex,EmptyString)87 TEST(ParseArrayIndex, EmptyString)
88 {
89 size_t nameLengthWithoutArrayIndex;
90 EXPECT_EQ(GL_INVALID_INDEX, gl::ParseArrayIndex("", &nameLengthWithoutArrayIndex));
91 EXPECT_EQ(0u, nameLengthWithoutArrayIndex);
92 }
93
94 // A valid array index is parsed correctly from the end of the string.
TEST(ParseArrayIndex,ArrayIndex)95 TEST(ParseArrayIndex, ArrayIndex)
96 {
97 size_t nameLengthWithoutArrayIndex;
98 EXPECT_EQ(123u, gl::ParseArrayIndex("foo[123]", &nameLengthWithoutArrayIndex));
99 EXPECT_EQ(3u, nameLengthWithoutArrayIndex);
100 }
101
102 // An array index from the middle of the string is not parsed.
TEST(ParseArrayIndex,ArrayIndexInMiddle)103 TEST(ParseArrayIndex, ArrayIndexInMiddle)
104 {
105 size_t nameLengthWithoutArrayIndex;
106 EXPECT_EQ(GL_INVALID_INDEX, gl::ParseArrayIndex("foo[123].bar", &nameLengthWithoutArrayIndex));
107 EXPECT_EQ(12u, nameLengthWithoutArrayIndex);
108 }
109
110 // Trailing whitespace in the parsed string is taken into account.
TEST(ParseArrayIndex,TrailingWhitespace)111 TEST(ParseArrayIndex, TrailingWhitespace)
112 {
113 size_t nameLengthWithoutArrayIndex;
114 EXPECT_EQ(GL_INVALID_INDEX, gl::ParseArrayIndex("foo[123] ", &nameLengthWithoutArrayIndex));
115 EXPECT_EQ(9u, nameLengthWithoutArrayIndex);
116 }
117
118 // Only the last index is parsed.
TEST(ParseArrayIndex,MultipleArrayIndices)119 TEST(ParseArrayIndex, MultipleArrayIndices)
120 {
121 size_t nameLengthWithoutArrayIndex;
122 EXPECT_EQ(34u, gl::ParseArrayIndex("foo[12][34]", &nameLengthWithoutArrayIndex));
123 EXPECT_EQ(7u, nameLengthWithoutArrayIndex);
124 }
125
126 // GetProgramResourceLocation spec in GLES 3.1 November 2016 page 87 mentions "decimal" integer.
127 // So an integer in hexadecimal format should not parse as an array index.
TEST(ParseArrayIndex,HexArrayIndex)128 TEST(ParseArrayIndex, HexArrayIndex)
129 {
130 size_t nameLengthWithoutArrayIndex;
131 EXPECT_EQ(GL_INVALID_INDEX, gl::ParseArrayIndex("foo[0xff]", &nameLengthWithoutArrayIndex));
132 EXPECT_EQ(9u, nameLengthWithoutArrayIndex);
133 }
134
135 // GetProgramResourceLocation spec in GLES 3.1 November 2016 page 87 mentions that the array
136 // index should not contain a leading plus sign.
TEST(ParseArrayIndex,ArrayIndexLeadingPlus)137 TEST(ParseArrayIndex, ArrayIndexLeadingPlus)
138 {
139 size_t nameLengthWithoutArrayIndex;
140 EXPECT_EQ(GL_INVALID_INDEX, gl::ParseArrayIndex("foo[+1]", &nameLengthWithoutArrayIndex));
141 EXPECT_EQ(7u, nameLengthWithoutArrayIndex);
142 }
143
144 // GetProgramResourceLocation spec in GLES 3.1 November 2016 page 87 says that index should not
145 // contain whitespace. Test leading whitespace.
TEST(ParseArrayIndex,ArrayIndexLeadingWhiteSpace)146 TEST(ParseArrayIndex, ArrayIndexLeadingWhiteSpace)
147 {
148 size_t nameLengthWithoutArrayIndex;
149 EXPECT_EQ(GL_INVALID_INDEX, gl::ParseArrayIndex("foo[ 0]", &nameLengthWithoutArrayIndex));
150 EXPECT_EQ(7u, nameLengthWithoutArrayIndex);
151 }
152
153 // GetProgramResourceLocation spec in GLES 3.1 November 2016 page 87 says that index should not
154 // contain whitespace. Test trailing whitespace.
TEST(ParseArrayIndex,ArrayIndexTrailingWhiteSpace)155 TEST(ParseArrayIndex, ArrayIndexTrailingWhiteSpace)
156 {
157 size_t nameLengthWithoutArrayIndex;
158 EXPECT_EQ(GL_INVALID_INDEX, gl::ParseArrayIndex("foo[0 ]", &nameLengthWithoutArrayIndex));
159 EXPECT_EQ(7u, nameLengthWithoutArrayIndex);
160 }
161
162 // GetProgramResourceLocation spec in GLES 3.1 November 2016 page 87 says that index should only
163 // contain an integer.
TEST(ParseArrayIndex,ArrayIndexBogus)164 TEST(ParseArrayIndex, ArrayIndexBogus)
165 {
166 size_t nameLengthWithoutArrayIndex;
167 EXPECT_EQ(GL_INVALID_INDEX, gl::ParseArrayIndex("foo[0bogus]", &nameLengthWithoutArrayIndex));
168 EXPECT_EQ(11u, nameLengthWithoutArrayIndex);
169 }
170
171 // Verify that using an index value out-of-range fails.
TEST(ParseArrayIndex,ArrayIndexOutOfRange)172 TEST(ParseArrayIndex, ArrayIndexOutOfRange)
173 {
174 size_t nameLengthWithoutArrayIndex;
175 EXPECT_EQ(GL_INVALID_INDEX,
176 gl::ParseArrayIndex("foo[4294967296]", &nameLengthWithoutArrayIndex));
177 EXPECT_EQ(15u, nameLengthWithoutArrayIndex);
178 }
179
180 } // anonymous namespace
181