Qucs-core  0.0.18
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
test_libqucs.cpp
Go to the documentation of this file.
1 /*
2  * test_libqucs.cpp - unit tests for Qucs core library
3  *
4  * Copyright (C) 2014 Guilherme Brondani Torri <guitorri@gmail.com>
5  *
6  * This is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * This software is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this package; see the file COPYING. If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 
24 /*
25 
26 Not always needed to use qucs::
27 */
28 
29 #include <iostream>
30 
31 #include "qucs_typedefs.h"
32 #include "real.h"
33 #include "complex.h"
34 
35 #include "matrix.h"
36 #include "object.h"
37 #include "vector.h"
38 
39 #include "fourier.h"
40 
41 #include "module.h"
42 
43 #include "components.h"
44 
45 #include "gtest/gtest.h" // Google Test
46 
47 // tolerance on numeric comparison
48 double tol = 0.0001;
49 
51  ASSERT_EQ (18.0, qucs::sqrt (324.0));
52  ASSERT_EQ (0.0, qucs::sqrt (0.0));
53  //ASSERT_EQ (-1, qucs::sqrt(-22.0)); //error NaN (depending on implementation)
54 }
55 
56 TEST(real, jn_BesselFistKind) {
57  // FIXME not in qucs::
58  EXPECT_NEAR ( 0.44, jn (1, 1), 0.0001);
59 }
60 
61 TEST(real, Factorial) {
62  ASSERT_EQ ( 479001600, qucs::factorial (12) );
63 }
64 
65 TEST (complex, cos) {
66  nr_complex_t z = nr_complex_t (1.0, 1.0);
67  EXPECT_NEAR ( 0.83370025, qucs::cos (z).real(), tol);
68  EXPECT_NEAR (-0.98889770, qucs::cos (z).imag(), tol);
69 }
70 
71 TEST (complex, jn_BesselFistKind) {
72  nr_complex_t z = nr_complex_t (1.0, 1.0);
73  EXPECT_NEAR ( 0.61416033492290361, qucs::jn (1, z).real(), tol);
74  EXPECT_NEAR ( 0.36502802882708778, qucs::jn (1, z).imag(), tol);
75 }
76 
77 
78 TEST (matrix, getCols) {
80  EXPECT_EQ ( 3 , data.getCols() );
81 }
82 
83 TEST (vector, sum) {
84  qucs::vector vec = qucs::vector(3);
85  EXPECT_EQ ( 3 , vec.getSize() );
86  for (int k = 0; k < vec.getSize(); k++)
87  vec.set(1, k);
88  EXPECT_EQ ( 3.0 , qucs::sum(vec) );
89 }
90 
91 TEST (fourier, fft) {
92  // fft of a DC vector
93  // in [1, 1, 1, 1, 1, 1, 1, 1]
94  // out [8, 0, 0, 0, 0, 0, 0, 0]
95  qucs::vector vec = qucs::vector(8);
96 
97  for (int k = 0; k < vec.getSize(); k++)
98  vec.set(1, k);
99 
100  qucs::vector vdif = qucs::fourier::fft_1d ( vec ) ;
101 
102  for (int k = 0; k < vec.getSize(); k++)
103  if (k==0)
104  EXPECT_EQ ( 8 , vdif.get(k).real() );
105  else
106  EXPECT_EQ ( 0 , vdif.get(k).real() );
107 }
108 
109 TEST (module, constructor) {
110  //std::cout << "can we print info" << std::endl;
111  module *m = new module ();
112  EXPECT_EQ(NULL, m->definition);
113  EXPECT_EQ(NULL, m->circreate);
114  EXPECT_EQ(NULL, m->anacreate);
115 }
116 
117 TEST (component, resistor_getType) {
118  resistor *res = new resistor();
119  //res->initDC();
120  std::cout << CIR_RESISTOR << " -- "<< res->getType() << std::endl;
121  EXPECT_EQ( CIR_RESISTOR, res->getType());
122 }
123 
std::complex< nr_double_t > nr_complex_t
Definition: complex.h:31
matrix real(matrix a)
Real part matrix.
Definition: matrix.cpp:568
int getSize(void) const
Definition: vector.cpp:192
qucs::vector fft_1d(qucs::vector, int isign=1)
nr_complex_t cos(const nr_complex_t z)
Compute complex cosine.
Definition: complex.cpp:57
nr_complex_t sum(vector)
Definition: vector.cpp:251
matrix imag(matrix a)
Imaginary part matrix.
Definition: matrix.cpp:581
nr_complex_t sqrt(const nr_complex_t z)
Compute principal value of square root.
Definition: complex.cpp:271
double tol
int getType(void)
Definition: circuit.h:137
TEST(real, sqrt)
void set(nr_double_t, int)
Definition: vector.cpp:183
Dense matrix class header file.
Dense complex matrix class This class defines a matrix object with its methods, operators and operati...
Definition: matrix.h:92
int getCols(void)
Definition: matrix.h:103
nr_complex_t jn(const int n, const nr_complex_t z)
Bessel function of first kind.
Definition: complex.cpp:729
matrix eye(int rs, int cs)
Create identity matrix with specified number of rows and columns.
Definition: matrix.cpp:603
unsigned int factorial(unsigned int n)
Compute factorial n ie $n!$.
Definition: real.cpp:444
nr_complex_t get(int)
Definition: vector.cpp:179
data
Definition: parse_citi.y:117