algebramix_doc 0.3
base_test.cpp
/******************************************************************************
* MODULE     : base_test.cpp
* DESCRIPTION: Test base transformers
* COPYRIGHT  : (C) 2009  Gregoire Lecerf
*******************************************************************************
* This software falls under the GNU general public license and comes WITHOUT
* ANY WARRANTY WHATSOEVER. See the file $TEXMACS_PATH/LICENSE for more details.
* If you don't have this file, write to the Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
******************************************************************************/

#include <algebramix/base_int.hpp>
#include <algebramix/base_integer.hpp>

using namespace mmx;

template<typename I> void
sample (I& a, nat n) {
  unsigned int mask= ~ ((~ 0u) << n);
  a= ((unsigned int) random ()) & mask;
}

void
sample (integer& a, nat n) {
  if (n == 0) { a= 0; return; }
  integer b (1);
  while (bit_size (a) < n) { a= b; b = b * random (); }
  if (random () & 1) a= -a;
}

template<typename Baser> void
test_base (nat m= 500) {
  typedef typename Baser::base C;
  typedef typename Baser::modulus_base I;  
  typedef typename Baser::modulus_base_variant V;  
  typedef modulus<I,V> M;

  mmout << "Testing " << cpp_demangle(typeid (Baser).name ()) << "\n";
  for (nat s=0; s < m;  s++) {
    mmout << "\rup to size " << s << "   " << flush_now;
    C a, b; sample (a, s); M p (3);
    Baser baser (p);
    vector<I> c;
    direct_base (c, a, baser);
    inverse_base (b, c, baser);
    if (a != b) {
      mmout << "\nError detected with:\n";
      mmout << "p= " << p << "\n";
      mmout << "a= " << a << "\n";
      mmout << "b= " << b << "\n";
      mmout << "c= " << c << "\n";
      exit (-1);
    }
    mmout << "\r";
  }
  mmout << "\n";
}

struct std_integer_ui {
  typedef integer base;
  typedef unsigned int modulus_base;
  typedef Modulus_variant(modulus_base) modulus_base_variant;
};

struct std_integer_si {
  typedef integer base;
  typedef int modulus_base;
  typedef Modulus_variant(modulus_base) modulus_base_variant;
};

int
main () {
  try {
    test_base<base_naive_transformer<unsigned int> > ();

    test_base<base_naive_transformer<integer> > ();

    test_base<base_naive_transformer<integer, std_integer_si> > ();

    test_base<base_dicho_transformer<integer> > ();

    test_base<base_blocks_transformer<
      base_naive_transformer<integer>,
      base_dicho_transformer<integer> > > ();

    test_base<base_integer_transformer<int> > ();
  }
  catch (exception& e) {
    mmout << e << "\n";
    return -1;
  }
  return 0;
}

 All Classes Namespaces Files Functions Variables Typedefs Friends Defines