summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/polygon/test/voronoi_robust_fpt_test.cpp
blob: 91853069181b237c421420b3828048bb1b3f9b9c (plain)
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
// Boost.Polygon library voronoi_robust_fpt_test.cpp file

//          Copyright Andrii Sydorchuk 2010-2012.
// Distributed under the Boost Software License, Version 1.0.
//    (See accompanying file LICENSE_1_0.txt or copy at
//          http://www.boost.org/LICENSE_1_0.txt)

// See http://www.boost.org for updates, documentation, and revision history.

#include <boost/core/lightweight_test.hpp>
#include <boost/polygon/detail/voronoi_ctypes.hpp>
#include <boost/polygon/detail/voronoi_robust_fpt.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <vector>
#include <cmath>
#include <ctime>

using boost::polygon::detail::int32;
using boost::polygon::detail::uint32;
using boost::polygon::detail::int64;
using boost::polygon::detail::fpt64;
using boost::polygon::detail::efpt64;
using boost::polygon::detail::extended_int;
using boost::polygon::detail::extended_exponent_fpt;
using boost::polygon::detail::robust_fpt;
using boost::polygon::detail::robust_dif;
using boost::polygon::detail::robust_sqrt_expr;
using boost::polygon::detail::type_converter_fpt;
using boost::polygon::detail::type_converter_efpt;
using boost::polygon::detail::ulp_comparison;

typedef robust_fpt<double> rfpt_type;
typedef type_converter_fpt to_fpt_type;
typedef type_converter_efpt to_efpt_type;
type_converter_fpt to_fpt;

void robust_fpt_constructors_test1()
{
  rfpt_type a = rfpt_type();
  BOOST_TEST_EQ(a.fpv(), 0.0);
  BOOST_TEST_EQ(a.re(), 0.0);
  BOOST_TEST_EQ(a.ulp(), 0);
}

void robust_fpt_constructors_test2()
{
  rfpt_type a(10.0, 1.0);
  BOOST_TEST_EQ(a.fpv(), 10.0);
  BOOST_TEST_EQ(a.re(), 1.0);
  BOOST_TEST_EQ(a.ulp(), 1.0);
}

void robust_fpt_constructors_test3()
{
  rfpt_type a(10.0);
  BOOST_TEST_EQ(a.fpv(), 10.0);
  BOOST_TEST_EQ(a.re(), 0.0);
  BOOST_TEST_EQ(a.ulp(), 0.0);
}

void robust_fpt_constructors_test4()
{
  rfpt_type a(10.0, 3.0);
  BOOST_TEST_EQ(a.fpv(), 10.0);
  BOOST_TEST_EQ(a.re(), 3.0);
  BOOST_TEST_EQ(a.ulp(), 3.0);

  rfpt_type b(10.0, 2.75);
  BOOST_TEST_EQ(b.fpv(), 10.0);
  BOOST_TEST_EQ(b.re(), 2.75);
  BOOST_TEST_EQ(b.ulp(), 2.75);
}

void robust_fpt_sum_test1()
{
  rfpt_type a(2.0, 5.0);
  rfpt_type b(3.0, 4.0);
  rfpt_type c = a + b;
  BOOST_TEST_EQ(c.fpv(), 5.0);
  BOOST_TEST_EQ(c.re(), 6.0);
  BOOST_TEST_EQ(c.ulp(), 6.0);

  c += b;
  BOOST_TEST_EQ(c.fpv(), 8.0);
  BOOST_TEST_EQ(c.re(), 7.0);
  BOOST_TEST_EQ(c.ulp(), 7.0);
}

void robust_fpt_sum_test2()
{
  rfpt_type a(3.0, 2.0);
  rfpt_type b(-2.0, 3.0);
  rfpt_type c = a + b;
  BOOST_TEST_EQ(c.fpv(), 1.0);
  BOOST_TEST_EQ(c.re(), 13.0);
  BOOST_TEST_EQ(c.ulp(), 13.0);

  c += b;
  BOOST_TEST_EQ(c.fpv(), -1.0);
  BOOST_TEST_EQ(c.re(), 20.0);
  BOOST_TEST_EQ(c.ulp(), 20.0);
}

void robust_fpt_dif_test1()
{
  rfpt_type a(2.0, 5.0);
  rfpt_type b(-3.0, 4.0);
  rfpt_type c = a - b;
  BOOST_TEST_EQ(c.fpv(), 5.0);
  BOOST_TEST_EQ(c.re(), 6.0);
  BOOST_TEST_EQ(c.ulp(), 6.0);

  c -= b;
  BOOST_TEST_EQ(c.fpv(), 8.0);
  BOOST_TEST_EQ(c.re(), 7.0);
  BOOST_TEST_EQ(c.ulp(), 7.0);
}

void robust_fpt_dif_test2()
{
  rfpt_type a(3.0, 2.0);
  rfpt_type b(2.0, 3.0);
  rfpt_type c = a - b;
  BOOST_TEST_EQ(c.fpv(), 1.0);
  BOOST_TEST_EQ(c.re(), 13.0);
  BOOST_TEST_EQ(c.ulp(), 13.0);

  c -= b;
  BOOST_TEST_EQ(c.fpv(), -1.0);
  BOOST_TEST_EQ(c.re(), 20.0);
  BOOST_TEST_EQ(c.ulp(), 20.0);
}

void robust_fpt_mult_test3()
{
  rfpt_type a(2.0, 3.0);
  rfpt_type b(4.0, 1.0);
  rfpt_type c = a * b;
  BOOST_TEST_EQ(c.fpv(), 8.0);
  BOOST_TEST_EQ(c.re(), 5.0);
  BOOST_TEST_EQ(c.ulp(), 5.0);

  c *= b;
  BOOST_TEST_EQ(c.fpv(), 32.0);
  BOOST_TEST_EQ(c.re(), 7.0);
  BOOST_TEST_EQ(c.ulp(), 7.0);
}

void robust_fpt_div_test1()
{
  rfpt_type a(2.0, 3.0);
  rfpt_type b(4.0, 1.0);
  rfpt_type c = a / b;
  BOOST_TEST_EQ(c.fpv(), 0.5);
  BOOST_TEST_EQ(c.re(), 5.0);
  BOOST_TEST_EQ(c.ulp(), 5.0);

  c /= b;
  BOOST_TEST_EQ(c.fpv(), 0.125);
  BOOST_TEST_EQ(c.re(), 7.0);
  BOOST_TEST_EQ(c.ulp(), 7.0);
}

void robust_dif_constructors_test()
{
  robust_dif<int> rd1;
  BOOST_TEST_EQ(rd1.pos(), 0);
  BOOST_TEST_EQ(rd1.neg(), 0);
  BOOST_TEST_EQ(rd1.dif(), 0);

  robust_dif<int> rd2(1);
  BOOST_TEST_EQ(rd2.pos(), 1);
  BOOST_TEST_EQ(rd2.neg(), 0);
  BOOST_TEST_EQ(rd2.dif(), 1);

  robust_dif<int> rd3(-1);
  BOOST_TEST_EQ(rd3.pos(), 0);
  BOOST_TEST_EQ(rd3.neg(), 1);
  BOOST_TEST_EQ(rd3.dif(), -1);

  robust_dif<int> rd4(1, 2);
  BOOST_TEST_EQ(rd4.pos(), 1);
  BOOST_TEST_EQ(rd4.neg(), 2);
  BOOST_TEST_EQ(rd4.dif(), -1);
}

void robust_dif_operators_test1()
{
  robust_dif<int> a(5, 2), b(1, 10);
  int dif_a = a.dif();
  int dif_b = b.dif();
  robust_dif<int> sum = a + b;
  robust_dif<int> dif = a - b;
  robust_dif<int> mult = a * b;
  robust_dif<int> umin = -a;
  BOOST_TEST_EQ(sum.dif(), dif_a + dif_b);
  BOOST_TEST_EQ(dif.dif(), dif_a - dif_b);
  BOOST_TEST_EQ(mult.dif(), dif_a * dif_b);
  BOOST_TEST_EQ(umin.dif(), -dif_a);
}

void robust_dif_operators_test2()
{
  robust_dif<int> a(5, 2);
  for (int b = -3; b <= 3; b += 6) {
    int dif_a = a.dif();
    int dif_b = b;
    robust_dif<int> sum = a + b;
    robust_dif<int> dif = a - b;
    robust_dif<int> mult = a * b;
    robust_dif<int> div = a / b;
    BOOST_TEST_EQ(sum.dif(), dif_a + dif_b);
    BOOST_TEST_EQ(dif.dif(), dif_a - dif_b);
    BOOST_TEST_EQ(mult.dif(), dif_a * dif_b);
    BOOST_TEST_EQ(div.dif(), dif_a / dif_b);
  }
}

void robust_dif_operators_test3()
{
  robust_dif<int> b(5, 2);
  for (int a = -3; a <= 3; a += 6) {
    int dif_a = a;
    int dif_b = b.dif();
    robust_dif<int> sum = a + b;
    robust_dif<int> dif = a - b;
    robust_dif<int> mult = a * b;
    BOOST_TEST_EQ(sum.dif(), dif_a + dif_b);
    BOOST_TEST_EQ(dif.dif(), dif_a - dif_b);
    BOOST_TEST_EQ(mult.dif(), dif_a * dif_b);
  }
}

void robust_dif_operators_test4()
{
  std::vector< robust_dif<int> > a4(4, robust_dif<int>(5, 2));
  std::vector< robust_dif<int> > b4(4, robust_dif<int>(1, 2));
  std::vector< robust_dif<int> > c4 = a4;
  c4[0] += b4[0];
  c4[1] -= b4[1];
  c4[2] *= b4[2];
  BOOST_TEST_EQ(c4[0].dif(), a4[0].dif() + b4[0].dif());
  BOOST_TEST_EQ(c4[1].dif(), a4[1].dif() - b4[1].dif());
  BOOST_TEST_EQ(c4[2].dif(), a4[2].dif() * b4[2].dif());
  a4[0] += b4[0].dif();
  a4[1] -= b4[1].dif();
  a4[2] *= b4[2].dif();
  a4[3] /= b4[3].dif();
  BOOST_TEST_EQ(c4[0].dif(), a4[0].dif());
  BOOST_TEST_EQ(c4[1].dif(), a4[1].dif());
  BOOST_TEST_EQ(c4[2].dif(), a4[2].dif());
  BOOST_TEST_EQ(c4[3].dif() / b4[3].dif(), a4[3].dif());
}

void robust_sqrt_expr_test1()
{
  robust_sqrt_expr<int32, fpt64, to_fpt_type> sqrt_expr;
  int32 A[1] = {10};
  int32 B[1] = {100};
  BOOST_TEST_EQ(sqrt_expr.eval1(A, B), 100.0);
}

void robust_sqrt_expr_test2()
{
  robust_sqrt_expr<int32, fpt64, to_fpt_type> sqrt_expr;
  int32 A[2] = {10, 30};
  int32 B[2] = {400, 100};
  BOOST_TEST_EQ(sqrt_expr.eval2(A, B), 500.0);
}

void robust_sqrt_expr_test3()
{
  robust_sqrt_expr<int32, fpt64, to_fpt_type> sqrt_expr;
  int32 A[2] = {10, -30};
  int32 B[2] = {400, 100};
  BOOST_TEST_EQ(sqrt_expr.eval2(A, B), -100.0);
}

void robust_sqrt_expr_test4()
{
  robust_sqrt_expr<int32, fpt64, to_fpt_type> sqrt_expr;
  int32 A[3] = {10, 30, 20};
  int32 B[3] = {4, 1, 9};
  BOOST_TEST_EQ(sqrt_expr.eval3(A, B), 110.0);
}

void robust_sqrt_expr_test5()
{
  robust_sqrt_expr<int32, fpt64, to_fpt_type> sqrt_expr;
  int32 A[3] = {10, 30, -20};
  int32 B[3] = {4, 1, 9};
  BOOST_TEST_EQ(sqrt_expr.eval3(A, B), -10.0);
}

void robust_sqrt_expr_test6()
{
  robust_sqrt_expr<int32, fpt64, to_fpt_type> sqrt_expr;
  int32 A[4] = {10, 30, 20, 5};
  int32 B[4] = {4, 1, 9, 16};
  BOOST_TEST_EQ(sqrt_expr.eval4(A, B), 130.0);
}

void robust_sqrt_expr_test7()
{
  robust_sqrt_expr<int32, fpt64, to_fpt_type> sqrt_expr;
  int32 A[4] = {10, 30, -20, -5};
  int32 B[4] = {4, 1, 9, 16};
  BOOST_TEST_EQ(sqrt_expr.eval4(A, B), -30.0);
}

void robust_sqrt_expr_test8()
{
  typedef extended_int<16> eint512;
  robust_sqrt_expr<eint512, efpt64, to_efpt_type> sqrt_expr;
  int32 A[4] = {1000, 3000, -2000, -500};
  int32 B[4] = {400, 100, 900, 1600};
  eint512 AA[4], BB[4];
  for (std::size_t i = 0; i < 4; ++i) {
    AA[i] = A[i];
    BB[i] = B[i];
  }
  BOOST_TEST_EQ(to_fpt(sqrt_expr.eval4(AA, BB)), -30000.0);
}

template <typename _int, typename _fpt>
class sqrt_expr_tester {
 public:
  static const std::size_t MX_SQRTS = 4;

  bool run() {
    static boost::mt19937 gen(static_cast<uint32>(time(NULL)));
    bool ret_val = true;
    for (std::size_t i = 0; i < MX_SQRTS; ++i) {
      a[i] = gen() & 1048575;
      int64 temp = gen() & 1048575;
      b[i] = temp * temp;
    }
    uint32 mask = (1 << MX_SQRTS);
    for (std::size_t i = 0; i < mask; i++) {
      fpt64 expected_val = 0.0;
      for (std::size_t j = 0; j < MX_SQRTS; j++) {
        if (i & (1 << j)) {
          A[j] = a[j];
          B[j] = b[j];
          expected_val += static_cast<fpt64>(a[j]) *
                          std::sqrt(static_cast<fpt64>(b[j]));
        } else {
          A[j] = -a[j];
          B[j] = b[j];
          expected_val -= static_cast<fpt64>(a[j]) *
                          std::sqrt(static_cast<fpt64>(b[j]));
        }
      }
      fpt64 received_val = to_fpt(sqrt_expr_.eval4(A, B));
      ret_val &= ulp_cmp(expected_val, received_val, 25) ==
                 ulp_comparison<fpt64>::EQUAL;
    }
    return ret_val;
  }

 private:
  robust_sqrt_expr<_int, _fpt, to_efpt_type> sqrt_expr_;
  ulp_comparison<fpt64> ulp_cmp;
  _int A[MX_SQRTS];
  _int B[MX_SQRTS];
  int64 a[MX_SQRTS];
  int64 b[MX_SQRTS];
};

void mpz_sqrt_evaluator_test()
{
  typedef extended_int<16> eint512;
  sqrt_expr_tester<eint512, efpt64> tester;
  for (int i = 0; i < 2000; ++i)
    BOOST_TEST(tester.run());
}

int main()
{
    robust_fpt_constructors_test1();
    robust_fpt_constructors_test2();
    robust_fpt_constructors_test3();
    robust_fpt_constructors_test4();
    robust_fpt_sum_test1();
    robust_fpt_sum_test2();
    robust_fpt_dif_test1();
    robust_fpt_dif_test2();
    robust_fpt_mult_test3();
    robust_fpt_div_test1();
    robust_dif_constructors_test();
    robust_dif_operators_test1();
    robust_dif_operators_test2();
    robust_dif_operators_test3();
    robust_dif_operators_test4();
    robust_sqrt_expr_test1();
    robust_sqrt_expr_test2();
    robust_sqrt_expr_test3();
    robust_sqrt_expr_test4();
    robust_sqrt_expr_test5();
    robust_sqrt_expr_test6();
    robust_sqrt_expr_test7();
    robust_sqrt_expr_test8();
    mpz_sqrt_evaluator_test();
    return boost::report_errors();
}