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(TextStartsWithOp,YesAtStart)24 TEST(TextStartsWithOp, YesAtStart) {
25   EXPECT_TRUE(AssemblyContext(AutoText("OpFoo"), nullptr).isStartOfNewInst());
26   EXPECT_TRUE(AssemblyContext(AutoText("OpFoo"), nullptr).isStartOfNewInst());
27   EXPECT_TRUE(AssemblyContext(AutoText("OpEnCL"), nullptr).isStartOfNewInst());
28 }
29 
TEST(TextStartsWithOp,YesAtMiddle)30 TEST(TextStartsWithOp, YesAtMiddle) {
31   {
32     AutoText text("  OpFoo");
33     AssemblyContext dat(text, nullptr);
34     dat.seekForward(2);
35     EXPECT_TRUE(dat.isStartOfNewInst());
36   }
37   {
38     AutoText text("xx OpFoo");
39     AssemblyContext dat(text, nullptr);
40     dat.seekForward(2);
41     EXPECT_TRUE(dat.isStartOfNewInst());
42   }
43 }
44 
TEST(TextStartsWithOp,NoIfTooFar)45 TEST(TextStartsWithOp, NoIfTooFar) {
46   AutoText text("  OpFoo");
47   AssemblyContext dat(text, nullptr);
48   dat.seekForward(3);
49   EXPECT_FALSE(dat.isStartOfNewInst());
50 }
51 
TEST(TextStartsWithOp,NoRegular)52 TEST(TextStartsWithOp, NoRegular) {
53   EXPECT_FALSE(
54       AssemblyContext(AutoText("Fee Fi Fo Fum"), nullptr).isStartOfNewInst());
55   EXPECT_FALSE(AssemblyContext(AutoText("123456"), nullptr).isStartOfNewInst());
56   EXPECT_FALSE(AssemblyContext(AutoText("123456"), nullptr).isStartOfNewInst());
57   EXPECT_FALSE(AssemblyContext(AutoText("OpenCL"), nullptr).isStartOfNewInst());
58 }
59 
TEST(TextStartsWithOp,YesForValueGenerationForm)60 TEST(TextStartsWithOp, YesForValueGenerationForm) {
61   EXPECT_TRUE(
62       AssemblyContext(AutoText("%foo = OpAdd"), nullptr).isStartOfNewInst());
63   EXPECT_TRUE(
64       AssemblyContext(AutoText("%foo  =  OpAdd"), nullptr).isStartOfNewInst());
65 }
66 
TEST(TextStartsWithOp,NoForNearlyValueGeneration)67 TEST(TextStartsWithOp, NoForNearlyValueGeneration) {
68   EXPECT_FALSE(
69       AssemblyContext(AutoText("%foo = "), nullptr).isStartOfNewInst());
70   EXPECT_FALSE(AssemblyContext(AutoText("%foo "), nullptr).isStartOfNewInst());
71   EXPECT_FALSE(AssemblyContext(AutoText("%foo"), nullptr).isStartOfNewInst());
72 }
73 
74 }  // namespace
75 }  // namespace spvtools
76