-
Notifications
You must be signed in to change notification settings - Fork 4
/
numbers.py
executable file
·51 lines (37 loc) · 1.38 KB
/
numbers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import random
from Peach.fixup import Fixup
class EvenNumber(Fixup):
def __init__(self, ref):
Fixup.__init__(self)
self.ref = ref
def fixup(self):
ref = self.context.findDataElementByName(self.ref)
stuff = ref.getValue()
if stuff is None:
raise Exception("Error: EvenNumberFixup was unable to locate "
"[{}]".format(self.ref))
return stuff if stuff % 2 == 0 else stuff + 1
class SequenceIncrementFixup(Fixup):
"""
Allows a field to emit a sequential value without adding additional test
cases. This is useful for sequence numbers common in network protocols.
"""
num = 1
def __init__(self):
Fixup.__init__(self)
def fixup(self):
SequenceIncrementFixup.num += 1
return SequenceIncrementFixup.num
class SequenceRandomFixup(Fixup):
"""
Allows a field to emit a random value without adding additional test cases.
This is useful for sequence numbers common in network protocols.
"""
def __init__(self):
random.seed()
Fixup.__init__(self)
def fixup(self):
return random.randint(0, (1 << self.context.size) - 1)