Skip to content

Commit

Permalink
Merge pull request #3 from visionmedia/master
Browse files Browse the repository at this point in the history
Buffer support + tests
  • Loading branch information
alexgorbatchev committed Apr 27, 2012
2 parents 64ef353 + d5cb6f2 commit cab56a1
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 67 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
node_modules
7 changes: 7 additions & 0 deletions Makefile
@@ -0,0 +1,7 @@

test:
@./node_modules/.bin/mocha \
--require should \
--reporter spec

.PHONY: test
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -25,3 +25,7 @@ or

npm install crc

## Running tests

$ npm install
$ make test
33 changes: 31 additions & 2 deletions lib/crc.js
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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.
*/
Expand Down Expand Up @@ -357,6 +381,11 @@
'crc32' : crc32,
'hex8' : hex8,
'hex16' : hex16,
'hex32' : hex32
'hex32' : hex32,
'buffer' : {
crc8: crc8Buffer,
crc16: crc16Buffer,
crc32: crc32Buffer
}
};
})();
4 changes: 4 additions & 0 deletions package.json
Expand Up @@ -7,6 +7,10 @@
"main": "./lib/crc.js",
"scripts": {},
"directories" : {},
"devDependencies": {
"mocha": "*",
"should": "*"
},
"repository": {
"type": "git",
"url": "git://github.com/alexgorbatchev/node-crc.git"
Expand Down
63 changes: 63 additions & 0 deletions 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');
})
})
64 changes: 0 additions & 64 deletions tests/crc_tests.js

This file was deleted.

1 change: 0 additions & 1 deletion tests/nodeunit
Submodule nodeunit deleted from 8a31df

0 comments on commit cab56a1

Please sign in to comment.