|
26 | 26 | diffie_hellman_key_exchange, krishnamurthy_number,
|
27 | 27 | num_perfect_squares,
|
28 | 28 | chinese_remainder_theorem,
|
| 29 | + fft |
29 | 30 | )
|
30 | 31 |
|
31 | 32 | import unittest
|
@@ -556,5 +557,41 @@ def test_empty_lists(self):
|
556 | 557 | chinese_remainder_theorem.solve_chinese_remainder(num, rem)
|
557 | 558 |
|
558 | 559 |
|
| 560 | +class TestFFT(unittest.TestCase): |
| 561 | + """[summary] |
| 562 | + Test for the file fft.py |
| 563 | +
|
| 564 | + Arguments: |
| 565 | + unittest {[type]} -- [description] |
| 566 | + """ |
| 567 | + def test_real_numbers(self): |
| 568 | + x = [1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0] |
| 569 | + y = [4.000, 2.613, 0.000, 1.082, 0.000, 1.082, 0.000, 2.613] |
| 570 | + # abs(complex) returns the magnitude |
| 571 | + result = [float("%.3f" % abs(f)) for f in fft.fft(x)] |
| 572 | + self.assertEqual(result, y) |
| 573 | + |
| 574 | + def test_all_zero(self): |
| 575 | + x = [0.0, 0.0, 0.0, 0.0] |
| 576 | + y = [0.0, 0.0, 0.0, 0.0] |
| 577 | + result = [float("%.1f" % abs(f)) for f in fft.fft(x)] |
| 578 | + self.assertEqual(result, y) |
| 579 | + |
| 580 | + def test_all_ones(self): |
| 581 | + x = [1.0, 1.0, 1.0, 1.0] |
| 582 | + y = [4.0, 0.0, 0.0, 0.0] |
| 583 | + result = [float("%.1f" % abs(f)) for f in fft.fft(x)] |
| 584 | + self.assertEqual(result, y) |
| 585 | + |
| 586 | + def test_complex_numbers(self): |
| 587 | + x = [2.0+2j, 1.0+3j, 3.0+1j, 2.0+2j] |
| 588 | + real = [8.0, 0.0, 2.0, -2.0] |
| 589 | + imag = [8.0, 2.0, -2.0, 0.0] |
| 590 | + realResult = [float("%.1f" % f.real) for f in fft.fft(x)] |
| 591 | + imagResult = [float("%.1f" % f.imag) for f in fft.fft(x)] |
| 592 | + self.assertEqual(real, realResult) |
| 593 | + self.assertEqual(imag, imagResult) |
| 594 | + |
| 595 | + |
559 | 596 | if __name__ == "__main__":
|
560 | 597 | unittest.main()
|
0 commit comments