• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2017 Google 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 <algorithm>
16 #include <memory>
17 #include <utility>
18 #include <vector>
19 
20 #include "gmock/gmock.h"
21 #include "gtest/gtest.h"
22 #include "source/opt/instruction.h"
23 #include "source/opt/instruction_list.h"
24 
25 namespace spvtools {
26 namespace opt {
27 namespace {
28 
29 using ::testing::ContainerEq;
30 using ::testing::ElementsAre;
31 using InstructionListTest = ::testing::Test;
32 
33 // A class that overrides the destructor, so we can trace it.
34 class TestInstruction : public Instruction {
35  public:
TestInstruction()36   TestInstruction() : Instruction() { created_instructions_.push_back(this); }
37 
~TestInstruction()38   ~TestInstruction() override{ deleted_instructions_.push_back(this); }
39 
40   static std::vector<TestInstruction*> created_instructions_;
41   static std::vector<TestInstruction*> deleted_instructions_;
42 };
43 
44 std::vector<TestInstruction*> TestInstruction::created_instructions_;
45 std::vector<TestInstruction*> TestInstruction::deleted_instructions_;
46 
47 // Test that the destructor for InstructionList is calling the destructor
48 // for every element that is in the list.
TEST(InstructionListTest,Destructor)49 TEST(InstructionListTest, Destructor) {
50   InstructionList* list = new InstructionList();
51   list->push_back(std::unique_ptr<Instruction>(new Instruction()));
52   list->push_back(std::unique_ptr<Instruction>(new Instruction()));
53   delete list;
54 
55   // Sorting because we do not care if the order of create and destruction is
56   // the same.  Using generic sort just incase things are changed above.
57   std::sort(TestInstruction::created_instructions_.begin(),
58             TestInstruction::created_instructions_.end());
59   std::sort(TestInstruction::deleted_instructions_.begin(),
60             TestInstruction::deleted_instructions_.end());
61   EXPECT_THAT(TestInstruction::created_instructions_,
62               ContainerEq(TestInstruction::deleted_instructions_));
63 }
64 
65 // Test the |InsertBefore| with a single instruction in the iterator class.
66 // Need to make sure the elements are inserted in the correct order, and the
67 // return value points to the correct location.
68 //
69 // Comparing addresses to make sure they remain stable, so other data structures
70 // can have pointers to instructions in InstructionList.
TEST(InstructionListTest,InsertBefore1)71 TEST(InstructionListTest, InsertBefore1) {
72   InstructionList list;
73   std::vector<Instruction*> inserted_instructions;
74   for (int i = 0; i < 4; i++) {
75     std::unique_ptr<Instruction> inst(new Instruction());
76     inserted_instructions.push_back(inst.get());
77     auto new_element = list.end().InsertBefore(std::move(inst));
78     EXPECT_EQ(&*new_element, inserted_instructions.back());
79   }
80 
81   std::vector<Instruction*> output;
82   for (auto& i : list) {
83     output.push_back(&i);
84   }
85   EXPECT_THAT(output, ContainerEq(inserted_instructions));
86 }
87 
88 // Test inserting an entire vector of instructions using InsertBefore.  Checking
89 // the order of insertion and the return value.
90 //
91 // Comparing addresses to make sure they remain stable, so other data structures
92 // can have pointers to instructions in InstructionList.
TEST(InstructionListTest,InsertBefore2)93 TEST(InstructionListTest, InsertBefore2) {
94   InstructionList list;
95   std::vector<std::unique_ptr<Instruction>> new_instructions;
96   std::vector<Instruction*> created_instructions;
97   for (int i = 0; i < 4; i++) {
98     std::unique_ptr<Instruction> inst(new Instruction());
99     created_instructions.push_back(inst.get());
100     new_instructions.push_back(std::move(inst));
101   }
102   auto new_element = list.begin().InsertBefore(std::move(new_instructions));
103   EXPECT_TRUE(new_instructions.empty());
104   EXPECT_EQ(&*new_element, created_instructions.front());
105 
106   std::vector<Instruction*> output;
107   for (auto& i : list) {
108     output.push_back(&i);
109   }
110   EXPECT_THAT(output, ContainerEq(created_instructions));
111 }
112 
113 }  // namespace
114 }  // namespace opt
115 }  // namespace spvtools
116