The following test case is typical. Bamboo: 2, 2, 2, 3, 4, 5, 5,
5; dots: 2, 2, 2; green dragon, green dragon, green dragon. In this
case, we will attempt to put the three 2 bamboo tiles into a set of
four, pop that set, and put them into a set of three. The 3 will be put
into a set of four, a set of three and then a straight with the 4 and 5.
The next two fives will be put into a set of four, a set of three, a
straight and a pair. The 2 dots tiles and the green dragon tiles will
both be put into four sets and three sets. The final set stack will have
a three set, a straight, a pair, and two three sets.
def testHand1():
t1= [ SuitTile( 2, "Bamboo" ), SuitTile( 2, "Bamboo" ),
SuitTile( 2, "Bamboo" ), SuitTile( 3, "Bamboo" ),
SuitTile( 4, "Bamboo" ), SuitTile( 5, "Bamboo" ),
SuitTile( 5, "Bamboo" ), SuitTile( 5, "Bamboo" ),
SuitTile( 2, "Dot" ), SuitTile( 2, "Dot" ),
SuitTile( 2, "Dot" ), HonorsTile( "Green" ),
HonorsTile( "Green" ), HonorsTile( "Green" ), ]
h1= Hand( *t1 )
print h1.mahjongg()
The following test case is a little more difficult. Bamboo: 2, 2,
2, 2, 3, 4, 3 × green dragon, 3 × red dragon, 3 × north wind. The
initial run of four 2 bamboo tiles will be put into a set of four. The
next 3 bamboo and 4 bamboo will be put into a four set, three set and
straight. The first green dragon won't fit into the straight, causing us
to pop the straight, attempt a pair, and pop this. We then pop the
initial set of four two's and replace that with a set of three. The 2
bamboo and 3 bamboo will be checked against a four set and a three set
before being put into a straight.
Here's a challenging test case with two groups of tiles that
require multiple retries.
def testHand2():
t2= [ SuitTile( 2, "Bamboo" ), SuitTile( 2, "Bamboo" ),
SuitTile( 2, "Bamboo" ), SuitTile( 3, "Bamboo" ),
SuitTile( 4, "Bamboo" ), SuitTile( 5, "Bamboo" ),
SuitTile( 5, "Bamboo" ), SuitTile( 5, "Bamboo" ),
SuitTile( 2, "Dot" ), SuitTile( 2, "Dot" ),
SuitTile( 2, "Dot" ), SuitTile( 2, "Dot" ),
SuitTile( 3, "Dot" ), SuitTile( 4, "Dot" ), ]
h2= Hand( *t2 )
print h2.mahjongg()
Ideally, your overall unit test looks something like the
following.
import unittest
class TestHand(unittest.TestCase):
def testHand1( self ):
body of testHand1
self.assert_( h1.mahjongg() )
self.assertEqual( str(h1.sets[0]),
"ThreeSet['2B', '2B', '2B']" )
self.assertEqual( str(h1.sets[1]),
"SequenceSet['3B', '4B', '5B']" )
self.assertEqual( str(h1.sets[2]),
"PairSet['5B', '5B']" )
self.assertEqual( str(h1.sets[3]),
"ThreeSet['2D', '2D', '2D']" )
self.assertEqual( str(h1.sets[4]),
"ThreeSet['Green', 'Green', 'Green']" )
def testHand2( self ):
body of testHand2
self.assert_( h2.mahjongg() )
check individual Sets
if __name__ == "__main__":
unittest.main()
A set of nine interesting test cases can be built around the
following set of tiles: 3×1's, 2, 3, 4, 5, 6, 7, 8, and 3×9's all of the
same suit. Adding any number tile of the same suit to this set of 13
will create a winning hand. Develop a test function that iterates
through the nine possible hands and prints the results.