Skip to content

Commit

Permalink
add enum test pattern and strict option enable
Browse files Browse the repository at this point in the history
  • Loading branch information
you21979 committed Mar 28, 2018
1 parent 4a89390 commit 56f539e
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 31 deletions.
24 changes: 11 additions & 13 deletions README.md
Expand Up @@ -66,21 +66,19 @@ if(!ba2.at(EVENT_CLEAR.QUEST4)) console.log("ok")

```
import BinaryArray = require('binaryarray')
import {getSpecMax} from 'binaryarray/dist/lib/util';
const EVENT_CLEAR = {
TUTORIAL : 0,
QUEST1 : 1,
QUEST2 : 2,
QUEST3 : 3,
QUEST4 : 4,
QUEST5 : 5,
QUEST6 : 6,
LASTBOSS : 7,
enum EVENT_CLEAR {
TUTORIAL,
QUEST1,
QUEST2,
QUEST3,
QUEST4,
QUEST5,
QUEST6,
LASTBOSS,
_SIZEOF
}
const EVENT_CLEAR_MAX = getSpecMax(EVENT_CLEAR);
const ba = new BinaryArray(EVENT_CLEAR_MAX)
const ba = new BinaryArray(EVENT_CLEAR._SIZEOF)
ba.bitOn(EVENT_CLEAR.TUTORIAL);
ba.bitOn(EVENT_CLEAR.QUEST1);
Expand Down
14 changes: 7 additions & 7 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "binaryarray",
"version": "0.1.1",
"version": "0.1.2",
"description": "the binary array",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand All @@ -21,12 +21,12 @@
"binary"
],
"devDependencies": {
"@types/node": "",
"@types/mocha": "",
"typescript": "",
"istanbul": "",
"coveralls": "",
"mocha": ""
"@types/node": "^9.6.0",
"@types/mocha": "^5.0.0",
"typescript": "^2.8.0",
"istanbul": "^0.4.5",
"coveralls": "^3.0.0",
"mocha": "^5.0.5"
},
"scripts": {
"build": "./node_modules/.bin/tsc -p ./",
Expand Down
16 changes: 9 additions & 7 deletions src/lib/binaryarray.ts
@@ -1,9 +1,9 @@
import assert = require('assert');
import util = require('./util');
import * as assert from 'assert';
import * as util from './util';

export class BinaryArray{
maxnum: number;
storage: Uint32Array;
readonly maxnum: number;
readonly storage: Uint32Array;
constructor(maxnum : number){
this.maxnum = maxnum
this.storage = util.createArray(util.getArraySize(maxnum), 0)
Expand Down Expand Up @@ -37,7 +37,7 @@ export class BinaryArray{
return this.storage[idx] & flag ? 1 : 0;
}
toArray() : Array<number>{
const w = [];
const w : Array<number> = [];
const max = this.maxnum;
for(let i = 0; i < max; ++i ){
w.push(this.at(i));
Expand All @@ -49,7 +49,6 @@ export class BinaryArray{
const w = this.toArray();
return Object.keys(spec)
.filter((k) => w[spec[k]])
.map((k) => k)
}
toJSON() : string{
return JSON.stringify(this.toArray())
Expand Down Expand Up @@ -83,11 +82,14 @@ export class BinaryArray{
toHexString() : string{
let str = '';
const n = this.storage.length;
for(let i=n-1;i>=0;--i){
for(let i = n - 1; i >= 0; --i){
str = str + util.NumberToHexString(this.storage[i], 8);
}
return str;
}
clone() : BinaryArray{
return BinaryArray.loadFromArray(this.toArray());
}
static loadFromHexString(maxnum : number, str : string) : BinaryArray{
const ba = new BinaryArray(maxnum);
const s = 8;
Expand Down
4 changes: 2 additions & 2 deletions src/lib/util.ts
Expand Up @@ -4,12 +4,12 @@ const BITS : number = 32;

// フラグ数から配列のサイズを求める
export const getArraySize = (flagmax : number) : number => {
return Math.ceil(flagmax/BITS);
return Math.ceil(flagmax / BITS);
}

// フラグ番号から配列の位置を求める
export const getArrayIndex = (no : number) : number => {
return Math.floor(no/BITS);
return Math.floor(no / BITS);
}

// フラグ番号から実際のフラグの位置を求める
Expand Down
35 changes: 33 additions & 2 deletions src/test/binaryarray.test.ts
@@ -1,5 +1,5 @@
import assert = require('assert');
import BinaryArray = require('..');
import * as assert from 'assert'
import {BinaryArray} from '../lib/binaryarray'
import {getSpecMax} from "../lib/util"

describe('test', () => {
Expand Down Expand Up @@ -238,6 +238,37 @@ describe('test', () => {
}
assert(flag === 0);
},
()=>{
const ba = new BinaryArray(1024);
ba.bitOn(3);
const ba2 = ba.clone()
assert(ba2.at(3))
},
()=>{
enum EVENT_CLEAR {
TUTORIAL,
QUEST1,
QUEST2,
QUEST3,
QUEST4,
QUEST5,
QUEST6,
LASTBOSS,
_SIZEOF
}
const ba = new BinaryArray(EVENT_CLEAR._SIZEOF)
assert(ba.maxnum === EVENT_CLEAR._SIZEOF)
ba.bitOn(EVENT_CLEAR.TUTORIAL);
ba.bitOn(EVENT_CLEAR.QUEST1);
ba.bitOn(EVENT_CLEAR.QUEST4);
assert(ba.at(EVENT_CLEAR.TUTORIAL))
assert(ba.at(EVENT_CLEAR.QUEST1))
assert(ba.at(EVENT_CLEAR.QUEST4))
assert(!ba.at(EVENT_CLEAR.LASTBOSS))
const output = ba.serialize(EVENT_CLEAR);
const ba2 = BinaryArray.deserialize(output, EVENT_CLEAR, EVENT_CLEAR._SIZEOF)
assert(JSON.stringify(output) === JSON.stringify(ba2.serialize(EVENT_CLEAR)))
},
()=>{
const ba = new BinaryArray(1024);
assert(ba.isRange(1023));
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Expand Up @@ -8,6 +8,7 @@
"sourceMap": false,
"removeComments": true,
"moduleResolution": "node",
"strict": true,
"declaration": true
},
"exclude": [
Expand Down

0 comments on commit 56f539e

Please sign in to comment.