Qucs-core  0.0.19
test_libqucs.cpp
Go to the documentation of this file.
00001 /*
00002  * test_libqucs.cpp - Miscellaneous unit tests for Qucs core library
00003  *
00004  * Copyright (C) 2014, 2015 Guilherme Brondani Torri <guitorri@gmail.com>
00005  *
00006  * This is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2, or (at your option)
00009  * any later version.
00010  *
00011  * This software is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this package; see the file COPYING.  If not, write to
00018  * the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
00019  * Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #include <iostream>
00024 
00025 #include "qucs_typedefs.h"
00026 #include "object.h"
00027 #include "module.h"
00028 #include "components.h"
00029 
00030 #include "testDefine.h"   // constants used on tests
00031 #include "gtest/gtest.h"  // Google Test
00032 
00033 TEST (module, constructor) {
00034   //std::cout << "can we print info"  << std::endl;
00035   module *m = new module ();
00036   EXPECT_EQ(NULL, m->definition);
00037   EXPECT_EQ(NULL, m->circreate);
00038   EXPECT_EQ(NULL, m->anacreate);
00039 }
00040 
00041 TEST (component, resistor_getType) {
00042   resistor *res = new resistor();
00043   //res->initDC();
00044   std::cout << CIR_RESISTOR << " -- "<< res->getType() << std::endl;
00045   EXPECT_EQ( CIR_RESISTOR, res->getType());
00046 }
00047 
00048 
00049 // --------------------
00050 
00051 #include "tridiag.h"
00052 #include "tvector.h"
00053 
00054 TEST (tridiag, solve_s_cyc) {
00055 /* # test program in Python
00056   import numpy as np
00057   A = np.array(
00058         [[-2,  1,  0,  0,  2],
00059          [ 1, -2,  1,  0,  0],
00060          [ 0,  1, -2,  1,  0],
00061          [ 0,  0,  1, -2,  1],
00062          [ 2,  0,  0,  1, -2]])
00063   b = np.array([1, 2, 3, 4, 5])
00064   x = np.linalg.solve(A,b)
00065   print x
00066   [ 8.21428571  3.85714286  1.5  2.14285714  6.78571429]
00067 */
00068 
00069   int n = 5;
00070   std::vector<nr_double_t> x (n);
00071   x[0] = 8.21428571;
00072   x[1] = 3.85714286;
00073   x[2] = 1.5;
00074   x[3] = 2.14285714;
00075   x[4] = 6.78571429;
00076 
00077   tridiag<nr_double_t> sys;
00078   std::vector<nr_double_t> o (n);
00079   std::vector<nr_double_t> d (n);
00080   std::vector<nr_double_t> b (n);
00081 
00082   for (int i = 0; i < n; i++) {
00083     d[i] = -2.;
00084     o[i] =  1.;
00085     b[i] = i+1.;
00086   }
00087   o[n-1] = 2.;
00088 
00089   /*
00090   for (int i = 0; i < n; i++) {
00091     std::cout << d(i);
00092   }
00093   std::cout << '\n';
00094   for (int i = 0; i < n; i++) {
00095     std::cout << o(i);
00096   }
00097   std::cout << '\n';
00098   for (int i = 0; i < n; i++) {
00099     std::cout << b(i);
00100   }
00101   std::cout << '\n';
00102   */
00103   sys.setDiagonal (&d);
00104   sys.setOffDiagonal (&o);
00105   sys.setRHS (&b);
00106   sys.setType (TRIDIAG_SYM_CYCLIC);
00107   sys.solve ();
00108   // in-place solver, result in b
00109 
00110   //for (int i = 0; i < n; i++) {
00111   //  std::cout << b(i) << ", ";
00112   //}
00113   //
00114   for (int i = 0; i < n; i++) {
00115     EXPECT_NEAR (x[i], b[i],tol);
00116   }
00117 }