def testInput(self): try: x = "asdfasdf" y = DFT(x) raise TypeError('You dont check the type of input') except ValueError: pass
def test_type(self): N=5 a = np.zeros(N, complex) for n in range(N): a[n] = (-1 + 2 * random.random()) + (-1 + 2 * random.random())*1j y = DFT(a) self.assertIsInstance(y, np.ndarray)
def test_shape(self): for N in [5,8,10]: a = np.zeros(N, complex) for n in range(N): a[n] = (-1 + 2 * random.random()) + (-1 + 2 * random.random())*1j y = DFT(a) temp=np.array(y) self.assertEqual(temp.shape, (N,))
def test_dft_return(self): ''' Make sure that the value returned from DFT() is a numpy.ndarray, of length N, and of shape (N,) Test this for a list and a bytearray; tuples and numpy.array tests might be added in the future. Together these constitute the list of valid inputs. ''' x = [1,2,3,4,0,5,0] X = DFT(x) #make sure you give me an ndarray of the proper shape and length self.assertIsInstance(X, ndarray, msg='You did not return a numpy.ndarray') self.assertEqual(X.shape,(len(x),), msg = 'You must return a one-dimensional array of the same length as the input') y = bytearray([1,2,3,4,0,5,0]) Y = DFT(y) #make sure you give me an ndarray of the proper shape and length self.assertIsInstance(Y, ndarray, msg='You did not return a numpy.ndarray') self.assertEqual(Y.shape,(len(y),), msg = 'You must return a one-dimensional array of the same length as the input')
def test_std(self): for N in range(2, 21): a = np.zeros(N, complex) for i in range(0, 10): for n in range(0, N): a[n] = (-1 + 2 * random.random()) + (-1 + 2 * random.random())*1j y=DFT(a) sub = y - np.fft.fft(a) for k in sub: err = abs(k) self.assertTrue(err < 0.0001*abs(y[1]))
def test_imputtype(self): a="2+3j,3+5j,2" self.assertRaises(ValueError,DFT,a) a=['h',2+3j] self.assertRaises(ValueError,DFT,a) a={1:2j,2:2j} self.assertRaises(ValueError,DFT,a) try: DFT([2+3j]) except: self.fail()
def testResult(self): for i in range(2, 21): N = i a = [0] * N for kk in range(10): for k in range(N): a[k] = (2 * random.random() - 1) * (2 * random.random() - 1) * 1j a = np.array(a) b = DFT(a) c = np.fft.fft(a) if numpy.allclose(b, c): pass else: raise ValueError
def test_random_x(self): ''' Automatically generate a number of test cases for DFT() and compare results to numpy.fft.ftt This will generate and test input lists of length 2 to max_N a total of 10 times each ''' max_N=20 #for x of length 2 to max_N (inclusive) for N in range(2,max_N+1): #generate and test x ten times for t in range(0,10): #randomly generate x x = [] for i in range(0,N): x.append((random()-0.5)*2+(random()-0.5)*2j) #test DFT by comparing to fft.fft out to 6 decimal places testing.assert_array_almost_equal(DFT(x),fft.fft(x), err_msg='Your results do not agree with numpy.fft.fft',verbose=True)
def test_dft_error_hnd(self): ''' For invalid inputs to DFT, make sure a ValueError is generated Also make sure we catch case where no error occurs in DFT() for an invalid input This test includes invalid test cases of type list, tuple, and ndarray ''' #self.assertRaises(ValueError, DFT(None)) #create some invalid input test cases r = True s = None t = 1 u = "fail" v = {1:4,6:7} w = ["1","2","3"] x = ("1", 2, 3) y = array(["1","2","3"]) z = array([[1,2],[3,4]]) w_msg = 'You need to throw an error for lists containing a string' x_msg = 'You need to throw an error for tuples containing a string' y_msg = 'You need to throw an error for ndarrays containing a string' z_msg = 'You need to throw an error for multidimensional arrays' v_msg = 'You need to throw an error for dictionaries' u_msg = 'You need to throw an error for a string' t_msg = 'You need to throw an error for an integer' s_msg = 'You need to throw an error for None' r_msg = 'You need to throw an error for a boolean' #examine errors that are thrown for test_case,err_msg in [[w,w_msg],[x,x_msg],[y,y_msg],[z,z_msg],[v,v_msg],[u,u_msg],[t,t_msg],[s,s_msg],[r,r_msg]]: error_occurred = False try: DFT(test_case) except Exception as e: error_occurred = True #did you give me a ValueError? If not, your program is wrong. self.assertEqual(type(e), ValueError, msg='You need to throw a ValueError for invalid input') finally: #if your program didn't throw an error AT ALL for invalid input, it's wrong. self.assertEqual(error_occurred, True, msg=err_msg)
def testTypeAndShape(self): x = np.array([1, 1, 1]) y = DFT(x) self.assertEqual(type(x), type(y)) N = len(y) self.assertEqual(3, N)