• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2022 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/range.h"
18 
19 #ifdef PANDA_CATCH2
20 #include <catch2/catch.hpp>
21 #include <rapidcheck/catch.h>
22 #include "util/tests/environment.h"
23 #endif
24 
25 #ifdef PANDA_GTEST
26 #include <gtest/gtest.h>
27 #include <rapidcheck/gtest.h>
28 #endif
29 
30 #include <initializer_list>
31 #include <utility>
32 #include <string>
33 
34 using namespace panda::verifier;
35 
36 namespace panda::verifier::test {
37 
38 using namespace rc;
39 
40 #ifdef PANDA_CATCH2
41 
42 namespace {
43 
44 const EnvOptions Options {"VERIFIER_TEST"};
45 
46 using Interval = Range<size_t>;
47 using Intervals = std::initializer_list<Interval>;
48 
ClassifySize(size_t size,const Intervals & intervals)49 void ClassifySize(size_t size, const Intervals &intervals)
50 {
51     for (const auto &i : intervals) {
52         RC_CLASSIFY(i.Contains(size), std::to_string(i));
53     }
54 }
55 
56 }  // namespace
57 
58 TEST_CASE("Test operations over sets0", "verifier_set_operations0")
59 {
60     using set = std::set<int>;
61 
62     Intervals intervals = {{0, 10}, {11, 30}, {31, 10000}};
63 
__anon36fc55b10202(const std::initializer_list<size_t> &sizes) 64     auto stat = [&intervals](const std::initializer_list<size_t> &sizes) {
65         if (Options.Get<bool>("verbose", false)) {
66             for (size_t size : sizes) {
67                 ClassifySize(size, intervals);
68             }
69         }
70     };
71 
72     SECTION("Conversion to set")
73     {
__anon36fc55b10302(std::vector<int> &&vec) 74         prop("ToSet", [&stat](std::vector<int> &&vec) {
75             stat({vec.size()});
76             set result = ToSet<set>(vec);
77             for (const auto &elt : vec) {
78                 RC_ASSERT(result.count(elt) > 0U);
79             }
80         });
81     }
82 }
83 
84 TEST_CASE("Test operations over sets1", "verifier_set_operations1")
85 {
86     using set = std::set<int>;
87 
88     SECTION("2 sets")
89     {
__anon36fc55b10402(set &&set1, set &&set2) 90         prop("Intersection", [](set &&set1, set &&set2) {
91             set result = SetIntersection(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 || set2.count(elt) > 0U);
97             }
98             for (int elt : set2) {
99                 RC_ASSERT(result.count(elt) == 0U || set1.count(elt) > 0U);
100             }
101         });
__anon36fc55b10502(set &&set1, set &&set2) 102         prop("Union", [](set &&set1, set &&set2) {
103             set result = SetUnion(set1, set2);
104             for (int elt : result) {
105                 RC_ASSERT(set1.count(elt) > 0U || set2.count(elt) > 0U);
106             }
107             for (int elt : set1) {
108                 RC_ASSERT(result.count(elt) > 0U);
109             }
110             for (int elt : set2) {
111                 RC_ASSERT(result.count(elt) > 0U);
112             }
113         });
__anon36fc55b10602(set &&set1, set &&set2) 114         prop("Difference", [](set &&set1, set &&set2) {
115             set result = SetDifference(set1, set2);
116             for (int elt : result) {
117                 RC_ASSERT(set1.count(elt) > 0U && set2.count(elt) == 0U);
118             }
119         });
120     }
121 }
122 
123 TEST_CASE("Test operations over sets2", "verifier_set_operations2")
124 {
125     using set = std::set<int>;
126 
127     SECTION("3 sets")
128     {
__anon36fc55b10702(set &&set1, set &&set2, set &&set3) 129         prop("Intersection", [](set &&set1, set &&set2, set &&set3) {
130             set result = SetIntersection(set1, set2, set3);
131             for (int elt : result) {
132                 RC_ASSERT(set1.count(elt) > 0U && set2.count(elt) > 0U && set3.count(elt) > 0U);
133             }
134             for (int elt : set1) {
135                 RC_ASSERT(result.count(elt) == 0U || (set2.count(elt) > 0U && set3.count(elt) > 0U));
136             }
137             for (int elt : set2) {
138                 RC_ASSERT(result.count(elt) == 0U || (set1.count(elt) > 0U && set3.count(elt) > 0U));
139             }
140             for (int elt : set3) {
141                 RC_ASSERT(result.count(elt) == 0U || (set2.count(elt) > 0U && set1.count(elt) > 0U));
142             }
143         });
__anon36fc55b10802(set &&set1, set &&set2, set &&set3) 144         prop("Union", [](set &&set1, set &&set2, set &&set3) {
145             set result = SetUnion(set1, set2, set3);
146             for (int elt : result) {
147                 RC_ASSERT(set1.count(elt) > 0U || set2.count(elt) > 0U || set3.count(elt) > 0U);
148             }
149             for (int elt : set1) {
150                 RC_ASSERT(result.count(elt) > 0U);
151             }
152             for (int elt : set2) {
153                 RC_ASSERT(result.count(elt) > 0U);
154             }
155             for (int elt : set3) {
156                 RC_ASSERT(result.count(elt) > 0U);
157             }
158         });
__anon36fc55b10902(set &&set1, set &&set2, set &&set3) 159         prop("Difference", [](set &&set1, set &&set2, set &&set3) {
160             set result = SetDifference(set1, set2, set3);
161             for (int elt : result) {
162                 RC_ASSERT(set1.count(elt) > 0U && set2.count(elt) == 0U && set3.count(elt) == 0U);
163             }
164         });
165     }
166 }
167 
168 #endif  // !PANDA_CATCH2
169 
170 }  // namespace panda::verifier::test
171