Skip to content
Browse files

Merge pull request #3 from visionmedia/master

Buffer support + tests
  • Loading branch information...
2 parents 64ef353 + d5cb6f2 commit cab56a174cf3070cdc8e146d4c94b4c4150fbac3 @alexgorbatchev committed Apr 27, 2012
Showing with 110 additions and 67 deletions.
  1. +1 −0 .gitignore
  2. +7 −0 Makefile
  3. +4 −0 README.md
  4. +31 −2 lib/crc.js
  5. +4 −0 package.json
  6. +63 −0 test/crc.js
  7. +0 −64 tests/crc_tests.js
  8. +0 −1 tests/nodeunit
View
1 .gitignore
@@ -0,0 +1 @@
+node_modules
View
7 Makefile
@@ -0,0 +1,7 @@
+
+test:
+ @./node_modules/.bin/mocha \
+ --require should \
+ --reporter spec
+
+.PHONY: test
View
4 README.md
@@ -25,3 +25,7 @@ or
npm install crc
+## Running tests
+
+ $ npm install
+ $ make test
View
33 lib/crc.js
@@ -247,12 +247,20 @@
crc = 0
;
- for(i = 0; i < len; i++)
+ for(var i = 0; i < len; i++)
crc = crc8Add(crc, str.charCodeAt(i));
return crc;
};
+ function crc8Buffer(buf) {
+ var crc = 0;
+ for (var i = 0, len = buf.length; i < len; ++i) {
+ crc = crc8Add(crc, buf[i]);
+ }
+ return crc;
+ }
+
function crcArc(str)
{
var i,
@@ -279,6 +287,14 @@
return crc;
};
+ function crc16Buffer(buf) {
+ var crc = 0;
+ for (var i = 0, len = buf.length; i < len; ++i) {
+ crc = crc16Add(crc, buf[i]);
+ }
+ return crc;
+ }
+
function fcs16(str)
{
var i,
@@ -305,6 +321,14 @@
return crc^0xFFFFFFFF;
};
+ function crc32Buffer(buf) {
+ var crc = 0xFFFFFFFF;
+ for (var i = 0, len = buf.length; i < len; ++i) {
+ crc = crc32Add(crc, buf[i]);
+ }
+ return crc ^ 0xFFFFFFFF;
+ }
+
/**
* Convert value as 8-bit unsigned integer to 2 digit hexadecimal number.
*/
@@ -357,6 +381,11 @@
'crc32' : crc32,
'hex8' : hex8,
'hex16' : hex16,
- 'hex32' : hex32
+ 'hex32' : hex32,
+ 'buffer' : {
+ crc8: crc8Buffer,
+ crc16: crc16Buffer,
+ crc32: crc32Buffer
+ }
};
})();
View
4 package.json
@@ -7,6 +7,10 @@
"main": "./lib/crc.js",
"scripts": {},
"directories" : {},
+ "devDependencies": {
+ "mocha": "*",
+ "should": "*"
+ },
"repository": {
"type": "git",
"url": "git://github.com/alexgorbatchev/node-crc.git"
View
63 test/crc.js
@@ -0,0 +1,63 @@
+#!/usr/bin/env ./nodeunit/bin/nodeunit
+
+var crc = require('../lib/crc');
+
+describe('crc8()', function(){
+ it('should work with strings', function(){
+ crc.crc8('hello world').should.equal(64);
+ })
+
+ it('should work with Buffers', function(){
+ crc.buffer.crc8(new Buffer('hello world')).should.equal(64);
+ })
+})
+
+describe('crc16()', function(){
+ it('should work with strings', function(){
+ crc.crc16('hello world').should.equal(15332);
+ })
+
+ it('should work with Buffers', function(){
+ crc.buffer.crc16(new Buffer('hello world')).should.equal(15332);
+ })
+})
+
+describe('crc32()', function(){
+ it('should work with strings', function(){
+ crc.crc32('hello world').should.equal(222957957);
+ })
+
+ it('should work with Buffers', function(){
+ crc.buffer.crc32(new Buffer('hello world')).should.equal(222957957);
+ })
+})
+
+describe('crcArc()', function(){
+ it('should work with strings', function(){
+ crc.crcArc('hello world').should.equal(14785);
+ })
+})
+
+describe('fcs16()', function(){
+ it('should work with strings', function(){
+ crc.fcs16('hello world').should.equal(44550);
+ })
+})
+
+describe('hex8()', function(){
+ it('should work with strings', function(){
+ crc.hex8(64).should.equal('40');
+ })
+})
+
+describe('hex16()', function(){
+ it('should work with strings', function(){
+ crc.hex16(15332).should.equal('3BE4');
+ })
+})
+
+describe('hex32()', function(){
+ it('should work with strings', function(){
+ crc.hex32(222957957).should.equal('0D4A1185');
+ })
+})
View
64 tests/crc_tests.js
@@ -1,64 +0,0 @@
-#!/usr/bin/env ./nodeunit/bin/nodeunit
-
-var crc = require('../lib/crc');
-
-var fixture = {
- 'crc8': [
- ['hello world', 64]
- ],
-
- 'crc16': [
- ['hello world', 15332]
- ],
-
- 'crc32': [
- ['hello world', 222957957]
- ],
-
- 'crcArc': [
- ['hello world', 14785]
- ],
-
- 'fcs16': [
- ['hello world', 44550]
- ],
-
- 'hex8': [
- [64, '40']
- ],
-
- 'hex16': [
- [15332, '3BE4']
- ],
-
- 'hex32': [
- [222957957, '0D4A1185']
- ]
-};
-
-var suite = module.exports['crc'] = {};
-
-for(var func in fixture)
-{
- var list = fixture[func];
-
- for(var i = 0; i < list.length; i++)
- {
- var input = list[i][0],
- output = list[i][1],
- name = [ func, input, output ].join(' - ')
- ;
-
- suite[name] = (function(func, input, output)
- {
-
- return function(assert)
- {
- assert.deepEqual(crc[func](input), output);
- assert.done();
- };
-
- })(func, input, output);
- }
-};
-
1 tests/nodeunit
@@ -1 +0,0 @@
-Subproject commit 8a31df2da23f57942a305ed9e2b92bf0db19d5f7

0 comments on commit cab56a1

Please sign in to comment.
Something went wrong with that request. Please try again.