| 1 |
// Copyright (c) 2019 Google LLC |
| 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 <set> |
| 16 |
|
| 17 |
#include "source/fuzz/equivalence_relation.h" |
| 18 |
|
| 19 |
#include "gmock/gmock.h" |
| 20 |
#include "gtest/gtest.h" |
| 21 |
|
| 22 |
namespace spvtools { |
| 23 |
namespace fuzz { |
| 24 |
namespace { |
| 25 |
|
| 26 |
struct UInt32Equals { |
| 27 |
bool operator()(const uint32_t* first, const uint32_t* second) const { |
| 28 |
return *first == *second; |
| 29 |
} |
| 30 |
}; |
| 31 |
|
| 32 |
struct UInt32Hash { |
| 33 |
size_t operator()(const uint32_t* element) const { |
| 34 |
return static_cast<size_t>(*element); |
| 35 |
} |
| 36 |
}; |
| 37 |
|
| 38 |
std::vector<uint32_t> ToUIntVector( |
| 39 |
const std::vector<const uint32_t*>& pointers) { |
| 40 |
std::vector<uint32_t> result; |
| 41 |
for (auto pointer : pointers) { |
| 42 |
result.push_back(*pointer); |
| 43 |
} |
| 44 |
return result; |
| 45 |
} |
| 46 |
|
| 47 |
TEST(EquivalenceRelationTest, BasicTest) { |
| 48 |
EquivalenceRelation<uint32_t, UInt32Hash, UInt32Equals> relation; |
| 49 |
ASSERT_TRUE(relation.GetAllKnownValues().empty()); |
| 50 |
|
| 51 |
for (uint32_t element = 0; element < 100; element++) { |
| 52 |
relation.Register(element); |
| 53 |
} |
| 54 |
|
| 55 |
for (uint32_t element = 2; element < 80; element += 2) { |
| 56 |
relation.MakeEquivalent(0, element); |
| 57 |
relation.MakeEquivalent(element - 1, element + 1); |
| 58 |
} |
| 59 |
|
| 60 |
for (uint32_t element = 82; element < 100; element += 2) { |
| 61 |
relation.MakeEquivalent(80, element); |
| 62 |
relation.MakeEquivalent(element - 1, element + 1); |
| 63 |
} |
| 64 |
|
| 65 |
relation.MakeEquivalent(78, 80); |
| 66 |
|
| 67 |
std::vector<uint32_t> class1; |
| 68 |
for (uint32_t element = 0; element < 98; element += 2) { |
| 69 |
ASSERT_TRUE(relation.IsEquivalent(0, element)); |
| 70 |
ASSERT_TRUE(relation.IsEquivalent(element, element + 2)); |
| 71 |
class1.push_back(element); |
| 72 |
} |
| 73 |
class1.push_back(98); |
| 74 |
|
| 75 |
ASSERT_THAT(ToUIntVector(relation.GetEquivalenceClass(0)), |
| 76 |
testing::WhenSorted(class1)); |
| 77 |
ASSERT_THAT(ToUIntVector(relation.GetEquivalenceClass(4)), |
| 78 |
testing::WhenSorted(class1)); |
| 79 |
ASSERT_THAT(ToUIntVector(relation.GetEquivalenceClass(40)), |
| 80 |
testing::WhenSorted(class1)); |
| 81 |
|
| 82 |
std::vector<uint32_t> class2; |
| 83 |
for (uint32_t element = 1; element < 79; element += 2) { |
| 84 |
ASSERT_TRUE(relation.IsEquivalent(1, element)); |
| 85 |
ASSERT_TRUE(relation.IsEquivalent(element, element + 2)); |
| 86 |
class2.push_back(element); |
| 87 |
} |
| 88 |
class2.push_back(79); |
| 89 |
ASSERT_THAT(ToUIntVector(relation.GetEquivalenceClass(1)), |
| 90 |
testing::WhenSorted(class2)); |
| 91 |
ASSERT_THAT(ToUIntVector(relation.GetEquivalenceClass(11)), |
| 92 |
testing::WhenSorted(class2)); |
| 93 |
ASSERT_THAT(ToUIntVector(relation.GetEquivalenceClass(31)), |
| 94 |
testing::WhenSorted(class2)); |
| 95 |
|
| 96 |
std::vector<uint32_t> class3; |
| 97 |
for (uint32_t element = 81; element < 99; element += 2) { |
| 98 |
ASSERT_TRUE(relation.IsEquivalent(81, element)); |
| 99 |
ASSERT_TRUE(relation.IsEquivalent(element, element + 2)); |
| 100 |
class3.push_back(element); |
| 101 |
} |
| 102 |
class3.push_back(99); |
| 103 |
ASSERT_THAT(ToUIntVector(relation.GetEquivalenceClass(81)), |
| 104 |
testing::WhenSorted(class3)); |
| 105 |
ASSERT_THAT(ToUIntVector(relation.GetEquivalenceClass(91)), |
| 106 |
testing::WhenSorted(class3)); |
| 107 |
ASSERT_THAT(ToUIntVector(relation.GetEquivalenceClass(99)), |
| 108 |
testing::WhenSorted(class3)); |
| 109 |
|
| 110 |
bool first = true; |
| 111 |
std::vector<const uint32_t*> previous_class; |
| 112 |
for (auto representative : relation.GetEquivalenceClassRepresentatives()) { |
| 113 |
std::vector<const uint32_t*> current_class = |
| 114 |
relation.GetEquivalenceClass(*representative); |
| 115 |
ASSERT_TRUE(std::find(current_class.begin(), current_class.end(), |
| 116 |
representative) != current_class.end()); |
| 117 |
if (!first) { |
| 118 |
ASSERT_TRUE(std::find(previous_class.begin(), previous_class.end(), |
| 119 |
representative) == previous_class.end()); |
| 120 |
} |
| 121 |
previous_class = current_class; |
| 122 |
first = false; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
TEST(EquivalenceRelationTest, DeterministicEquivalenceClassOrder) { |
| 127 |
EquivalenceRelation<uint32_t, UInt32Hash, UInt32Equals> relation1; |
| 128 |
EquivalenceRelation<uint32_t, UInt32Hash, UInt32Equals> relation2; |
| 129 |
|
| 130 |
for (uint32_t i = 0; i < 1000; ++i) { |
| 131 |
relation1.Register(i); |
| 132 |
relation2.Register(i); |
| 133 |
} |
| 134 |
|
| 135 |
for (uint32_t i = 0; i < 1000; ++i) { |
| 136 |
if (i >= 10) { |
| 137 |
relation1.MakeEquivalent(i, i - 10); |
| 138 |
relation2.MakeEquivalent(i, i - 10); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
// We constructed the equivalence relations in the same way, so we would like |
| 143 |
// them to have identical representatives, and identically-ordered equivalence |
| 144 |
// classes per representative. |
| 145 |
ASSERT_THAT(ToUIntVector(relation1.GetEquivalenceClassRepresentatives()), |
| 146 |
ToUIntVector(relation2.GetEquivalenceClassRepresentatives())); |
| 147 |
for (auto representative : relation1.GetEquivalenceClassRepresentatives()) { |
| 148 |
ASSERT_THAT(ToUIntVector(relation1.GetEquivalenceClass(*representative)), |
| 149 |
ToUIntVector(relation2.GetEquivalenceClass(*representative))); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
} // namespace |
| 154 |
} // namespace fuzz |
| 155 |
} // namespace spvtools |