Remove use of generatePseudoRandomAddress in favor of randomAddress

This commit is contained in:
Amir Bandeali
2019-09-17 10:46:58 -07:00
parent 94738444de
commit bb46f184ed
7 changed files with 42 additions and 39 deletions

View File

@@ -1,4 +1,4 @@
import { addressUtils, chaiSetup, provider, txDefaults, web3Wrapper } from '@0x/contracts-test-utils';
import { chaiSetup, provider, randomAddress, txDefaults, web3Wrapper } from '@0x/contracts-test-utils';
import { BlockchainLifecycle } from '@0x/dev-utils';
import { BigNumber, LibAddressArrayRevertErrors } from '@0x/utils';
import * as chai from 'chai';
@@ -29,23 +29,23 @@ describe('LibAddressArray', () => {
describe('append', () => {
it('should append to empty array', async () => {
const addr = addressUtils.generatePseudoRandomAddress();
const addr = randomAddress();
const result = await lib.publicAppend.callAsync([], addr);
const expected = [addr];
expect(result).to.deep.equal(expected);
});
it('should append to non-empty array', async () => {
const arr = _.times(3, () => addressUtils.generatePseudoRandomAddress());
const addr = addressUtils.generatePseudoRandomAddress();
const arr = _.times(3, () => randomAddress());
const addr = randomAddress();
const expected = [...arr, addr];
const result = await lib.publicAppend.callAsync(arr, addr);
expect(result).to.deep.equal(expected);
});
it('should revert if the free memory pointer was moved to before the end of the array', async () => {
const arr = _.times(3, () => addressUtils.generatePseudoRandomAddress());
const addr = addressUtils.generatePseudoRandomAddress();
const arr = _.times(3, () => randomAddress());
const addr = randomAddress();
const freeMemOffset = new BigNumber(-1);
const addressArrayEndPtr = new BigNumber(256);
const expectedError = new LibAddressArrayRevertErrors.MismanagedMemoryError(
@@ -56,8 +56,8 @@ describe('LibAddressArray', () => {
});
it('should keep the same memory address if free memory pointer does not move', async () => {
const arr = _.times(3, () => addressUtils.generatePseudoRandomAddress());
const addr = addressUtils.generatePseudoRandomAddress();
const arr = _.times(3, () => randomAddress());
const addr = randomAddress();
const freeMemOffset = new BigNumber(0);
const expected = [...arr, addr];
const [result, oldArrayMemStart, newArrayMemStart] = await lib.testAppendRealloc.callAsync(
@@ -70,8 +70,8 @@ describe('LibAddressArray', () => {
});
it('should change memory address if free memory pointer advances', async () => {
const arr = _.times(3, () => addressUtils.generatePseudoRandomAddress());
const addr = addressUtils.generatePseudoRandomAddress();
const arr = _.times(3, () => randomAddress());
const addr = randomAddress();
const freeMemOffset = new BigNumber(1);
const expectedArray = [...arr, addr];
const [result, oldArrayMemStart, newArrayMemStart] = await lib.testAppendRealloc.callAsync(
@@ -88,27 +88,27 @@ describe('LibAddressArray', () => {
describe('contains', () => {
it('should return false on an empty array', async () => {
const addr = addressUtils.generatePseudoRandomAddress();
const addr = randomAddress();
const isFound = await lib.publicContains.callAsync([], addr);
expect(isFound).to.equal(false);
});
it('should return false on a missing item', async () => {
const arr = _.times(3, () => addressUtils.generatePseudoRandomAddress());
const addr = addressUtils.generatePseudoRandomAddress();
const arr = _.times(3, () => randomAddress());
const addr = randomAddress();
const isFound = await lib.publicContains.callAsync(arr, addr);
expect(isFound).to.equal(false);
});
it('should return true on an included item', async () => {
const arr = _.times(4, () => addressUtils.generatePseudoRandomAddress());
const arr = _.times(4, () => randomAddress());
const addr = _.sample(arr) as string;
const isFound = await lib.publicContains.callAsync(arr, addr);
expect(isFound).to.equal(true);
});
it('should return true on the only item in the array', async () => {
const arr = _.times(1, () => addressUtils.generatePseudoRandomAddress());
const arr = _.times(1, () => randomAddress());
const isFound = await lib.publicContains.callAsync(arr, arr[0]);
expect(isFound).to.equal(true);
});
@@ -116,20 +116,20 @@ describe('LibAddressArray', () => {
describe('indexOf', () => {
it('should fail on an empty array', async () => {
const addr = addressUtils.generatePseudoRandomAddress();
const addr = randomAddress();
const [isSuccess] = await lib.publicIndexOf.callAsync([], addr);
expect(isSuccess).to.equal(false);
});
it('should fail on a missing item', async () => {
const arr = _.times(3, () => addressUtils.generatePseudoRandomAddress());
const addr = addressUtils.generatePseudoRandomAddress();
const arr = _.times(3, () => randomAddress());
const addr = randomAddress();
const [isSuccess] = await lib.publicIndexOf.callAsync(arr, addr);
expect(isSuccess).to.equal(false);
});
it('should succeed on an included item', async () => {
const arr = _.times(4, () => addressUtils.generatePseudoRandomAddress());
const arr = _.times(4, () => randomAddress());
const expectedIndexOf = _.random(0, arr.length - 1);
const addr = arr[expectedIndexOf];
const [isSuccess, index] = await lib.publicIndexOf.callAsync(arr, addr);
@@ -138,7 +138,7 @@ describe('LibAddressArray', () => {
});
it('should succeed on the only item in the array', async () => {
const arr = _.times(1, () => addressUtils.generatePseudoRandomAddress());
const arr = _.times(1, () => randomAddress());
const [isSuccess, index] = await lib.publicIndexOf.callAsync(arr, arr[0]);
expect(isSuccess).to.equal(true);
expect(index).bignumber.to.equal(0);