instruction_list_test.cpp 4.2 KB

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