-
Notifications
You must be signed in to change notification settings - Fork 11.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add AddressToUintMap #3150
Merged
Merged
Add AddressToUintMap #3150
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
83429dc
add AddressToUintMap
gaspardip 2496934
Update contracts/utils/structs/EnumerableMap.sol
gaspardip 9366783
address comments
gaspardip aabf397
lint code
gaspardip 9e23aaf
merge mocks into a single file
Amxx 7f4db4a
add PR link to changelog entry
Amxx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
157 changes: 157 additions & 0 deletions
157
test/utils/structs/EnumerableMap/AddressToUintMap.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
const { BN, expectEvent, expectRevert } = require('@openzeppelin/test-helpers'); | ||
const { expect } = require('chai'); | ||
const { expectMembersMatch } = require('./helpers'); | ||
|
||
const AddressToUintMapMock = artifacts.require('AddressToUintMapMock'); | ||
|
||
contract('AddressToUintMap', function (accounts) { | ||
const [accountA, accountB, accountC] = accounts; | ||
|
||
const valueA = new BN('7891'); | ||
const valueB = new BN('451'); | ||
const valueC = new BN('9592328'); | ||
|
||
beforeEach(async function () { | ||
this.map = await AddressToUintMapMock.new(); | ||
}); | ||
|
||
it('starts empty', async function () { | ||
expect(await this.map.contains(accountA)).to.equal(false); | ||
|
||
await expectMembersMatch(this.map, [], []); | ||
}); | ||
|
||
describe('set', function () { | ||
it('adds a key', async function () { | ||
const receipt = await this.map.set(accountA, valueA); | ||
|
||
expectEvent(receipt, 'OperationResult', { result: true }); | ||
|
||
await expectMembersMatch(this.map, [accountA], [valueA]); | ||
}); | ||
|
||
it('adds several keys', async function () { | ||
await this.map.set(accountA, valueA); | ||
await this.map.set(accountB, valueB); | ||
|
||
await expectMembersMatch(this.map, [accountA, accountB], [valueA, valueB]); | ||
|
||
expect(await this.map.contains(accountC)).to.equal(false); | ||
}); | ||
|
||
it('returns false when adding keys already in the set', async function () { | ||
await this.map.set(accountA, valueA); | ||
|
||
const receipt = await this.map.set(accountA, valueA); | ||
|
||
expectEvent(receipt, 'OperationResult', { result: false }); | ||
|
||
await expectMembersMatch(this.map, [accountA], [valueA]); | ||
}); | ||
|
||
it('updates values for keys already in the set', async function () { | ||
await this.map.set(accountA, valueA); | ||
await this.map.set(accountA, valueB); | ||
|
||
await expectMembersMatch(this.map, [accountA], [valueB]); | ||
}); | ||
}); | ||
|
||
describe('remove', function () { | ||
it('removes added keys', async function () { | ||
await this.map.set(accountA, valueA); | ||
|
||
const receipt = await this.map.remove(accountA); | ||
|
||
expectEvent(receipt, 'OperationResult', { result: true }); | ||
|
||
expect(await this.map.contains(accountA)).to.equal(false); | ||
|
||
await expectMembersMatch(this.map, [], []); | ||
}); | ||
|
||
it('returns false when removing keys not in the set', async function () { | ||
const receipt = await this.map.remove(accountA); | ||
|
||
expectEvent(receipt, 'OperationResult', { result: false }); | ||
|
||
expect(await this.map.contains(accountA)).to.equal(false); | ||
}); | ||
|
||
it('adds and removes multiple keys', async function () { | ||
// [] | ||
|
||
await this.map.set(accountA, valueA); | ||
await this.map.set(accountC, valueC); | ||
|
||
// [A, C] | ||
|
||
await this.map.remove(accountA); | ||
await this.map.remove(accountB); | ||
|
||
// [C] | ||
|
||
await this.map.set(accountB, valueB); | ||
|
||
// [C, B] | ||
|
||
await this.map.set(accountA, valueA); | ||
await this.map.remove(accountC); | ||
|
||
// [A, B] | ||
|
||
await this.map.set(accountA, valueA); | ||
await this.map.set(accountB, valueB); | ||
|
||
// [A, B] | ||
|
||
await this.map.set(accountC, valueC); | ||
await this.map.remove(accountA); | ||
|
||
// [B, C] | ||
|
||
await this.map.set(accountA, valueA); | ||
await this.map.remove(accountB); | ||
|
||
// [A, C] | ||
|
||
await expectMembersMatch(this.map, [accountA, accountC], [valueA, valueC]); | ||
|
||
expect(await this.map.contains(accountB)).to.equal(false); | ||
}); | ||
}); | ||
|
||
describe('read', function () { | ||
beforeEach(async function () { | ||
await this.map.set(accountA, valueA); | ||
}); | ||
|
||
describe('get', function () { | ||
it('existing value', async function () { | ||
expect(await this.map.get(accountA)).to.bignumber.equal(valueA); | ||
}); | ||
|
||
it('missing value', async function () { | ||
await expectRevert(this.map.get(accountB), 'EnumerableMap: nonexistent key'); | ||
}); | ||
}); | ||
|
||
describe('tryGet', function () { | ||
const stringifyTryGetValue = ({ 0: result, 1: value }) => ({ 0: result, 1: value.toString() }); | ||
|
||
it('existing value', async function () { | ||
const actual = stringifyTryGetValue(await this.map.tryGet(accountA)); | ||
const expected = stringifyTryGetValue({ 0: true, 1: valueA }); | ||
|
||
expect(actual).to.deep.equal(expected); | ||
}); | ||
|
||
it('missing value', async function () { | ||
const actual = stringifyTryGetValue(await this.map.tryGet(accountB)); | ||
const expected = stringifyTryGetValue({ 0: false, 1: new BN('0') }); | ||
|
||
expect(actual).to.deep.equal(expected); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need these changes to
.prettierrc
? Can you explain what they do exactly ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The lint config doesn't match the prettier config for what I've noticed using the vscode prettier extension (although running the package.json lint scripts would give the same result)