js-crc32/test.js

102 lines
3.7 KiB
JavaScript
Raw Normal View History

2014-06-16 21:27:47 +00:00
/* vim: set ts=2: */
var X;
if(typeof require !== 'undefined') {
assert = require('assert');
describe('source',function(){it('should load',function(){X=require('./');});});
bits = require('./misc/bits.js');
crc32table = require('./misc/table.js');
fs = require("fs");
2014-06-16 21:27:47 +00:00
} else { X = CRC32; }
function readlines(f) { return fs.readFileSync(f, "ascii").split("\n"); }
function msieversion()
{
if(typeof window == 'undefined') return Infinity;
if(typeof window.navigator == 'undefined') return Infinity;
var ua = window.navigator.userAgent
var msie = ua.indexOf ( "MSIE " )
if(msie < 0) return Infinity;
return parseInt (ua.substring (msie+5, ua.indexOf (".", msie )));
}
2018-07-08 01:02:33 +00:00
var Buffer_from = function(){};
if(typeof Buffer !== 'undefined') {
var nbfs = !Buffer.from;
if(!nbfs) try { Buffer.from("foo", "utf8"); } catch(e) { nbfs = true; }
Buffer_from = nbfs ? function(buf, enc) { return (enc) ? new Buffer(buf, enc) : new Buffer(buf); } : Buffer.from.bind(Buffer);
}
2014-06-16 21:27:47 +00:00
describe('crc32 table', function() {
it('should match fixed table', function() {
var overflow = 0;
2014-06-16 21:27:47 +00:00
for(var i = 0; i != crc32table.length; ++i) {
assert.equal(crc32table[i]|0, X.table[i]);
if(crc32table[i] !== X.table[i]) ++overflow;
2014-06-16 21:27:47 +00:00
}
assert.equal(overflow, 128);
2014-06-16 21:27:47 +00:00
});
});
describe('crc32 bits', function() {
bits.forEach(function(i) {
var msg = i[0], l = i[0].length, L = i[1]|0;
2014-06-21 02:25:47 +00:00
if(l > 20) msg = i[0].substr(0,5) + "...(" + l + ")..." + i[0].substr(-5);
2016-10-10 23:45:29 +00:00
if(l > 100 && msieversion() < 9) return;
if(l > 20000 && typeof Buffer === 'undefined') return;
2014-06-16 21:27:47 +00:00
it(msg, function() {
if(i[2] === 1) assert.equal(X.bstr(i[0]), L);
2014-06-16 21:27:47 +00:00
assert.equal(X.str(i[0]), i[1]|0);
2018-07-08 01:02:33 +00:00
if(typeof Buffer !== 'undefined') assert.equal(X.buf(Buffer_from(i[0])), L);
var len = i[0].length, step = len < 20000 ? 1 : len < 50000 ? Math.ceil(len / 20000) : Math.ceil(len / 2000);
for(var x = 0; x < len; x += step) {
if(i[0].charCodeAt(x) >= 0xD800 && i[0].charCodeAt(x) < 0xE000) continue;
if(i[2] === 1) {
var bstrcrc = X.bstr(i[0].substr(x), X.bstr(i[0].substr(0, x)));
assert.equal(bstrcrc, L);
}
var strcrc = X.str(i[0].substr(x), X.str(i[0].substr(0, x)));
assert.equal(strcrc, i[1]|0);
if(typeof Buffer !== 'undefined') {
2018-07-08 01:02:33 +00:00
var buf = Buffer_from(i[0]);
var bufcrc = X.buf(buf.slice(x), X.buf(buf.slice(0, x)));
assert.equal(bufcrc, L);
}
}
2014-06-16 21:27:47 +00:00
});
});
});
if(typeof require !== 'undefined') describe("unicode", function() {
if(!fs.existsSync("./test_files/uccat.txt")) return;
var uccat = readlines("./test_files/uccat.txt");
uccat.forEach(function(cat) {
it("Category " + cat, function() {
if(!fs.existsSync("./test_files/baseline." + cat + ".txt")) return;
var corpus = readlines("./test_files/baseline." + cat + ".txt");
var uctable = require("./test_files/uctable." + cat + ".js");
for(var ucidx = 0; ucidx < uctable.length; ++ucidx) {
var c = uctable[ucidx];
/* since the baselines are passed via utf8, discard invalid codes */
if(c.charCodeAt(0) >= 0xD800 && c.charCodeAt(0) < 0xE000) continue;
var cc = corpus[ucidx], dd = X.str(c);
assert.equal(dd, cc, ":" + ucidx + ":" + c + ":" + cc + ":" + dd);
if(typeof Buffer !== 'undefined') {
2018-07-08 01:02:33 +00:00
var ee = X.buf(Buffer_from(c, "utf8"));
assert.equal(ee, cc, ":" + ucidx + ":" + c + ":" + cc + ":" + ee);
2018-07-08 01:02:33 +00:00
var ff = X.bstr(String.fromCharCode.apply(null, Buffer_from(c, "utf8")));
assert.equal(ff, cc, ":" + ucidx + ":" + c + ":" + cc + ":" + ff);
}
};
});
});
});
if(typeof require !== 'undefined') describe("corpora", function() {
require("./test_files/corpus.json").forEach(function(text) {
if(!fs.existsSync(text[1])) return;
it("should match '" + text[0] + "' (" + text[2] + ")", function() {
assert.equal(text[2], X.buf(fs.readFileSync(text[1])));
});
});
});