Skip to content

Commit

Permalink
Grains : sync expected test results and input data with problem-speci…
Browse files Browse the repository at this point in the history
…fications (#1794)

* grains: sync expected test results and input data with problem-specifications.
  • Loading branch information
cmccandless authored May 28, 2019
2 parents 09b499c + 107cc5a commit 7c64637
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 33 deletions.
25 changes: 12 additions & 13 deletions exercises/grains/example.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
def on_square(square):
check_square_input(square)
return 2 ** (square - 1)
def square(number):
check_square_input(number)

return 2 ** (number - 1)

def total_after(square):
check_square_input(square)
return sum(
on_square(n + 1) for n
in range(square)
)

def total(number):
check_square_input(number)

def check_square_input(square):
if square == 0:
return sum(square(n + 1) for n in range(number))


def check_square_input(number):
if number == 0:
raise ValueError("Square input of zero is invalid.")
elif square < 0:
elif number < 0:
raise ValueError("Negative square input is invalid.")
elif square > 64:
elif number > 64:
raise ValueError("Square input greater than 64 is invalid.")
4 changes: 2 additions & 2 deletions exercises/grains/grains.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
def on_square(integer_number):
def square(number):
pass


def total_after(integer_number):
def total(number):
pass
33 changes: 15 additions & 18 deletions exercises/grains/grains_test.py
Original file line number Diff line number Diff line change
@@ -1,55 +1,52 @@
import unittest

from grains import (
on_square,
total_after,
)
from grains import square, total


# Tests adapted from `problem-specifications//canonical-data.json` @ v1.2.0

class GrainsTest(unittest.TestCase):
def test_square_1(self):
self.assertEqual(on_square(1), 1)
self.assertEqual(square(1), 1)

def test_square_2(self):
self.assertEqual(on_square(2), 2)
self.assertEqual(square(2), 2)

def test_square_3(self):
self.assertEqual(on_square(3), 4)
self.assertEqual(square(3), 4)

def test_square_4(self):
self.assertEqual(on_square(4), 8)
self.assertEqual(square(4), 8)

def test_square_16(self):
self.assertEqual(on_square(16), 32768)
self.assertEqual(square(16), 32768)

def test_square_32(self):
self.assertEqual(on_square(32), 2147483648)
self.assertEqual(square(32), 2147483648)

def test_square_64(self):
self.assertEqual(on_square(64), 9223372036854775808)
self.assertEqual(square(64), 9223372036854775808)

def test_square_0_raises_exception(self):
with self.assertRaisesWithMessage(ValueError):
on_square(0)
square(0)
with self.assertRaisesWithMessage(ValueError):
total_after(0)
total(0)

def test_square_negative_raises_exception(self):
with self.assertRaisesWithMessage(ValueError):
on_square(-1)
square(-1)
with self.assertRaisesWithMessage(ValueError):
total_after(-1)
total(-1)

def test_square_gt_64_raises_exception(self):
with self.assertRaisesWithMessage(ValueError):
on_square(65)
square(65)
with self.assertRaisesWithMessage(ValueError):
total_after(65)
total(65)

def test_total(self):
self.assertEqual(total_after(64), 18446744073709551615)
self.assertEqual(total(64), 18446744073709551615)

# Utility functions
def setUp(self):
Expand Down

0 comments on commit 7c64637

Please sign in to comment.