Skip to content

Commit

Permalink
fix: testing scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
swiing committed Dec 9, 2023
1 parent cf539f2 commit 8a50cb2
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 77 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ jobs:
- name: Install dependencies
run: npm install # npm ci
- run: npm run build --if-present
- run: npm test
- run: npm test:ci
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "@bitarray/typedarray",
"version": "1.1.1",
"description": "A BitArray object exhibiting the interface of standard ecmascript TypedArray's",
"type": "module",
"typings": "./dist/esm/bit-typedarray.d.ts",
"exports": {
".": {
Expand All @@ -25,7 +26,8 @@
"prepublishOnly": "npm run test",
"style:fix": "prettier src/**/*.ts --write",
"style:check": "prettier src/**/*.ts --check",
"test": "node --loader ts-node/esm test/index.ts"
"test": "ts-node --esm --project ./tsconfig.esm.json ./test/index.ts",
"test:ci": "set TS_NODE_PROJECT=./tsconfig.esm.json && node --no-warnings=ExperimentalWarning --loader ts-node/esm ./test/index.ts"
},
"repository": {
"type": "git",
Expand Down
6 changes: 3 additions & 3 deletions test/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import suite from "./suite";
import test from "./test";
import suite from './suite.js';
import test from './test.js';

test( suite );
test(suite);
181 changes: 109 additions & 72 deletions test/suite.ts
Original file line number Diff line number Diff line change
@@ -1,123 +1,160 @@
import BitArray from "../src/bit-typedarray";
import BitArray from '../src/bit-typedarray.js';

// whatever value >=4
// Some test cases are not accounting for a zero length (and would appear as failures)
// (but, in general, it is obviously allowed to have a zero-size bit array)
const length = 48;

const bits = new BitArray( length );

const bits = new BitArray(length);

/** suite 1 */
const instantiating = {
"new BitArray( length )": bits instanceof BitArray,
"length is set": bits.length === length,
"empty bit array has length 0": new BitArray(0) .length === 0,
"new BitArray( iterable )": new BitArray("011010") instanceof BitArray
&& new BitArray("011010") .length === 6
'new BitArray( length )': bits instanceof BitArray,
'length is set': bits.length === length,
'empty bit array has length 0': new BitArray(0).length === 0,
'new BitArray( iterable )':
new BitArray('011010') instanceof BitArray &&
new BitArray('011010').length === 6,
};


/** suite 2 */
const reading_writing = {
"default value set to 0": bits[0] === 0 && bits[length-1] === 0,
// @ts-ignore
"bits[n] = true sets bit value to 1": (bits[length-1] = true, bits[length-1] === 1),
"bits[n] = 1 sets bit value to 1": (bits[length-1] = 1, bits[length-1] === 1),
".at(n)": bits.at(length-1) === 1,
'default value set to 0': bits[0] === 0 && bits[length - 1] === 0,
'bits[n] = true sets bit value to 1':
// @ts-ignore true type as bit
((bits[length - 1] = true), bits[length - 1] === 1),
'bits[n] = 1 sets bit value to 1':
((bits[length - 1] = 1), bits[length - 1] === 1),
'.at(n)': bits.at(length - 1) === 1,
// this test assumes a minimal length, otherwise we don't run and simply return "pass ok".
".set([true,false], 2)": length < 4 ? true : (bits.set([true,false], 2), bits[2]===1 && bits[3]===0),
".set([1,1], 2)": length < 4 ? true : (bits.set([1,1], 2), bits[2]===1 && bits[3]===1)
'.set([true,false], 2)':
length < 4
? true
: (bits.set([true, false], 2), bits[2] === 1 && bits[3] === 0),
'.set([1,1], 2)':
length < 4 ? true : (bits.set([1, 1], 2), bits[2] === 1 && bits[3] === 1),
};


/** suite 3 */
// we check only that the last value is as expected,
// and assume this is enough evidence to consider the loop was okay
const iterating = {

"for loop": (()=>{
'for loop': (() => {
let item, index;
for( let i=0; i<length; i++ ) item = bits[ index=i ];
return (index === length-1) && (item === 1);
for (let i = 0; i < length; i++) item = bits[(index = i)];
return index === length - 1 && item === 1;
})(),

"for..in loop": (()=>{
'for..in loop': (() => {
let item, index;
for( let i in bits ) item = bits[index=i];
return (index === ""+(length-1)) && (item === 1);
for (let i in bits) item = bits[(index = i)];
return index === '' + (length - 1) && item === 1;
})(),

".forEach()": (()=>{
'.forEach()': (() => {
let res, index;
bits.forEach( (val, i, arr) => { index=i; res = arr[i]===val; } );
return (index === length-1) && (res === true);
bits.forEach((val, i, arr) => {
index = i;
res = arr[i] === val;
});
return index === length - 1 && res === true;
})(),

"for..of loop": (()=>{
let item, index=0;
for( item of bits ) index++;
return (item===1) && (index===length);
})()

'for..of loop': (() => {
let item,
index = 0;
for (item of bits) index++;
return item === 1 && index === length;
})(),
};


/** suite 4 */
const formatting = {
".toString()": bits.toString() === new Array(length)
.fill(0)
.map((c,i)=>(i+1)%8?c:c+" ").join("").trim()
// bits 2 and 3 have been set to 1 in my test cases
.replace(/0000/,"0011")
// last bit have been set to 1 in my test cases
.replace(/.$/,"1")
}
'.toString()':
bits.toString() ===
new Array(length)
.fill(0)
.map((c, i) => ((i + 1) % 8 ? c : c + ' '))
.join('')
.trim()
// bits 2 and 3 have been set to 1 in my test cases
.replace(/0000/, '0011')
// last bit have been set to 1 in my test cases
.replace(/.$/, '1'),
};

/** suite 5 */
const static_metods = {

"BitArray.from( boolean[] )": (()=>{
let arr = BitArray.from( [true,true,false,false,true] );
return arr instanceof BitArray
&& arr.length === 5
&& arr[0]===1 && arr[1]===1 && arr[2]===0 && arr[3]===0 && arr[4]===1
'BitArray.from( boolean[] )': (() => {
let arr = BitArray.from([true, true, false, false, true]);
return (
arr instanceof BitArray &&
arr.length === 5 &&
arr[0] === 1 &&
arr[1] === 1 &&
arr[2] === 0 &&
arr[3] === 0 &&
arr[4] === 1
);
})(),

"BitArray.from( number[] )": (()=>{
let arr = BitArray.from( [1,1,0,0,1] );
return arr instanceof BitArray
&& arr.length === 5
&& arr[0]===1 && arr[1]===1 && arr[2]===0 && arr[3]===0 && arr[4]===1
'BitArray.from( number[] )': (() => {
let arr = BitArray.from([1, 1, 0, 0, 1]);
return (
arr instanceof BitArray &&
arr.length === 5 &&
arr[0] === 1 &&
arr[1] === 1 &&
arr[2] === 0 &&
arr[3] === 0 &&
arr[4] === 1
);
})(),

"BitArray.from( string )": (()=>{
let arr = BitArray.from("11001");
return arr instanceof BitArray
&& arr.length === 5
&& arr[0]===1 && arr[1]===1 && arr[2]===0 && arr[3]===0 && arr[4]===1
'BitArray.from( string )': (() => {
let arr = BitArray.from('11001');
return (
arr instanceof BitArray &&
arr.length === 5 &&
arr[0] === 1 &&
arr[1] === 1 &&
arr[2] === 0 &&
arr[3] === 0 &&
arr[4] === 1
);
})(),

"BitArray.of( ...booleans )": (()=>{
let arr = BitArray.of(true,true,false,false,true);
return arr instanceof BitArray
&& arr.length === 5
&& arr[0]===1 && arr[1]===1 && arr[2]===0 && arr[3]===0 && arr[4]===1
'BitArray.of( ...booleans )': (() => {
let arr = BitArray.of(true, true, false, false, true);
return (
arr instanceof BitArray &&
arr.length === 5 &&
arr[0] === 1 &&
arr[1] === 1 &&
arr[2] === 0 &&
arr[3] === 0 &&
arr[4] === 1
);
})(),

"BitArray.of( ...numbers )": (()=>{
let arr = BitArray.of(1,1,0,0,1);
return arr instanceof BitArray
&& arr.length === 5
&& arr[0]===1 && arr[1]===1 && arr[2]===0 && arr[3]===0 && arr[4]===1
})()

}
'BitArray.of( ...numbers )': (() => {
let arr = BitArray.of(1, 1, 0, 0, 1);
return (
arr instanceof BitArray &&
arr.length === 5 &&
arr[0] === 1 &&
arr[1] === 1 &&
arr[2] === 0 &&
arr[3] === 0 &&
arr[4] === 1
);
})(),
};

export default {
instantiating,
reading_writing,
iterating,
formatting,
static_metods
static_metods,
};

0 comments on commit 8a50cb2

Please sign in to comment.