diff options
author | Remco Bloemen <remco@wicked.ventures> | 2018-05-28 18:50:03 +0800 |
---|---|---|
committer | Greg Hysen <greg.hysen@gmail.com> | 2018-06-08 06:38:47 +0800 |
commit | f5bc0b205c217eac8abdfee9dcdb3c4d21b5c31e (patch) | |
tree | 748c799d5a97e881459d64fc3e99495867a45844 /packages/contracts/test | |
parent | b3c253ea2a685bea3add43a81229911269eb4e89 (diff) | |
download | dexon-0x-contracts-f5bc0b205c217eac8abdfee9dcdb3c4d21b5c31e.tar dexon-0x-contracts-f5bc0b205c217eac8abdfee9dcdb3c4d21b5c31e.tar.gz dexon-0x-contracts-f5bc0b205c217eac8abdfee9dcdb3c4d21b5c31e.tar.bz2 dexon-0x-contracts-f5bc0b205c217eac8abdfee9dcdb3c4d21b5c31e.tar.lz dexon-0x-contracts-f5bc0b205c217eac8abdfee9dcdb3c4d21b5c31e.tar.xz dexon-0x-contracts-f5bc0b205c217eac8abdfee9dcdb3c4d21b5c31e.tar.zst dexon-0x-contracts-f5bc0b205c217eac8abdfee9dcdb3c4d21b5c31e.zip |
Generate tests from vectors
Diffstat (limited to 'packages/contracts/test')
-rw-r--r-- | packages/contracts/test/libraries/lib_mem.ts | 42 |
1 files changed, 40 insertions, 2 deletions
diff --git a/packages/contracts/test/libraries/lib_mem.ts b/packages/contracts/test/libraries/lib_mem.ts index 6ce4ab9b6..c884d5fea 100644 --- a/packages/contracts/test/libraries/lib_mem.ts +++ b/packages/contracts/test/libraries/lib_mem.ts @@ -18,6 +18,14 @@ chaiSetup.configure(); const expect = chai.expect; const blockchainLifecycle = new BlockchainLifecycle(web3Wrapper); +// BUG: Ideally we would use Buffer.from(memory).toString('hex') +// https://github.com/Microsoft/TypeScript/issues/23155 +const toHex = (buf: Uint8Array): string => + buf.reduce((a, v) => a + ('00' + v.toString(16)).slice(-2), '0x'); + +const fromHex = (str: string): Uint8Array => + Uint8Array.from(Buffer.from(str.slice(2), 'hex')); + describe('LibMem', () => { let owner: string; let testLibMem: TestLibMemContract; @@ -36,7 +44,37 @@ describe('LibMem', () => { await blockchainLifecycle.revertAsync(); }); - describe('LibMem', () => { + describe('memcpy', () => { + + // Create memory 0x000102...FF + const memSize = 256; + const memory = (new Uint8Array(memSize)).map((_, i) => i); + const memHex = toHex(memory); + + // Reference implementation to test against + const refMemcpy = (mem: Uint8Array, dest: number, source: number, length: number): Uint8Array => + Uint8Array.from(memory).copyWithin(dest, source, source + length); + + // Test vectors: destination, source, length, job description + const tests: Array<[number, number, number, string]> = [ + [1, 5, 4, 'four bytes within one word'], + ]; + + // Construct test cases + tests.forEach(([dest, source, length, job]) => + it(`copies ${job}`, async () => { + const expected = refMemcpy(memory, dest, source, length); + const resultStr = await testLibMem.testMemcpy.callAsync( + memHex, + new BigNumber(dest), + new BigNumber(source), + new BigNumber(length), + ); + const result = fromHex(resultStr); + expect(result).to.deep.equal(expected); + }), + ); + it('should )', async () => { await testLibMem.test1.sendTransactionAsync(); }); @@ -62,7 +100,7 @@ describe('LibMem', () => { }); it('should )', async () => { - return expect(testLibMem.test7.sendTransactionAsync()).to.be.rejectedWith(constants.REVERT); + return expect(testLibMem.test7.sendTransactionAsync()).to.be.rejectedWith(constants.REVERT ); }); }); }); |