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