comparison tests/TestFFT.cpp @ 170:8c86761a5533

Fix overrun in reading inverse complex-to-real FFT input (contrary to docs)
author Chris Cannam
date Fri, 09 May 2014 14:35:46 +0100
parents 8d2d04f2fb51
children 2de6184b2ce0
comparison
equal deleted inserted replaced
169:783fb5f0e626 170:8c86761a5533
23 23
24 //!!! need at least one test with complex time-domain signal 24 //!!! need at least one test with complex time-domain signal
25 25
26 BOOST_AUTO_TEST_CASE(forwardArrayBounds) 26 BOOST_AUTO_TEST_CASE(forwardArrayBounds)
27 { 27 {
28 // initialise bins to something recognisable, so we can tell 28 // initialise bins to something recognisable, so we can tell if
29 // if they haven't been written 29 // they haven't been written; and allocate the inputs on the heap
30 double in[] = { 1, 1, -1, -1 }; 30 // so that, if running under valgrind, we get warnings about
31 // overruns
32 double *in = new double[4];
33 in[0] = 1;
34 in[1] = 1;
35 in[2] = -1;
36 in[3] = -1;
31 double re[] = { 999, 999, 999, 999, 999, 999 }; 37 double re[] = { 999, 999, 999, 999, 999, 999 };
32 double im[] = { 999, 999, 999, 999, 999, 999 }; 38 double im[] = { 999, 999, 999, 999, 999, 999 };
33 FFT(4).process(false, in, 0, re+1, im+1); 39 FFT(4).process(false, in, 0, re+1, im+1);
34 // And check we haven't overrun the arrays 40 // And check we haven't overrun the arrays
35 BOOST_CHECK_EQUAL(re[0], 999.0); 41 BOOST_CHECK_EQUAL(re[0], 999.0);
36 BOOST_CHECK_EQUAL(im[0], 999.0); 42 BOOST_CHECK_EQUAL(im[0], 999.0);
37 BOOST_CHECK_EQUAL(re[5], 999.0); 43 BOOST_CHECK_EQUAL(re[5], 999.0);
38 BOOST_CHECK_EQUAL(im[5], 999.0); 44 BOOST_CHECK_EQUAL(im[5], 999.0);
45 delete[] in;
39 } 46 }
40 47
41 BOOST_AUTO_TEST_CASE(r_forwardArrayBounds) 48 BOOST_AUTO_TEST_CASE(r_forwardArrayBounds)
42 { 49 {
43 // initialise bins to something recognisable, so we can tell 50 // initialise bins to something recognisable, so we can tell if
44 // if they haven't been written 51 // they haven't been written; and allocate the inputs on the heap
45 double in[] = { 1, 1, -1, -1 }; 52 // so that, if running under valgrind, we get warnings about
53 // overruns
54 double *in = new double[4];
55 in[0] = 1;
56 in[1] = 1;
57 in[2] = -1;
58 in[3] = -1;
46 double re[] = { 999, 999, 999, 999, 999, 999 }; 59 double re[] = { 999, 999, 999, 999, 999, 999 };
47 double im[] = { 999, 999, 999, 999, 999, 999 }; 60 double im[] = { 999, 999, 999, 999, 999, 999 };
48 FFTReal(4).forward(in, re+1, im+1); 61 FFTReal(4).forward(in, re+1, im+1);
49 // And check we haven't overrun the arrays 62 // And check we haven't overrun the arrays
50 BOOST_CHECK_EQUAL(re[0], 999.0); 63 BOOST_CHECK_EQUAL(re[0], 999.0);
51 BOOST_CHECK_EQUAL(im[0], 999.0); 64 BOOST_CHECK_EQUAL(im[0], 999.0);
52 BOOST_CHECK_EQUAL(re[5], 999.0); 65 BOOST_CHECK_EQUAL(re[5], 999.0);
53 BOOST_CHECK_EQUAL(im[5], 999.0); 66 BOOST_CHECK_EQUAL(im[5], 999.0);
67 delete[] in;
54 } 68 }
55 69
56 BOOST_AUTO_TEST_CASE(inverseArrayBounds) 70 BOOST_AUTO_TEST_CASE(inverseArrayBounds)
57 { 71 {
58 // initialise bins to something recognisable, so we can tell 72 // initialise bins to something recognisable, so we can tell if
59 // if they haven't been written 73 // they haven't been written; and allocate the inputs on the heap
60 double re[] = { 0, 1, 0, 1 }; 74 // so that, if running under valgrind, we get warnings about
61 double im[] = { 0, -2, 0, 2 }; 75 // overruns
76 double *re = new double[4];
77 double *im = new double[4];
78 re[0] = 0;
79 re[1] = 1;
80 re[2] = 0;
81 re[3] = 1;
82 im[0] = 0;
83 im[1] = -2;
84 im[2] = 0;
85 im[3] = 2;
62 double outre[] = { 999, 999, 999, 999, 999, 999 }; 86 double outre[] = { 999, 999, 999, 999, 999, 999 };
63 double outim[] = { 999, 999, 999, 999, 999, 999 }; 87 double outim[] = { 999, 999, 999, 999, 999, 999 };
64 FFT(4).process(true, re, im, outre+1, outim+1); 88 FFT(4).process(true, re, im, outre+1, outim+1);
65 // And check we haven't overrun the arrays 89 // And check we haven't overrun the arrays
66 BOOST_CHECK_EQUAL(outre[0], 999.0); 90 BOOST_CHECK_EQUAL(outre[0], 999.0);
67 BOOST_CHECK_EQUAL(outim[0], 999.0); 91 BOOST_CHECK_EQUAL(outim[0], 999.0);
68 BOOST_CHECK_EQUAL(outre[5], 999.0); 92 BOOST_CHECK_EQUAL(outre[5], 999.0);
69 BOOST_CHECK_EQUAL(outim[5], 999.0); 93 BOOST_CHECK_EQUAL(outim[5], 999.0);
94 delete[] re;
95 delete[] im;
70 } 96 }
71 97
72 BOOST_AUTO_TEST_CASE(r_inverseArrayBounds) 98 BOOST_AUTO_TEST_CASE(r_inverseArrayBounds)
73 { 99 {
74 // initialise bins to something recognisable, so we can tell 100 // initialise bins to something recognisable, so we can tell if
75 // if they haven't been written 101 // they haven't been written; and allocate the inputs on the heap
76 double re[] = { 0, 1, 0 }; 102 // so that, if running under valgrind, we get warnings about
77 double im[] = { 0, -2, 0 }; 103 // overruns
104 double *re = new double[3];
105 double *im = new double[3];
106 re[0] = 0;
107 re[1] = 1;
108 re[2] = 0;
109 im[0] = 0;
110 im[1] = -2;
111 im[2] = 0;
78 double outre[] = { 999, 999, 999, 999, 999, 999 }; 112 double outre[] = { 999, 999, 999, 999, 999, 999 };
79 FFTReal(4).inverse(re, im, outre+1); 113 FFTReal(4).inverse(re, im, outre+1);
80 // And check we haven't overrun the arrays 114 // And check we haven't overrun the arrays
81 BOOST_CHECK_EQUAL(outre[0], 999.0); 115 BOOST_CHECK_EQUAL(outre[0], 999.0);
82 BOOST_CHECK_EQUAL(outre[5], 999.0); 116 BOOST_CHECK_EQUAL(outre[5], 999.0);
117 delete[] re;
118 delete[] im;
83 } 119 }
84 120
85 BOOST_AUTO_TEST_CASE(dc) 121 BOOST_AUTO_TEST_CASE(dc)
86 { 122 {
87 // DC-only signal. The DC bin is purely real 123 // DC-only signal. The DC bin is purely real