Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Buffer support + tests #3

Merged
merged 5 commits into from

2 participants

@tj
tj commented

I only really need crc36 but yea :D buffer support would be great

@alexgorbatchev alexgorbatchev merged commit cab56a1 into from
@alexgorbatchev

Thanks! I'll update the npm as well shortly.

@tj
tj commented

thanks!

@alexgorbatchev

Published 0.2.0 to NPM registry.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 27, 2012
  1. @tj

    Added mocha tests

    tj authored
  2. @tj

    Fixed "i" global

    tj authored
  3. @tj

    Added tests for other methods

    tj authored
  4. @tj
  5. @tj

    docs for tests

    tj authored
This page is out of date. Refresh to see the latest.
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
Something went wrong with that request. Please try again.