Click here to Skip to main content
15,891,621 members
Articles / Programming Languages / C++

Error Detection Based on Check Digit Schemes

Rate me:
Please Sign up or sign in to vote.
4.72/5 (27 votes)
27 Nov 2007CPOL12 min read 91K   2.9K   50  
A Survey of Popular Check Digit Schemes
/* coretest.js  Version 1.0.0  24-Jun-05
 * http://modp.com/release/checkdigits/
 * Copyright 2005, Nick Galbreath.  All Rights Reserved.
 * Terms of use: standard BSD License at http://modp.com/license-bsd.txt
 * or http://www.opensource.org/licenses/bsd-license.php
 */
var checkcases= [
		 "0000000000",
		 "1111111111",
		 "2222222222",
		 "3333333333",
		 "4444444444",
		 "5555555555",
		 "6666666666",
		 "7777777777",
		 "8888888888",
		 "9999999999",
		 "1234567890",
		 "0123456789",
		 "0246813579",
		 "0369147258",
		 "9048152637",
		 "0516273849"
		 ];

function testCheck() {
  var check = getCheckInstance();
  for (var i = 0; i < checkcases.length; ++i) {
    var e = check.encode(checkcases[i]);
    // make sure it did something
    assertTrue(e.length > checkcases[i].length);

    // make sure data is correct
    assertEquals(checkcases[i], check.getData(e));
    
    // make sure it verifies
    assertTrue(e + " did not verify", check.verify(e))
  }
}

/**
 * At each position, test every possible error
 */
function testSingleError() {
  var check = getCheckInstance();
  for (var i = 0; i < checkcases.length; ++i) {
     var c = check.encode(checkcases[i]);
    for (var j = 0; j < c.length; ++j) {
      for (var k = 1; k <= 9; ++k) {
	// nc is the 'new character' that is in error
	var nc = (c.charCodeAt(j) - 48 + k) % 10;
	// create new string
	var nsx = c.substring(0, j) + nc + c.substring(j+1, c.length);
	//Assert.IsFalse(check.verify(nsx));
	assertFalse(c + " to " + nsx + " should not validate", check.verify(nsx));
      }
    }
  }
}

function testAdjacentTraspositions() {
  var check = getCheckInstance();
  for (var i = 0; i < checkcases.length; ++i) {
    var c = check.encode(checkcases[i]);
    for (var j = 0; j < c.length - 1; ++j) {
      // transpose at (j, j+1)
      var nc = c.substring(0,j) + 
	c.charAt(j+1) + c.charAt(j)+
	c.substring(j+2);
      // make sure we didn't screw up
      assertEquals(nc.length, c.length);
      
      // if original input isn't symmetrical, do a test
      if (!(c == nc || badTransposition(c.charAt(j), c.charAt(j+1)))) {
	assertFalse("i=" + i + 
		    ", j=" + j + ", " + c + ", " + nc,
		    check.verify(nc));
      }
    }
  }
}


By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Systems / Hardware Administrator
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions