Skip to content

Commit

Permalink
added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thearn committed Feb 28, 2014
1 parent 2305144 commit ced9c53
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 2 deletions.
29 changes: 29 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# vim ft=yaml
# travis-ci.org definition for game-of-life build
#

language: c
env:
- PYTHON=python PYSUF='' PYVER=2.7
- PYTHON=python3 PYSUF='3' PYVER=3.3
install:
- sudo apt-get update # needed for python3-numpy
- sudo apt-get install $PYTHON-dev
- sudo apt-get install $PYTHON-numpy
- sudo apt-get install $PYTHON-scipy
- sudo apt-get install $PYTHON-setuptools
- sudo apt-get install $PYTHON-nose
- sudo easy_install$PYSUF pip
- sudo pip install Pillow
- if [[ $PYVER == '2.7' ]]; then sudo apt-get install $PYTHON-matplotlib; fi
- if [[ $PYVER == '3.3' ]]; then sudo pip-$PYVER install git+git://github.com/matplotlib/matplotlib.git@v1.2.x; fi
- $PYTHON setup.py build
- sudo $PYTHON setup.py install
script:
# Change into an innocuous directory and find tests from installation
- mkdir $HOME/.matplotlib
- "echo 'backend : Agg' > $HOME/.matplotlib/matplotlibrc"
- "echo 'backend.qt4 : PyQt4' >> $HOME/.matplotlib/matplotlibrc"
- "cd game-of-life"
- if [[ $PYVER == '2.7' ]]; then nosetests-2.7; fi
- if [[ $PYVER == '3.2' ]]; then nosetests-3.2; fi
11 changes: 9 additions & 2 deletions conway.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@
import matplotlib.pyplot as plt
plt.ion()

def conway(state, k):
def conway(state, k=None):
"""
Conway's game of life state transition
"""

# set up kernel if not given
if k == None:
m, n = state.shape
k = np.zeros((m, n))
k[m/2-1 : m/2+2, n/2-1 : n/2+2] = np.array([[1,1,1],[1,0,1],[1,1,1]])

# computes sums around each pixel
b = fft_convolve2d(state,k).round()

Expand All @@ -27,7 +34,7 @@ def conway(state, k):
m,n = 100,100
A = np.random.random(m*n).reshape((m, n)).round()

# construct convolution kernel
# construct convolution kernel (most efficient to do this once)
k = np.zeros((m, n))
k[m/2-1 : m/2+2, n/2-1 : n/2+2] = np.array([[1,1,1],[1,0,1],[1,1,1]])

Expand Down
35 changes: 35 additions & 0 deletions lib/test/test_automata.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from conway import conway
import unittest
import numpy as np

class TestConway(unittest.TestCase):

def test_still(self):
"""2x2 block"""

A = np.zeros((10,10))
A[1:3,1:3] = 1
B = conway(A)
assert (A == B).all()

def test_scillator(self):
"""blinker"""
A = np.zeros((10,10))
A[1:4,1] = 1
B = conway(A)
assert (B[2, 0:3] == 1).all()

B = conway(B)
assert (A == B).all()

def test_evolution(self):
"""test that something changes"""
m, n = 10, 10
A = np.random.random(m*n).reshape((m, n)).round()
B = conway(A)
assert (B != A).any()



if __name__ == '__main__':
unittest.main()

0 comments on commit ced9c53

Please sign in to comment.