• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
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 
16 #include "util/set_operations.h"
17 #include "util/tests/environment.h"
18 #include "util/range.h"
19 
20 #include <gtest/gtest.h>
21 #include <rapidcheck/gtest.h>
22 
23 #include <initializer_list>
24 #include <utility>
25 #include <string>
26 
27 // NOLINTNEXTLINE(google-build-using-namespace)
28 using namespace ark::verifier;
29 
30 namespace ark::verifier::test {
31 
32 namespace {
33 
34 // NOLINTNEXTLINE(fuchsia-statically-constructed-objects,cert-err58-cpp)
35 const EnvOptions OPTIONS {"VERIFIER_TEST"};
36 
37 using Interval = Range<size_t>;
38 using Intervals = std::initializer_list<Interval>;
39 
ClassifySize(size_t size,const Intervals & intervals)40 void ClassifySize(size_t size, const Intervals &intervals)
41 {
42     for (const auto &i : intervals) {
43         RC_CLASSIFY(i.Contains(size), std::to_string(i));
44     }
45 }
46 
47 }  // namespace
48 
49 using Set = std::set<int>;
50 
51 // 1 set
52 RC_GTEST_PROP(OperationsOverSets0, ConversionToSet, (std::vector<int> && vec))
53 {
54     // NOLINTNEXTLINE(readability-magic-numbers)
55     Intervals intervals = {{0, 10}, {11, 30}, {31, 10000}};
56 
__anonb818e2710202(const std::initializer_list<size_t> &sizes) 57     auto stat = [&intervals](const std::initializer_list<size_t> &sizes) {
58         if (OPTIONS.Get<bool>("verbose", false)) {
59             for (size_t size : sizes) {
60                 ClassifySize(size, intervals);
61             }
62         }
63     };
64     stat({vec.size()});
65     Set result = ToSet<Set>(vec);
66     for (const auto &elt : vec) {
67         RC_ASSERT(result.count(elt) > 0U);
68     }
69 }
70 
71 // 2 sets
72 RC_GTEST_PROP(OperationsOverSets1, Intersection, (Set && set1, Set &&set2))
73 {
74     Set result;
75 
76     result = SetIntersection(set1, set2);
77     for (int elt : result) {
78         RC_ASSERT(set1.count(elt) > 0U && set2.count(elt) > 0U);
79     }
80     for (int elt : set1) {
81         RC_ASSERT(result.count(elt) == 0U || set2.count(elt) > 0U);
82     }
83     for (int elt : set2) {
84         RC_ASSERT(result.count(elt) == 0U || set1.count(elt) > 0U);
85     }
86 }
87 
88 RC_GTEST_PROP(OperationsOverSets1, Union, (Set && set1, Set &&set2))
89 {
90     Set result;
91     result = SetUnion(set1, set2);
92     for (int elt : result) {
93         RC_ASSERT(set1.count(elt) > 0U || set2.count(elt) > 0U);
94     }
95     for (int elt : set1) {
96         RC_ASSERT(result.count(elt) > 0U);
97     }
98     for (int elt : set2) {
99         RC_ASSERT(result.count(elt) > 0U);
100     }
101 }
102 RC_GTEST_PROP(OperationsOverSets1, Difference, (Set && set1, Set &&set2))
103 {
104     Set result;
105     result = SetDifference(set1, set2);
106     for (int elt : result) {
107         RC_ASSERT(set1.count(elt) > 0U && set2.count(elt) == 0U);
108     }
109 }
110 
111 // 3 sets
112 RC_GTEST_PROP(OperationsOverSets2, Intersection, (Set && set1, Set &&set2, Set &&set3))
113 {
114     Set result;
115 
116     result = SetIntersection(set1, set2, set3);
117     for (int elt : result) {
118         RC_ASSERT(set1.count(elt) > 0U && set2.count(elt) > 0U && set3.count(elt) > 0U);
119     }
120     for (int elt : set1) {
121         RC_ASSERT(result.count(elt) == 0U || (set2.count(elt) > 0U && set3.count(elt) > 0U));
122     }
123     for (int elt : set2) {
124         RC_ASSERT(result.count(elt) == 0U || (set1.count(elt) > 0U && set3.count(elt) > 0U));
125     }
126     for (int elt : set3) {
127         RC_ASSERT(result.count(elt) == 0U || (set2.count(elt) > 0U && set1.count(elt) > 0U));
128     }
129 }
130 
131 RC_GTEST_PROP(OperationsOverSets2, Union, (Set && set1, Set &&set2, Set &&set3))
132 {
133     Set result;
134 
135     result = SetUnion(set1, set2, set3);
136     for (int elt : result) {
137         RC_ASSERT(set1.count(elt) > 0U || set2.count(elt) > 0U || set3.count(elt) > 0U);
138     }
139     for (int elt : set1) {
140         RC_ASSERT(result.count(elt) > 0U);
141     }
142     for (int elt : set2) {
143         RC_ASSERT(result.count(elt) > 0U);
144     }
145     for (int elt : set3) {
146         RC_ASSERT(result.count(elt) > 0U);
147     }
148 }
149 RC_GTEST_PROP(OperationsOverSets2, Difference, (Set && set1, Set &&set2, Set &&set3))
150 {
151     Set result;
152     result = SetDifference(set1, set2, set3);
153     for (int elt : result) {
154         RC_ASSERT(set1.count(elt) > 0U && set2.count(elt) == 0U && set3.count(elt) == 0U);
155     }
156 }
157 
158 }  // namespace ark::verifier::test
159