text_word_get_test.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. // Copyright (c) 2015-2016 The Khronos Group Inc.
  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. #include <string>
  15. #include "test/unit_spirv.h"
  16. namespace spvtools {
  17. namespace {
  18. using spvtest::AutoText;
  19. #define TAB "\t"
  20. #define NEWLINE "\n"
  21. #define BACKSLASH R"(\)"
  22. #define QUOTE R"(")"
  23. TEST(TextWordGet, NullTerminator) {
  24. std::string word;
  25. spv_position_t endPosition = {};
  26. ASSERT_EQ(
  27. SPV_SUCCESS,
  28. AssemblyContext(AutoText("Word"), nullptr).getWord(&word, &endPosition));
  29. ASSERT_EQ(4u, endPosition.column);
  30. ASSERT_EQ(0u, endPosition.line);
  31. ASSERT_EQ(4u, endPosition.index);
  32. ASSERT_STREQ("Word", word.c_str());
  33. }
  34. TEST(TextWordGet, TabTerminator) {
  35. std::string word;
  36. spv_position_t endPosition = {};
  37. ASSERT_EQ(SPV_SUCCESS, AssemblyContext(AutoText("Word\t"), nullptr)
  38. .getWord(&word, &endPosition));
  39. ASSERT_EQ(4u, endPosition.column);
  40. ASSERT_EQ(0u, endPosition.line);
  41. ASSERT_EQ(4u, endPosition.index);
  42. ASSERT_STREQ("Word", word.c_str());
  43. }
  44. TEST(TextWordGet, SpaceTerminator) {
  45. std::string word;
  46. spv_position_t endPosition = {};
  47. ASSERT_EQ(
  48. SPV_SUCCESS,
  49. AssemblyContext(AutoText("Word "), nullptr).getWord(&word, &endPosition));
  50. ASSERT_EQ(4u, endPosition.column);
  51. ASSERT_EQ(0u, endPosition.line);
  52. ASSERT_EQ(4u, endPosition.index);
  53. ASSERT_STREQ("Word", word.c_str());
  54. }
  55. TEST(TextWordGet, SemicolonTerminator) {
  56. std::string word;
  57. spv_position_t endPosition = {};
  58. ASSERT_EQ(
  59. SPV_SUCCESS,
  60. AssemblyContext(AutoText("Wo;rd"), nullptr).getWord(&word, &endPosition));
  61. ASSERT_EQ(2u, endPosition.column);
  62. ASSERT_EQ(0u, endPosition.line);
  63. ASSERT_EQ(2u, endPosition.index);
  64. ASSERT_STREQ("Wo", word.c_str());
  65. }
  66. TEST(TextWordGet, NoTerminator) {
  67. const std::string full_text = "abcdefghijklmn";
  68. for (size_t len = 1; len <= full_text.size(); ++len) {
  69. std::string word;
  70. spv_text_t text = {full_text.data(), len};
  71. spv_position_t endPosition = {};
  72. ASSERT_EQ(SPV_SUCCESS,
  73. AssemblyContext(&text, nullptr).getWord(&word, &endPosition));
  74. ASSERT_EQ(0u, endPosition.line);
  75. ASSERT_EQ(len, endPosition.column);
  76. ASSERT_EQ(len, endPosition.index);
  77. ASSERT_EQ(full_text.substr(0, len), word);
  78. }
  79. }
  80. TEST(TextWordGet, MultipleWords) {
  81. AutoText input("Words in a sentence");
  82. AssemblyContext data(input, nullptr);
  83. spv_position_t endPosition = {};
  84. const char* words[] = {"Words", "in", "a", "sentence"};
  85. std::string word;
  86. for (uint32_t wordIndex = 0; wordIndex < 4; ++wordIndex) {
  87. ASSERT_EQ(SPV_SUCCESS, data.getWord(&word, &endPosition));
  88. ASSERT_EQ(strlen(words[wordIndex]),
  89. endPosition.column - data.position().column);
  90. ASSERT_EQ(0u, endPosition.line);
  91. ASSERT_EQ(strlen(words[wordIndex]),
  92. endPosition.index - data.position().index);
  93. ASSERT_STREQ(words[wordIndex], word.c_str());
  94. data.setPosition(endPosition);
  95. if (3 != wordIndex) {
  96. ASSERT_EQ(SPV_SUCCESS, data.advance());
  97. } else {
  98. ASSERT_EQ(SPV_END_OF_STREAM, data.advance());
  99. }
  100. }
  101. }
  102. TEST(TextWordGet, QuotesAreKept) {
  103. AutoText input(R"("quotes" "around words")");
  104. const char* expected[] = {R"("quotes")", R"("around words")"};
  105. AssemblyContext data(input, nullptr);
  106. std::string word;
  107. spv_position_t endPosition = {};
  108. ASSERT_EQ(SPV_SUCCESS, data.getWord(&word, &endPosition));
  109. EXPECT_EQ(8u, endPosition.column);
  110. EXPECT_EQ(0u, endPosition.line);
  111. EXPECT_EQ(8u, endPosition.index);
  112. EXPECT_STREQ(expected[0], word.c_str());
  113. // Move to the next word.
  114. data.setPosition(endPosition);
  115. data.seekForward(1);
  116. ASSERT_EQ(SPV_SUCCESS, data.getWord(&word, &endPosition));
  117. EXPECT_EQ(23u, endPosition.column);
  118. EXPECT_EQ(0u, endPosition.line);
  119. EXPECT_EQ(23u, endPosition.index);
  120. EXPECT_STREQ(expected[1], word.c_str());
  121. }
  122. TEST(TextWordGet, QuotesBetweenWordsActLikeGlue) {
  123. AutoText input(R"(quotes" "between words)");
  124. const char* expected[] = {R"(quotes" "between)", "words"};
  125. AssemblyContext data(input, nullptr);
  126. std::string word;
  127. spv_position_t endPosition = {};
  128. ASSERT_EQ(SPV_SUCCESS, data.getWord(&word, &endPosition));
  129. EXPECT_EQ(16u, endPosition.column);
  130. EXPECT_EQ(0u, endPosition.line);
  131. EXPECT_EQ(16u, endPosition.index);
  132. EXPECT_STREQ(expected[0], word.c_str());
  133. // Move to the next word.
  134. data.setPosition(endPosition);
  135. data.seekForward(1);
  136. ASSERT_EQ(SPV_SUCCESS, data.getWord(&word, &endPosition));
  137. EXPECT_EQ(22u, endPosition.column);
  138. EXPECT_EQ(0u, endPosition.line);
  139. EXPECT_EQ(22u, endPosition.index);
  140. EXPECT_STREQ(expected[1], word.c_str());
  141. }
  142. TEST(TextWordGet, QuotingWhitespace) {
  143. AutoText input(QUOTE "white " NEWLINE TAB " space" QUOTE);
  144. // Whitespace surrounded by quotes acts like glue.
  145. std::string word;
  146. spv_position_t endPosition = {};
  147. ASSERT_EQ(SPV_SUCCESS,
  148. AssemblyContext(input, nullptr).getWord(&word, &endPosition));
  149. EXPECT_EQ(input.str.length(), endPosition.column);
  150. EXPECT_EQ(0u, endPosition.line);
  151. EXPECT_EQ(input.str.length(), endPosition.index);
  152. EXPECT_EQ(input.str, word);
  153. }
  154. TEST(TextWordGet, QuoteAlone) {
  155. AutoText input(QUOTE);
  156. std::string word;
  157. spv_position_t endPosition = {};
  158. ASSERT_EQ(SPV_SUCCESS,
  159. AssemblyContext(input, nullptr).getWord(&word, &endPosition));
  160. ASSERT_EQ(1u, endPosition.column);
  161. ASSERT_EQ(0u, endPosition.line);
  162. ASSERT_EQ(1u, endPosition.index);
  163. ASSERT_STREQ(QUOTE, word.c_str());
  164. }
  165. TEST(TextWordGet, EscapeAlone) {
  166. AutoText input(BACKSLASH);
  167. std::string word;
  168. spv_position_t endPosition = {};
  169. ASSERT_EQ(SPV_SUCCESS,
  170. AssemblyContext(input, nullptr).getWord(&word, &endPosition));
  171. ASSERT_EQ(1u, endPosition.column);
  172. ASSERT_EQ(0u, endPosition.line);
  173. ASSERT_EQ(1u, endPosition.index);
  174. ASSERT_STREQ(BACKSLASH, word.c_str());
  175. }
  176. TEST(TextWordGet, EscapeAtEndOfInput) {
  177. AutoText input("word" BACKSLASH);
  178. std::string word;
  179. spv_position_t endPosition = {};
  180. ASSERT_EQ(SPV_SUCCESS,
  181. AssemblyContext(input, nullptr).getWord(&word, &endPosition));
  182. ASSERT_EQ(5u, endPosition.column);
  183. ASSERT_EQ(0u, endPosition.line);
  184. ASSERT_EQ(5u, endPosition.index);
  185. ASSERT_STREQ("word" BACKSLASH, word.c_str());
  186. }
  187. TEST(TextWordGet, Escaping) {
  188. AutoText input("w" BACKSLASH QUOTE "o" BACKSLASH NEWLINE "r" BACKSLASH ";d");
  189. std::string word;
  190. spv_position_t endPosition = {};
  191. ASSERT_EQ(SPV_SUCCESS,
  192. AssemblyContext(input, nullptr).getWord(&word, &endPosition));
  193. ASSERT_EQ(10u, endPosition.column);
  194. ASSERT_EQ(0u, endPosition.line);
  195. ASSERT_EQ(10u, endPosition.index);
  196. ASSERT_EQ(input.str, word);
  197. }
  198. TEST(TextWordGet, EscapingEscape) {
  199. AutoText input("word" BACKSLASH BACKSLASH " abc");
  200. std::string word;
  201. spv_position_t endPosition = {};
  202. ASSERT_EQ(SPV_SUCCESS,
  203. AssemblyContext(input, nullptr).getWord(&word, &endPosition));
  204. ASSERT_EQ(6u, endPosition.column);
  205. ASSERT_EQ(0u, endPosition.line);
  206. ASSERT_EQ(6u, endPosition.index);
  207. ASSERT_STREQ("word" BACKSLASH BACKSLASH, word.c_str());
  208. }
  209. TEST(TextWordGet, CRLF) {
  210. AutoText input("abc\r\nd");
  211. AssemblyContext data(input, nullptr);
  212. std::string word;
  213. spv_position_t pos = {};
  214. ASSERT_EQ(SPV_SUCCESS, data.getWord(&word, &pos));
  215. EXPECT_EQ(3u, pos.column);
  216. EXPECT_STREQ("abc", word.c_str());
  217. data.setPosition(pos);
  218. data.advance();
  219. ASSERT_EQ(SPV_SUCCESS, data.getWord(&word, &pos));
  220. EXPECT_EQ(1u, pos.column);
  221. EXPECT_STREQ("d", word.c_str());
  222. }
  223. } // namespace
  224. } // namespace spvtools