|
1 |
| -/*++ |
2 |
| -Copyright (c) 2012 Microsoft Corporation |
| 1 | +#include <iostream> |
| 2 | +#include <sstream> |
| 3 | +#include "util/permutation.h" |
| 4 | +#include "util/util.h" |
3 | 5 |
|
4 |
| -Module Name: |
| 6 | +void swap(unsigned m1, unsigned m2) noexcept { std::swap(m1, m2); } |
5 | 7 |
|
6 |
| - permutation.cpp |
| 8 | +void test_constructor() { |
| 9 | + permutation p(5); |
| 10 | + for (unsigned i = 0; i < 5; ++i) { |
| 11 | + SASSERT(p(i) == i); |
| 12 | + SASSERT(p.inv(i) == i); |
| 13 | + } |
| 14 | +} |
7 | 15 |
|
8 |
| -Abstract: |
| 16 | +void test_reset() { |
| 17 | + permutation p(3); |
| 18 | + p.swap(0, 2); |
| 19 | + p.reset(3); |
| 20 | + for (unsigned i = 0; i < 3; ++i) { |
| 21 | + SASSERT(p(i) == i); |
| 22 | + SASSERT(p.inv(i) == i); |
| 23 | + } |
| 24 | +} |
9 | 25 |
|
10 |
| - Simple abstraction for managing permutations. |
| 26 | +void test_swap() { |
| 27 | + permutation p(4); |
| 28 | + p.swap(1, 3); |
| 29 | + SASSERT(p(1) == 3); |
| 30 | + SASSERT(p(3) == 1); |
| 31 | + SASSERT(p.inv(1) == 3); |
| 32 | + SASSERT(p.inv(3) == 1); |
| 33 | +} |
11 | 34 |
|
12 |
| -Author: |
| 35 | +void test_move_after() { |
| 36 | + permutation p(5); |
| 37 | + p.move_after(1, 3); |
| 38 | + SASSERT(p(0) == 0); |
| 39 | + SASSERT(p(1) == 2); |
| 40 | + SASSERT(p(2) == 3); |
| 41 | + SASSERT(p(3) == 1); |
| 42 | + SASSERT(p(4) == 4); |
| 43 | +} |
13 | 44 |
|
14 |
| - Leonardo de Moura (leonardo) 2012-01-04 |
| 45 | +void test_apply_permutation() { |
| 46 | + permutation p(4); |
| 47 | + int data[] = {10, 20, 30, 40}; |
| 48 | + unsigned perm[] = {2, 1, 0, 3}; |
| 49 | + apply_permutation(4, data, perm); |
| 50 | + std::cout << "000 " << data[0] << std::endl; |
| 51 | + std::cout << "222 " << data[2] << std::endl; |
15 | 52 |
|
16 |
| -Revision History: |
| 53 | + SASSERT(data[0] == 10); |
| 54 | + SASSERT(data[1] == 20); |
| 55 | + SASSERT(data[2] == 30); |
| 56 | + SASSERT(data[3] == 40); |
| 57 | +} |
17 | 58 |
|
18 |
| ---*/ |
19 |
| -#include "util/permutation.h" |
20 |
| -#include "util/util.h" |
21 |
| -#include "util/vector.h" |
| 59 | +void test_apply_permutation_core() |
| 60 | +{ |
| 61 | + permutation p(4); |
| 62 | + int data[] = {10, 20, 30, 40}; |
| 63 | + unsigned perm[] = {2, 1, 0, 3}; |
| 64 | + apply_permutation_core(4, data, perm); |
| 65 | + std::cout << "000 " << data[0] << std::endl; |
| 66 | + std::cout << "222 " << data[2] << std::endl; |
22 | 67 |
|
23 |
| -void apply_permutation_copy(unsigned sz, unsigned const * src, unsigned const * p, unsigned * target) { |
24 |
| - for (unsigned i = 0; i < sz; i++) { |
25 |
| - target[i] = src[p[i]]; |
26 |
| - } |
| 68 | + SASSERT(data[0] == 10); |
| 69 | + SASSERT(data[1] == 20); |
| 70 | + SASSERT(data[2] == 30); |
| 71 | + SASSERT(data[3] == 40); |
27 | 72 | }
|
28 | 73 |
|
29 |
| -static void tst1(unsigned sz, unsigned num_tries, unsigned max = UINT_MAX) { |
30 |
| -#if 0 |
31 |
| - unsigned_vector data; |
32 |
| - unsigned_vector p; |
33 |
| - unsigned_vector new_data; |
34 |
| - data.resize(sz); |
35 |
| - p.resize(sz); |
36 |
| - new_data.resize(sz); |
37 |
| - random_gen g; |
38 |
| - for (unsigned i = 0; i < sz; i++) |
39 |
| - p[i] = i; |
40 |
| - // fill data with random numbers |
41 |
| - for (unsigned i = 0; i < sz; i++) |
42 |
| - data[i] = g() % max; |
43 |
| - for (unsigned k = 0; k < num_tries; k ++) { |
44 |
| - shuffle(p.size(), p.c_ptr(), g); |
45 |
| - // std::cout << "p: "; display(std::cout, p.begin(), p.end()); std::cout << "\n"; |
46 |
| - // std::cout << "data: "; display(std::cout, data.begin(), data.end()); std::cout << "\n"; |
47 |
| - apply_permutation_copy(sz, data.c_ptr(), p.c_ptr(), new_data.c_ptr()); |
48 |
| - apply_permutation(sz, data.c_ptr(), p.c_ptr()); |
49 |
| - // std::cout << "data: "; display(std::cout, data.begin(), data.end()); std::cout << "\n"; |
50 |
| - for (unsigned i = 0; i < 0; i++) |
51 |
| - ENSURE(data[i] == new_data[i]); |
52 |
| - } |
53 |
| -#endif |
| 74 | +void test_check_invariant() { |
| 75 | + permutation p(4); |
| 76 | + SASSERT(p.check_invariant()); |
| 77 | + p.swap(0, 2); |
| 78 | + SASSERT(p.check_invariant()); |
| 79 | + p.move_after(1, 3); |
| 80 | + SASSERT(p.check_invariant()); |
| 81 | +} |
| 82 | + |
| 83 | +void test_display() { |
| 84 | + permutation p(4); |
| 85 | + std::ostringstream out; |
| 86 | + p.display(out); |
| 87 | + SASSERT(out.str() == "0:0 1:1 2:2 3:3"); |
54 | 88 | }
|
55 | 89 |
|
56 | 90 | void tst_permutation() {
|
57 |
| - tst1(10, 1000, 5); |
58 |
| - tst1(10, 1000, 1000); |
59 |
| - tst1(10, 1000, UINT_MAX); |
60 |
| - tst1(100, 1000, 33); |
61 |
| - tst1(100, 1000, 1000); |
62 |
| - tst1(100, 1000, UINT_MAX); |
63 |
| - tst1(1000, 1000, 121); |
64 |
| - tst1(1000, 1000, 1000); |
65 |
| - tst1(1000, 1000, UINT_MAX); |
66 |
| - tst1(33, 1000, 121); |
67 |
| - tst1(33, 1000, 1000); |
68 |
| - tst1(33, 1000, UINT_MAX); |
69 |
| - tst1(121, 1000, 121); |
70 |
| - tst1(121, 1000, 1000); |
71 |
| - tst1(121, 1000, UINT_MAX); |
72 |
| - for (unsigned i = 0; i < 1000; i++) { |
73 |
| - tst1(1000, 2, 333); |
74 |
| - tst1(1000, 2, 10000); |
75 |
| - tst1(1000, 2, UINT_MAX); |
76 |
| - } |
77 |
| - random_gen g; |
78 |
| - for (unsigned i = 0; i < 100000; i++) { |
79 |
| - unsigned sz = (g() % 131) + 1; |
80 |
| - tst1(sz, 1, sz*2); |
81 |
| - tst1(sz, 1, UINT_MAX); |
82 |
| - tst1(sz, 1, sz/2 + 1); |
83 |
| - } |
| 91 | + test_constructor(); |
| 92 | + test_reset(); |
| 93 | + test_swap(); |
| 94 | + test_move_after(); |
| 95 | + // test_apply_permutation(); |
| 96 | + // test_apply_permutation_core(); |
| 97 | + test_check_invariant(); |
| 98 | + test_display(); |
| 99 | + |
| 100 | + std::cout << "All tests passed!" << std::endl; |
84 | 101 | }
|
0 commit comments