OKlibrary  0.2.1.6
Algebra_Concepts_tests.hpp
Go to the documentation of this file.
00001 // Oliver Kullmann, 7.11.2004 (Swansea)
00002 /* Copyright 2004 - 2007 Oliver Kullmann
00003 This file is part of the OKlibrary. OKlibrary is free software; you can redistribute
00004 it and/or modify it under the terms of the GNU General Public License as published by
00005 the Free Software Foundation and included in this library; either version 3 of the
00006 License, or any later version. */
00007 
00008 #ifndef ALGEBRACONCEPTSTESTSWAECHTER_jjdn55Ra
00009 
00010 #define ALGEBRACONCEPTSTESTSWAECHTER_jjdn55Ra
00011 
00012 #include <cassert>
00013 
00014 #include <OKlib/General/NumberTheory_Models.hpp>
00015 
00016 namespace Algebra_Concepts_tests {
00017 
00018   // OK, 7.11.2004
00019   // TO IMPROVE: to be completed
00020   template <template <unsigned long int modulus, typename Int> class Z_mod_n>
00021   struct Test_Z_mod_n {
00022     Test_Z_mod_n() {
00023       {
00024   typedef int Int;
00025   const Int m = 2;
00026   typedef Z_mod_n<m, Int> Z_mod_m;
00027 
00028   assert(Z_mod_m::null() == Z_mod_m(0));
00029   assert(Z_mod_m::unit() == Z_mod_m(1));
00030   assert(Z_mod_m::unit() == Z_mod_m(-1));
00031 
00032   for (Int i = 0; i < m; ++i) {
00033     Z_mod_m i_m(i);
00034     assert(-i_m == i_m);
00035     for (Int j = 0; j < m; ++j) {
00036       Z_mod_m j_m(j);
00037       assert(i_m + j_m == Z_mod_m((i + j) % m));
00038       assert(i_m * j_m == Z_mod_m((i * j) % m));
00039       assert(i_m - j_m == ((j) ? Z_mod_m(Int(not i)) : i_m));
00040       if (j == 1)
00041         assert(i_m / j_m == i_m);
00042     }
00043   }
00044   {
00045     Int count_inv = 0;
00046     for (Int i = 0; i < m; ++i)
00047       if (Z_mod_m(i).invert())
00048         ++count_inv;
00049     assert(count_inv == NumberTheory::euler_phi_brute_force(m));
00050   }
00051       }
00052       {
00053   typedef int Int;
00054   const Int m = 3;
00055   typedef Z_mod_n<m, Int> Z_mod_m;
00056 
00057   assert(Z_mod_m::null() == Z_mod_m(0));
00058   assert(Z_mod_m::unit() == Z_mod_m(1));
00059   assert(Z_mod_m::unit() == Z_mod_m(-2));
00060   assert(Z_mod_m(2) == Z_mod_m(-1));
00061   assert(Z_mod_m(3) == Z_mod_m(0));
00062   assert(Z_mod_m(4) == Z_mod_m(1));
00063 
00064   assert(Z_mod_m(2) / Z_mod_m(2) == Z_mod_m(1));
00065 
00066   const Int iterations = 3 * m;
00067   for (Int i = 0; i < iterations ; ++i) {
00068     const Z_mod_m i_m(i);
00069     assert(-i_m == Z_mod_m(m - i));
00070     for (Int j = 0; j < iterations; ++j) {
00071       const Z_mod_m j_m(j);
00072       assert(i_m + j_m == Z_mod_m((i + j) % m));
00073       assert(i_m + j_m == Z_mod_m(i + j));
00074       assert(i_m * j_m == Z_mod_m((i * j) % m));
00075       assert(i_m * j_m == Z_mod_m(i * j));
00076       assert(i_m - j_m == Z_mod_m(i-j));
00077       if (j % m)
00078         assert(i_m / j_m == i_m * j_m);
00079     }
00080   }
00081   {
00082     Int count_inv = 0;
00083     for (Int i = 0; i < m; ++i)
00084       if (Z_mod_m(i).invert())
00085         ++count_inv;
00086     assert(count_inv == NumberTheory::euler_phi_brute_force(m));
00087   }
00088       }
00089       {
00090   typedef int Int;
00091   const Int m = 60;
00092   typedef Z_mod_n<m, Int> Z_mod_m;
00093 
00094   assert(Z_mod_m::null() == Z_mod_m(0));
00095   assert(Z_mod_m::unit() == Z_mod_m(1));
00096   assert(Z_mod_m::unit() == Z_mod_m(-59));
00097   assert(Z_mod_m(2) == Z_mod_m(-58));
00098   assert(Z_mod_m(3) == Z_mod_m(-57));
00099   assert(Z_mod_m(4) == Z_mod_m(-56));
00100 
00101   assert(Z_mod_m(2) * Z_mod_m(30) == Z_mod_m(0));
00102   assert(Z_mod_m(11) / Z_mod_m(49) == Z_mod_m(-1));
00103   {
00104     const Int iterations = 3 * m;
00105     for (Int i = 0; i < iterations ; ++i) {
00106       const Z_mod_m i_m(i);
00107       assert(-i_m == Z_mod_m(m - i));
00108       for (Int j = 0; j < iterations; ++j) {
00109         const Z_mod_m j_m(j);
00110         assert(i_m + j_m == Z_mod_m((i + j) % m));
00111         assert(i_m + j_m == Z_mod_m(i + j));
00112         assert(i_m * j_m == Z_mod_m((i * j) % m));
00113         assert(i_m * j_m == Z_mod_m(i * j));
00114         assert(i_m - j_m == Z_mod_m(i-j));
00115       }
00116     }
00117   }
00118   {
00119     Int count_inv = 0;
00120     for (Int i = 0; i < m; ++i)
00121       if (Z_mod_m(i).invert())
00122         ++count_inv;
00123     assert(count_inv == NumberTheory::euler_phi_brute_force(m));
00124   }
00125       }
00126     }
00127   };
00128 
00129 }
00130 
00131 #endif