• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
15 #include <string>
16 
17 #include "test/unit_spirv.h"
18 
19 namespace spvtools {
20 namespace {
21 
22 using spvtest::AutoText;
23 
TEST(TextAdvance,LeadingNewLines)24 TEST(TextAdvance, LeadingNewLines) {
25   AutoText input("\n\nWord");
26   AssemblyContext data(input, nullptr);
27   ASSERT_EQ(SPV_SUCCESS, data.advance());
28   ASSERT_EQ(0u, data.position().column);
29   ASSERT_EQ(2u, data.position().line);
30   ASSERT_EQ(2u, data.position().index);
31 }
32 
TEST(TextAdvance,LeadingSpaces)33 TEST(TextAdvance, LeadingSpaces) {
34   AutoText input("    Word");
35   AssemblyContext data(input, nullptr);
36   ASSERT_EQ(SPV_SUCCESS, data.advance());
37   ASSERT_EQ(4u, data.position().column);
38   ASSERT_EQ(0u, data.position().line);
39   ASSERT_EQ(4u, data.position().index);
40 }
41 
TEST(TextAdvance,LeadingTabs)42 TEST(TextAdvance, LeadingTabs) {
43   AutoText input("\t\t\tWord");
44   AssemblyContext data(input, nullptr);
45   ASSERT_EQ(SPV_SUCCESS, data.advance());
46   ASSERT_EQ(3u, data.position().column);
47   ASSERT_EQ(0u, data.position().line);
48   ASSERT_EQ(3u, data.position().index);
49 }
50 
TEST(TextAdvance,LeadingNewLinesSpacesAndTabs)51 TEST(TextAdvance, LeadingNewLinesSpacesAndTabs) {
52   AutoText input("\n\n\t  Word");
53   AssemblyContext data(input, nullptr);
54   ASSERT_EQ(SPV_SUCCESS, data.advance());
55   ASSERT_EQ(3u, data.position().column);
56   ASSERT_EQ(2u, data.position().line);
57   ASSERT_EQ(5u, data.position().index);
58 }
59 
TEST(TextAdvance,LeadingWhitespaceAfterCommentLine)60 TEST(TextAdvance, LeadingWhitespaceAfterCommentLine) {
61   AutoText input("; comment\n \t \tWord");
62   AssemblyContext data(input, nullptr);
63   ASSERT_EQ(SPV_SUCCESS, data.advance());
64   ASSERT_EQ(4u, data.position().column);
65   ASSERT_EQ(1u, data.position().line);
66   ASSERT_EQ(14u, data.position().index);
67 }
68 
TEST(TextAdvance,EOFAfterCommentLine)69 TEST(TextAdvance, EOFAfterCommentLine) {
70   AutoText input("; comment");
71   AssemblyContext data(input, nullptr);
72   ASSERT_EQ(SPV_END_OF_STREAM, data.advance());
73 }
74 
TEST(TextAdvance,NullTerminator)75 TEST(TextAdvance, NullTerminator) {
76   AutoText input("");
77   AssemblyContext data(input, nullptr);
78   ASSERT_EQ(SPV_END_OF_STREAM, data.advance());
79 }
80 
TEST(TextAdvance,NoNullTerminatorAfterCommentLine)81 TEST(TextAdvance, NoNullTerminatorAfterCommentLine) {
82   std::string input = "; comment|padding beyond the end";
83   spv_text_t text = {input.data(), 9};
84   AssemblyContext data(&text, nullptr);
85   ASSERT_EQ(SPV_END_OF_STREAM, data.advance());
86   EXPECT_EQ(9u, data.position().index);
87 }
88 
TEST(TextAdvance,NoNullTerminator)89 TEST(TextAdvance, NoNullTerminator) {
90   spv_text_t text = {"OpNop\nSomething else in memory", 6};
91   AssemblyContext data(&text, nullptr);
92   const spv_position_t line_break = {1u, 5u, 5u};
93   data.setPosition(line_break);
94   ASSERT_EQ(SPV_END_OF_STREAM, data.advance());
95 }
96 
97 // Invokes AssemblyContext::advance() on text, asserts success, and returns
98 // AssemblyContext::position().
PositionAfterAdvance(const char * text)99 spv_position_t PositionAfterAdvance(const char* text) {
100   AutoText input(text);
101   AssemblyContext data(input, nullptr);
102   EXPECT_EQ(SPV_SUCCESS, data.advance());
103   return data.position();
104 }
105 
TEST(TextAdvance,SkipOverCR)106 TEST(TextAdvance, SkipOverCR) {
107   const auto pos = PositionAfterAdvance("\rWord");
108   EXPECT_EQ(1u, pos.column);
109   EXPECT_EQ(0u, pos.line);
110   EXPECT_EQ(1u, pos.index);
111 }
112 
TEST(TextAdvance,SkipOverCRs)113 TEST(TextAdvance, SkipOverCRs) {
114   const auto pos = PositionAfterAdvance("\r\r\rWord");
115   EXPECT_EQ(3u, pos.column);
116   EXPECT_EQ(0u, pos.line);
117   EXPECT_EQ(3u, pos.index);
118 }
119 
TEST(TextAdvance,SkipOverCRLF)120 TEST(TextAdvance, SkipOverCRLF) {
121   const auto pos = PositionAfterAdvance("\r\nWord");
122   EXPECT_EQ(0u, pos.column);
123   EXPECT_EQ(1u, pos.line);
124   EXPECT_EQ(2u, pos.index);
125 }
126 
TEST(TextAdvance,SkipOverCRLFs)127 TEST(TextAdvance, SkipOverCRLFs) {
128   const auto pos = PositionAfterAdvance("\r\n\r\nWord");
129   EXPECT_EQ(0u, pos.column);
130   EXPECT_EQ(2u, pos.line);
131   EXPECT_EQ(4u, pos.index);
132 }
133 
TEST(TextAdvance,HandleLotsOfWhitespace)134 TEST(TextAdvance, HandleLotsOfWhitespace) {
135   std::string lots_of_spaces(10000, ' ');
136   lots_of_spaces += "Word";
137   const auto pos = PositionAfterAdvance(lots_of_spaces.c_str());
138   EXPECT_EQ(10000u, pos.column);
139   EXPECT_EQ(0u, pos.line);
140   EXPECT_EQ(10000u, pos.index);
141 }
142 }  // namespace
143 }  // namespace spvtools
144