2016-01-07 18:44:22 +00:00
|
|
|
// Copyright (c) 2015-2016 The Khronos Group Inc.
|
2015-05-22 17:26:19 +00:00
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2015-05-22 17:26:19 +00:00
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2015-05-22 17:26:19 +00:00
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2015-05-22 17:26:19 +00:00
|
|
|
|
2018-08-03 19:06:09 +00:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "test/unit_spirv.h"
|
2015-05-22 17:26:19 +00:00
|
|
|
|
2018-07-07 13:38:00 +00:00
|
|
|
namespace spvtools {
|
2015-08-31 19:24:32 +00:00
|
|
|
namespace {
|
|
|
|
|
2015-10-01 20:58:17 +00:00
|
|
|
using spvtest::AutoText;
|
2015-09-24 14:26:51 +00:00
|
|
|
|
2015-09-14 14:05:37 +00:00
|
|
|
#define TAB "\t"
|
|
|
|
#define NEWLINE "\n"
|
2015-08-25 18:21:13 +00:00
|
|
|
#define BACKSLASH R"(\)"
|
2015-09-14 14:05:37 +00:00
|
|
|
#define QUOTE R"(")"
|
2015-08-25 18:21:13 +00:00
|
|
|
|
2015-05-22 17:26:19 +00:00
|
|
|
TEST(TextWordGet, NullTerminator) {
|
|
|
|
std::string word;
|
|
|
|
spv_position_t endPosition = {};
|
2016-03-15 20:52:40 +00:00
|
|
|
ASSERT_EQ(
|
|
|
|
SPV_SUCCESS,
|
|
|
|
AssemblyContext(AutoText("Word"), nullptr).getWord(&word, &endPosition));
|
2015-11-18 14:22:10 +00:00
|
|
|
ASSERT_EQ(4u, endPosition.column);
|
|
|
|
ASSERT_EQ(0u, endPosition.line);
|
|
|
|
ASSERT_EQ(4u, endPosition.index);
|
2015-05-22 17:26:19 +00:00
|
|
|
ASSERT_STREQ("Word", word.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TextWordGet, TabTerminator) {
|
|
|
|
std::string word;
|
|
|
|
spv_position_t endPosition = {};
|
2015-09-24 14:26:51 +00:00
|
|
|
ASSERT_EQ(SPV_SUCCESS, AssemblyContext(AutoText("Word\t"), nullptr)
|
2016-03-15 20:52:40 +00:00
|
|
|
.getWord(&word, &endPosition));
|
2015-11-18 14:22:10 +00:00
|
|
|
ASSERT_EQ(4u, endPosition.column);
|
|
|
|
ASSERT_EQ(0u, endPosition.line);
|
|
|
|
ASSERT_EQ(4u, endPosition.index);
|
2015-05-22 17:26:19 +00:00
|
|
|
ASSERT_STREQ("Word", word.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TextWordGet, SpaceTerminator) {
|
|
|
|
std::string word;
|
|
|
|
spv_position_t endPosition = {};
|
2016-03-15 20:52:40 +00:00
|
|
|
ASSERT_EQ(
|
|
|
|
SPV_SUCCESS,
|
|
|
|
AssemblyContext(AutoText("Word "), nullptr).getWord(&word, &endPosition));
|
2015-11-18 14:22:10 +00:00
|
|
|
ASSERT_EQ(4u, endPosition.column);
|
|
|
|
ASSERT_EQ(0u, endPosition.line);
|
|
|
|
ASSERT_EQ(4u, endPosition.index);
|
2015-05-22 17:26:19 +00:00
|
|
|
ASSERT_STREQ("Word", word.c_str());
|
|
|
|
}
|
|
|
|
|
2015-08-25 17:53:19 +00:00
|
|
|
TEST(TextWordGet, SemicolonTerminator) {
|
|
|
|
std::string word;
|
|
|
|
spv_position_t endPosition = {};
|
2016-03-15 20:52:40 +00:00
|
|
|
ASSERT_EQ(
|
|
|
|
SPV_SUCCESS,
|
|
|
|
AssemblyContext(AutoText("Wo;rd"), nullptr).getWord(&word, &endPosition));
|
2015-11-18 14:22:10 +00:00
|
|
|
ASSERT_EQ(2u, endPosition.column);
|
|
|
|
ASSERT_EQ(0u, endPosition.line);
|
|
|
|
ASSERT_EQ(2u, endPosition.index);
|
2015-08-25 17:53:19 +00:00
|
|
|
ASSERT_STREQ("Wo", word.c_str());
|
|
|
|
}
|
|
|
|
|
2016-02-22 22:17:25 +00:00
|
|
|
TEST(TextWordGet, NoTerminator) {
|
|
|
|
const std::string full_text = "abcdefghijklmn";
|
|
|
|
for (size_t len = 1; len <= full_text.size(); ++len) {
|
|
|
|
std::string word;
|
|
|
|
spv_text_t text = {full_text.data(), len};
|
|
|
|
spv_position_t endPosition = {};
|
|
|
|
ASSERT_EQ(SPV_SUCCESS,
|
2016-03-15 20:52:40 +00:00
|
|
|
AssemblyContext(&text, nullptr).getWord(&word, &endPosition));
|
2016-02-22 22:17:25 +00:00
|
|
|
ASSERT_EQ(0u, endPosition.line);
|
|
|
|
ASSERT_EQ(len, endPosition.column);
|
|
|
|
ASSERT_EQ(len, endPosition.index);
|
|
|
|
ASSERT_EQ(full_text.substr(0, len), word);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-22 17:26:19 +00:00
|
|
|
TEST(TextWordGet, MultipleWords) {
|
2015-09-24 14:26:51 +00:00
|
|
|
AutoText input("Words in a sentence");
|
|
|
|
AssemblyContext data(input, nullptr);
|
2015-05-22 17:26:19 +00:00
|
|
|
|
|
|
|
spv_position_t endPosition = {};
|
2015-11-02 14:41:20 +00:00
|
|
|
const char* words[] = {"Words", "in", "a", "sentence"};
|
2015-05-22 17:26:19 +00:00
|
|
|
|
|
|
|
std::string word;
|
|
|
|
for (uint32_t wordIndex = 0; wordIndex < 4; ++wordIndex) {
|
2016-03-15 20:52:40 +00:00
|
|
|
ASSERT_EQ(SPV_SUCCESS, data.getWord(&word, &endPosition));
|
2015-05-22 17:26:19 +00:00
|
|
|
ASSERT_EQ(strlen(words[wordIndex]),
|
2015-09-24 14:26:51 +00:00
|
|
|
endPosition.column - data.position().column);
|
2015-11-18 14:22:10 +00:00
|
|
|
ASSERT_EQ(0u, endPosition.line);
|
2015-05-22 17:26:19 +00:00
|
|
|
ASSERT_EQ(strlen(words[wordIndex]),
|
2015-09-24 14:26:51 +00:00
|
|
|
endPosition.index - data.position().index);
|
2015-05-22 17:26:19 +00:00
|
|
|
ASSERT_STREQ(words[wordIndex], word.c_str());
|
|
|
|
|
2015-09-24 14:26:51 +00:00
|
|
|
data.setPosition(endPosition);
|
2015-05-22 17:26:19 +00:00
|
|
|
if (3 != wordIndex) {
|
2015-09-24 14:26:51 +00:00
|
|
|
ASSERT_EQ(SPV_SUCCESS, data.advance());
|
2015-05-22 17:26:19 +00:00
|
|
|
} else {
|
2015-09-24 14:26:51 +00:00
|
|
|
ASSERT_EQ(SPV_END_OF_STREAM, data.advance());
|
2015-05-22 17:26:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-08-31 19:24:32 +00:00
|
|
|
|
2015-08-25 18:21:13 +00:00
|
|
|
TEST(TextWordGet, QuotesAreKept) {
|
|
|
|
AutoText input(R"("quotes" "around words")");
|
2015-11-02 14:41:20 +00:00
|
|
|
const char* expected[] = {R"("quotes")", R"("around words")"};
|
2015-09-24 14:26:51 +00:00
|
|
|
AssemblyContext data(input, nullptr);
|
2015-08-25 18:21:13 +00:00
|
|
|
|
|
|
|
std::string word;
|
|
|
|
spv_position_t endPosition = {};
|
2016-03-15 20:52:40 +00:00
|
|
|
ASSERT_EQ(SPV_SUCCESS, data.getWord(&word, &endPosition));
|
2015-11-18 14:22:10 +00:00
|
|
|
EXPECT_EQ(8u, endPosition.column);
|
|
|
|
EXPECT_EQ(0u, endPosition.line);
|
|
|
|
EXPECT_EQ(8u, endPosition.index);
|
2015-08-25 18:21:13 +00:00
|
|
|
EXPECT_STREQ(expected[0], word.c_str());
|
|
|
|
|
|
|
|
// Move to the next word.
|
2015-09-24 14:26:51 +00:00
|
|
|
data.setPosition(endPosition);
|
|
|
|
data.seekForward(1);
|
2015-08-25 18:21:13 +00:00
|
|
|
|
2016-03-15 20:52:40 +00:00
|
|
|
ASSERT_EQ(SPV_SUCCESS, data.getWord(&word, &endPosition));
|
2015-11-18 14:22:10 +00:00
|
|
|
EXPECT_EQ(23u, endPosition.column);
|
|
|
|
EXPECT_EQ(0u, endPosition.line);
|
|
|
|
EXPECT_EQ(23u, endPosition.index);
|
2015-08-25 18:21:13 +00:00
|
|
|
EXPECT_STREQ(expected[1], word.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TextWordGet, QuotesBetweenWordsActLikeGlue) {
|
|
|
|
AutoText input(R"(quotes" "between words)");
|
2015-11-02 14:41:20 +00:00
|
|
|
const char* expected[] = {R"(quotes" "between)", "words"};
|
2015-09-24 14:26:51 +00:00
|
|
|
AssemblyContext data(input, nullptr);
|
2015-08-25 18:21:13 +00:00
|
|
|
|
|
|
|
std::string word;
|
|
|
|
spv_position_t endPosition = {};
|
2016-03-15 20:52:40 +00:00
|
|
|
ASSERT_EQ(SPV_SUCCESS, data.getWord(&word, &endPosition));
|
2015-11-18 14:22:10 +00:00
|
|
|
EXPECT_EQ(16u, endPosition.column);
|
|
|
|
EXPECT_EQ(0u, endPosition.line);
|
|
|
|
EXPECT_EQ(16u, endPosition.index);
|
2015-08-25 18:21:13 +00:00
|
|
|
EXPECT_STREQ(expected[0], word.c_str());
|
|
|
|
|
|
|
|
// Move to the next word.
|
2015-09-24 14:26:51 +00:00
|
|
|
data.setPosition(endPosition);
|
|
|
|
data.seekForward(1);
|
2015-08-25 18:21:13 +00:00
|
|
|
|
2016-03-15 20:52:40 +00:00
|
|
|
ASSERT_EQ(SPV_SUCCESS, data.getWord(&word, &endPosition));
|
2015-11-18 14:22:10 +00:00
|
|
|
EXPECT_EQ(22u, endPosition.column);
|
|
|
|
EXPECT_EQ(0u, endPosition.line);
|
|
|
|
EXPECT_EQ(22u, endPosition.index);
|
2015-08-25 18:21:13 +00:00
|
|
|
EXPECT_STREQ(expected[1], word.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TextWordGet, QuotingWhitespace) {
|
|
|
|
AutoText input(QUOTE "white " NEWLINE TAB " space" QUOTE);
|
2015-09-24 14:26:51 +00:00
|
|
|
// Whitespace surrounded by quotes acts like glue.
|
2015-08-25 18:21:13 +00:00
|
|
|
std::string word;
|
|
|
|
spv_position_t endPosition = {};
|
|
|
|
ASSERT_EQ(SPV_SUCCESS,
|
2016-03-15 20:52:40 +00:00
|
|
|
AssemblyContext(input, nullptr).getWord(&word, &endPosition));
|
2015-08-25 18:21:13 +00:00
|
|
|
EXPECT_EQ(input.str.length(), endPosition.column);
|
2015-11-18 14:22:10 +00:00
|
|
|
EXPECT_EQ(0u, endPosition.line);
|
2015-08-25 18:21:13 +00:00
|
|
|
EXPECT_EQ(input.str.length(), endPosition.index);
|
|
|
|
EXPECT_EQ(input.str, word);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TextWordGet, QuoteAlone) {
|
|
|
|
AutoText input(QUOTE);
|
|
|
|
std::string word;
|
|
|
|
spv_position_t endPosition = {};
|
|
|
|
ASSERT_EQ(SPV_SUCCESS,
|
2016-03-15 20:52:40 +00:00
|
|
|
AssemblyContext(input, nullptr).getWord(&word, &endPosition));
|
2015-11-18 14:22:10 +00:00
|
|
|
ASSERT_EQ(1u, endPosition.column);
|
|
|
|
ASSERT_EQ(0u, endPosition.line);
|
|
|
|
ASSERT_EQ(1u, endPosition.index);
|
2015-08-25 18:21:13 +00:00
|
|
|
ASSERT_STREQ(QUOTE, word.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TextWordGet, EscapeAlone) {
|
|
|
|
AutoText input(BACKSLASH);
|
|
|
|
std::string word;
|
|
|
|
spv_position_t endPosition = {};
|
|
|
|
ASSERT_EQ(SPV_SUCCESS,
|
2016-03-15 20:52:40 +00:00
|
|
|
AssemblyContext(input, nullptr).getWord(&word, &endPosition));
|
2015-11-18 14:22:10 +00:00
|
|
|
ASSERT_EQ(1u, endPosition.column);
|
|
|
|
ASSERT_EQ(0u, endPosition.line);
|
|
|
|
ASSERT_EQ(1u, endPosition.index);
|
2015-08-25 18:21:13 +00:00
|
|
|
ASSERT_STREQ(BACKSLASH, word.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TextWordGet, EscapeAtEndOfInput) {
|
|
|
|
AutoText input("word" BACKSLASH);
|
|
|
|
std::string word;
|
|
|
|
spv_position_t endPosition = {};
|
|
|
|
ASSERT_EQ(SPV_SUCCESS,
|
2016-03-15 20:52:40 +00:00
|
|
|
AssemblyContext(input, nullptr).getWord(&word, &endPosition));
|
2015-11-18 14:22:10 +00:00
|
|
|
ASSERT_EQ(5u, endPosition.column);
|
|
|
|
ASSERT_EQ(0u, endPosition.line);
|
|
|
|
ASSERT_EQ(5u, endPosition.index);
|
2015-08-25 18:21:13 +00:00
|
|
|
ASSERT_STREQ("word" BACKSLASH, word.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TextWordGet, Escaping) {
|
|
|
|
AutoText input("w" BACKSLASH QUOTE "o" BACKSLASH NEWLINE "r" BACKSLASH ";d");
|
|
|
|
std::string word;
|
|
|
|
spv_position_t endPosition = {};
|
|
|
|
ASSERT_EQ(SPV_SUCCESS,
|
2016-03-15 20:52:40 +00:00
|
|
|
AssemblyContext(input, nullptr).getWord(&word, &endPosition));
|
2015-11-18 14:22:10 +00:00
|
|
|
ASSERT_EQ(10u, endPosition.column);
|
|
|
|
ASSERT_EQ(0u, endPosition.line);
|
|
|
|
ASSERT_EQ(10u, endPosition.index);
|
2015-08-25 18:21:13 +00:00
|
|
|
ASSERT_EQ(input.str, word);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TextWordGet, EscapingEscape) {
|
|
|
|
AutoText input("word" BACKSLASH BACKSLASH " abc");
|
|
|
|
std::string word;
|
|
|
|
spv_position_t endPosition = {};
|
|
|
|
ASSERT_EQ(SPV_SUCCESS,
|
2016-03-15 20:52:40 +00:00
|
|
|
AssemblyContext(input, nullptr).getWord(&word, &endPosition));
|
2015-11-18 14:22:10 +00:00
|
|
|
ASSERT_EQ(6u, endPosition.column);
|
|
|
|
ASSERT_EQ(0u, endPosition.line);
|
|
|
|
ASSERT_EQ(6u, endPosition.index);
|
2015-08-25 18:21:13 +00:00
|
|
|
ASSERT_STREQ("word" BACKSLASH BACKSLASH, word.c_str());
|
|
|
|
}
|
|
|
|
|
2016-04-01 04:47:02 +00:00
|
|
|
TEST(TextWordGet, CRLF) {
|
|
|
|
AutoText input("abc\r\nd");
|
|
|
|
AssemblyContext data(input, nullptr);
|
|
|
|
std::string word;
|
|
|
|
spv_position_t pos = {};
|
|
|
|
ASSERT_EQ(SPV_SUCCESS, data.getWord(&word, &pos));
|
|
|
|
EXPECT_EQ(3u, pos.column);
|
|
|
|
EXPECT_STREQ("abc", word.c_str());
|
|
|
|
data.setPosition(pos);
|
|
|
|
data.advance();
|
|
|
|
ASSERT_EQ(SPV_SUCCESS, data.getWord(&word, &pos));
|
|
|
|
EXPECT_EQ(1u, pos.column);
|
|
|
|
EXPECT_STREQ("d", word.c_str());
|
|
|
|
}
|
|
|
|
|
2018-07-07 13:38:00 +00:00
|
|
|
} // namespace
|
|
|
|
} // namespace spvtools
|