1 // 2 // Copyright (c) 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 "PreprocessorTest.h" 8 #include "Input.h" 9 #include "Token.h" 10 11 class InitTest : public PreprocessorTest 12 { 13 }; 14 15 TEST_F(InitTest, NegativeCount) 16 { 17 EXPECT_FALSE(mPreprocessor.init(-1, NULL, NULL)); 18 } 19 20 TEST_F(InitTest, ZeroCount) 21 { 22 EXPECT_TRUE(mPreprocessor.init(0, NULL, NULL)); 23 24 pp::Token token; 25 mPreprocessor.lex(&token); 26 EXPECT_EQ(pp::Token::LAST, token.type); 27 } 28 29 TEST_F(InitTest, NullString) 30 { 31 EXPECT_FALSE(mPreprocessor.init(1, NULL, NULL)); 32 } 33 34 TEST(InputTest, DefaultConstructor) 35 { 36 pp::Input input; 37 EXPECT_EQ(0u, input.count()); 38 EXPECT_EQ(0u, input.read(NULL, 1)); 39 } 40 41 TEST(InputTest, NullLength) 42 { 43 const char* str[] = {"foo"}; 44 pp::Input input(1, str, NULL); 45 EXPECT_EQ(3u, input.length(0)); 46 } 47 48 TEST(InputTest, NegativeLength) 49 { 50 const char* str[] = {"foo"}; 51 int length[] = {-1}; 52 pp::Input input(1, str, length); 53 EXPECT_EQ(3u, input.length(0)); 54 } 55 56 TEST(InputTest, ActualLength) 57 { 58 const char* str[] = {"foobar"}; 59 int length[] = {3}; 60 pp::Input input(1, str, length); 61 // Note that strlen(str[0]) != length[0]. 62 // Even then Input should just accept any non-negative number. 63 EXPECT_EQ(static_cast<size_t>(length[0]), input.length(0)); 64 } 65 66 TEST(InputTest, String) 67 { 68 const char* str[] = {"foo"}; 69 pp::Input input(1, str, NULL); 70 EXPECT_STREQ(str[0], input.string(0)); 71 } 72 73 TEST(InputTest, ReadSingleString) 74 { 75 int count = 1; 76 const char* str[] = {"foo"}; 77 char buf[4] = {'\0', '\0', '\0', '\0'}; 78 79 int maxSize = 1; 80 pp::Input input1(count, str, NULL); 81 EXPECT_EQ(1u, input1.read(buf, maxSize)); 82 EXPECT_EQ('f', buf[0]); 83 EXPECT_EQ(1u, input1.read(buf, maxSize)); 84 EXPECT_EQ('o', buf[0]); 85 EXPECT_EQ(1u, input1.read(buf, maxSize)); 86 EXPECT_EQ('o', buf[0]); 87 EXPECT_EQ(0u, input1.read(buf, maxSize)); 88 89 maxSize = 2; 90 pp::Input input2(count, str, NULL); 91 EXPECT_EQ(2u, input2.read(buf, maxSize)); 92 EXPECT_STREQ("fo", buf); 93 EXPECT_EQ(1u, input2.read(buf, maxSize)); 94 EXPECT_EQ('o', buf[0]); 95 EXPECT_EQ(0u, input2.read(buf, maxSize)); 96 97 maxSize = 3; 98 pp::Input input3(count, str, NULL); 99 EXPECT_EQ(3u, input3.read(buf, maxSize)); 100 EXPECT_STREQ("foo", buf); 101 EXPECT_EQ(0u, input3.read(buf, maxSize)); 102 103 maxSize = 4; 104 pp::Input input4(count, str, NULL); 105 EXPECT_EQ(3u, input4.read(buf, maxSize)); 106 EXPECT_STREQ("foo", buf); 107 EXPECT_EQ(0u, input4.read(buf, maxSize)); 108 } 109 110 TEST(InputTest, ReadMultipleStrings) 111 { 112 int count = 3; 113 const char* str[] = {"f", "o", "o"}; 114 char buf[4] = {'\0', '\0', '\0', '\0'}; 115 116 int maxSize = 1; 117 pp::Input input1(count, str, NULL); 118 EXPECT_EQ(1u, input1.read(buf, maxSize)); 119 EXPECT_EQ('f', buf[0]); 120 EXPECT_EQ(1u, input1.read(buf, maxSize)); 121 EXPECT_EQ('o', buf[0]); 122 EXPECT_EQ(1u, input1.read(buf, maxSize)); 123 EXPECT_EQ('o', buf[0]); 124 EXPECT_EQ(0u, input1.read(buf, maxSize)); 125 126 maxSize = 2; 127 pp::Input input2(count, str, NULL); 128 EXPECT_EQ(2u, input2.read(buf, maxSize)); 129 EXPECT_STREQ("fo", buf); 130 EXPECT_EQ(1u, input2.read(buf, maxSize)); 131 EXPECT_EQ('o', buf[0]); 132 EXPECT_EQ(0u, input2.read(buf, maxSize)); 133 134 maxSize = 3; 135 pp::Input input3(count, str, NULL); 136 EXPECT_EQ(3u, input3.read(buf, maxSize)); 137 EXPECT_STREQ("foo", buf); 138 EXPECT_EQ(0u, input3.read(buf, maxSize)); 139 140 maxSize = 4; 141 pp::Input input4(count, str, NULL); 142 EXPECT_EQ(3u, input4.read(buf, maxSize)); 143 EXPECT_STREQ("foo", buf); 144 EXPECT_EQ(0u, input4.read(buf, maxSize)); 145 } 146 147 TEST(InputTest, ReadStringsWithLength) 148 { 149 int count = 2; 150 const char* str[] = {"foo", "bar"}; 151 // Note that the length for the first string is 2 which is less than 152 // strlen(str[0]. We want to make sure that the last character is ignored. 153 int length[] = {2, 3}; 154 char buf[6] = {'\0', '\0', '\0', '\0', '\0', '\0'}; 155 size_t maxSize = 5; 156 157 pp::Input input(count, str, length); 158 EXPECT_EQ(maxSize, input.read(buf, maxSize)); 159 EXPECT_STREQ("fobar", buf); 160 } 161 162