|
| 1 | +package com.thealgorithms.maths; |
| 2 | +import org.junit.jupiter.api.Test; |
| 3 | +import java.util.ArrayList; |
| 4 | +import static org.junit.jupiter.api.Assertions.*; |
| 5 | + |
| 6 | +class FFTTest { |
| 7 | + |
| 8 | + // Testing the simple function getReal |
| 9 | + @Test |
| 10 | + void getRealtest() |
| 11 | + { |
| 12 | + FFT.Complex complex = new FFT.Complex(1.0,1.0); |
| 13 | + assertEquals(1.0,complex.getReal()); |
| 14 | + } |
| 15 | + |
| 16 | + // Testing the simple function getImaginary |
| 17 | + @Test |
| 18 | + void getImaginaryTest() |
| 19 | + { |
| 20 | + FFT.Complex complex = new FFT.Complex(); |
| 21 | + assertEquals(0.0,complex.getImaginary()); |
| 22 | + } |
| 23 | + |
| 24 | + // Testing the function add, assertEqual test |
| 25 | + @Test |
| 26 | + void addTest() |
| 27 | + { |
| 28 | + FFT.Complex complex1 = new FFT.Complex(1.0,1.0); |
| 29 | + FFT.Complex complex2 = new FFT.Complex(2.0,2.0); |
| 30 | + double add = complex1.add(complex2).getReal(); |
| 31 | + assertEquals(3.0,add); |
| 32 | + } |
| 33 | + |
| 34 | + // Testing the function add, assertNotEqual test |
| 35 | + @Test |
| 36 | + void addFalseTest() |
| 37 | + { |
| 38 | + FFT.Complex complex1 = new FFT.Complex(1.0,1.0); |
| 39 | + FFT.Complex complex2 = new FFT.Complex(2.0,2.0); |
| 40 | + double add = complex1.add(complex2).getReal(); |
| 41 | + assertNotEquals(2.0,add); |
| 42 | + } |
| 43 | + |
| 44 | + // Testing the function substract, assertEqual test |
| 45 | + @Test |
| 46 | + void subtractTest() |
| 47 | + { |
| 48 | + FFT.Complex complex1 = new FFT.Complex(2.0,2.0); |
| 49 | + FFT.Complex complex2 = new FFT.Complex(1.0,1.0); |
| 50 | + double sub = complex1.subtract(complex2).getReal(); |
| 51 | + assertEquals(1.0,sub); |
| 52 | + } |
| 53 | + |
| 54 | + // Testing the function multiply complex, assertEqual test |
| 55 | + @Test |
| 56 | + void multiplyWithComplexTest() |
| 57 | + { |
| 58 | + FFT.Complex complex1 = new FFT.Complex(2.0,2.0); |
| 59 | + FFT.Complex complex2 = new FFT.Complex(1.0,1.0); |
| 60 | + double multiReal = complex1.multiply(complex2).getReal(); |
| 61 | + double multiImg = complex1.multiply(complex2).getImaginary(); |
| 62 | + assertEquals(0.0,multiReal); |
| 63 | + assertEquals(4.0,multiImg); |
| 64 | + } |
| 65 | + |
| 66 | + // Testing the function multiply scalar, assertEqual test |
| 67 | + @Test |
| 68 | + void multiplyWithScalarTest() |
| 69 | + { |
| 70 | + FFT.Complex complex1 = new FFT.Complex(2.0,2.0); |
| 71 | + |
| 72 | + double multiReal = complex1.multiply(2).getReal(); |
| 73 | + double multiImg = complex1.multiply(3).getImaginary(); |
| 74 | + assertEquals(4.0,multiReal); |
| 75 | + assertEquals(6.0,multiImg); |
| 76 | + } |
| 77 | + |
| 78 | + // Testing the function conjugate, assertEqual test |
| 79 | + @Test |
| 80 | + void conjugateTest() |
| 81 | + { |
| 82 | + FFT.Complex complex1 = new FFT.Complex(2.0,2.0); |
| 83 | + double conReal = complex1.conjugate().getReal(); |
| 84 | + double conImg = complex1.conjugate().getImaginary(); |
| 85 | + assertEquals(2.0,conReal); |
| 86 | + assertEquals(-2.0,conImg); |
| 87 | + } |
| 88 | + |
| 89 | + // Testing the function abs, assertEqual test |
| 90 | + @Test |
| 91 | + void abs() |
| 92 | + { |
| 93 | + FFT.Complex complex1 = new FFT.Complex(2.0,3.0); |
| 94 | + double abs = complex1.abs(); |
| 95 | + assertEquals(Math.sqrt(13),abs); |
| 96 | + } |
| 97 | + |
| 98 | + // Testing the function divide complex, assertEqual test. |
| 99 | + @Test |
| 100 | + void divideWithComplexTest() |
| 101 | + { |
| 102 | + FFT.Complex complex1 = new FFT.Complex(2.0,2.0); |
| 103 | + FFT.Complex complex2 = new FFT.Complex(1.0,2.0); |
| 104 | + double divReal = complex1.divide(complex2).getReal(); |
| 105 | + double divImg = complex1.divide(complex2).getImaginary(); |
| 106 | + assertEquals(1.2,divReal); |
| 107 | + assertEquals(-0.4,divImg); |
| 108 | + } |
| 109 | + |
| 110 | + // Testing the function divide scalar, assertEqual test. |
| 111 | + @Test |
| 112 | + void divideWithScalarTest() |
| 113 | + { |
| 114 | + FFT.Complex complex1 = new FFT.Complex(2.0,2.0); |
| 115 | + double divReal = complex1.divide(2).getReal(); |
| 116 | + double divImg = complex1.divide(2).getImaginary(); |
| 117 | + assertEquals(1,divReal); |
| 118 | + assertEquals(1,divImg); |
| 119 | + } |
| 120 | + |
| 121 | + // Testing the function fft, assertEqual test. |
| 122 | + // https://scistatcalc.blogspot.com/2013/12/fft-calculator.html used this link to |
| 123 | + // ensure the result |
| 124 | + @Test |
| 125 | + void fft() |
| 126 | + { |
| 127 | + ArrayList<FFT.Complex> arr = new ArrayList<FFT.Complex>(); |
| 128 | + FFT.Complex complex1 = new FFT.Complex(2.0,2.0); |
| 129 | + FFT.Complex complex2 = new FFT.Complex(1.0,3.0); |
| 130 | + FFT.Complex complex3 = new FFT.Complex(3.0,1.0); |
| 131 | + FFT.Complex complex4 = new FFT.Complex(2.0,2.0); |
| 132 | + |
| 133 | + arr.add(complex1); |
| 134 | + arr.add(complex2); |
| 135 | + arr.add(complex3); |
| 136 | + arr.add(complex4); |
| 137 | + arr = FFT.fft(arr,false); |
| 138 | + double realV1= arr.get(0).getReal(); |
| 139 | + double realV2= arr.get(2).getReal(); |
| 140 | + double imgV1 = arr.get(0).getImaginary(); |
| 141 | + double imgV2 = arr.get(2).getImaginary(); |
| 142 | + assertEquals(8.0,realV1); |
| 143 | + assertEquals(2.0,realV2); |
| 144 | + assertEquals(8.0, imgV1); |
| 145 | + assertEquals(-2.0,imgV2); |
| 146 | + } |
| 147 | +} |
0 commit comments