|
| 1 | +const{default:CharSet}=require('../lib/charset') |
| 2 | + |
| 3 | +const{ |
| 4 | + charset64, charset32, charset16, charset8, charset4, charset2 |
| 5 | +}=require('../lib/entropy') |
| 6 | + |
| 7 | +test('charset64',()=>{ |
| 8 | +constcharset=newCharSet('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_') |
| 9 | +const{ length}=charset.getChars() |
| 10 | +expect(length).toBe(64) |
| 11 | +constbitsPerChar=Math.log2(length) |
| 12 | +expect(charset.getBitsPerChar()).toBe(bitsPerChar) |
| 13 | +expect(charset.getCharsPerChunk()).toBe(4) |
| 14 | +}) |
| 15 | + |
| 16 | +test('charset32',()=>{ |
| 17 | +constcharset=newCharSet('2346789bdfghjmnpqrtBDFGHJLMNPQRT') |
| 18 | +const{ length}=charset.getChars() |
| 19 | +expect(length).toBe(32) |
| 20 | +constbitsPerChar=Math.log2(length) |
| 21 | +expect(charset.getBitsPerChar()).toBe(bitsPerChar) |
| 22 | +expect(charset.getCharsPerChunk()).toBe(8) |
| 23 | +}) |
| 24 | + |
| 25 | +test('charset16',()=>{ |
| 26 | +constcharset=newCharSet('0123456789abcdef') |
| 27 | +const{ length}=charset.getChars() |
| 28 | +expect(length).toBe(16) |
| 29 | +constbitsPerChar=Math.log2(length) |
| 30 | +expect(charset.getBitsPerChar()).toBe(bitsPerChar) |
| 31 | +expect(charset.getCharsPerChunk()).toBe(2) |
| 32 | +}) |
| 33 | + |
| 34 | +test('charset8',()=>{ |
| 35 | +constcharset=newCharSet('01234567') |
| 36 | +const{ length}=charset.getChars() |
| 37 | +expect(length).toBe(8) |
| 38 | +constbitsPerChar=Math.log2(length) |
| 39 | +expect(charset.getBitsPerChar()).toBe(bitsPerChar) |
| 40 | +expect(charset.getCharsPerChunk()).toBe(8) |
| 41 | +}) |
| 42 | + |
| 43 | +test('charset4',()=>{ |
| 44 | +constcharset=newCharSet('ATCG') |
| 45 | +const{ length}=charset.getChars() |
| 46 | +expect(length).toBe(4) |
| 47 | +constbitsPerChar=Math.log2(length) |
| 48 | +expect(charset.getBitsPerChar()).toBe(bitsPerChar) |
| 49 | +expect(charset.getCharsPerChunk()).toBe(4) |
| 50 | +}) |
| 51 | + |
| 52 | +test('charset2',()=>{ |
| 53 | +constcharset=newCharSet('01') |
| 54 | +const{ length}=charset.getChars() |
| 55 | +expect(length).toBe(2) |
| 56 | +constbitsPerChar=Math.log2(length) |
| 57 | +expect(charset.getBitsPerChar()).toBe(bitsPerChar) |
| 58 | +expect(charset.getCharsPerChunk()).toBe(8) |
| 59 | +}) |
| 60 | + |
| 61 | +test('Custom chars: 64',()=>{ |
| 62 | +expect(()=>{ |
| 63 | +const_=newCharSet('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ab') |
| 64 | +}).toThrow(Error) |
| 65 | + |
| 66 | +expect(()=>{ |
| 67 | +const_=newCharSet('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz)!@#$%^&*(+=0') |
| 68 | +}).toThrowError(Error) |
| 69 | + |
| 70 | +expect(()=>{ |
| 71 | +const_=newCharSet('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz)!@#$%^&*(+') |
| 72 | +}).toThrowError(Error) |
| 73 | +}) |
| 74 | + |
| 75 | +test('Custom chars: 32',()=>{ |
| 76 | +expect(()=>{ |
| 77 | +const_=newCharSet('01234567890123456789012345678901') |
| 78 | +}).toThrowError(Error) |
| 79 | + |
| 80 | +expect(()=>{ |
| 81 | +const_=newCharSet('0123456789abcdefghijklmnopqrstu') |
| 82 | +}).toThrowError(Error) |
| 83 | + |
| 84 | +expect(()=>{ |
| 85 | +const_=newCharSet('0123456789abcdefghijklmnopqrstuvw') |
| 86 | +}).toThrowError(Error) |
| 87 | +}) |
| 88 | + |
| 89 | +test('Custom chars: 16',()=>{ |
| 90 | +expect(()=>{ |
| 91 | +const_=newCharSet('0123456789abcde0') |
| 92 | +}).toThrowError(Error) |
| 93 | + |
| 94 | +expect(()=>{ |
| 95 | +const_=newCharSet('0123456789abcde') |
| 96 | +}).toThrowError(Error) |
| 97 | + |
| 98 | +expect(()=>{ |
| 99 | +const_=newCharSet('0123456789abcdefg') |
| 100 | +}).toThrowError(Error) |
| 101 | +}) |
| 102 | + |
| 103 | +test('Custom chars: 8',()=>{ |
| 104 | +expect(()=>{ |
| 105 | +const_=newCharSet('abcdefga') |
| 106 | +}).toThrowError(Error) |
| 107 | + |
| 108 | +expect(()=>{ |
| 109 | +const_=newCharSet('abcdefg') |
| 110 | +}).toThrowError(Error) |
| 111 | + |
| 112 | +expect(()=>{ |
| 113 | +const_=newCharSet('abcdefghi') |
| 114 | +}).toThrowError(Error) |
| 115 | +}) |
| 116 | + |
| 117 | +test('Custom chars: 4',()=>{ |
| 118 | +expect(()=>{ |
| 119 | +const_=newCharSet('abbc') |
| 120 | +}).toThrowError(Error) |
| 121 | + |
| 122 | +expect(()=>{ |
| 123 | +const_=newCharSet('abc') |
| 124 | +}).toThrowError(Error) |
| 125 | + |
| 126 | +expect(()=>{ |
| 127 | +const_=newCharSet('abcde') |
| 128 | +}).toThrowError(Error) |
| 129 | +}) |
| 130 | + |
| 131 | +test('Custom chars: 2',()=>{ |
| 132 | +expect(()=>{ |
| 133 | +const_=newCharSet('TT') |
| 134 | +}).toThrowError(Error) |
| 135 | + |
| 136 | +expect(()=>{ |
| 137 | +const_=newCharSet('T') |
| 138 | +}).toThrowError(Error) |
| 139 | + |
| 140 | +expect(()=>{ |
| 141 | +const_=newCharSet('H20') |
| 142 | +}).toThrowError(Error) |
| 143 | +}) |
| 144 | + |
| 145 | +test('Bytes needed',()=>{ |
| 146 | +constBITS_PER_BYTE=8 |
| 147 | +constdoTest=(charset,bits)=>{ |
| 148 | +constbytesNeeded=charset.bytesNeeded(bits) |
| 149 | +constatLeast=Math.ceil(bits/BITS_PER_BYTE) |
| 150 | +expect(atLeast<=bytesNeeded).toBe(true) |
| 151 | +constatMost=atLeast+1 |
| 152 | +expect(bytesNeeded<=atMost).toBe(true) |
| 153 | +} |
| 154 | + |
| 155 | +constcharsets=[charset64,charset32,charset16,charset8,charset4,charset2] |
| 156 | +charsets.forEach((charset)=>{ |
| 157 | +for(letbits=0;bits<=10;bits+=1){ |
| 158 | +doTest(charset,bits) |
| 159 | +} |
| 160 | +for(letbits=12;bits<=132;bits+=5){ |
| 161 | +doTest(charset,bits) |
| 162 | +} |
| 163 | +}) |
| 164 | +}) |