Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmillr committed Jan 17, 2025
1 parent 8e9c399 commit cae80f9
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 75 deletions.
2 changes: 1 addition & 1 deletion test/bitmap.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { deepStrictEqual } from 'node:assert';
import { should } from 'micro-should';
import { deepStrictEqual } from 'node:assert';
import { GifReader } from 'omggif';
import encodeQR, { _tests } from '../esm/index.js';
const { Bitmap } = _tests;
Expand Down
108 changes: 50 additions & 58 deletions test/decode.test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
import { deepStrictEqual } from 'node:assert';
import { should } from 'micro-should';
import { readFileSync, readdirSync, statSync } from 'node:fs';
import { decode as jpegDecode } from 'jpeg-js';
import { deepStrictEqual } from 'node:assert';
import { readdirSync, statSync } from 'node:fs';
import readQR, { _tests } from '../esm/decode.js';

const DIR = './test/vectors/boofcv-v3/';

function readJPEG(path) {
return jpegDecode(readFileSync(path))
}
import { DETECTION_PATH, readJPEG } from './utils.js';

function parseCoordinates(c) {
const qrSize = 4 * 2;
Expand Down Expand Up @@ -55,7 +49,7 @@ should('parseCoordinates', () => {
});

should('FindFinder', () => {
const bmp = _tests.toBitmap(readJPEG(DIR + 'detection/blurred/image007.jpg'));
const bmp = _tests.toBitmap(readJPEG('detection/blurred/image007.jpg'));
const finder = _tests.findFinder(bmp);
deepStrictEqual(finder, {
bl: { x: 357.5, y: 659.5, moduleSize: 5.142857142857143, count: 2 },
Expand All @@ -65,7 +59,7 @@ should('FindFinder', () => {
});

should('Detector', () => {
const bmp = _tests.toBitmap(readJPEG(DIR + 'detection/blurred/image007.jpg'));
const bmp = _tests.toBitmap(readJPEG('detection/blurred/image007.jpg'));
const { bits, points } = _tests.detect(bmp);
// console.log(bits.toASCII());
const ascii = `
Expand Down Expand Up @@ -95,7 +89,7 @@ should('Detector', () => {
});

should('Decode', () => {
const f = DIR + 'detection/blurred/image007.jpg';
const f = 'detection/blurred/image007.jpg';
const bmp = _tests.toBitmap(readJPEG(f));
const { bits } = _tests.detect(bmp);
const res = _tests.decodeBitmap(bits);
Expand All @@ -107,21 +101,19 @@ const listFiles = (path, isDir = false) =>

const percent = (a, b) => `${((a / b) * 100).toFixed(2)}%`;

const DETECTION_PATH = DIR + 'detection';

// for (const category of listFiles(DETECTION_PATH, true)) {
// const DIR_PATH = `${DETECTION_PATH}/${category}`;
// should(`Decoding/${category}`, () => {
// for (const f of listFiles(DIR_PATH)) {
// if (!f.endsWith('.jpg')) continue;
// const p = `${DIR_PATH}/${f}`;
// const EXCLUDE = [
// DIR + 'detection/blurred/image001.jpg',
// DIR + 'detection/blurred/image002.jpg',
// DIR + 'detection/blurred/image003.jpg',
// DIR + 'detection/blurred/image004.jpg',
// DIR + 'detection/blurred/image005.jpg',
// DIR + 'detection/blurred/image006.jpg',
// 'detection/blurred/image001.jpg',
// 'detection/blurred/image002.jpg',
// 'detection/blurred/image003.jpg',
// 'detection/blurred/image004.jpg',
// 'detection/blurred/image005.jpg',
// 'detection/blurred/image006.jpg',
// ];
// if (EXCLUDE.includes(p)) continue;
// console.log('FILE', p);
Expand Down Expand Up @@ -369,38 +361,38 @@ for (const category of listFiles(DETECTION_PATH, true)) {
let currDecoded = 0;
for (const f of listFiles(DIR_PATH)) {
if (!f.endsWith('.jpg')) continue;
const p = `${DIR_PATH}/${f}`;
const p = `detection/${category}/${f}`;
const EXCLUDE = [
DIR + 'detection/blurred/image025.jpg',
DIR + 'detection/brightness/image006.jpg',
DIR + 'detection/brightness/image007.jpg',
DIR + 'detection/curved/image015.jpg',
DIR + 'detection/curved/image022.jpg',
DIR + 'detection/curved/image049.jpg',
DIR + 'detection/glare/image050.jpg',
DIR + 'detection/high_version/image031.jpg',
DIR + 'detection/high_version/image032.jpg',
DIR + 'detection/nominal/image015.jpg',
DIR + 'detection/nominal/image020.jpg',
DIR + 'detection/nominal/image021.jpg',
DIR + 'detection/nominal/image022.jpg',
DIR + 'detection/nominal/image023.jpg',
DIR + 'detection/nominal/image055.jpg',
DIR + 'detection/perspective/image001.jpg',
DIR + 'detection/perspective/image002.jpg',
DIR + 'detection/perspective/image003.jpg',
DIR + 'detection/perspective/image004.jpg',
DIR + 'detection/perspective/image005.jpg',
DIR + 'detection/perspective/image006.jpg',
DIR + 'detection/perspective/image007.jpg',
DIR + 'detection/rotations/image017.jpg',
DIR + 'detection/rotations/image018.jpg',
DIR + 'detection/rotations/image023.jpg',
DIR + 'detection/rotations/image040.jpg',
DIR + 'detection/shadows/image007.jpg',
DIR + 'detection/shadows/image008.jpg',
'blurred/image025.jpg',
'brightness/image006.jpg',
'brightness/image007.jpg',
'curved/image015.jpg',
'curved/image022.jpg',
'curved/image049.jpg',
'glare/image050.jpg',
'high_version/image031.jpg',
'high_version/image032.jpg',
'nominal/image015.jpg',
'nominal/image020.jpg',
'nominal/image021.jpg',
'nominal/image022.jpg',
'nominal/image023.jpg',
'nominal/image055.jpg',
'perspective/image001.jpg',
'perspective/image002.jpg',
'perspective/image003.jpg',
'perspective/image004.jpg',
'perspective/image005.jpg',
'perspective/image006.jpg',
'perspective/image007.jpg',
'rotations/image017.jpg',
'rotations/image018.jpg',
'rotations/image023.jpg',
'rotations/image040.jpg',
'shadows/image007.jpg',
'shadows/image008.jpg',
];
if (EXCLUDE.includes(p)) continue;
if (EXCLUDE.some((end) => p.endsWith(end))) continue;
count += 1;
// Slow as hell, but at least doesn't force binary modules for nodejs in dev env
const jpg = readJPEG(p);
Expand Down Expand Up @@ -429,18 +421,18 @@ for (const category of listFiles(DETECTION_PATH, true)) {
const p1 = percent(hadDecoded, count);
const p2 = percent(currDecoded, count);
const p3 = percent(currDecoded, hadDecoded);
// console.log(
// `${category}
// total: ${count}
// decoded before: ${hadDecoded} (${p1} of total)
// decoded now: ${currDecoded} (${p2} of total, ${p3} of decoded before)`
// );
// console.log(
// `${category}
// total: ${count}
// decoded before: ${hadDecoded} (${p1} of total)
// decoded now: ${currDecoded} (${p2} of total, ${p3} of decoded before)`
// );
});
}

// should.only('DEBUG', () => {
// //const f = DIR + 'detection/high_version/image031.jpg';
// const f = DIR + 'detection/high_version/image032.jpg';
// //const f = 'detection/high_version/image031.jpg';
// const f = 'detection/high_version/image032.jpg';
// const jpg = readJPEG(f);
// const res = readQR(jpg);
// console.log('DECODED', res);
Expand Down
2 changes: 1 addition & 1 deletion test/encode.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { deepStrictEqual } from 'node:assert';
import { should } from 'micro-should';
import { deepStrictEqual } from 'node:assert';
import { _tests } from '../esm/index.js';

should('detectType', () => {
Expand Down
4 changes: 2 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { should } from 'micro-should';

// Should be first to catch obvious things
import './bitmap.test.js';
import './utils.test.js';
import './encode.test.js';
import './decode.test.js';
import './encode.test.js';
import './qr.test.js';
import './utils.test.js';

should.run();
2 changes: 1 addition & 1 deletion test/qr.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { should } from 'micro-should';
import { deepStrictEqual } from 'node:assert';
import { readFileSync } from 'node:fs';
import { gunzipSync } from 'node:zlib';
import { should } from 'micro-should';
import encodeQR, { _tests } from '../esm/index.js';
const jsonGZ = (path) => JSON.parse(gunzipSync(readFileSync(path)));
const TEST_CASES = jsonGZ('./test/vectors/small-vectors.json.gz');
Expand Down
34 changes: 34 additions & 0 deletions test/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { decode as jpegDecode } from 'jpeg-js';
import { readFileSync } from 'node:fs';
import { dirname, join as joinPath } from 'node:path';
import { fileURLToPath } from 'node:url';
import { gunzipSync } from 'node:zlib';

export const _dirname = dirname(fileURLToPath(import.meta.url));

function readRel(path, opts) {
return readFileSync(joinPath(_dirname, path), opts);
}

export function json(path) {
try {
// Node.js
return JSON.parse(readRel(path, { encoding: 'utf-8' }));
} catch (error) {
// Bundler
const file = path.replace(/^\.\//, '').replace(/\.json$/, '');
if (path !== './' + file + '.json') throw new Error('Can not load non-json file');
// return require('./' + file + '.json'); // in this form so that bundler can glob this
}
}

export function jsonGZ(path) {
return JSON.parse(gunzipSync(readRel(path)).toString('utf8'));
}

const vectorPath = joinPath(_dirname, 'vectors', 'boofcv-v3');
export const DETECTION_PATH = joinPath(vectorPath, 'detection');
export function readJPEG(path) {
console.log('readJPEG', vectorPath, path);
return jpegDecode(readFileSync(joinPath(vectorPath, path)));
}
24 changes: 12 additions & 12 deletions test/utils.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, should } from 'micro-should';
import { deepStrictEqual } from 'node:assert';
import { should, describe } from 'micro-should';
import { _tests } from '../esm/index.js';

const ALIGNMENT_PATTERN_POSITIONS = [
Expand Down Expand Up @@ -55,22 +55,22 @@ describe('utils', () => {
);
}
});

should('sizeType', () => {
const names = ['small', 'medium', 'large'];

const exp = [];
for (let i = 1; i <= 9; i++) exp.push('small'); // SMALL("version 1-9"),
for (let i = 10; i <= 26; i++) exp.push('medium'); // MEDIUM("version 10-26"),
for (let i = 27; i <= 40; i++) exp.push('large'); // LARGE("version 27-40"),

const actual = [];
for (let ver = 1; ver <= 40; ver++) {
actual.push(names[_tests.info.sizeType(ver)]);
}
deepStrictEqual(actual, exp);
});

should('versionBits', () => {
const VECTORS = {
7: 0x07c94,
Expand Down Expand Up @@ -112,7 +112,7 @@ describe('utils', () => {
deepStrictEqual(_tests.info.versionBits(ver), VECTORS[ver], ver);
}
});

should('bitLimit', () => {
const VECTORS = [
{ l: 152, m: 128, q: 104, h: 72 },
Expand Down Expand Up @@ -159,21 +159,21 @@ describe('utils', () => {
for (let i = 0; i < VECTORS.length; i++) {
const ver = i + 1;
const v = VECTORS[i];

for (const ecc of ['l', 'm', 'q', 'h']) {
const eccName = { l: 'low', m: 'medium', q: 'quartile', h: 'high' }[ecc];
deepStrictEqual(_tests.info.capacity(ver, eccName).capacity, v[ecc]);
}
}
});

describe('crosstest', () => {
should('formatBits', () => {
// NOTE: copy-paste from python qr-code, for verification only.
// Remove, so we don't need to include license
const G15 = (1 << 10) | (1 << 8) | (1 << 5) | (1 << 4) | (1 << 2) | (1 << 1) | (1 << 0);
const G15_MASK = (1 << 14) | (1 << 12) | (1 << 10) | (1 << 4) | (1 << 1);

function BCH_digit(data) {
let digit = 0;
while (data != 0) {
Expand All @@ -195,11 +195,11 @@ describe('utils', () => {
deepStrictEqual(_tests.info.formatBits(ecc, mask_pattern), BCH_type_info(data));
}
});

should('versionBits', () => {
var G18 =
(1 << 12) | (1 << 11) | (1 << 10) | (1 << 9) | (1 << 8) | (1 << 5) | (1 << 2) | (1 << 0);

function BCH_digit(data) {
let digit = 0;
while (data != 0) {
Expand All @@ -208,7 +208,7 @@ describe('utils', () => {
}
return digit;
}

function BCH_type_number(data) {
let d = data << 12;
while (BCH_digit(d) - BCH_digit(G18) >= 0) {
Expand Down

0 comments on commit cae80f9

Please sign in to comment.