|
| 1 | +/// <reference types="mocha" /> |
| 2 | + |
| 3 | +import { |
| 4 | + basename, |
| 5 | + join, |
| 6 | +} from '@waiting/shared-core' |
| 7 | +import * as assert from 'power-assert' |
| 8 | +import rewire = require('rewire') |
| 9 | +import { TextDecoder, TextEncoder } from 'util' |
| 10 | + |
| 11 | +import { b64byteLength, b64encode } from '../src' |
| 12 | +import { fromUint8Array } from '../src/lib/from_buffer' |
| 13 | +import { toUint8Array } from '../src/lib/to_buffer' |
| 14 | + |
| 15 | +import { input1, input2, input3 } from './config' |
| 16 | +import { equal } from './helper' |
| 17 | + |
| 18 | + |
| 19 | +const filename = basename(__filename) |
| 20 | + |
| 21 | + |
| 22 | +describe(filename, () => { |
| 23 | + describe('Should fromUint8Array() works with big input', () => { |
| 24 | + it('test1', () => { |
| 25 | + const big = new Uint8Array(128 * 1024 * 1024) |
| 26 | + |
| 27 | + for (let i = 0, length = big.length; i < length; ++i) { |
| 28 | + big[i] = i % 256 |
| 29 | + } |
| 30 | + const b64 = fromUint8Array(big) |
| 31 | + const u8arr = toUint8Array(b64) |
| 32 | + |
| 33 | + assert(equal(u8arr, big)) |
| 34 | + assert(b64byteLength(b64) === u8arr.length) |
| 35 | + }) |
| 36 | + |
| 37 | + it('test2', () => { |
| 38 | + const str = input1.concat(input2, input3).join('').repeat(500_000) |
| 39 | + const len = (str.length / 1024 / 1024).toFixed(2) |
| 40 | + const ret1 = b64encode(str, TextEncoder) |
| 41 | + const ret2 = Buffer.from(str.toString()).toString('base64') |
| 42 | + |
| 43 | + console.info(`test2: string length ${len} MB`) |
| 44 | + assert(ret1 === ret2, `input: "${str.toString()}"`) |
| 45 | + }) |
| 46 | + }) |
| 47 | +}) |
0 commit comments