Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Qucsator unit tests #23

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@ libqucsUnitTest_LDADD = $(top_builddir)/src/libqucsator.la
libqucsUnitTest_CPPFLAGS = $(AM_CPPFLAGS) @GTEST_CPPFLAGS@ \
-DGTEST_HAS_PTHREAD=0
libqucsUnitTest_SOURCES = testMain.cpp \
test_complex.cpp \
test_dataset.cpp \
test_equation.cpp \
test_libqucs.cpp \
test_property.cpp \
test_strlist.cpp \
test_vector.cpp \
Fourier.cpp \
Math.cpp \
Matrix.cpp \
Expand Down
51 changes: 51 additions & 0 deletions tests/test_complex.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* test_complex.cpp - Miscellaneous unit tests for Qucs core library
*
* Author Szymon Blachuta
* Copyright (C) 2014, 2015, 2020 Qucs Team
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this package; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*/
#include <iostream>

#include "config.h"
#include "precision.h"
#include "math/complex.h"

#include "testDefine.h" // constants used on tests
#include "gtest/gtest.h" // Google Test
using namespace qucs;

TEST (complex, constructor) {
//std::cout << "can we print info" << std::endl;
nr_complex_t val(1.0);
EXPECT_EQ( 1.0, val.real());
}
TEST (complex, math) {
nr_complex_t val(1.0);
val += 2.0; EXPECT_EQ( 3.0, val.real());
val -= 1.0; EXPECT_EQ( 2.0, val.real());
val *= 4.5; EXPECT_EQ( 9.0, val.real());
val /= 2.0; EXPECT_EQ( 4.5, val.real());

EXPECT_EQ ( 0.0, qucs::acos(1.0));
EXPECT_NEAR(1.0472, qucs::acos(0.5), 0.0001);

EXPECT_EQ ( 0.0, qucs::asin(0.0));
EXPECT_NEAR(1.5708 , qucs::asin(1.0), 0.0001);
EXPECT_NEAR(0.523599, qucs::asin(0.5), 0.0001);
}
177 changes: 177 additions & 0 deletions tests/test_dataset.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
/*
* test_dataset.cpp - Miscellaneous unit tests for Qucs core library
*
* Author Szymon Blachuta
* Copyright (C) 2014, 2015, 2020 Qucs Team
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this package; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*/
#include <iostream>
#include <stdio.h>

#if HAVE_CONFIG_H
# include <config.h>
#endif
#include "qucs_typedefs.h"
#include "object.h"
#include "strlist.h"
#include "vector.h"
#include "dataset.h"
#include "math/complex.h"

#include "testDefine.h" // constants used on tests
#include "gtest/gtest.h" // Google Test
using namespace qucs;

void doTest(const char *dtaSet, nr_complex_t *cVal=NULL); //proto
TEST (dataset, load_simple) {
const char * qData =
"<Qucs Dataset 0.0.20>\n"
"<indep time 2\n>"
" +0.00000000000000000000e+00\n"
" +1.00000000000000000000e+00\n"
"</indep>\n"
"<dep Pr1.It time>\n"
" +0.12300000000000000000e+00\n"
" +1.33920000000000000000e-12\n"
"</dep>\n";
doTest(qData);
}
TEST (dataset, load_ctrlRctrlN) {
const char * qData =
"<Qucs Dataset 0.0.20>\r\n"
"<indep time 2\n>"
" +0.00000000000000000000e+00\r\n"
" +1.00000000000000000000e+00\n"
"</indep>\n"
"<dep Pr1.It time>\n"
" +0.12300000000000000000e+00\n"
" +1.33920000000000000000e-12\r\n"
"</dep>\n";
doTest(qData);
}
TEST (dataset, load_comment) {
const char * qData =
"# comment line\n"
"<Qucs Dataset 0.0.20>\n"
"<indep time 2>\n"
" \n" //empty line
" +0.00000000000000000000e+00\n"
" +1.00000000000000000000e+00\n"
"</indep>\n"
"<dep Pr1.It time>\n"
" +0.12300000000000000000e+00\n"
" +1.33920000000000000000e-12\n"
"</dep>\n";
doTest(qData);
}
TEST (dataset, load_continuation) { // \ at end of line
const char * qData =
"<Qucs Dataset 0.0.20>\n"
"<indep\ttime\t2> 0.0 1.0</indep>\n" // tabs
"<dep Pr1.It time> 0.123 +1.3392e-12\\\n</dep>\n";
doTest(qData);
}
TEST (dataset, load_complex) {
const char * qData =
"<Qucs Dataset 0.0.20>\n"
"<indep\ttime\t2> 0.0 1.0</indep>\n" // tabs
"<dep Pr1.It time> 0.123 +25e-12-j12e33\\\n</dep>\n";
nr_complex_t cv(25e-12, -12e33);
doTest(qData, &cv);
}
void doTest(const char *dtaSet, nr_complex_t * cVal){
const char *FNAME = "test_dataset_tmp.dat";
FILE *fo = fopen(FNAME, "wb");
fputs(dtaSet, fo);
fclose(fo);

dataset dta;
vector *dVect = new vector();
dta.addDependency(dVect);
vector *vect1 = new vector();
dta.addVariable(vect1);
dataset *result = dta.load(FNAME);

nr_complex_t rt;
EXPECT_EQ(1, result->countDependencies());
EXPECT_EQ(1, result->countVariables());

EXPECT_STREQ("time" , result->getDependencies()->getName());
rt = result->getDependencies()->get(0);
EXPECT_EQ(0.0 , rt.real());
rt = result->getDependencies()->get(1);
EXPECT_EQ(1.0 , rt.real());

EXPECT_STREQ("Pr1.It", result->getVariables()->getName());
rt = result->getVariables()->get(0);
EXPECT_EQ(0.123 , rt.real());
rt = result->getVariables()->get(1);
if (cVal==NULL)
EXPECT_EQ(1.3392e-12, rt.real());
else{
EXPECT_EQ(cVal->real(), rt.real());
EXPECT_EQ(cVal->imag(), rt.imag());
}
remove(FNAME);
}
// see scan_dataset.lpp
// WS [ \t\n\r]
// IDENT1 [a-zA-Z_][a-zA-Z0-9_]*
// IDENT2 [a-zA-Z_][a-zA-Z0-9_\[\],]*
// IDENT3 [a-zA-Z0-9_][a-zA-Z0-9_]*
// IDENT {IDENT1}|{IDENT2}
// PIDENT {IDENT1}|{IDENT2}|{IDENT3}
// SIMPLEID {IDENT}
// POSTID "."{PIDENT}
// ID {SIMPLEID}{POSTID}*
// DIGIT [0-9]
// EXPONENT [Ee][+-]?{DIGIT}+ # e-21
// RINT [+-]?{DIGIT}+ # -73
// IINT [+-]?[ij]{1}{DIGIT}+ # +i36
// RFLOAT1 [+-]?{DIGIT}+{EXPONENT} # +25e-12
// RFLOAT2 [+-]?{DIGIT}*"."{DIGIT}+({EXPONENT})? #
// IFLOAT1 [+-]?[ij]{1}{DIGIT}+{EXPONENT} # -j12e33
// IFLOAT2 [+-]?[ij]{1}{DIGIT}*"."{DIGIT}+({EXPONENT})? #
// CREAL ({RFLOAT1}|{RFLOAT2}|{RINT}) # +25e-12
// CIMAG ({IFLOAT1}|{IFLOAT2}|{IINT}) # -j12e12
// COMPLEX {CREAL}{CIMAG} # +25e-12-j12e33
// SPACE [ \t]
// VERSION "<Qucs Dataset "{DIGIT}+"."{DIGIT}+"."{DIGIT}+">"
// DBEGIN "dep"
// IBEGIN "indep"
// DEND "/dep"
// IEND "/indep"
//
// <INITIAL>{VERSION} { return Version; }
// <DESCRIPTION>{DBEGIN} { return DepBegin; }
// <DESCRIPTION>{IBEGIN} { return IndepBegin; }
// <DESCRIPTION>{DEND} { return DepEnd; }
// <DESCRIPTION>{IEND} { return IndepEnd; }
// <INITIAL,DESCRIPTION>{ID} { return Identifier; }
// <INITIAL>{CREAL} { return REAL; }
// <INITIAL>{CIMAG} { return IMAG; }
// <INITIAL>{COMPLEX} { return COMPLEX; }
// <DESCRIPTION>{RINT} { return Integer; }
// <INITIAL>"<" { return '<'; }
// <DESCRIPTION>">" { return '>'; }
// <INITIAL>\r?\n { return Eol; }
// <*>{SPACE}|\\\r?\n /* skip spaces and the trailing '\' */
// <INITIAL>"#" { BEGIN(COMMENT); }
// <INITIAL,DESCRIPTION>. { return InvalidCharacter; }
// <COMMENT>. { /* skip any character in here */ }
// <COMMENT>\r?\n { BEGIN(INITIAL); /* skipping ends here */ }
109 changes: 109 additions & 0 deletions tests/test_equation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* test_equation.cpp - Miscellaneous unit tests for Qucs core library
*
* Author Szymon Blachuta
* Copyright (C) 2014, 2015, 2020 Qucs Team
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this package; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*/
#include <iostream>

#if HAVE_CONFIG_H
# include <config.h>
#endif
#include "math/complex.h"
#include "property.h"
#include "qucs_typedefs.h"
#include "object.h"
#include "vector.h"
#include "variable.h"
#include "equation.h"

#include "testDefine.h" // constants used on tests
#include "gtest/gtest.h" // Google Test
using namespace qucs;
using namespace eqn;

//---------------------node--------------------
TEST (equation, node) {
//std::cout << "can we print info" << std::endl;
node nde;
nde.setInstance("abc");
EXPECT_STREQ(nde.getInstance(), "abc");
}

//---------------------constant--------------------
TEST (equation, constant) {
constant cnst(TAG_STRING);
cnst.s = strdup("abc");
cnst.dataref = true; //do free const.s
EXPECT_STREQ(cnst.toString(), "'abc'");
}
//---------------------reference--------------------
TEST (equation, reference){
reference ref;
ref.ref = new node(CONSTANT);
ref.n = strdup("efg");
EXPECT_STREQ(ref.toString(), "efg");
}
//---------------------assignment--------------------
TEST (equation, assignment){
assignment assg;
assg.body = new node();
assg.body->txt = strdup("bdy");
assg.body->setType(CONSTANT);
assg.result = strdup("hij");
EXPECT_STREQ(assg.toString(), "hij = bdy");
}
//---------------------application--------------------
TEST (equation, application){
application app;
constant *n1 = new constant(TAG_DOUBLE); n1->d = 1;
constant *n2 = new constant(TAG_DOUBLE); n2->d = 2;
n1->append(n2);
app.n = strdup("+"); //"+";
app.args = n1;
app.nargs = 2;
EXPECT_STREQ(app.toString(), "(1+2)");
}
//---------------------checker--------------------
TEST (equation, checker){
constant *cnst = new constant(TAG_DOUBLE);
cnst->d = 1.1;

assignment *assg = new assignment();
assg->body = cnst;
assg->result = strdup("rslt");

checker chkr;
chkr.addEquation(assg);
EXPECT_EQ( 0, chkr.check()); // 0 errors
}
//---------------------solver--------------------
TEST (equation, solver){
constant *cnst = new constant(TAG_DOUBLE);
cnst->d = 1.1;

assignment *assg = new assignment();
assg->body = cnst;
assg->result = strdup("rslt");

checker chkr;
chkr.addEquation(assg);
solver slvr(&chkr);
slvr.evaluate();
}
Loading