LCOV - code coverage report
Current view: top level - utilities/test - big_natural_number_test.cpp (source / functions) Hit Total Coverage
Test: mcrl2_coverage.info.cleaned Lines: 76 76 100.0 %
Date: 2024-04-21 03:44:01 Functions: 10 10 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : // Author(s): Jan Friso Groote
       2             : // Copyright: see the accompanying file COPYING or copy at
       3             : // https://github.com/mCRL2org/mCRL2/blob/master/COPYING
       4             : //
       5             : // Distributed under the Boost Software License, Version 1.0.
       6             : // (See accompanying file LICENSE_1_0.txt or copy at
       7             : // http://www.boost.org/LICENSE_1_0.txt)
       8             : //
       9             : /// \file big_numbers_test.cpp
      10             : /// \brief Test whether the class big_natural_numbers works properly.
      11             : 
      12             : #define BOOST_AUTO_TEST_MAIN
      13             : #include <boost/test/included/unit_test.hpp>
      14             : 
      15             : #include "mcrl2/utilities/big_numbers.h"
      16             : 
      17             : using namespace mcrl2;
      18             : using namespace mcrl2::utilities;
      19             : 
      20          22 : void test_print(const std::string& xs)
      21             : {
      22          22 :   const big_natural_number x(xs);
      23          22 :   std::stringstream ss;
      24          22 :   ss << x;
      25          22 :   std::cerr << "Check print \n" << xs << "\n" << x << "\n" << ss.str() << "\n-------------\n";
      26          22 :   BOOST_CHECK(xs==ss.str());
      27          22 : }
      28             : 
      29          22 : void test_simple(const std::string& xs, const std::string& ys)
      30             : {
      31          22 :   const big_natural_number x(xs);
      32          22 :   const big_natural_number y(ys);
      33             :   
      34          22 :   std::cerr << "Check simple add and subtract " << xs << " and " << ys << "\n";
      35          22 :   std::cerr << "LHS: " << x << "\n";
      36          22 :   std::cerr << "RHS: " << (x+y)-y << "\n";
      37          22 :   BOOST_CHECK(x==(x+y)-y);
      38          22 : }
      39             : 
      40          22 : void test_div_mod(const std::string& xs, const std::string& ys)
      41             : {
      42          22 :   const big_natural_number x(xs);
      43          22 :   const big_natural_number y(ys);
      44             :   
      45          22 :   if (!y.is_zero())
      46             :   { 
      47          21 :     std::cerr << "Check div mod for " << xs << " and " << ys << "\n";
      48             :     // std::cerr << "x/y " << (x/y) << "\n";
      49             :     // std::cerr << "y*(x/y) " << (y*(x/y)) << "\n";
      50             :     // std::cerr << "x%y " << (x%y) << "\n";
      51          21 :     std::cerr << "LHS: " << x << "\n";
      52          21 :     std::cerr << "RHS: " << y*(x/y)+(x % y) << "\n";
      53          21 :     BOOST_CHECK(x==y*(x/y)+(x % y));
      54             :   }
      55          22 : }
      56             : 
      57          22 : void test_multiply(const std::string& xs, const std::string& ys)
      58             : { 
      59          22 :   big_natural_number x(xs);
      60          22 :   big_natural_number y(ys);
      61             :   
      62          22 :   std::cerr << "Check multiply " << xs << " and " << ys << "\n";
      63          22 :   std::cerr << "LHS: " << x*y << "\n";
      64          22 :   std::cerr << "RHS: " << y*x << "\n";
      65          22 :   BOOST_CHECK(x*y==y*x);
      66          22 : }
      67             : 
      68          22 : void test_plus_minus_multiply(const std::string& xs, const std::string& ys)
      69             : { 
      70          22 :   big_natural_number x(xs);
      71          22 :   big_natural_number y(ys);
      72             :   
      73          22 :   if (y>x)
      74             :   {
      75          10 :     swap(x,y);
      76             :   }
      77          22 :   BOOST_CHECK(x>=y);
      78             : 
      79          22 :   std::cerr << "Check plus minus " << xs << " and " << ys << "\n";
      80             : 
      81             :   // std::cerr << "x+y: " << (x+y) << "\n";
      82             :   // std::cerr << "x-y: " << (x-y) << "\n";
      83             :   // std::cerr << "x*x: " << x*x << "\n";
      84             :   // std::cerr << "y*y: " << y*y << "\n";
      85          22 :   std::cerr << "LHS: " << (x+y)*(x-y) << "\n";
      86          22 :   std::cerr << "RHS: " << x*x-y*y << "\n";
      87          22 :   BOOST_CHECK((x+y)*(x-y)==x*x-y*y);
      88          22 : }
      89             : 
      90          11 : void test(const std::string& xs, const std::string& ys)
      91             : {
      92          11 :   test_print(xs);
      93          11 :   test_print(ys);
      94          11 :   test_simple(xs,ys);
      95          11 :   test_simple(ys,xs);
      96          11 :   test_multiply(xs,ys);
      97          11 :   test_multiply(ys,xs);
      98          11 :   test_plus_minus_multiply(xs,ys);
      99          11 :   test_plus_minus_multiply(ys,xs);
     100          11 :   test_div_mod(xs,ys);
     101          11 :   test_div_mod(ys,xs);
     102          11 : }
     103             : 
     104           2 : BOOST_AUTO_TEST_CASE(big_natural_number_test)
     105             : {
     106           1 :   const big_natural_number x(10);
     107           1 :   const big_natural_number y(0);
     108           1 :   const big_natural_number z(100);
     109           1 :   BOOST_CHECK(x-x==y); 
     110           1 :   BOOST_CHECK(x*x==z); 
     111           1 : }
     112             : 
     113           2 : BOOST_AUTO_TEST_CASE(cumulative_tests)
     114             : {
     115           1 :   test("12","14");
     116           1 :   test("15","9");
     117           1 :   test("123987498734298734987","0");
     118           1 :   test("123987498734298734987","1");
     119           1 :   test("1400000000000000000021498639574985789345798","12000000000000000000123");
     120           1 :   test("12000000000000000000123","1400000000000000000021498639574985789345798");
     121           1 :   test("349857349587453098713409835719348571930857","34908657984275902384759028475908345");
     122           1 :   test("34985430320129384710938471039561390847109398734601956601293846019285609853607349587453098713409835719348571930857","34908657984275902384759028475908345");
     123           1 :   test("34985430320129384710938471039561390847109398734601956601293846019285609853607349587453098713409835719348571930857",
     124             :        "349086579842759023847590143958749587439587209346509145610983654498365918365918650914657816458768976519846519846519846123456789000909029485610945645198435628475908345");
     125           1 :   test("34985431223981954640133634673587613874569183765329875682716348576138476108576387546187658127653201876510287356021876530287165023817650187635081237650812376501876350871236501287365012873650182735610237560000000000000000000000000320129384710938471039561390847109398734601956601293846019285609853607349587453098713409835719348571930857",
     126             :        "34908657984275902384759014395874958743958720934650914561098365449836591836591865091465781645876897651984651984651984612345678900090913498173498173419837409136798130865139580010203999992929934847837413984719835610934601958632091863509826159836159861938651983651348716325918635748372987429029485610945645198435628475908345");
     127             : 
     128           1 :   std::string big_number("34985431223981954640133634673587613874569183765329875682716348576138476108576387546187658127653201876510287356021876530287165023817650187635081237650812376501876350871236501287365012873650182735610237560000000000000000000000000320129384710938471039561390847109398734601956601293846019285609853607349587453098713409835719348571930857");
     129           1 :   test(big_number,big_number);
     130           1 : }

Generated by: LCOV version 1.14