Skip to content

Commit 7372fe3

Browse files
committed
testing
1 parent 3689efe commit 7372fe3

3 files changed

Lines changed: 41 additions & 20 deletions

File tree

js/alpha-5/alpha-5.js

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -61,33 +61,33 @@ const _compactUInt8ArrayFrom0To31Numbers = _0To31Numbers => {
6161
let byteIndex = 0;
6262

6363
const saveByte = (number) => {
64-
console.log(`save ${number} (${number.toString(2).padStart(8, "0")})`)
64+
//console.log(`save ${number} (${number.toString(2).padStart(8, "0")})`)
6565
compactUInt8Array[byteIndex] = number;
6666
byteIndex += 1;
6767
numberToBeSaved = 0;
6868
bitPosition = 0;
6969
};
7070
_0To31Numbers.forEach(number => {
71-
if (bitPosition + LETTER_LENGTH <= BYTE_LENGTH) { // can fit at once
71+
if (bitPosition + LETTER_LENGTH < BYTE_LENGTH) { // can fit at once
7272
const toShift = BYTE_LENGTH - LETTER_LENGTH - bitPosition;
7373
numberToBeSaved += number << toShift;
7474
bitPosition += LETTER_LENGTH;
7575

76-
if (bitPosition === BYTE_LENGTH) {
76+
if (bitPosition +1 === BYTE_LENGTH) {
7777
saveByte(numberToBeSaved);
7878
}
7979
} else { // cannot fit at once
80-
const shiftRight = -BYTE_LENGTH + bitPosition + LETTER_LENGTH; // 2
81-
const missedBits = LETTER_LENGTH - shiftRight; // 3
82-
const clamped = number >> shiftRight;
83-
numberToBeSaved += clamped;
80+
const shiftRight = -BYTE_LENGTH + LETTER_LENGTH + bitPosition; // 2
81+
const spaceLeft = LETTER_LENGTH - shiftRight; // 3
82+
83+
const firstHalfToAdd = number >> shiftRight;
84+
const secondHalf = number - (firstHalfToAdd << shiftRight)
85+
numberToBeSaved += firstHalfToAdd;
8486

8587
saveByte(numberToBeSaved);
8688

87-
const substractHead = clamped << shiftRight;
88-
const rest = number - substractHead;
89-
numberToBeSaved += rest << (BYTE_LENGTH+1) - missedBits;
90-
bitPosition += missedBits-1; //3
89+
numberToBeSaved += secondHalf << (BYTE_LENGTH -LETTER_LENGTH + spaceLeft);
90+
bitPosition += LETTER_LENGTH-spaceLeft; //3
9191
}
9292
});
9393
// todo handle all cases
@@ -97,7 +97,7 @@ const _compactUInt8ArrayFrom0To31Numbers = _0To31Numbers => {
9797
} //else {
9898
// compactUInt8Array[byteIndex] = remainder;
9999
// }
100-
console.log("3_compactUInt8ArrayFrom0To31Numbers RESULT",compactUInt8Array)
100+
// console.log("3_compactUInt8ArrayFrom0To31Numbers RESULT",compactUInt8Array)
101101
return compactUInt8Array;
102102
};
103103

@@ -214,11 +214,11 @@ const decode = uInt8Array => {
214214
return string;
215215
};
216216

217-
console.log(37,_compactUInt8ArrayFrom0To31Numbers([1,2,3]))
218-
console.log(38,Uint8Array.from([
219-
0b00001000, // 1-2
220-
0b10000110, // 2-3
221-
]))
217+
// console.log(37,_compactUInt8ArrayFrom0To31Numbers([1,2,3]))
218+
// console.log(38,Uint8Array.from([
219+
// 0b00001000, // 1-2
220+
// 0b10000110, // 2-3
221+
// ]))
222222
// console.log(encode('abc def zxw aaa zzz'));
223223
// console.log(encode('a - z'));
224224
// console.log(encode('a'));

js/alpha-5/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"type": "module",
77
"main": "alpha-5.js",
88
"scripts": {
9-
"test": "ava --timeout 1000h"
9+
"test": "ava --timeout 24h"
1010
},
1111
"devDependencies": {
1212
"ava": "^3.4.0"

js/alpha-5/tests/specification/alpha-5.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import test from "ava";
22
import {
3-
encode, decode, BIT_LENGTH,// public
3+
encode, decode,// public
44
_0To31NumberFromCharacter,
55
_0To31NumbersFromString,
66
_compactUInt8ArrayFrom0To31Numbers,
77
} from "../../alpha-5.js";
8-
import { deepEqualAdded } from "../../deep.js";
98

109

1110
test(`_0To31NumberFromCharacter a`, t => {
@@ -49,6 +48,28 @@ test(`_compactUInt8ArrayFrom0To31Numbers
4948
]));
5049
});
5150

51+
test(`_compactUInt8ArrayFrom0To31Numbers
52+
1, 00001
53+
2, 00010
54+
3, 00011
55+
4, 00100`, t => {
56+
57+
console.log(56,_compactUInt8ArrayFrom0To31Numbers([1,2,3,4,]))
58+
console.log(58,Uint8Array.from([
59+
0b00001000, // 1-2
60+
0b10000110, // 2-3-4
61+
0b01000000, // 4
62+
]))
63+
64+
t.deepEqual(_compactUInt8ArrayFrom0To31Numbers([1,2,3,4,
65+
]), Uint8Array.from([
66+
0b00001000, // 1-2
67+
0b10000110, // 2-3-4
68+
0b01000000, // 4
69+
]));
70+
71+
});
72+
5273
test(`_compactUInt8ArrayFrom0To31Numbers
5374
1, 00001
5475
2, 00010

0 commit comments

Comments
 (0)