Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r73074 - sandbox/SOC/2011/checks/libs/checks/test
From: pierre.talbot.6114_at_[hidden]
Date: 2011-07-13 17:40:12


Author: trademark
Date: 2011-07-13 17:40:12 EDT (Wed, 13 Jul 2011)
New Revision: 73074
URL: http://svn.boost.org/trac/boost/changeset/73074

Log:
Add tests on the modulus 11 function.
Correction of the "CHECK_MESSAGE" sentences.
Text files modified:
   sandbox/SOC/2011/checks/libs/checks/test/testModulus.cpp | 241 ++++++++++++++++++++++++++++++++++++++-
   1 files changed, 229 insertions(+), 12 deletions(-)

Modified: sandbox/SOC/2011/checks/libs/checks/test/testModulus.cpp
==============================================================================
--- sandbox/SOC/2011/checks/libs/checks/test/testModulus.cpp (original)
+++ sandbox/SOC/2011/checks/libs/checks/test/testModulus.cpp 2011-07-13 17:40:12 EDT (Wed, 13 Jul 2011)
@@ -89,6 +89,23 @@
 
 BOOST_AUTO_TEST_SUITE_END() //modulus_minimum_test
 
+
+BOOST_AUTO_TEST_SUITE(modulus_size_test)
+
+BOOST_AUTO_TEST_CASE(luhn_size_test)
+{
+}
+
+BOOST_AUTO_TEST_CASE(mod10_size_test)
+{
+}
+
+BOOST_AUTO_TEST_CASE(mod11_size_test)
+{
+}
+
+BOOST_AUTO_TEST_SUITE_END() //modulus_size_test
+
 BOOST_AUTO_TEST_SUITE(modulus_remainders_test)
 
 BOOST_AUTO_TEST_CASE(luhn_remainders_test)
@@ -208,11 +225,69 @@
     BOOST_CHECK_EQUAL( boost::checks::check_mod10( new_mod10_even.begin(), new_mod10_even.end(), NULL, new_mod10_even.size()), true);
     BOOST_CHECK_EQUAL( boost::checks::check_mod10( new_mod10_odd.begin(), new_mod10_odd.end(), NULL, new_mod10_odd.size()), true);
   }
-}
+} // BOOST_AUTO_TEST_CASE(mod10_remainders_test)
 
 BOOST_AUTO_TEST_CASE(mod11_remainders_test)
 {
   BOOST_TEST_MESSAGE( "Boost.Checks.Modulus.Modulus11 : tests on the remainders.");
+
+ std::string mod11_numbers_odd[] =
+ {
+ "123456711",
+ "123456762",
+ "123456703",
+ "123456754",
+ "123456835",
+ "123456746",
+ "123456797",
+ "123456738",
+ "123456789",
+ "12345672X",
+ "123456770"
+ };
+ std::string mod11_numbers_even[] =
+ {
+ "1234567881",
+ "1234567822",
+ "1234567873",
+ "1234567814",
+ "1234567865",
+ "1234567806",
+ "1234567857",
+ "1234567938",
+ "1234567849",
+ "123456789X",
+ "1234567830"
+ };
+ char check_digit;
+ // Test the numbers with a different remainder and an odd and even size of digits.
+ for(int i=0; i<11; ++i)
+ {
+ // Test the validation of the check digit with the mod11 algorithm with the size precised.
+ BOOST_CHECK( boost::checks::check_mod11 ( mod11_numbers_even[i].begin(), mod11_numbers_even[i].end(), mod11_numbers_even[i].size()) );
+ BOOST_CHECK( boost::checks::check_mod11 ( mod11_numbers_odd[i].begin(), mod11_numbers_odd[i].end(), mod11_numbers_odd[i].size() ) );
+ // Test the validation of the check digit with the mod11 algorithm without the size precised.
+ BOOST_CHECK( boost::checks::check_mod11 ( mod11_numbers_even[i].begin(), mod11_numbers_even[i].end()) );
+ BOOST_CHECK( boost::checks::check_mod11 ( mod11_numbers_odd[i].begin(), mod11_numbers_odd[i].end()) );
+ // Test the equality between the check digit computed and the last digit with the size precised.
+ BOOST_CHECK_EQUAL( boost::checks::compute_mod11(mod11_numbers_even[i].begin(), mod11_numbers_even[i].end(), mod11_numbers_even[i].size()-1), *(--mod11_numbers_even[i].end()) );
+ BOOST_CHECK_EQUAL( boost::checks::compute_mod11(mod11_numbers_odd[i].begin(), mod11_numbers_odd[i].end(), mod11_numbers_odd[i].size()-1), *(--mod11_numbers_odd[i].end()) );
+ // Test the equality between the check digit computed and the last digit without the size precised.
+ BOOST_CHECK_EQUAL( boost::checks::compute_mod11(mod11_numbers_even[i].begin(), mod11_numbers_even[i].end()-1), *(--mod11_numbers_even[i].end()) );
+ BOOST_CHECK_EQUAL( boost::checks::compute_mod11(mod11_numbers_odd[i].begin(), mod11_numbers_odd[i].end() -1), *(--mod11_numbers_odd[i].end()) );
+ // Test the coherence between compute and check
+ // compute
+ if(i != 9) // It don't work with 'X' because 'X' can only be the check digit and not another part of the number
+ {
+ BOOST_REQUIRE_NE ( check_digit = boost::checks::compute_mod11(mod11_numbers_even[i].begin(), mod11_numbers_even[i].end(), mod11_numbers_even[i].size()) , 0);
+ std::string new_mod11_even = mod11_numbers_even[i] + check_digit ;
+ BOOST_REQUIRE_NE ( check_digit = boost::checks::compute_mod11(mod11_numbers_odd[i].begin(), mod11_numbers_odd[i].end(), mod11_numbers_odd[i].size()) , 0);
+ std::string new_mod11_odd = mod11_numbers_odd[i] + check_digit ;
+ // check
+ BOOST_CHECK_EQUAL( boost::checks::check_mod11( new_mod11_even.begin(), new_mod11_even.end(), new_mod11_even.size()), true);
+ BOOST_CHECK_EQUAL( boost::checks::check_mod11( new_mod11_odd.begin(), new_mod11_odd.end(), new_mod11_odd.size()), true);
+ }
+ }
 }
 BOOST_AUTO_TEST_SUITE_END() //modulus_remainders_test
 
@@ -258,8 +333,8 @@
           {
         std::string luhn_alterate = luhn_numbers_odd[i] ;
                 luhn_alterate[j] = '0' + (luhn_alterate[j] + k) % 58 % 48 ;
- BOOST_CHECK_MESSAGE ( !boost::checks::check_luhn( luhn_alterate.begin(), luhn_alterate.end(), luhn_alterate.size() ), "The digit number " << j << "[" << luhn_numbers_odd[i][j] << "] is altered with "<< luhn_alterate[j] << "." );
- BOOST_CHECK_MESSAGE ( !boost::checks::check_luhn( luhn_alterate.begin(), luhn_alterate.end()), "The digit number " << j << "[" << luhn_numbers_odd[i][j] << "] is altered with "<< luhn_alterate[j] << "." );
+ BOOST_CHECK_MESSAGE ( !boost::checks::check_luhn( luhn_alterate.begin(), luhn_alterate.end(), luhn_alterate.size() ), "The digit " << luhn_numbers_odd[i][j] << " is altered by " << luhn_alterate[j] << "." );
+ BOOST_CHECK_MESSAGE ( !boost::checks::check_luhn( luhn_alterate.begin(), luhn_alterate.end()), "The digit " << luhn_numbers_odd[i][j] << " is altered by "<< luhn_alterate[j] << "." );
           }
         }
         // even : test the alteration of each digits
@@ -270,8 +345,8 @@
           {
         std::string luhn_alterate = luhn_numbers_even[i] ;
                 luhn_alterate[j] = '0' + (luhn_alterate[j] + k) % 58 % 48 ;
- BOOST_CHECK_MESSAGE ( !boost::checks::check_luhn( luhn_alterate.begin(), luhn_alterate.end(), luhn_alterate.size() ), "The digit number " << j << "[" << luhn_numbers_even[i][j] << "] is altered with "<< luhn_alterate[j] << "." );
- BOOST_CHECK_MESSAGE ( !boost::checks::check_luhn( luhn_alterate.begin(), luhn_alterate.end() ), "The digit number " << j << "[" << luhn_numbers_even[i][j] << "] is altered with "<< luhn_alterate[j] << "." );
+ BOOST_CHECK_MESSAGE ( !boost::checks::check_luhn( luhn_alterate.begin(), luhn_alterate.end(), luhn_alterate.size() ), "The digit " << luhn_numbers_even[i][j] << " is altered by "<< luhn_alterate[j] << "." );
+ BOOST_CHECK_MESSAGE ( !boost::checks::check_luhn( luhn_alterate.begin(), luhn_alterate.end() ), "The digit " << luhn_numbers_even[i][j] << " is altered by "<< luhn_alterate[j] << "." );
           }
         }
   }
@@ -317,8 +392,8 @@
           {
         std::string mod10_alterate = mod10_numbers_odd[i] ;
                 mod10_alterate[j] = '0' + (mod10_alterate[j] + k) % 58 % 48 ;
- BOOST_CHECK_MESSAGE ( !boost::checks::check_mod10( mod10_alterate.begin(), mod10_alterate.end(), NULL, mod10_alterate.size() ), "The digit number " << j << "[" << mod10_numbers_odd[i][j] << "] is altered with "<< mod10_alterate[j] << "." );
- BOOST_CHECK_MESSAGE ( !boost::checks::check_mod10( mod10_alterate.begin(), mod10_alterate.end(), NULL), "The digit number " << j << "[" << mod10_numbers_odd[i][j] << "] is altered with "<< mod10_alterate[j] << "." );
+ BOOST_CHECK_MESSAGE ( !boost::checks::check_mod10( mod10_alterate.begin(), mod10_alterate.end(), NULL, mod10_alterate.size() ), "The digit " << mod10_numbers_odd[i][j] << " is altered by " << mod10_alterate[j] << "." );
+ BOOST_CHECK_MESSAGE ( !boost::checks::check_mod10( mod10_alterate.begin(), mod10_alterate.end(), NULL), "The digit " << mod10_numbers_odd[i][j] << " is altered by " << mod10_alterate[j] << "." );
           }
         }
         // even : test the alteration of each digits
@@ -329,13 +404,80 @@
           {
         std::string mod10_alterate = mod10_numbers_even[i] ;
                 mod10_alterate[j] = '0' + (mod10_alterate[j] + k) % 58 % 48 ;
- BOOST_CHECK_MESSAGE ( !boost::checks::check_mod10( mod10_alterate.begin(), mod10_alterate.end(), NULL, mod10_alterate.size() ), "The digit number " << j << "[" << mod10_numbers_even[i][j] << "] is altered with "<< mod10_alterate[j] << "." );
- BOOST_CHECK_MESSAGE ( !boost::checks::check_mod10( mod10_alterate.begin(), mod10_alterate.end(), NULL ), "The digit number " << j << "[" << mod10_numbers_even[i][j] << "] is altered with "<< mod10_alterate[j] << ".");
+ BOOST_CHECK_MESSAGE ( !boost::checks::check_mod10( mod10_alterate.begin(), mod10_alterate.end(), NULL, mod10_alterate.size() ), "The digit " << mod10_numbers_even[i][j] << " is altered by " << mod10_alterate[j] << "." );
+ BOOST_CHECK_MESSAGE ( !boost::checks::check_mod10( mod10_alterate.begin(), mod10_alterate.end(), NULL ), "The digit " << mod10_numbers_even[i][j] << " is altered by " << mod10_alterate[j] << ".");
           }
         }
   }
 } //BOOST_AUTO_TEST_CASE(mod10_alteration_test)
 
+BOOST_AUTO_TEST_CASE(mod11_alteration_test)
+{
+ // Initialization of the numbers with a odd and even size of digit (11 digits for 11 remainders).
+ std::string mod11_numbers_odd[] =
+ {
+ "123456711",
+ "123456762",
+ "123456703",
+ "123456754",
+ "123456835",
+ "123456746",
+ "123456797",
+ "123456738",
+ "123456789",
+ "12345672X",
+ "123456770"
+ };
+ std::string mod11_numbers_even[] =
+ {
+ "1234567881",
+ "1234567822",
+ "1234567873",
+ "1234567814",
+ "1234567865",
+ "1234567806",
+ "1234567857",
+ "1234567938",
+ "1234567849",
+ "123456789X",
+ "1234567830"
+ };
+ // Test the alteration of one digit
+ for(int i=0; i<11; ++i)
+ {
+ // odd : test the alteration on each digits
+ for(unsigned int j=0; j< mod11_numbers_odd[i].length(); ++j)
+ {
+ // alteration + [1|2|3|4|5|6|7|8|9] % 10
+ for(int k=1; k <= 9; ++k)
+ {
+ std::string mod11_alterate = mod11_numbers_odd[i] ;
+ if(mod11_alterate[j] != 'X')
+ mod11_alterate[j] = '0' + (mod11_alterate[j] + k) % 58 % 48 ;
+ else
+ mod11_alterate[j] = '0' + k;
+ BOOST_CHECK_MESSAGE ( !boost::checks::check_mod11( mod11_alterate.begin(), mod11_alterate.end(), mod11_alterate.size() ), "The digit " << mod11_numbers_odd[i][j] << " is altered by "<< mod11_alterate[j] << "." );
+ BOOST_CHECK_MESSAGE ( !boost::checks::check_mod11( mod11_alterate.begin(), mod11_alterate.end()), "The digit " << mod11_numbers_odd[i][j] << " is altered by " << mod11_alterate[j] << "." );
+ }
+ }
+ // even : test the alteration of each digits
+ for(unsigned int j=0; j< mod11_numbers_even[i].length(); ++j)
+ {
+ // alteration + ([1|2|3|4|5|6|7|8|9] % 10)
+ for(int k=1; k <= 9; ++k)
+ {
+ std::string mod11_alterate = mod11_numbers_even[i] ;
+ if(mod11_alterate[j] != 'X')
+ mod11_alterate[j] = '0' + (mod11_alterate[j] + k) % 58 % 48 ;
+ else
+ mod11_alterate[j] = '0' + k;
+ BOOST_CHECK_MESSAGE ( !boost::checks::check_mod11( mod11_alterate.begin(), mod11_alterate.end(), mod11_alterate.size() ), "The digit " << mod11_numbers_even[i][j] << " is altered by "<< mod11_alterate[j] << "." );
+ BOOST_CHECK_MESSAGE ( !boost::checks::check_mod11( mod11_alterate.begin(), mod11_alterate.end() ), "The digit " << mod11_numbers_even[i][j] << " is altered by "<< mod11_alterate[j] << "." );
+ }
+ }
+ }
+} //BOOST_AUTO_TEST_CASE(mod11_alteration_test)
+
 BOOST_AUTO_TEST_SUITE_END() //modulus_alteration_test
 
 BOOST_AUTO_TEST_SUITE(modulus_transposition_test)
@@ -357,19 +499,50 @@
       luhn_transposition = luhn_transposition_odd;
       luhn_transposition[i] = luhn_transposition[j];
       luhn_transposition[j] = luhn_transposition_odd[i];
- BOOST_CHECK_MESSAGE ( !boost::checks::check_luhn( luhn_transposition.begin(), luhn_transposition.end(), luhn_transposition.size() ), "The digit number " << j << "[" << luhn_transposition[j] << "] is transposed with "<< luhn_transposition[i] << "." );
+ BOOST_CHECK_MESSAGE ( !boost::checks::check_luhn( luhn_transposition.begin(), luhn_transposition.end(), luhn_transposition.size() ), "The digit " << luhn_transposition[j] << " is transposed with "<< luhn_transposition[i] << "." );
       // Even
       luhn_transposition = luhn_transposition_even;
       luhn_transposition[i] = luhn_transposition[j];
       luhn_transposition[j] = luhn_transposition_even[i];
- BOOST_CHECK_MESSAGE ( !boost::checks::check_luhn( luhn_transposition.begin(), luhn_transposition.end(), luhn_transposition.size() ), "The digit number " << j << "[" << luhn_transposition[j] << "] is transposed with "<< luhn_transposition[i] << "." );
+ BOOST_CHECK_MESSAGE ( !boost::checks::check_luhn( luhn_transposition.begin(), luhn_transposition.end(), luhn_transposition.size() ), "The digit " << luhn_transposition[j] << " is transposed with "<< luhn_transposition[i] << "." );
     }
   }
 } // BOOST_AUTO_TEST_CASE(luhn_transposition_test)
 
+BOOST_AUTO_TEST_CASE(mod10_transposition_test)
+{
+ // Untestable because the weight is customizable.
+} // BOOST_AUTO_TEST_CASE(mod10_transposition_test)
+
+BOOST_AUTO_TEST_CASE(mod11_transposition_test)
+{
+ std::string mod11_transposition_odd = "12345678909"; //odd
+ std::string mod11_transposition_even = "123456789009"; // even
+ std::string mod11_transposition;
+ // For each digits.
+ for(int i=0; i < 9; ++i)
+ {
+ // Test a permutation with all the digits remaining.
+ for(int j=i+1; j < 10; j+=2)
+ {
+ // Odd
+ mod11_transposition = mod11_transposition_odd;
+ mod11_transposition[i] = mod11_transposition[j];
+ mod11_transposition[j] = mod11_transposition_odd[i];
+ BOOST_CHECK_MESSAGE ( !boost::checks::check_mod11( mod11_transposition.begin(), mod11_transposition.end(), mod11_transposition.size() ), "The digit " << mod11_transposition[j] << " is transposed with "<< mod11_transposition[i] << "." );
+ // Even
+ mod11_transposition = mod11_transposition_even;
+ mod11_transposition[i] = mod11_transposition[j];
+ mod11_transposition[j] = mod11_transposition_even[i];
+ BOOST_CHECK_MESSAGE ( !boost::checks::check_mod11( mod11_transposition.begin(), mod11_transposition.end(), mod11_transposition.size() ), "The digit " << mod11_transposition[j] << " is transposed with "<< mod11_transposition[i] << "." );
+ }
+ }
+} // BOOST_AUTO_TEST_CASE(mod11_transposition_test)
+
 BOOST_AUTO_TEST_SUITE_END() //modulus_transposition_test
   
 BOOST_AUTO_TEST_SUITE(modulus_encoding_test)
+
 BOOST_AUTO_TEST_CASE(luhn_encoding_test)
 {
   std::string raw_luhn = "123456781";
@@ -382,6 +555,28 @@
   BOOST_CHECK_EQUAL (boost::checks::check_luhn(luhn.begin(), luhn.end()), true);
 } // BOOST_AUTO_TEST_CASE(modulus_encoding_test)
 
+BOOST_AUTO_TEST_CASE(mod10_encoding_test)
+{
+ std::string raw_mod10 = "123456781";
+ std::wstring mod10(raw_mod10.begin(), raw_mod10.end());
+ BOOST_CHECK_EQUAL (boost::checks::compute_mod10<wchar_t>(mod10.begin(), mod10.end(), NULL), '3');
+ BOOST_CHECK_EQUAL (boost::checks::compute_mod10<wchar_t>(mod10.begin(), mod10.end(), NULL, 9), '3');
+
+ mod10 += '3';
+ BOOST_CHECK_EQUAL (boost::checks::check_mod10(mod10.begin(), mod10.end(), NULL), true);
+} // BOOST_AUTO_TEST_CASE(mod10_encoding_test)
+
+BOOST_AUTO_TEST_CASE(mod11_encoding_test)
+{
+ std::string raw_mod11 = "123456781";
+ std::wstring mod11(raw_mod11.begin(), raw_mod11.end());
+ BOOST_CHECK_EQUAL (boost::checks::compute_mod11<wchar_t>(mod11.begin(), mod11.end()), '4');
+ BOOST_CHECK_EQUAL (boost::checks::compute_mod11<wchar_t>(mod11.begin(), mod11.end(), 9), '4');
+
+ mod11 += '4';
+ BOOST_CHECK_EQUAL (boost::checks::check_mod11(mod11.begin(), mod11.end()), true);
+} // BOOST_AUTO_TEST_CASE(mod11_encoding_test)
+
 BOOST_AUTO_TEST_SUITE_END() //modulus_encoding_test
   
 BOOST_AUTO_TEST_SUITE(modulus_collection_test)
@@ -411,6 +606,20 @@
     BOOST_CHECK_EQUAL( boost::checks::check_mod10(mod10.begin(), mod10.end(), NULL), true);
 } // BOOST_AUTO_TEST_CASE(mod10_collection_test)
 
+BOOST_AUTO_TEST_CASE(mod11_collection_test)
+{
+ std::list<char> mod11;
+ for(char i=48; i < 53; ++i)
+ mod11.push_back(i);
+ char check_digit;
+ BOOST_REQUIRE_NE( check_digit = boost::checks::compute_mod11(mod11.begin(), mod11.end(), 5) , 0);
+ BOOST_REQUIRE_EQUAL( boost::checks::compute_mod11(mod11.begin(), mod11.end()) , check_digit);
+ mod11.push_back(check_digit);
+ BOOST_CHECK_EQUAL( boost::checks::check_mod11(mod11.begin(), mod11.end(), 6), true);
+ BOOST_CHECK_EQUAL( boost::checks::check_mod11(mod11.begin(), mod11.end()), true);
+} // BOOST_AUTO_TEST_CASE(mod11_collection_test)
+
+
 BOOST_AUTO_TEST_SUITE_END() //modulus_collection_test
 
 
@@ -423,7 +632,15 @@
   BOOST_CHECK_EQUAL( boost::checks::compute_mod10(mod10.begin(), mod10.end(), weight, 4),
                      boost::checks::compute_mod10(mod10.begin(), mod10.end(), NULL, 4));
 
+ // Test with a weight of 1234
   for(int i=0; i<4; ++i) weight[i] = i+1;
   BOOST_CHECK_EQUAL( boost::checks::compute_mod10(mod10.begin(), mod10.end(), weight, 4), '0');
-}
+
+ // Test with a weight of 0
+ for(int i=0; i<4; ++i) weight[i] = i+1;
+ unsigned int weight2[] = {0};
+ BOOST_CHECK_EQUAL( boost::checks::compute_mod10(mod10.begin(), mod10.end(), weight, 4),
+ boost::checks::compute_mod10(mod10.begin(), mod10.end(), weight2, 4));
+}// mod10_weight_test
+
 BOOST_AUTO_TEST_SUITE_END() //modulus_weight_test
\ No newline at end of file


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk