Macaulay2 Engine
Loading...
Searching...
No Matches
SubsetTest.cpp
Go to the documentation of this file.
1// Copyright 2013 Michael E. Stillman
2
30
31#include <cstdio>
32#include <string>
33#include <iostream>
34#include <memory>
35#include <gtest/gtest.h>
36
37#include "comb.hpp"
38
39TEST(Subsets, encode1)
40{
41 Subsets C(5, 2);
42
43 Subset a(2, 0);
44
45 for (int i = 0; i < 10; i++)
46 {
47 C.decode(i, a);
48 EXPECT_TRUE(C.isValid(a));
49 size_t j = C.encode(a);
50 EXPECT_EQ(i, j);
51 }
52}
53
54TEST(Subsets, encode2)
55{
56 Subsets C(12, 6);
57
58 Subset a(6, 0);
59
60 for (int i = 0; i < 924; i++)
61 {
62 C.decode(i, a);
63 EXPECT_TRUE(C.isValid(a));
64 size_t j = C.encode(a);
65 EXPECT_EQ(i, j);
66 }
67}
68
69TEST(Subsets, encode3)
70{
71 Subsets C(12, 0);
72
73 Subset a(0, 0);
74
75 for (int i = 0; i < 1; i++)
76 {
77 C.decode(i, a);
78 EXPECT_TRUE(C.isValid(a));
79 size_t j = C.encode(a);
80 EXPECT_EQ(i, j);
81 }
82}
83
84TEST(Subsets, encode4)
85{
86 Subsets C(21, 7);
87
88 Subset a(7, 0);
89
90 for (int i = 0; i < 116280; i++)
91 {
92 C.decode(i, a);
93 EXPECT_TRUE(C.isValid(a));
94 size_t j = C.encode(a);
95 EXPECT_EQ(i, j);
96 }
97}
98
99TEST(Subsets, encode5)
100{
101 Subsets C(21, 21);
102
103 Subset a(21, 0);
104
105 for (int i = 0; i < 1; i++)
106 {
107 C.decode(i, a);
108 EXPECT_TRUE(C.isValid(a));
109 size_t j = C.encode(a);
110 EXPECT_EQ(i, j);
111 }
112}
113
114bool sameSubset(const Subset &a, const Subset &b)
115{
116 if (a.size() != b.size()) return false;
117 for (size_t i = 0; i < a.size(); i++)
118 if (a[i] != b[i]) return false;
119 return true;
120}
121
122TEST(Subsets, encode6)
123{
124 // test the increment and decrement functions too
125 const int n = 21;
126 const int p = 7;
127 const int n_choose_p = 116280;
128
129 Subsets C(n, p);
130
131 Subset a(p, 0);
132 Subset b(p, 0);
133 for (size_t i = 0; i < p; i++) b[i] = i;
134
135 for (size_t i = 0; i < n_choose_p; i++)
136 {
137 C.decode(i, a);
138 EXPECT_TRUE(sameSubset(a, b));
139 EXPECT_TRUE(C.isValid(a));
140 size_t j = C.encode(a);
141 EXPECT_EQ(i, j);
142 bool ret = Subsets::increment(n, b);
143 EXPECT_EQ(ret, i + 1 != n_choose_p);
144 }
145}
146
147TEST(Subsets, concatenateSubsets)
148{
149 const int n = 7;
150 const int p = 3;
151 const int q = 2;
152 const int n_choose_p = 35;
153 Subsets C(n, std::max(p, q));
154
155 Subset a(p, 0);
156 Subset b(q, 0);
157 Subset c(p + q, 0);
158 Subset d(p + q, 0);
159
160 int sign;
161 if ((p % 2 == 1) && (q % 2 == 1))
162 sign = -1;
163 else
164 sign = 1;
165 for (size_t i = 0; i < n_choose_p; i++)
166 {
167 C.decode(i, a);
168 EXPECT_TRUE(C.isValid(a));
169 for (size_t j = 0; j < n_choose_p; j++)
170 {
171 C.decode(j, b);
172 EXPECT_TRUE(C.isValid(b));
173 int ret1 = Subsets::concatenateSubsets(a, b, c);
174 int ret2 = Subsets::concatenateSubsets(b, a, d);
175 if (ret1 == 0 || ret2 == 0)
176 {
177 EXPECT_EQ(ret1, ret2);
178 break;
179 }
180 EXPECT_EQ(ret1, sign * ret2);
181 EXPECT_TRUE(sameSubset(c, d));
182 }
183 }
184}
185
186TEST(Subsets, outOfRange)
187{
188 const int n = 7;
189 const int p = 3;
190 const int q = 2;
191 Subsets C(n, std::max(p, q));
192
193 Subset b(q, 0);
194
195 for (size_t i = 0; i < 21; i++)
196 {
197 C.decode(i, b);
198 std::cout << "i=" << i << " set=";
199 Subsets::show(std::cout, b);
200 std::cout << std::endl;
201 EXPECT_TRUE(C.isValid(b));
202 }
203}
204
205TEST(Subsets, encodeBoundary)
206{
207 const int n = 7;
208 const int p = 3;
209 const int n_choose_p = 35;
210 Subsets C(n, p);
211
212 Subset a(p, 0);
213 Subset b(p - 1, 0);
214
215 for (size_t i = 0; i < n_choose_p; i++)
216 {
217 C.decode(i, a);
218 std::cout << "i=" << i << "set=";
219 Subsets::show(std::cout, a);
220 std::cout << " bds= ";
221 for (size_t j = 0; j < p; j++)
222 {
223 size_t x = C.encodeBoundary(j, a);
224 C.decode(x, b);
225 Subsets::show(std::cout, b);
226 std::cout << " ";
227 }
228 std::cout << std::endl;
229 }
230}
231
232// Local Variables:
233// compile-command: "make -C $M2BUILDDIR/Macaulay2/e/unit-tests check "
234// indent-tabs-mode: nil
235// End:
bool sameSubset(const Subset &a, const Subset &b)
TEST(Subsets, encode1)
static bool increment(size_t n, Subset &s)
Definition comb.cpp:124
bool isValid(const Subset &a)
Definition comb.cpp:41
static int concatenateSubsets(const Subset &s, const Subset &t, Subset &result)
Definition comb.cpp:163
size_t encode(const Subset &a)
Definition comb.cpp:66
void decode(size_t val, Subset &result)
Definition comb.cpp:96
size_t encodeBoundary(size_t index, const Subset &a)
Definition comb.cpp:80
static void show(std::ostream &o, const Subset &a)
Definition comb.cpp:203
Bijective integer encoding of q-subsets of {0, ..., n-1} via binomial(a_0, 1) + binomial(a_1,...
Definition comb.hpp:74
std::vector< size_t > Subset
Definition comb.hpp:58
Subsets — combinatorial-number-system encoding of p-subsets of {0,...,n-1}.
int p
volatile int x