File: DegLexOrderTest.cc

package info (click to toggle)
brial 1.2.12-1.1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 11,404 kB
  • sloc: cpp: 219,610; ansic: 43,783; python: 4,172; makefile: 336; sh: 5
file content (261 lines) | stat: -rw-r--r-- 9,706 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// -*- c++ -*-
//*****************************************************************************
/** @file DegLexOrderTest.cc
 *
 * @author Ket Shcherbakova, Alexander Dreyer
 * @date 2010-12-07
 *
 * boost/test-driven unit test
 * 
 * @par Copyright:
 *   (c) 2010 by The PolyBoRi Team
 *
 **/
//*****************************************************************************


#include <boost/test/unit_test.hpp>
#include <boost/version.hpp>
#if BOOST_VERSION < 107100
#include <boost/test/output_test_stream.hpp>
#else
#include <boost/test/tools/output_test_stream.hpp>
#endif

using boost::test_tools::output_test_stream;

#include <polybori/DegLexOrder.h>

USING_NAMESPACE_PBORI

class Fdeglex {
public:
  typedef DegLexOrder order_type;
  Fdeglex(const BoolePolyRing& input_ring = 
          BoolePolyRing(5, COrderEnums::lp)):  
    ring(input_ring),
    x(0, input_ring), y(1, input_ring), z(2, input_ring),
    v(3, input_ring), w(4, input_ring) {

    BOOST_TEST_MESSAGE( "setup fixture" );
    ring.setVariableName(0, "x");
    ring.setVariableName(1, "y");
    ring.setVariableName(2, "z");
    ring.setVariableName(3, "v");
    ring.setVariableName(4, "w");
  }
  ~Fdeglex() { BOOST_TEST_MESSAGE( "teardown fixture" ); }

  BoolePolyRing ring;
  BooleVariable x, y, z, v, w;
};

BOOST_FIXTURE_TEST_SUITE(DegLexOrderTestSuite, Fdeglex )

BOOST_AUTO_TEST_CASE(test_properties) {

  order_type order;
  BOOST_TEST_MESSAGE( "isLexicographical, isSymmetric, isDegreeOrder, isBlockOrder, isTotalDegreeOrder, isDegreeReverseLexicographical, ascendingVariables, descendingVariables, orderedStandardIteration" );
  BOOST_CHECK(!order.isLexicographical());
  BOOST_CHECK(order.isSymmetric());
  BOOST_CHECK(order.isDegreeOrder());
  BOOST_CHECK(!order.isBlockOrder());
  BOOST_CHECK(order.isTotalDegreeOrder());
  BOOST_CHECK(!order.isDegreeReverseLexicographical());
  BOOST_CHECK(!order.ascendingVariables());
  BOOST_CHECK(order.descendingVariables());
  BOOST_CHECK(!order.orderedStandardIteration());
}

BOOST_AUTO_TEST_CASE(test_getters) {

  order_type order;
  BOOST_TEST_MESSAGE( "getOrderCode, getBaseOrderCode" );
  BOOST_CHECK_EQUAL(order.getOrderCode(), COrderEnums::dlex);
  BOOST_CHECK_EQUAL(order.getBaseOrderCode(), COrderEnums::dlex);
}

BOOST_AUTO_TEST_CASE(test_compare) {

  order_type order;
  BOOST_TEST_MESSAGE( "compare" );
  BooleMonomial monom1 = x;
  BooleMonomial monom2 = x*x;
  BOOST_CHECK_EQUAL(order.compare(monom1, monom2) , CTypes::equality);
  monom1 = x*y;
  monom2 = x*z*v;
  BOOST_CHECK_EQUAL(order.compare(monom1, monom2) , CTypes::less_than);
  monom1 = v*y;
  monom2 = x;
  BOOST_CHECK_EQUAL(order.compare(monom1, monom2) , CTypes::greater_than);
  monom1 = BooleMonomial(ring);
  monom2 = w;
  BOOST_CHECK_EQUAL(order.compare(monom1, monom2) , CTypes::less_than);
  monom1 = BooleMonomial(ring);
  monom2 = BooleMonomial(ring);
  BOOST_CHECK_EQUAL(order.compare(monom1, monom2) , CTypes::equality);
  BooleExponent exp1(x);
  BooleExponent exp2(x*x);
  BOOST_CHECK_EQUAL(order.compare(exp1, exp2) , CTypes::equality);
  exp1 = BooleExponent(w*x);
  exp2 = BooleExponent(v*x);
  BOOST_CHECK_EQUAL(order.compare(exp1, exp2) , CTypes::less_than);
  exp1 = BooleExponent(x*y*z*v*w);
  exp2 = BooleExponent(x*y*z*v);
  BOOST_CHECK_EQUAL(order.compare(exp1, exp2) , CTypes::greater_than);
  BOOST_CHECK_EQUAL(order.compare(0,1) , CTypes::greater_than);
  BOOST_CHECK_EQUAL(order.compare(2,1) , CTypes::less_than);
  BOOST_CHECK_EQUAL(order.compare(-1,-1) , CTypes::equality);
  BOOST_CHECK_EQUAL(order.compare(1,-1) , CTypes::less_than);
}

BOOST_AUTO_TEST_CASE(test_lead) {

  BOOST_TEST_MESSAGE( "lead, leadExp, leadFirst" );
  order_type order;
  BoolePolynomial poly = x + x*y + y*z*v*w + 1;

  // testing leadexp first, since they do not cache results (but call the cache)
  BOOST_CHECK_EQUAL(order.leadExp(poly)    , BooleExponent(y*z*v*w));
  BOOST_CHECK_EQUAL(order.leadExp(poly,-1), BooleExponent());
  BOOST_CHECK_EQUAL(order.leadExp(poly,0), BooleExponent());
  BOOST_CHECK_EQUAL(order.leadExp(poly-1,0), BooleExponent());
  BOOST_CHECK_EQUAL(order.leadExp(poly,1), BooleExponent(x));
  BOOST_CHECK_EQUAL(order.leadExp(x + y, 0), BooleExponent());

  BOOST_CHECK_EQUAL(order.lead(poly)    , BooleMonomial(y*z*v*w));
  BOOST_CHECK_EQUAL(order.lead(poly,1), BooleMonomial(x));
  BOOST_CHECK_EQUAL(order.lead(poly,0), BooleMonomial(ring));

  BOOST_CHECK_THROW(order.lead(x + y, 0), PBoRiError);
  BOOST_CHECK_THROW(order.lead(poly,-1), PBoRiError);

  poly = x + x*y + y + 1;
  BOOST_CHECK_EQUAL(order.leadExp(poly), BooleExponent(x*y));
  BOOST_CHECK_EQUAL(order.leadExp(poly,-1), BooleExponent());
  BOOST_CHECK_EQUAL(order.leadExp(poly,1), BooleExponent(x));

  BOOST_CHECK_EQUAL(order.lead(poly), BooleMonomial(x*y));
  BOOST_CHECK_EQUAL(order.lead(poly, 1), BooleMonomial(x));
  BOOST_CHECK_THROW(order.lead(poly, -1), PBoRiError);

  // Empty (zero) poly
  poly = BoolePolynomial(ring);
  BOOST_CHECK_THROW(order.lead(poly), PBoRiGenericError<CTypes::illegal_on_zero>);
  BOOST_CHECK_THROW(order.lead(poly,1), PBoRiGenericError<CTypes::illegal_on_zero>);
  BOOST_CHECK_THROW(order.lead(poly),PBoRiGenericError<CTypes::illegal_on_zero>);
  BOOST_CHECK_EQUAL(order.leadExp(poly,1), BooleExponent());
  BOOST_CHECK_EQUAL(order.leadExp(poly,0), BooleExponent());
  BOOST_CHECK_EQUAL(order.leadExp(poly), BooleExponent());
  BOOST_CHECK_THROW(order.leadFirst(poly),PBoRiGenericError<CTypes::illegal_on_zero>);
  poly = 1;
  BOOST_CHECK_EQUAL(order.lead(poly, 1), BooleMonomial(ring));
  BOOST_CHECK_EQUAL(order.lead(poly), BooleMonomial(ring));
  BOOST_CHECK_EQUAL(order.leadExp(poly, 1), BooleExponent());
  BOOST_CHECK_EQUAL(order.leadExp(poly), BooleExponent());
  BOOST_CHECK_EQUAL(order.leadFirst(poly), poly);
  poly = x*w + x*z + w*v*y;
  BOOST_CHECK_THROW(order.lead(poly, 0), PBoRiError);
  BOOST_CHECK_THROW(order.lead(poly, 0), std::exception);

  BOOST_CHECK_EQUAL(order.leadExp(poly,0), BooleExponent());
  poly=y;
  BOOST_CHECK_EQUAL(order.lead(poly,1), BooleMonomial(y));
  BOOST_CHECK_EQUAL(order.lead(poly,2), BooleMonomial(y));
  BOOST_CHECK_THROW(order.lead(poly,-1), std::exception);
  BOOST_CHECK_EQUAL(order.leadExp(poly,1), BooleExponent(y));
  BOOST_CHECK_EQUAL(order.leadExp(poly,2), BooleExponent(y));
  BOOST_CHECK_EQUAL(order.leadExp(poly,-1), BooleExponent());
  BooleMonomial leadterm = z*v*w;
  poly = x*y + x*v + leadterm;

  BOOST_CHECK_EQUAL(order.lead(poly, 3), BooleMonomial(leadterm));
  BOOST_CHECK_EQUAL(order.leadExp(poly,3), BooleExponent(leadterm));
  BOOST_CHECK_EQUAL(poly, x*y + x*v + z*v*w);

  BOOST_CHECK_THROW(order.lead(poly, 1), PBoRiError);
  BOOST_CHECK_EQUAL(order.lead(poly, 2), x*y);
  BOOST_CHECK_EQUAL(order.lead(poly, 3), z*v*w);
  BOOST_CHECK_THROW(order.lead(poly, -1), PBoRiError);
  BOOST_CHECK_EQUAL(order.leadExp(poly, 1), BooleExponent());
  BOOST_CHECK_EQUAL(order.leadExp(poly, 2), (x*y).exp());
  BOOST_CHECK_EQUAL(order.leadExp(poly, 3), (z*v*w).exp());
  BOOST_CHECK_THROW(order.lead(poly,1), std::exception);
  BOOST_CHECK_EQUAL(order.lead(poly,2), BooleMonomial(x*y));
  BOOST_CHECK_EQUAL(order.lead(poly,3), leadterm);
  BOOST_CHECK_THROW(order.lead(poly,-1), std::exception);
  BOOST_CHECK_EQUAL(order.leadExp(poly,1), BooleExponent());
  BOOST_CHECK_EQUAL(order.leadExp(poly,2), BooleExponent(x*y));
  BOOST_CHECK_EQUAL(order.leadExp(poly,3), BooleExponent(leadterm));
  BOOST_CHECK_EQUAL(order.leadExp(poly,-1), BooleExponent());
  poly += y;
  BOOST_CHECK_EQUAL(order.lead(poly,1), BooleMonomial(y));// Even as valid term y exists
  BOOST_CHECK_EQUAL(order.lead(poly,2), BooleMonomial(x*y));
  BOOST_CHECK_EQUAL(order.lead(poly,3), leadterm);
  BOOST_CHECK_THROW(order.lead(poly,-1), std::exception);
  BOOST_CHECK_EQUAL(order.leadExp(poly,1), BooleExponent(y));// Even as valid term y exists
  BOOST_CHECK_EQUAL(order.leadExp(poly,2), BooleExponent(x*y));
  BOOST_CHECK_EQUAL(order.leadExp(poly,3), BooleExponent(leadterm));
  BOOST_CHECK_EQUAL(order.leadExp(poly,-1), BooleExponent());
}

BOOST_AUTO_TEST_CASE(test_blocks) {

  BOOST_TEST_MESSAGE( "blockBegin, blockEnd, appendBlock, clearBlocks, lastBlockStart, lieInSameBlock" );
  order_type order;
  output_test_stream output;
  BoolePolyRing::block_iterator start(order.blockBegin()),finish(order.blockEnd());
  while (start != finish) {
    output << *start <<", ";
    ++start;
  }
  BOOST_CHECK(output.is_equal(""));
  BOOST_CHECK_THROW(order.appendBlock(-1), std::exception);
  order.appendBlock(0);
  order.appendBlock(2);
  order.appendBlock(6);
  start = order.blockBegin();
  finish = order.blockEnd();
  while (start != finish) {
    output << *start <<", ";
    ++start;
  }
  BOOST_CHECK(output.is_equal(""));
  BOOST_CHECK(order.lieInSameBlock(0,1));
  BOOST_CHECK(order.lieInSameBlock(-1,4));
  BOOST_CHECK_EQUAL(order.lastBlockStart(), 0);
  order.clearBlocks();
  start = order.blockBegin();
  finish = order.blockEnd();
  BOOST_CHECK(start==finish);
}


BOOST_AUTO_TEST_CASE(test_cover_constructors_and_destructors) {
  int order_code = CTypes::dlex;
  BoolePolyRing block_ring(5, order_code);

  BOOST_CHECK_EQUAL(block_ring.ordering().getOrderCode(), order_code);

  class Inherited: public order_type {};

  Inherited dummy;
  BOOST_CHECK_EQUAL(dummy.getOrderCode(), order_code);

  order_type self;
  BOOST_CHECK_EQUAL(self.getOrderCode(), order_code);

  order_type* pSelf = new order_type;
  BOOST_CHECK_EQUAL(pSelf->getOrderCode(), order_code);
  delete pSelf;

  pSelf = new Inherited;
  BOOST_CHECK_EQUAL(pSelf->getOrderCode(), order_code);
  delete pSelf;

  Inherited* pDummy = new Inherited;
  BOOST_CHECK_EQUAL(pDummy->getOrderCode(), order_code);
  delete pDummy;
}

BOOST_AUTO_TEST_SUITE_END()