Mentions légales du service

Skip to content
Snippets Groups Projects
Commit 0ad1bdcb authored by hhakim's avatar hhakim
Browse files

Add a unit tests script for Python wrapper main class FaustPy.Faust.

Other minor changes: rename py function calls in test scripts as a consequence to a previous renaming in commit 57d19063 (getNbCol/Row() > get_nb_cols/rows()).
parent 8860f2dc
No related branches found
No related tags found
No related merge requests found
......@@ -232,8 +232,9 @@ if (BUILD_WRAPPER_PYTHON)
if(PYTHON_MODULE_SCIPY)
add_test(NAME PYTHON_FAUST_TIME COMMAND ${PYTHON_EXE} ${FAUST_SRC_TEST_SRC_PYTHON_DIR}/test_pyFaust_time.py ${FAUST_PYTHON_BIN_DIR} ${FAUST_BIN_TEST_FIG_DIR})
add_test(NAME PYTHON_FAUST_DEMO_INSTALL COMMAND ${PYTHON_EXE} ${CMAKE_INSTALL_PYTHON_PREFIX}/quickstart.py ${CMAKE_INSTALL_PYTHON_PREFIX})
endif(PYTHON_MODULE_SCIPY)
add_test(NAME PYTHON_FAUST_DEMO_INSTALL COMMAND ${PYTHON_EXE} ${FAUST_PYTHON_BIN_DIR}/quickstart.py)
add_test(NAME PYTHON_FAUST_UNIT_TESTS COMMAND ${PYTHON_EXE} ${FAUST_SRC_TEST_SRC_PYTHON_DIR}/test_FaustPy.py ${FAUST_PYTHON_BIN_DIR}) # second arg. is the FaustPy's dir. to add to PYTHONPATH
endif(PYTHON_MODULE_SCIPY)
endif(BUILD_WRAPPER_PYTHON)
......
import unittest
from scipy import sparse
import random
import tempfile
import os
import sys
import numpy as np
from scipy.io import savemat # , loadmat
class TestFaustPy(unittest.TestCase):
MAX_NUM_FACTORS = 64 # for the tested Faust
def setUp(self):
""" Initializes the tests objects """
r = random.Random() # initialized from time or system
num_factors = r.randint(1, TestFaustPy.MAX_NUM_FACTORS)
factors = []
d2 = r.randint(1, 1000)
for i in range(0, num_factors):
d1, d2 = d2, r.randint(1, 1000)
factors += [sparse.random(d1, d2, density=0.1, format='csr',
dtype=np.float64).todense()]
self.F = Faust(factors)
print("Tests on random Faust with dims=", self.F.get_nb_rows(),
self.F.get_nb_cols())
def testSave(self):
tmp_dir = tempfile.gettempdir()+os.sep
# save the Faust through Faust core API
test_file = tmp_dir+"A.mat"
self.F.save(test_file, format="Matlab_core")
# save the Faust relying on numpy API
ref_file = tmp_dir+"A_ref.mat"
mdict = {'faust_factors':
np.ndarray(shape=(1, self.F.get_nb_factors()), dtype=object)}
# self.F.display()
for i in range(0, self.F.get_nb_factors()):
mdict['faust_factors'][0, i] = self.F.get_factor(i)
savemat(ref_file, mdict)
# open the two saved files and compare the fausts
F_test = Faust(test_file)
F_ref = Faust(ref_file)
# print("self.F.get_nb_factors()=", self.F.get_nb_factors())
self.assertEqual(F_test.get_nb_factors(), F_ref.get_nb_factors())
for i in range(0, F_ref.get_nb_factors()):
fact_ref = F_ref.get_factor(i)
fact_test = F_test.get_factor(i)
self.assertEqual(fact_ref.shape, fact_test.shape)
self.assertTrue((fact_ref == fact_test).all())
if __name__ == "__main__":
if(len(sys.argv)> 1):
# argv[1] is for adding a directory in PYTHONPATH
# (to find FaustPy module)
sys.path.append(sys.argv[1])
del sys.argv[1] # deleted to avoid interfering with unittest
from FaustPy import Faust
unittest.main()
......@@ -87,8 +87,8 @@ print("Ok")
######################################
print("*** DIMENSION ***")
dim1F=F.getNbRow();
dim2F=F.getNbCol();
dim1F=F.get_nb_rows();
dim2F=F.get_nb_cols();
print("F dim1 : ",dim1F)
print("F dim2 : ",dim2F)
......
......@@ -136,7 +136,7 @@ for j in range(nb_dim):
F=FaustPy.Faust(list_factor_dense)
F_dense=F.todense()
if(F.getNbRow() != dim) or (F.getNbCol() != dim):
if(F.get_nb_rows() != dim) or (F.get_nb_cols() != dim):
raise ValueError('invalid Faust size')
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment