Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
daleroberts committed May 30, 2014
1 parent 23fce96 commit f605b49
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 140 deletions.
31 changes: 0 additions & 31 deletions tests/in_parallel.py

This file was deleted.

68 changes: 0 additions & 68 deletions tests/scatter.py

This file was deleted.

41 changes: 0 additions & 41 deletions tests/sendrecv.py

This file was deleted.

72 changes: 72 additions & 0 deletions tests/test_scatter.py
@@ -0,0 +1,72 @@
import unittest
import pypar as pp
import numpy as np


@pp.test.in_parallel(ncpus=4)
class TestScatter(unittest.TestCase):

def test_string(self):
myid, ncpu = pp.rank(), pp.size()
data = 'ABCDEFGHIJKLMNOP' # Length = 16
NP = len(data) / ncpu
X = ' ' * NP

pp.scatter(data, 0, buffer=X) # With buffer
Y = pp.scatter(data, 0) # With buffer automatically created

self.assertEqual(X, Y)
self.assertEqual(Y, data[myid * NP:(myid + 1) * NP])
self.assertEqual(X, data[myid * NP:(myid + 1) * NP])

def test_array(self):
myid, ncpu = pp.rank(), pp.size()
N = 16
NP = N / ncpu
data = np.array(range(N)).astype('i')
X = np.zeros(NP).astype('i')

pp.scatter(data, 0, buffer=X) # With buffer
Y = pp.scatter(data, 0) # With buffer automatically created

self.assertTrue(np.allclose(X, Y))
self.assertTrue(np.allclose(X, data[myid * NP:(myid + 1) * NP]))
self.assertTrue(np.allclose(Y, data[myid * NP:(myid + 1) * NP]))

def test_without_root(self):
myid, ncpu = pp.rank(), pp.size()
N = 16
NP = N / ncpu
data = np.array(range(N)).astype('i')
X = np.zeros(NP).astype('i')

pp.scatter(data, buffer=X) # With buffer
Y = pp.scatter(data) # With buffer automatically created

self.assertTrue(np.allclose(X, Y))
self.assertTrue(np.allclose(X, data[myid * NP:(myid + 1) * NP]))
self.assertTrue(np.allclose(Y, data[myid * NP:(myid + 1) * NP]))

def test_diff_master(self):
myid, ncpu = pp.rank(), pp.size()
N = 16
NP = N / ncpu

check = np.array(range(N)).astype('i')
X = np.zeros(NP).astype('i')

data = np.empty(N, 'i')

if myid == 0: # only generated on master
data = np.array(range(N)).astype('i')

pp.scatter(data, 0, buffer=X) # With buffer
Y = pp.scatter(data, 0) # With buffer automatically created

self.assertTrue(np.allclose(X, Y))
self.assertTrue(np.allclose(X, check[myid * NP:(myid + 1) * NP]))
self.assertTrue(np.allclose(Y, check[myid * NP:(myid + 1) * NP]))


if __name__ == '__main__':
pp.test.main()
45 changes: 45 additions & 0 deletions tests/test_sendrecv.py
@@ -0,0 +1,45 @@
import unittest
import numpy as np
import pypar as pp


@pp.test.in_parallel(ncpus=4)
class TestSendReceive(unittest.TestCase):

def test_int_array(self):
myid, ncpu = pp.rank(), pp.size()
N = 17 # Number of elements

if myid == 0:
A = np.array(range(N)).astype('i')
B = np.zeros(N).astype('i')

pp.send(A, 1, use_buffer=True)
B = pp.receive(ncpu - 1, buffer=B)

self.assertTrue(np.allclose(A, B))
else:
X = np.zeros(N).astype('i')

X = pp.receive(myid - 1, buffer=X)
pp.send(X, (myid + 1) % ncpu, use_buffer=True)

def test_longint_array(self):
myid, ncpu = pp.rank(), pp.size()
N = 17 # Number of elements

if myid == 0:
A = np.array(range(N)).astype('l')
B = np.zeros(N).astype('l')

pp.send(A, 1, use_buffer=True)
B = pp.receive(ncpu - 1, buffer=B)

self.assertTrue(np.allclose(A, B))
else:
X = np.zeros(N).astype('l')
X = pp.receive(myid - 1, buffer=X)
pp.send(X, (myid + 1) % ncpu, use_buffer=True)

if __name__ == '__main__':
pp.test.main()
27 changes: 27 additions & 0 deletions tests/test_testing.py
@@ -0,0 +1,27 @@
import unittest
import pypar as pp


@pp.test.in_parallel(ncpus=4)
class TestTesting(unittest.TestCase):

def setUp(self):
self.x = 31337

def test_ncpus(self):
self.assertTrue(pp.size() == 4)

def test_setup(self):
self.assertTrue(self.x, 31337)

def test_assertTrue(self):
self.assertTrue(True)

def test_assertEqual(self):
self.assertEqual("a", "a")

def test_assertFalse(self):
self.assertFalse(False)

if __name__ == '__main__':
pp.test.main()

0 comments on commit f605b49

Please sign in to comment.