S3 Fuzzer: set values can be weighted lists now

This commit is contained in:
Kyle Marsh 2011-08-10 15:10:24 -07:00
parent 4737652fc1
commit 3f1314f7c8
2 changed files with 31 additions and 4 deletions

View file

@ -52,8 +52,14 @@ def build_graph():
},
'choices': ['leaf']
}
graph['weighted_choices'] = {
'set': {},
graph['weighted_node'] = {
'set': {
'k1': [
'foo',
'2 bar',
'1 baz'
]
},
'choices': [
'foo',
'2 bar',
@ -169,7 +175,26 @@ def test_weighted_choices():
choices_made = {}
for _ in xrange(1000):
choice = make_choice(graph['weighted_choices']['choices'], prng)
choice = make_choice(graph['weighted_node']['choices'], prng)
if choices_made.has_key(choice):
choices_made[choice] += 1
else:
choices_made[choice] = 1
foo_percentage = choices_made['foo'] / 1000.0
bar_percentage = choices_made['bar'] / 1000.0
baz_percentage = choices_made['baz'] / 1000.0
nose.tools.assert_almost_equal(foo_percentage, 0.25, 1)
nose.tools.assert_almost_equal(bar_percentage, 0.50, 1)
nose.tools.assert_almost_equal(baz_percentage, 0.25, 1)
def test_weighted_set():
graph = build_graph()
prng = random.Random(1)
choices_made = {}
for _ in xrange(1000):
choice = make_choice(graph['weighted_node']['set']['k1'], prng)
if choices_made.has_key(choice):
choices_made[choice] += 1
else:

View file

@ -36,11 +36,13 @@ def descend_graph(decision_graph, node_name, prng):
for key in node['set']:
if decision.has_key(key):
raise KeyError("Node %s tried to set '%s', but that key was already set by a lower node!" %(node_name, key))
decision[key] = node['set'][key]
decision[key] = make_choice(node['set'][key], prng)
return decision
def make_choice(choices, prng):
if isinstance(choices, str):
return choices
weighted_choices = []
for option in choices:
fields = option.split(None, 1)