Imported Upstream version 3.0
[debian/gnuradio] / gnuradio-core / src / lib / general / qa_gr_fxpt.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2004 Free Software Foundation, Inc.
4  * 
5  * This file is part of GNU Radio
6  * 
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  * 
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with GNU Radio; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26 #include <qa_gr_fxpt.h>
27 #include <gr_fxpt.h>
28 #include <cppunit/TestAssert.h>
29 #include <iostream>
30 #include <stdio.h>
31 #include <unistd.h>
32 #include <math.h>
33
34 static const float SIN_COS_TOLERANCE = 1e-5;
35
36 void
37 qa_gr_fxpt::t0 ()
38 {
39   CPPUNIT_ASSERT_DOUBLES_EQUAL (M_PI/2, gr_fxpt::fixed_to_float (0x40000000), SIN_COS_TOLERANCE);
40   CPPUNIT_ASSERT_DOUBLES_EQUAL (0.0,    gr_fxpt::fixed_to_float (0x00000000), SIN_COS_TOLERANCE);
41   CPPUNIT_ASSERT_DOUBLES_EQUAL (-M_PI,  gr_fxpt::fixed_to_float (0x80000000), SIN_COS_TOLERANCE);
42
43   if (0){
44     /*
45      * These are disabled because of some precision issues.
46      *
47      * Different compilers seem to have different opinions on whether
48      * the calulations are done single or double (or extended)
49      * precision.  Any of the answers are fine for our real purpose, but
50      * sometimes the answer is off by a few bits at the bottom.
51      * Hence, the disabled check.
52      */
53     CPPUNIT_ASSERT_EQUAL ((gr_int32) 0x40000000, gr_fxpt::float_to_fixed (M_PI/2));
54     CPPUNIT_ASSERT_EQUAL ((gr_int32) 0,          gr_fxpt::float_to_fixed (0));
55     CPPUNIT_ASSERT_EQUAL ((gr_int32) 0x80000000, gr_fxpt::float_to_fixed (-M_PI));
56   }
57 }
58
59 void
60 qa_gr_fxpt::t1 ()
61 {
62
63   CPPUNIT_ASSERT_DOUBLES_EQUAL ( 0,           gr_fxpt::sin (0x00000000), SIN_COS_TOLERANCE);
64   CPPUNIT_ASSERT_DOUBLES_EQUAL ( 0.707106781, gr_fxpt::sin (0x20000000), SIN_COS_TOLERANCE);
65   CPPUNIT_ASSERT_DOUBLES_EQUAL ( 1,           gr_fxpt::sin (0x40000000), SIN_COS_TOLERANCE);
66   CPPUNIT_ASSERT_DOUBLES_EQUAL ( 0.707106781, gr_fxpt::sin (0x60000000), SIN_COS_TOLERANCE);
67   CPPUNIT_ASSERT_DOUBLES_EQUAL ( 0,           gr_fxpt::sin (0x7fffffff), SIN_COS_TOLERANCE);
68   CPPUNIT_ASSERT_DOUBLES_EQUAL ( 0,           gr_fxpt::sin (0x80000000), SIN_COS_TOLERANCE);
69   CPPUNIT_ASSERT_DOUBLES_EQUAL ( 0,           gr_fxpt::sin (0x80000001), SIN_COS_TOLERANCE);
70   CPPUNIT_ASSERT_DOUBLES_EQUAL (-1,           gr_fxpt::sin (-0x40000000), SIN_COS_TOLERANCE);
71   CPPUNIT_ASSERT_DOUBLES_EQUAL (-0.707106781, gr_fxpt::sin (-0x20000000), SIN_COS_TOLERANCE);
72
73
74   for (float p = -M_PI; p < M_PI; p += 2 * M_PI / 3600){
75     float expected = sin (p);
76     float actual = gr_fxpt::sin (gr_fxpt::float_to_fixed (p));
77     CPPUNIT_ASSERT_DOUBLES_EQUAL (expected, actual, SIN_COS_TOLERANCE);
78   }
79 }
80
81 void
82 qa_gr_fxpt::t2 ()
83 {
84   for (float p = -M_PI; p < M_PI; p += 2 * M_PI / 3600){
85     float expected = cos (p);
86     float actual = gr_fxpt::cos (gr_fxpt::float_to_fixed (p));
87     CPPUNIT_ASSERT_DOUBLES_EQUAL (expected, actual, SIN_COS_TOLERANCE);
88   }
89 }
90
91 void
92 qa_gr_fxpt::t3 ()
93 {
94 }