Skip to content
This repository has been archived by the owner on Jul 8, 2020. It is now read-only.

Commit

Permalink
Merge pull request #6 from project-rig/fix-probability-scaling
Browse files Browse the repository at this point in the history
Fix probability scaling
  • Loading branch information
mossblaser committed Jun 21, 2016
2 parents 11930e1 + 987afb0 commit e8d964d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
4 changes: 2 additions & 2 deletions network_tester/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,10 +316,10 @@ def probability(self, source_num, probability):
if self._probability[source_num] != probability:
self._probability[source_num] = probability
# Convert to value between 0 and 0xFFFFFFFF
if probability == 1.0:
if probability >= 1.0:
probability = 0xFFFFFFFF
else:
probability = int(round(probability * (1 << 32)))
probability = int(round(probability * ((1 << 32) - 1)))
self._commands.extend([NT_CMD.PROBABILITY | (source_num << 8),
probability])

Expand Down
10 changes: 6 additions & 4 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,18 +252,20 @@ def test_probability():
a.probability(1, 0.0)
assert len(a._commands) == 2

# If should produce command on change
a.probability(0, 0.5)
# If changed should produce command on change. Note that in this test we
# check that very-nearly-one values get rounded up to one. Previously an
# off-by-one bug rounded up *past* one.
a.probability(0, 0.9999999999999999)
assert len(a._commands) == 4
assert a._commands[-2:] == [NT_CMD.PROBABILITY | (0 << 8),
1 << 31]
(1 << 32) - 1]
a.probability(1, 0.25)
assert len(a._commands) == 6
assert a._commands[-2:] == [NT_CMD.PROBABILITY | (1 << 8),
1 << 30]

# No command should be produced on non-change again
a.probability(0, 0.5)
a.probability(0, 0.9999999999999999)
a.probability(1, 0.25)
assert len(a._commands) == 6

Expand Down

0 comments on commit e8d964d

Please sign in to comment.