From fe40294b7c3abb8d5c6272b51226c9631ddbaf6a Mon Sep 17 00:00:00 2001 From: oxmon-2500 Date: Thu, 25 Feb 2021 10:17:01 +0100 Subject: [PATCH 1/3] unit tests added: test_complex.cpp, test_dataset.cpp, ... --- tests/test_complex.cpp | 51 ++++++++++++ tests/test_dataset.cpp | 177 ++++++++++++++++++++++++++++++++++++++++ tests/test_equation.cpp | 109 +++++++++++++++++++++++++ tests/test_property.cpp | 134 ++++++++++++++++++++++++++++++ tests/test_strlist.cpp | 148 +++++++++++++++++++++++++++++++++ tests/test_vector.cpp | 92 +++++++++++++++++++++ 6 files changed, 711 insertions(+) create mode 100644 tests/test_complex.cpp create mode 100644 tests/test_dataset.cpp create mode 100644 tests/test_equation.cpp create mode 100644 tests/test_property.cpp create mode 100644 tests/test_strlist.cpp create mode 100644 tests/test_vector.cpp diff --git a/tests/test_complex.cpp b/tests/test_complex.cpp new file mode 100644 index 00000000..14ef9474 --- /dev/null +++ b/tests/test_complex.cpp @@ -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 + +#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); +} diff --git a/tests/test_dataset.cpp b/tests/test_dataset.cpp new file mode 100644 index 00000000..847cfeab --- /dev/null +++ b/tests/test_dataset.cpp @@ -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 +#include + +#if HAVE_CONFIG_H +# include +#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 = + "\n" + "" + " +0.00000000000000000000e+00\n" + " +1.00000000000000000000e+00\n" + "\n" + "\n" + " +0.12300000000000000000e+00\n" + " +1.33920000000000000000e-12\n" + "\n"; + doTest(qData); +} +TEST (dataset, load_ctrlRctrlN) { + const char * qData = + "\r\n" + "" + " +0.00000000000000000000e+00\r\n" + " +1.00000000000000000000e+00\n" + "\n" + "\n" + " +0.12300000000000000000e+00\n" + " +1.33920000000000000000e-12\r\n" + "\n"; + doTest(qData); +} +TEST (dataset, load_comment) { + const char * qData = + "# comment line\n" + "\n" + "\n" + " \n" //empty line + " +0.00000000000000000000e+00\n" + " +1.00000000000000000000e+00\n" + "\n" + "\n" + " +0.12300000000000000000e+00\n" + " +1.33920000000000000000e-12\n" + "\n"; + doTest(qData); +} +TEST (dataset, load_continuation) { // \ at end of line + const char * qData = + "\n" + " 0.0 1.0\n" // tabs + " 0.123 +1.3392e-12\\\n\n"; + doTest(qData); +} +TEST (dataset, load_complex) { + const char * qData = + "\n" + " 0.0 1.0\n" // tabs + " 0.123 +25e-12-j12e33\\\n\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 "" +// DBEGIN "dep" +// IBEGIN "indep" +// DEND "/dep" +// IEND "/indep" +// +// {VERSION} { return Version; } +// {DBEGIN} { return DepBegin; } +// {IBEGIN} { return IndepBegin; } +// {DEND} { return DepEnd; } +// {IEND} { return IndepEnd; } +// {ID} { return Identifier; } +// {CREAL} { return REAL; } +// {CIMAG} { return IMAG; } +// {COMPLEX} { return COMPLEX; } +// {RINT} { return Integer; } +// "<" { return '<'; } +// ">" { return '>'; } +// \r?\n { return Eol; } +// <*>{SPACE}|\\\r?\n /* skip spaces and the trailing '\' */ +// "#" { BEGIN(COMMENT); } +// . { return InvalidCharacter; } +// . { /* skip any character in here */ } +// \r?\n { BEGIN(INITIAL); /* skipping ends here */ } diff --git a/tests/test_equation.cpp b/tests/test_equation.cpp new file mode 100644 index 00000000..3fdb85b0 --- /dev/null +++ b/tests/test_equation.cpp @@ -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 + +#if HAVE_CONFIG_H +# include +#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(); +} diff --git a/tests/test_property.cpp b/tests/test_property.cpp new file mode 100644 index 00000000..3b943508 --- /dev/null +++ b/tests/test_property.cpp @@ -0,0 +1,134 @@ +/* + * test_property.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 + +#if HAVE_CONFIG_H +# include +#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; + +TEST (property, constructor) { + //std::cout << "can we print info" << std::endl; + property p; + EXPECT_EQ( 0, strlen(p.getString())); + EXPECT_EQ( 0, strlen(p.getReference())); + EXPECT_EQ( 0, p.getInteger()); + EXPECT_EQ( 0, p.getDouble()); + EXPECT_EQ(NULL, p.getVector()); +} +TEST (property, strng) { + property p; p.set("abc"); + EXPECT_STREQ("abc", p.getString()); + + constant *cnst = new constant(TAG_STRING); cnst->s = strdup("hij"); + variable *var = new variable("var_01"); var->setConstant(cnst); + p.set(var); + EXPECT_STREQ("hij", p.getString()); +} +TEST (property, dbl) { + property p; p.set(1.23); + EXPECT_DOUBLE_EQ(1.23, p.getDouble()); + + constant *cnst = new constant(TAG_DOUBLE); cnst->d = 123.45; + variable *var = new variable("var_01"); var->setConstant(cnst); + p.set(var); + EXPECT_EQ(123.45, p.getDouble()); +} + +TEST (property, intgr) { + property p0; p0.set(123); + EXPECT_EQ(123, p0.getInteger()); + + property p1; p1.set(123.45); + EXPECT_EQ(123, p1.getInteger()); //floor + + constant *cnst = new constant(TAG_DOUBLE); cnst->d = 89.0123; + variable *var = new variable("var_01"); var->setConstant(cnst); + p0.set(var); + EXPECT_EQ(89, p0.getInteger()); //floor +} + +TEST (property, ref) { + property p; p.set("abc"); + EXPECT_STREQ("abc", p.getReference()); + p.set(new variable("efgh")); + EXPECT_STREQ("efgh", p.getReference()); +} + +TEST (property, toString) { + variable *var = new variable("var_01"); + EXPECT_TRUE(var->getType()==VAR_UNKNOWN); + + constant *cnst = new constant(TAG_CHAR); + cnst->chr = 'a'; + var->setConstant(cnst); + EXPECT_TRUE(var->getType()==VAR_CONSTANT); + EXPECT_STREQ(var->getName(), "var_01"); + EXPECT_EQ(cnst, var->getConstant()); + + property prop_ref; + prop_ref.set(var); + EXPECT_STREQ("var_01", prop_ref.toString().c_str()); + EXPECT_STREQ("var_01", prop_ref.getReference()); + //EXPECT_TRUE(prop->getVector()!=NULL); TODO getVector is buggy but never used + + property prop_int; + prop_int.set(123); + EXPECT_EQ (123, prop_int.getInteger()); + EXPECT_STREQ("123.000000", prop_int.toString().c_str()); + + property prop_dbl; + prop_dbl.set(123.45); + EXPECT_EQ (123.45 , prop_dbl.getDouble()); + EXPECT_STREQ("123.450000", prop_dbl.toString().c_str()); + + property prop_str; + prop_str.set("abc"); + EXPECT_STREQ("abc", prop_str.getString()); + EXPECT_STREQ("abc", prop_str.toString().c_str()); +} + +TEST (properties, setGet) { + property p_str; p_str.set("abc"); + property p_int; p_int.set(123); + property p_dbl; p_dbl.set(123.45); + + properties prps; + prps.insert( {"ky01", p_str} ); + prps.insert( {"ky02", p_int} ); + prps["ky03"] = p_dbl; + EXPECT_STREQ( "abc", prps["ky01"].getString() ); + EXPECT_EQ ( 123, prps["ky02"].getInteger() ); + EXPECT_EQ (123.45, prps["ky03"].getDouble() ); +} diff --git a/tests/test_strlist.cpp b/tests/test_strlist.cpp new file mode 100644 index 00000000..7306a69a --- /dev/null +++ b/tests/test_strlist.cpp @@ -0,0 +1,148 @@ +/* + * test_strlist.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 + +#include "strlist.h" + +#include "testDefine.h" // constants used on tests +#include "gtest/gtest.h" // Google Test +using namespace qucs; + +TEST (strlist, constructor) { + //std::cout << "can we print info" << std::endl; + strlist sl; + EXPECT_EQ( 0, sl.length()); +} + +TEST (strlist, add) { + strlist sl; + EXPECT_STREQ("", sl.toString()); + sl.add("abc"); + EXPECT_EQ( 1, sl.length()); + EXPECT_STREQ("abc", sl.toString()); + + sl.add("def"); + EXPECT_EQ( 2, sl.length()); + EXPECT_STREQ("def abc", sl.toString()); +} + +TEST (strlist, contains) { + strlist sl; + sl.add("abc"); + EXPECT_EQ( 1, sl.length()); + EXPECT_STREQ("abc", sl.toString()); + + sl.add("def"); + EXPECT_EQ( 2, sl.length()); + EXPECT_STREQ("def abc", sl.toString()); + + EXPECT_EQ( 1, sl.contains("def")); + EXPECT_EQ( 1, sl.contains("abc")); + EXPECT_EQ( 0, sl.contains("x")); +} + +TEST (strlist, index) { + strlist sl; + // strdup because of Warning: deprecated conversion from string constant to ‘char*’ + EXPECT_EQ( -1, sl.index(strdup("def"))); + + sl.add("abc"); + EXPECT_EQ( 1, sl.length()); + EXPECT_STREQ("abc", sl.toString()); + + sl.add("def"); + EXPECT_EQ( 2, sl.length()); + EXPECT_STREQ("def abc", sl.toString()); + + EXPECT_EQ( 0, sl.index(strdup("def"))); + EXPECT_EQ( 1, sl.index(strdup("abc"))); + EXPECT_EQ( -1, sl.index(strdup("x"))); +} + +TEST (strlist, firstLast) { + strlist sl; + sl.add("abc"); + EXPECT_EQ( 1, sl.length()); + EXPECT_STREQ("abc", sl.toString()); + + sl.add("def"); + EXPECT_EQ( 2, sl.length()); + EXPECT_STREQ("def abc", sl.toString()); + + EXPECT_STREQ("def", sl.first()); + EXPECT_STREQ("abc", sl.last()); +} + +TEST (strlist, del) { + strlist sl1, sl2; + sl1.append("abc"); + sl1.append("def"); + sl1.append("ghi"); + + sl2.add("def"); + sl1.del(&sl2); + EXPECT_EQ( 2, sl1.length()); + EXPECT_STREQ("abc ghi", sl1.toString()); +} + +TEST (strlist, append) { + strlist sl1, sl2; + sl1.add("abc"); + sl1.add("def"); + sl1.append("ghi"); + EXPECT_STREQ("def abc ghi", sl1.toString()); + + sl2.add("jkl"); + sl1.append(&sl2); + EXPECT_STREQ("def abc ghi jkl", sl1.toString()); +} + +TEST (strlist, join) { + strlist sl1, sl2, sl3; + sl1.add("abc"); + sl1.append("def"); + + sl2.add("jkl"); + + strlist *ret = sl3.join(&sl1, &sl2); + EXPECT_STREQ("abc def" , sl1.toString()); + EXPECT_STREQ("jkl" , sl2.toString()); + EXPECT_STREQ("abc def jkl", ret->toString()); +} +TEST (strlistiterator, test) { + strlist sl; + sl.append("abc"); + sl.append("def"); + sl.append("ghi"); + strlistiterator it (sl); + EXPECT_EQ(3, it.count()); + EXPECT_STREQ("abc", *it); ++it; + EXPECT_STREQ("def", *it); ++it; + EXPECT_STREQ("ghi", *it); ++it; + EXPECT_EQ(NULL, *it); + EXPECT_STREQ("abc", it.toFirst()); ++it; + EXPECT_STREQ("def", it.current()); + EXPECT_STREQ("ghi", it.toLast()); + EXPECT_STREQ("abc", it.first()); + EXPECT_STREQ("ghi", it.last()); +} diff --git a/tests/test_vector.cpp b/tests/test_vector.cpp new file mode 100644 index 00000000..19d3da42 --- /dev/null +++ b/tests/test_vector.cpp @@ -0,0 +1,92 @@ +/* + * test_vector.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 + +#include "config.h" +#include "precision.h" +#include "qucs_typedefs.h" +#include "object.h" +#include "vector.h" +#include "math/complex.h" + +//#include "testDefine.h" // constants used on tests +#include "gtest/gtest.h" // Google Test +using namespace qucs; + +TEST (vector, constructor) { + //std::cout << "can we print info" << std::endl; + vector vect; + EXPECT_EQ( 0, vect.getSize()); + vector vt2(2); + EXPECT_EQ( 0, vect.getSize()); +} + +TEST (vector, add) { + nr_complex_t val(1.0); + vector vect(1); + vect.add(val); + EXPECT_EQ( 2, vect.getSize()); +} + +TEST (vector, get) { + nr_complex_t val0(0.0); + nr_complex_t val1(1.0); + vector vect(1); + vect.add(val1); + EXPECT_EQ( 2, vect.getSize()); + EXPECT_EQ(vect.get(0).real(), val0.real()); + EXPECT_EQ(vect.get(1).real(), val1.real()); +} + +TEST (vector, math) { + nr_complex_t val(1.0); + vector vect; + vect.add(val); + vect += 2.0; EXPECT_EQ( 3.0, vect.get(0).real()); + vect -= 1.0; EXPECT_EQ( 2.0, vect.get(0).real()); + vect /= 2.0; EXPECT_EQ( 1.0, vect.get(0).real()); + vect *= 3.8; EXPECT_EQ( 3.8, vect.get(0).real()); + vect.variance(); +} + +TEST (vector, variance) { + vector vect; + nr_complex_t val_1(1.0); + nr_complex_t val_2(2.0); + nr_complex_t val_3(3.0); + vect.add(val_1); + EXPECT_EQ( 0, vect.variance()); + vect.add(val_2); + vect.add(val_3); + EXPECT_EQ( 1.0, vect.variance()); +} + +TEST (vector, property) { + vector vect; + vect.addProperty("key_a", "A"); + vect.addProperty("key_b", "B"); + EXPECT_TRUE( vect.hasProperty("key_a")); + EXPECT_STREQ("B" , vect.getPropertyString("key_b")); + EXPECT_STREQ("key_b=\"B\"key_a=\"A\"", vect.propertyList()); +} +// TODO more test for better coverage From 6b8225ab72f33ef2a4f5b626b51bf9ee05be9ccd Mon Sep 17 00:00:00 2001 From: oxmon-2500 Date: Thu, 25 Feb 2021 10:20:55 +0100 Subject: [PATCH 2/3] new tests added --- tests/Makefile.am | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/Makefile.am b/tests/Makefile.am index 85e6ad7c..1199973d 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -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 \ From ef05ee787e6e97cbe5387a7c1e25ab4b6f198bd0 Mon Sep 17 00:00:00 2001 From: oxmon-2500 Date: Thu, 25 Feb 2021 10:17:01 +0100 Subject: [PATCH 3/3] unit tests added: test_complex.cpp, test_dataset.cpp, ... new tests added modified: tests/Makefile.am new file: tests/test_complex.cpp new file: tests/test_dataset.cpp new file: tests/test_equation.cpp new file: tests/test_property.cpp new file: tests/test_strlist.cpp new file: tests/test_vector.cpp --- tests/Makefile.am | 6 ++ tests/test_complex.cpp | 51 ++++++++++++ tests/test_dataset.cpp | 177 ++++++++++++++++++++++++++++++++++++++++ tests/test_equation.cpp | 109 +++++++++++++++++++++++++ tests/test_property.cpp | 134 ++++++++++++++++++++++++++++++ tests/test_strlist.cpp | 148 +++++++++++++++++++++++++++++++++ tests/test_vector.cpp | 92 +++++++++++++++++++++ 7 files changed, 717 insertions(+) create mode 100644 tests/test_complex.cpp create mode 100644 tests/test_dataset.cpp create mode 100644 tests/test_equation.cpp create mode 100644 tests/test_property.cpp create mode 100644 tests/test_strlist.cpp create mode 100644 tests/test_vector.cpp diff --git a/tests/Makefile.am b/tests/Makefile.am index 85e6ad7c..1199973d 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -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 \ diff --git a/tests/test_complex.cpp b/tests/test_complex.cpp new file mode 100644 index 00000000..14ef9474 --- /dev/null +++ b/tests/test_complex.cpp @@ -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 + +#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); +} diff --git a/tests/test_dataset.cpp b/tests/test_dataset.cpp new file mode 100644 index 00000000..847cfeab --- /dev/null +++ b/tests/test_dataset.cpp @@ -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 +#include + +#if HAVE_CONFIG_H +# include +#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 = + "\n" + "" + " +0.00000000000000000000e+00\n" + " +1.00000000000000000000e+00\n" + "\n" + "\n" + " +0.12300000000000000000e+00\n" + " +1.33920000000000000000e-12\n" + "\n"; + doTest(qData); +} +TEST (dataset, load_ctrlRctrlN) { + const char * qData = + "\r\n" + "" + " +0.00000000000000000000e+00\r\n" + " +1.00000000000000000000e+00\n" + "\n" + "\n" + " +0.12300000000000000000e+00\n" + " +1.33920000000000000000e-12\r\n" + "\n"; + doTest(qData); +} +TEST (dataset, load_comment) { + const char * qData = + "# comment line\n" + "\n" + "\n" + " \n" //empty line + " +0.00000000000000000000e+00\n" + " +1.00000000000000000000e+00\n" + "\n" + "\n" + " +0.12300000000000000000e+00\n" + " +1.33920000000000000000e-12\n" + "\n"; + doTest(qData); +} +TEST (dataset, load_continuation) { // \ at end of line + const char * qData = + "\n" + " 0.0 1.0\n" // tabs + " 0.123 +1.3392e-12\\\n\n"; + doTest(qData); +} +TEST (dataset, load_complex) { + const char * qData = + "\n" + " 0.0 1.0\n" // tabs + " 0.123 +25e-12-j12e33\\\n\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 "" +// DBEGIN "dep" +// IBEGIN "indep" +// DEND "/dep" +// IEND "/indep" +// +// {VERSION} { return Version; } +// {DBEGIN} { return DepBegin; } +// {IBEGIN} { return IndepBegin; } +// {DEND} { return DepEnd; } +// {IEND} { return IndepEnd; } +// {ID} { return Identifier; } +// {CREAL} { return REAL; } +// {CIMAG} { return IMAG; } +// {COMPLEX} { return COMPLEX; } +// {RINT} { return Integer; } +// "<" { return '<'; } +// ">" { return '>'; } +// \r?\n { return Eol; } +// <*>{SPACE}|\\\r?\n /* skip spaces and the trailing '\' */ +// "#" { BEGIN(COMMENT); } +// . { return InvalidCharacter; } +// . { /* skip any character in here */ } +// \r?\n { BEGIN(INITIAL); /* skipping ends here */ } diff --git a/tests/test_equation.cpp b/tests/test_equation.cpp new file mode 100644 index 00000000..3fdb85b0 --- /dev/null +++ b/tests/test_equation.cpp @@ -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 + +#if HAVE_CONFIG_H +# include +#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(); +} diff --git a/tests/test_property.cpp b/tests/test_property.cpp new file mode 100644 index 00000000..3b943508 --- /dev/null +++ b/tests/test_property.cpp @@ -0,0 +1,134 @@ +/* + * test_property.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 + +#if HAVE_CONFIG_H +# include +#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; + +TEST (property, constructor) { + //std::cout << "can we print info" << std::endl; + property p; + EXPECT_EQ( 0, strlen(p.getString())); + EXPECT_EQ( 0, strlen(p.getReference())); + EXPECT_EQ( 0, p.getInteger()); + EXPECT_EQ( 0, p.getDouble()); + EXPECT_EQ(NULL, p.getVector()); +} +TEST (property, strng) { + property p; p.set("abc"); + EXPECT_STREQ("abc", p.getString()); + + constant *cnst = new constant(TAG_STRING); cnst->s = strdup("hij"); + variable *var = new variable("var_01"); var->setConstant(cnst); + p.set(var); + EXPECT_STREQ("hij", p.getString()); +} +TEST (property, dbl) { + property p; p.set(1.23); + EXPECT_DOUBLE_EQ(1.23, p.getDouble()); + + constant *cnst = new constant(TAG_DOUBLE); cnst->d = 123.45; + variable *var = new variable("var_01"); var->setConstant(cnst); + p.set(var); + EXPECT_EQ(123.45, p.getDouble()); +} + +TEST (property, intgr) { + property p0; p0.set(123); + EXPECT_EQ(123, p0.getInteger()); + + property p1; p1.set(123.45); + EXPECT_EQ(123, p1.getInteger()); //floor + + constant *cnst = new constant(TAG_DOUBLE); cnst->d = 89.0123; + variable *var = new variable("var_01"); var->setConstant(cnst); + p0.set(var); + EXPECT_EQ(89, p0.getInteger()); //floor +} + +TEST (property, ref) { + property p; p.set("abc"); + EXPECT_STREQ("abc", p.getReference()); + p.set(new variable("efgh")); + EXPECT_STREQ("efgh", p.getReference()); +} + +TEST (property, toString) { + variable *var = new variable("var_01"); + EXPECT_TRUE(var->getType()==VAR_UNKNOWN); + + constant *cnst = new constant(TAG_CHAR); + cnst->chr = 'a'; + var->setConstant(cnst); + EXPECT_TRUE(var->getType()==VAR_CONSTANT); + EXPECT_STREQ(var->getName(), "var_01"); + EXPECT_EQ(cnst, var->getConstant()); + + property prop_ref; + prop_ref.set(var); + EXPECT_STREQ("var_01", prop_ref.toString().c_str()); + EXPECT_STREQ("var_01", prop_ref.getReference()); + //EXPECT_TRUE(prop->getVector()!=NULL); TODO getVector is buggy but never used + + property prop_int; + prop_int.set(123); + EXPECT_EQ (123, prop_int.getInteger()); + EXPECT_STREQ("123.000000", prop_int.toString().c_str()); + + property prop_dbl; + prop_dbl.set(123.45); + EXPECT_EQ (123.45 , prop_dbl.getDouble()); + EXPECT_STREQ("123.450000", prop_dbl.toString().c_str()); + + property prop_str; + prop_str.set("abc"); + EXPECT_STREQ("abc", prop_str.getString()); + EXPECT_STREQ("abc", prop_str.toString().c_str()); +} + +TEST (properties, setGet) { + property p_str; p_str.set("abc"); + property p_int; p_int.set(123); + property p_dbl; p_dbl.set(123.45); + + properties prps; + prps.insert( {"ky01", p_str} ); + prps.insert( {"ky02", p_int} ); + prps["ky03"] = p_dbl; + EXPECT_STREQ( "abc", prps["ky01"].getString() ); + EXPECT_EQ ( 123, prps["ky02"].getInteger() ); + EXPECT_EQ (123.45, prps["ky03"].getDouble() ); +} diff --git a/tests/test_strlist.cpp b/tests/test_strlist.cpp new file mode 100644 index 00000000..7306a69a --- /dev/null +++ b/tests/test_strlist.cpp @@ -0,0 +1,148 @@ +/* + * test_strlist.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 + +#include "strlist.h" + +#include "testDefine.h" // constants used on tests +#include "gtest/gtest.h" // Google Test +using namespace qucs; + +TEST (strlist, constructor) { + //std::cout << "can we print info" << std::endl; + strlist sl; + EXPECT_EQ( 0, sl.length()); +} + +TEST (strlist, add) { + strlist sl; + EXPECT_STREQ("", sl.toString()); + sl.add("abc"); + EXPECT_EQ( 1, sl.length()); + EXPECT_STREQ("abc", sl.toString()); + + sl.add("def"); + EXPECT_EQ( 2, sl.length()); + EXPECT_STREQ("def abc", sl.toString()); +} + +TEST (strlist, contains) { + strlist sl; + sl.add("abc"); + EXPECT_EQ( 1, sl.length()); + EXPECT_STREQ("abc", sl.toString()); + + sl.add("def"); + EXPECT_EQ( 2, sl.length()); + EXPECT_STREQ("def abc", sl.toString()); + + EXPECT_EQ( 1, sl.contains("def")); + EXPECT_EQ( 1, sl.contains("abc")); + EXPECT_EQ( 0, sl.contains("x")); +} + +TEST (strlist, index) { + strlist sl; + // strdup because of Warning: deprecated conversion from string constant to ‘char*’ + EXPECT_EQ( -1, sl.index(strdup("def"))); + + sl.add("abc"); + EXPECT_EQ( 1, sl.length()); + EXPECT_STREQ("abc", sl.toString()); + + sl.add("def"); + EXPECT_EQ( 2, sl.length()); + EXPECT_STREQ("def abc", sl.toString()); + + EXPECT_EQ( 0, sl.index(strdup("def"))); + EXPECT_EQ( 1, sl.index(strdup("abc"))); + EXPECT_EQ( -1, sl.index(strdup("x"))); +} + +TEST (strlist, firstLast) { + strlist sl; + sl.add("abc"); + EXPECT_EQ( 1, sl.length()); + EXPECT_STREQ("abc", sl.toString()); + + sl.add("def"); + EXPECT_EQ( 2, sl.length()); + EXPECT_STREQ("def abc", sl.toString()); + + EXPECT_STREQ("def", sl.first()); + EXPECT_STREQ("abc", sl.last()); +} + +TEST (strlist, del) { + strlist sl1, sl2; + sl1.append("abc"); + sl1.append("def"); + sl1.append("ghi"); + + sl2.add("def"); + sl1.del(&sl2); + EXPECT_EQ( 2, sl1.length()); + EXPECT_STREQ("abc ghi", sl1.toString()); +} + +TEST (strlist, append) { + strlist sl1, sl2; + sl1.add("abc"); + sl1.add("def"); + sl1.append("ghi"); + EXPECT_STREQ("def abc ghi", sl1.toString()); + + sl2.add("jkl"); + sl1.append(&sl2); + EXPECT_STREQ("def abc ghi jkl", sl1.toString()); +} + +TEST (strlist, join) { + strlist sl1, sl2, sl3; + sl1.add("abc"); + sl1.append("def"); + + sl2.add("jkl"); + + strlist *ret = sl3.join(&sl1, &sl2); + EXPECT_STREQ("abc def" , sl1.toString()); + EXPECT_STREQ("jkl" , sl2.toString()); + EXPECT_STREQ("abc def jkl", ret->toString()); +} +TEST (strlistiterator, test) { + strlist sl; + sl.append("abc"); + sl.append("def"); + sl.append("ghi"); + strlistiterator it (sl); + EXPECT_EQ(3, it.count()); + EXPECT_STREQ("abc", *it); ++it; + EXPECT_STREQ("def", *it); ++it; + EXPECT_STREQ("ghi", *it); ++it; + EXPECT_EQ(NULL, *it); + EXPECT_STREQ("abc", it.toFirst()); ++it; + EXPECT_STREQ("def", it.current()); + EXPECT_STREQ("ghi", it.toLast()); + EXPECT_STREQ("abc", it.first()); + EXPECT_STREQ("ghi", it.last()); +} diff --git a/tests/test_vector.cpp b/tests/test_vector.cpp new file mode 100644 index 00000000..19d3da42 --- /dev/null +++ b/tests/test_vector.cpp @@ -0,0 +1,92 @@ +/* + * test_vector.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 + +#include "config.h" +#include "precision.h" +#include "qucs_typedefs.h" +#include "object.h" +#include "vector.h" +#include "math/complex.h" + +//#include "testDefine.h" // constants used on tests +#include "gtest/gtest.h" // Google Test +using namespace qucs; + +TEST (vector, constructor) { + //std::cout << "can we print info" << std::endl; + vector vect; + EXPECT_EQ( 0, vect.getSize()); + vector vt2(2); + EXPECT_EQ( 0, vect.getSize()); +} + +TEST (vector, add) { + nr_complex_t val(1.0); + vector vect(1); + vect.add(val); + EXPECT_EQ( 2, vect.getSize()); +} + +TEST (vector, get) { + nr_complex_t val0(0.0); + nr_complex_t val1(1.0); + vector vect(1); + vect.add(val1); + EXPECT_EQ( 2, vect.getSize()); + EXPECT_EQ(vect.get(0).real(), val0.real()); + EXPECT_EQ(vect.get(1).real(), val1.real()); +} + +TEST (vector, math) { + nr_complex_t val(1.0); + vector vect; + vect.add(val); + vect += 2.0; EXPECT_EQ( 3.0, vect.get(0).real()); + vect -= 1.0; EXPECT_EQ( 2.0, vect.get(0).real()); + vect /= 2.0; EXPECT_EQ( 1.0, vect.get(0).real()); + vect *= 3.8; EXPECT_EQ( 3.8, vect.get(0).real()); + vect.variance(); +} + +TEST (vector, variance) { + vector vect; + nr_complex_t val_1(1.0); + nr_complex_t val_2(2.0); + nr_complex_t val_3(3.0); + vect.add(val_1); + EXPECT_EQ( 0, vect.variance()); + vect.add(val_2); + vect.add(val_3); + EXPECT_EQ( 1.0, vect.variance()); +} + +TEST (vector, property) { + vector vect; + vect.addProperty("key_a", "A"); + vect.addProperty("key_b", "B"); + EXPECT_TRUE( vect.hasProperty("key_a")); + EXPECT_STREQ("B" , vect.getPropertyString("key_b")); + EXPECT_STREQ("key_b=\"B\"key_a=\"A\"", vect.propertyList()); +} +// TODO more test for better coverage