Skip to content

Commit

Permalink
Merge 926fbee into 8dd7102
Browse files Browse the repository at this point in the history
  • Loading branch information
makoto committed Mar 28, 2020
2 parents 8dd7102 + 926fbee commit 97a620e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 29 deletions.
6 changes: 3 additions & 3 deletions src/participants.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ export const calculateFinalizeMaps = (participants, overrideMissingValue) => {
// check for missing participants
for (let i = 0; participants.length > i;) {
const currentIndex = participants[i].index
if (currentIndex !== i) {
if (currentIndex !== i + 1) {
if (overrideMissingValue === undefined) {
throw new Error(`Participant ${i} is missing`)
}

participants.splice(i, 0, {
overrideMissingValue,
index: i,
status: overrideMissingValue,
index: i + 1,
user: {
address: '0x0000000000000000000000000000000000000000'
},
Expand Down
50 changes: 24 additions & 26 deletions src/participants.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ describe('check if participant', () => {
{ user: { address: '0x123' } },
{ user: { address: '0xABdef1' } },
]

expect(isParticipant(ps, undefined)).toEqual(false)
expect(isParticipant(ps, null)).toEqual(false)
expect(isParticipant(ps, undefined)).toEqual(undefined)
expect(isParticipant(ps, null)).toEqual(null)
expect(isParticipant(ps, '0x456')).toEqual(false)
expect(isParticipant(ps, '0x123')).toEqual(true)
expect(isParticipant(ps, '0xabdef1')).toEqual(true)
Expand Down Expand Up @@ -95,7 +94,7 @@ describe('.calculateFinalizeMaps', () => {

for (let i = 0; 300 > i; i += 1) {
ps.push({
index: i,
index: i + 1,
status: PARTICIPANT_STATUS.REGISTERED
})
}
Expand Down Expand Up @@ -146,11 +145,11 @@ describe('.calculateFinalizeMaps', () => {
expect(calculateFinalizeMaps(ps)).toEqual(maps)
})

it.only('p1, p2, p256, p257, p298, p299 showed up', () => {
it('p1, p2, p256, p257, p298, p299 showed up', () => {
const maps = [
toBN(0).bincn(1).bincn(2).toString(10),
toBN(0).bincn(0).bincn(1).bincn(298 % 256)
.bincn(299 % 256)
toBN(0).bincn(1 - 1).bincn(2 - 1).bincn(256 - 1).toString(10),
toBN(0).bincn(1 - 1).bincn((298 % 256) - 1)
.bincn((299 % 256) - 1)
.toString(10),
]

Expand All @@ -168,14 +167,13 @@ describe('.calculateFinalizeMaps', () => {
break
}
})

expect(calculateFinalizeMaps(ps)).toEqual(maps)
})

it('p256 showed up', () => {
const maps = [
toBN(0).bincn(256 - 1).toString(10),
toBN(0).toString(10),
toBN(0).bincn(0).toString(10),
]

ps.forEach(p => {
Expand All @@ -193,8 +191,8 @@ describe('.calculateFinalizeMaps', () => {

it('p255, p257, p299 showed up', () => {
const maps = [
toBN(0).bincn(255).toString(10),
toBN(0).bincn(1).bincn(299 % 256).toString(10),
toBN(0).bincn(255 - 1).toString(10),
toBN(0).bincn((257 % 256) - 1).bincn((299 % 256) - 1).toString(10),
]

ps.forEach(p => {
Expand All @@ -215,7 +213,7 @@ describe('.calculateFinalizeMaps', () => {
it('p5 is missing, override REGISTERED', () => {
// We set #6 to SHOWED_UP and remove #5 from the list
const maps = [
toBN(0).bincn(6).toString(10),
toBN(0).bincn(6 - 1).toString(10),
toBN(0).toString(10),
]

Expand All @@ -230,15 +228,15 @@ describe('.calculateFinalizeMaps', () => {
})

ps.sort((a, b) => (a.index < b.index ? -1 : 1))
ps.splice(5, 1)
ps.splice(5 - 1, 1)

expect(calculateFinalizeMaps(ps, PARTICIPANT_STATUS.REGISTERED)).toEqual(maps)
})

it('p5 is missing, override SHOWED_UP', () => {
// We set #6 to SHOWED_UP and remove #5 from the list
const maps = [
toBN(0).bincn(6).bincn(5).toString(10),
toBN(0).bincn(6 - 1).bincn(5 - 1).toString(10),
toBN(0).toString(10),
]

Expand All @@ -253,7 +251,7 @@ describe('.calculateFinalizeMaps', () => {
})

ps.sort((a, b) => (a.index < b.index ? -1 : 1))
ps.splice(5, 1)
ps.splice(5 - 1, 1)

expect(calculateFinalizeMaps(ps, PARTICIPANT_STATUS.SHOWED_UP)).toEqual(maps)
})
Expand All @@ -270,7 +268,7 @@ describe('.calculateFinalizeMaps', () => {
})

ps.sort((a, b) => (a.index < b.index ? -1 : 1))
ps.splice(5, 1)
ps.splice(5 - 1, 1)

expect(() => calculateFinalizeMaps(ps)).toThrow()
})
Expand All @@ -289,7 +287,7 @@ describe('.updateParticipantListFromMaps', () => {

for (let i = 0; 300 > i; i += 1) {
ps.push({
index: i,
index: i + 1,
status: PARTICIPANT_STATUS.UNKNOWN
})
}
Expand Down Expand Up @@ -340,9 +338,9 @@ describe('.updateParticipantListFromMaps', () => {
expect.assertions(300)

const maps = [
toBN(0).bincn(1).bincn(2).toString(10),
toBN(0).bincn(0).bincn(1).bincn(298 % 256)
.bincn(299 % 256)
toBN(0).bincn(1 - 1).bincn(2 - 1).bincn(256 - 1).toString(10),
toBN(0).bincn(1 - 1).bincn((298 % 256) - 1)
.bincn((299 % 256) - 1)
.toString(10),
]

Expand All @@ -365,7 +363,7 @@ describe('.updateParticipantListFromMaps', () => {
})
})

it('handles p256 attended', () => {
it('handles p257 attended', () => {
expect.assertions(300)

const maps = [
Expand All @@ -377,7 +375,7 @@ describe('.updateParticipantListFromMaps', () => {

ps.forEach(({ index, status }) => {
switch (index) {
case 256:
case 257:
expect(status).toEqual(PARTICIPANT_STATUS.SHOWED_UP)
break
default:
Expand All @@ -391,8 +389,8 @@ describe('.updateParticipantListFromMaps', () => {
expect.assertions(300)

const maps = [
toBN(0).bincn(255).toString(10),
toBN(0).bincn(1).bincn(299 % 256).toString(10),
toBN(0).bincn(255 - 1).toString(10),
toBN(0).bincn(1 - 1).bincn((299 % 256) - 1).toString(10),
]

updateParticipantListFromMaps(ps, maps)
Expand Down Expand Up @@ -420,7 +418,7 @@ describe('list -> map -> list', () => {

for (let i = 0; 300 > i; i += 1) {
ps.push({
index: i,
index: i + 1,
status: PARTICIPANT_STATUS.REGISTERED
})
}
Expand Down

0 comments on commit 97a620e

Please sign in to comment.