Merge pull request #22551 from basepi/test.fib

Make test.fib much more naive
This commit is contained in:
Justin Findlay 2015-04-12 16:25:02 -06:00
commit 148ee03629
5 changed files with 26 additions and 22 deletions

View File

@ -253,8 +253,10 @@ def arg_repr(*args, **kwargs):
def fib(num):
'''
Return a Fibonacci sequence up to but not including the passed number,
and the time it took to compute in seconds. Used for performance tests.
Return the num-th Fibonacci number, and the time it took to compute in
seconds. Used for performance tests.
This function is designed to have terrible performance.
CLI Example:
@ -264,12 +266,18 @@ def fib(num):
'''
num = int(num)
start = time.time()
fib_a, fib_b = 0, 1
ret = [0]
while fib_b < num:
ret.append(fib_b)
fib_a, fib_b = fib_b, fib_a + fib_b
return ret, time.time() - start
if num < 2:
return num, time.time() - start
return _fib(num-1) + _fib(num-2), time.time() - start
def _fib(num):
'''
Helper method for test.fib, doesn't calculate the time.
'''
if num < 2:
return num
return _fib(num-1) + _fib(num-2)
def collatz(start):

View File

@ -28,9 +28,9 @@ class TestSyndic(integration.SyndicCase):
self.assertEqual(
self.run_function(
'test.fib',
['40'],
)[0][-1],
34
['20'],
)[0],
6765
)

View File

@ -88,10 +88,10 @@ class PublishModuleTest(integration.ModuleCase,
'''
ret = self.run_function(
'publish.full_data',
['minion', 'test.fib', 40]
['minion', 'test.fib', 20]
)
self.assertTrue(ret)
self.assertEqual(ret['minion']['ret'][0][-1], 34)
self.assertEqual(ret['minion']['ret'][0], 6765)
def test_kwarg(self):
'''

View File

@ -73,9 +73,9 @@ class TestModuleTest(integration.ModuleCase,
self.assertEqual(
self.run_function(
'test.fib',
['40'],
)[0][-1],
34
['20'],
)[0],
6765
)
def test_collatz(self):

View File

@ -34,18 +34,14 @@ class CallTest(integration.ShellCase, integration.ShellCaseCommonTestsMixIn):
out = self.run_call('-l quiet test.fib 3')
expect = ['local:',
' |_',
' - 0',
' - 1',
' - 1',
' - 2']
' - 2']
self.assertEqual(expect, out[:-1])
def test_text_output(self):
out = self.run_call('-l quiet --out txt test.fib 3')
expect = [
'local: ([0, 1, 1, 2]'
'local: (2'
]
self.assertEqual(''.join(expect), ''.join(out).rsplit(",", 1)[0])