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-05-22 17:26:19 +00:00
|
|
|
TEST(TextAdvance, LeadingNewLines) {
|
2015-09-24 14:26:51 +00:00
|
|
|
AutoText input("\n\nWord");
|
|
|
|
AssemblyContext data(input, nullptr);
|
|
|
|
ASSERT_EQ(SPV_SUCCESS, data.advance());
|
2015-11-18 14:22:10 +00:00
|
|
|
ASSERT_EQ(0u, data.position().column);
|
|
|
|
ASSERT_EQ(2u, data.position().line);
|
|
|
|
ASSERT_EQ(2u, data.position().index);
|
2015-05-22 17:26:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TextAdvance, LeadingSpaces) {
|
2015-09-24 14:26:51 +00:00
|
|
|
AutoText input(" Word");
|
|
|
|
AssemblyContext data(input, nullptr);
|
|
|
|
ASSERT_EQ(SPV_SUCCESS, data.advance());
|
2015-11-18 14:22:10 +00:00
|
|
|
ASSERT_EQ(4u, data.position().column);
|
|
|
|
ASSERT_EQ(0u, data.position().line);
|
|
|
|
ASSERT_EQ(4u, data.position().index);
|
2015-05-22 17:26:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TextAdvance, LeadingTabs) {
|
2015-09-24 14:26:51 +00:00
|
|
|
AutoText input("\t\t\tWord");
|
|
|
|
AssemblyContext data(input, nullptr);
|
|
|
|
ASSERT_EQ(SPV_SUCCESS, data.advance());
|
2015-11-18 14:22:10 +00:00
|
|
|
ASSERT_EQ(3u, data.position().column);
|
|
|
|
ASSERT_EQ(0u, data.position().line);
|
|
|
|
ASSERT_EQ(3u, data.position().index);
|
2015-05-22 17:26:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TextAdvance, LeadingNewLinesSpacesAndTabs) {
|
2015-09-24 14:26:51 +00:00
|
|
|
AutoText input("\n\n\t Word");
|
|
|
|
AssemblyContext data(input, nullptr);
|
|
|
|
ASSERT_EQ(SPV_SUCCESS, data.advance());
|
2015-11-18 14:22:10 +00:00
|
|
|
ASSERT_EQ(3u, data.position().column);
|
|
|
|
ASSERT_EQ(2u, data.position().line);
|
|
|
|
ASSERT_EQ(5u, data.position().index);
|
2015-05-22 17:26:19 +00:00
|
|
|
}
|
|
|
|
|
2015-08-21 15:51:28 +00:00
|
|
|
TEST(TextAdvance, LeadingWhitespaceAfterCommentLine) {
|
2015-09-24 14:26:51 +00:00
|
|
|
AutoText input("; comment\n \t \tWord");
|
|
|
|
AssemblyContext data(input, nullptr);
|
|
|
|
ASSERT_EQ(SPV_SUCCESS, data.advance());
|
2015-11-18 14:22:10 +00:00
|
|
|
ASSERT_EQ(4u, data.position().column);
|
|
|
|
ASSERT_EQ(1u, data.position().line);
|
|
|
|
ASSERT_EQ(14u, data.position().index);
|
2015-08-21 15:51:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TextAdvance, EOFAfterCommentLine) {
|
2015-09-24 14:26:51 +00:00
|
|
|
AutoText input("; comment");
|
|
|
|
AssemblyContext data(input, nullptr);
|
|
|
|
ASSERT_EQ(SPV_END_OF_STREAM, data.advance());
|
2015-08-21 15:51:28 +00:00
|
|
|
}
|
|
|
|
|
2015-05-22 17:26:19 +00:00
|
|
|
TEST(TextAdvance, NullTerminator) {
|
2015-09-24 14:26:51 +00:00
|
|
|
AutoText input("");
|
|
|
|
AssemblyContext data(input, nullptr);
|
|
|
|
ASSERT_EQ(SPV_END_OF_STREAM, data.advance());
|
2015-05-22 17:26:19 +00:00
|
|
|
}
|
2015-08-31 19:24:32 +00:00
|
|
|
|
2016-04-21 19:50:23 +00:00
|
|
|
TEST(TextAdvance, NoNullTerminatorAfterCommentLine) {
|
|
|
|
std::string input = "; comment|padding beyond the end";
|
|
|
|
spv_text_t text = {input.data(), 9};
|
|
|
|
AssemblyContext data(&text, nullptr);
|
|
|
|
ASSERT_EQ(SPV_END_OF_STREAM, data.advance());
|
|
|
|
EXPECT_EQ(9u, data.position().index);
|
|
|
|
}
|
|
|
|
|
2015-11-06 20:09:04 +00:00
|
|
|
TEST(TextAdvance, NoNullTerminator) {
|
|
|
|
spv_text_t text = {"OpNop\nSomething else in memory", 6};
|
|
|
|
AssemblyContext data(&text, nullptr);
|
2015-11-18 14:22:10 +00:00
|
|
|
const spv_position_t line_break = {1u, 5u, 5u};
|
2015-11-06 20:09:04 +00:00
|
|
|
data.setPosition(line_break);
|
|
|
|
ASSERT_EQ(SPV_END_OF_STREAM, data.advance());
|
|
|
|
}
|
|
|
|
|
2016-04-01 04:47:02 +00:00
|
|
|
// Invokes AssemblyContext::advance() on text, asserts success, and returns
|
|
|
|
// AssemblyContext::position().
|
|
|
|
spv_position_t PositionAfterAdvance(const char* text) {
|
|
|
|
AutoText input(text);
|
|
|
|
AssemblyContext data(input, nullptr);
|
|
|
|
EXPECT_EQ(SPV_SUCCESS, data.advance());
|
|
|
|
return data.position();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TextAdvance, SkipOverCR) {
|
|
|
|
const auto pos = PositionAfterAdvance("\rWord");
|
|
|
|
EXPECT_EQ(1u, pos.column);
|
|
|
|
EXPECT_EQ(0u, pos.line);
|
|
|
|
EXPECT_EQ(1u, pos.index);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TextAdvance, SkipOverCRs) {
|
|
|
|
const auto pos = PositionAfterAdvance("\r\r\rWord");
|
|
|
|
EXPECT_EQ(3u, pos.column);
|
|
|
|
EXPECT_EQ(0u, pos.line);
|
|
|
|
EXPECT_EQ(3u, pos.index);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TextAdvance, SkipOverCRLF) {
|
|
|
|
const auto pos = PositionAfterAdvance("\r\nWord");
|
|
|
|
EXPECT_EQ(0u, pos.column);
|
|
|
|
EXPECT_EQ(1u, pos.line);
|
|
|
|
EXPECT_EQ(2u, pos.index);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TextAdvance, SkipOverCRLFs) {
|
|
|
|
const auto pos = PositionAfterAdvance("\r\n\r\nWord");
|
|
|
|
EXPECT_EQ(0u, pos.column);
|
|
|
|
EXPECT_EQ(2u, pos.line);
|
|
|
|
EXPECT_EQ(4u, pos.index);
|
|
|
|
}
|
2018-07-07 13:38:00 +00:00
|
|
|
} // namespace
|
|
|
|
} // namespace spvtools
|