1 
2 #include <cmath>
3 #include <cstdlib>  // strtoul()
4 
5 #include "perm-all.h"
6 #include "bits/revbin.h"
7 #include "perm/revbinpermute.h"
8 //#include "aux1/auxprint.h"
9 //#include "aux1/aux1double.h"
10 
11 #include "fxtio.h"
12 #include "fxttypes.h"
13 #include "fxtalloca.h"
14 
15 
16 #include "aux1/copy.h"
17 #include "aux0/swap.h"
18 
19 
20 #include "test.h"  // last to include
21 
22 
23 static void
test_revbin_perm(ulong * gr,ulong ldn)24 test_revbin_perm(ulong *gr, ulong ldn)
25 {
26     ulong n = 1UL << ldn;
27     for (ulong z=0; z<n; ++z)
28     {
29         ulong rz=revbin(z,ldn);
30         if( rz>z )  swap2(gr[z],gr[rz]);
31     }
32 }
33 // -------------------------
34 
35 int
main(int argc,char ** argv)36 main(int argc, char **argv)
37 {
38   cout << "Running tests for permutations ... " << endl;
39 //  print_fxt_version();
40 
41   ulong minldn = 1, maxldn = 11;
42   if ( argc>1 )  minldn = strtoul(argv[1], nullptr, 10);
43   if ( argc>2 )  maxldn = strtoul(argv[2], nullptr, 10);
44 
45   ulong rep = 5;
46   if ( argc>3 )  rep = strtoul(argv[3], nullptr, 10);
47 
48 
49   ulong n = 1UL<<maxldn;
50   ALLOCA(ulong, tar, n);
51   ALLOCA(ulong, fr, n);
52   ALLOCA(ulong, gr, n);
53   const ulong *const ar = tar;
54 
55   for (ulong r=1; r<=rep; ++r)
56   {
57     cout << "+++++++++++++++++++++++++ PASS # " << r
58          << " (of " << rep << ") +++++++++++++++++++++++++" << endl;
59 
60     for (ulong ldn=minldn; ldn<=maxldn; ++ldn)
61     {
62       n = 1UL<<ldn;
63       cout << "====================  LDN = " << ldn
64            << "   N = " << n << " :" << endl;
65 
66       null(tar, n);
67 //      for (ulong i=0; i<n; ++i)  tar[i] = ;
68       random_permutation(tar, n);
69       reverse(tar, n);
70 //      cerr << "S=";  for (ulong i=0; i<n; ++i)  cerr << " " << tar[i];  cerr << endl;
71 //      set_seq(tar, n, 0.0, 1.0);
72 
73 
74       cout << "\n----- GRAY: -----" << endl;
75       EQUIV_EXACT( gray_permute(ar, fr, n);/**/ acopy(ar, gr, n); gray_permute(gr, n); );
76       if ( n==16 )
77       {
78           EQUIV_EXACT( gray_permute(ar, fr, n);/**/ acopy(ar, gr, n); gray_permute_16(gr); );
79       }
80       EQUIV_EXACT( acopy(ar, fr, n);/**/ acopy(ar, gr, n); gray_permute(ar, gr, n); inverse_gray_permute(gr, n); );
81 
82       EQUIV_EXACT( inverse_gray_permute(ar, fr, n);/**/ acopy(ar, gr, n); inverse_gray_permute(gr, n); );
83 
84       EQUIV_EXACT( gray_rev_permute(ar, fr, n);/**/ acopy(ar, gr, n); gray_rev_permute(gr, n); );
85       EQUIV_EXACT( inverse_gray_rev_permute(ar, fr, n);/**/ acopy(ar, gr, n); inverse_gray_rev_permute(gr, n); );
86 
87 
88       cout << "\n----- ZIP: -----" << endl;
89       EQUIV_EXACT( acopy(ar, fr, n); zip(fr, n);/**/ acopy(ar, gr, n); zip(gr, n); );
90 
91       EQUIV_EXACT( acopy(ar, fr, n);/**/ acopy(ar, gr, n); zip(gr, n); unzip(gr, n); );
92       EQUIV_EXACT( acopy(ar, fr, n);/**/ acopy(ar, gr, n); haar_permute(gr, n); inverse_haar_permute(gr, n); );
93 
94       EQUIV_EXACT( zip(ar, fr, n); unzip(fr,n); /**/ acopy(ar, gr, n); );
95       EQUIV_EXACT( unzip(ar, fr, n); zip(fr,n); /**/ acopy(ar, gr, n); );
96 
97       EQUIV_EXACT( zip(ar, fr, n);  /**/ acopy(ar, gr, n); zip(gr, n); );
98       EQUIV_EXACT( unzip(ar, fr, n);/**/ acopy(ar, gr, n); unzip(gr, n); );
99 
100 
101       cout << "\n----- REVBIN: -----" << endl;
102       EQUIV_EXACT( acopy(ar, fr, n); revbin_permute(fr, n);/**/
103                    acopy(ar, gr, n); test_revbin_perm(gr, ldn); );
104 //      for (ulong z=0; z<n; ++z) { ulong rz=revbin(z,ldn); if(r>z) swap2(gr[z],gr[rz]); }  );
105 
106 
107       cout << "\n----- EVENODDREV: -----" << endl;
108       EQUIV_EXACT( acopy(ar, fr, n); zip_rev(fr, n);/**/ acopy(ar, gr, n); zip_rev(gr, n); );
109 
110       EQUIV_EXACT( acopy(ar, fr, n);/**/ acopy(ar, gr, n); zip_rev(gr, n); unzip_rev(gr, n); );
111 
112       EQUIV_EXACT( zip_rev(ar, fr, n);/**/ acopy(ar, gr, n); zip_rev(gr, n); );
113 
114       EQUIV_EXACT( unzip_rev(ar, fr, n);/**/ acopy(ar, gr, n); unzip_rev(gr, n); );
115 
116 
117       cout << "\n----- PERMUTATION: -----" << endl;
118 //      acopy(ar, fr, n);/**/ acopy(ar, gr, n); make_inverse(gr, n);
119 //      cerr << "SS=";  for (ulong i=0; i<n; ++i)  cerr << " " << ar[i];  cerr << endl;
120 //      cerr << "II=";  for (ulong i=0; i<n; ++i)  cerr << " " << gr[i];  cerr << endl;
121 //      if ( 0==is_inverse(ar, gr, n) )  { exit(99); }
122 
123       EQUIV_EXACT( make_inverse(ar, fr, n);/**/ acopy(ar, gr, n); make_inverse(gr, n); );
124       random_permutation(fr, n);
125       EQUIV_EXACT( apply_permutation(ar, fr, gr, n); /**/ apply_permutation(ar, fr, n); );
126       random_permutation(fr, n);
127       EQUIV_EXACT( apply_inverse_permutation(ar, fr, gr, n); /**/ apply_inverse_permutation(ar, fr, n); );
128       random_permutation(fr, n);
129       EQUIV_EXACT( compose(ar, fr, gr, n); /**/ compose(ar, fr, n); );
130 
131 
132 // test makefile behavior with failure:
133 //      EQUIV_EXACT( unzip_rev(ar, fr, n);/**/ if (ldn<5) acopy(ar, gr, n); unzip_rev(gr, n); );
134     }
135 
136     cout << "+++++++++++++++++++ PASS # " << r
137          << " (of " << rep << ") ++++++ FINISHED +++++++++++++" << endl;
138   }
139 
140   cout << "\nOK, all passed." << endl;
141   return 0;
142 }
143 // -------------------------
144 /*
145 Local variables:
146 mode: C++
147 c-basic-offset: 2
148 End:
149 */
150