summaryrefslogtreecommitdiffstatshomepage
path: root/src/lib/netlist/tests/test_pfunction.cpp
diff options
context:
space:
mode:
author couriersud <couriersud@gmx.org>2020-07-05 11:40:22 +0200
committer couriersud <couriersud@gmx.org>2020-07-05 11:48:07 +0200
commit9e86f5e86612c4f61b27e5ada86ac9bdebcbc272 (patch)
treed40c51579d7575c0e4547dd17321e89450d39236 /src/lib/netlist/tests/test_pfunction.cpp
parentad6505ba636ecf8336eae91dce0b20bd5055b4ad (diff)
netlist: Add basic unit testing support.
* Add google test syntax compatible unit testing support. This is a very limited subset of the google test framework and not intended ever to be a replacement. Adding a dependency to google test for the functionality required was considered to be an overkill. * nltool -c tests runs unit tests if linked in. This is *not* the case for the version of nltool compiled with TOOLS=1. * Added unit tests for plib::pfunction.
Diffstat (limited to 'src/lib/netlist/tests/test_pfunction.cpp')
-rw-r--r--src/lib/netlist/tests/test_pfunction.cpp43
1 files changed, 43 insertions, 0 deletions
diff --git a/src/lib/netlist/tests/test_pfunction.cpp b/src/lib/netlist/tests/test_pfunction.cpp
new file mode 100644
index 00000000000..847865b4a4f
--- /dev/null
+++ b/src/lib/netlist/tests/test_pfunction.cpp
@@ -0,0 +1,43 @@
+// license:GPL-2.0+
+// copyright-holders:Couriersud
+
+///
+/// \file pfunction_test.cpp
+///
+/// tests for pfunction
+///
+
+#include "plib/ptests.h"
+
+#include "plib/pfunction.h"
+
+#define PFUNCEXPECT(formula, val) \
+ EXPECT_EQ(val, plib::pfunction<double>(formula)());
+
+TEST(pfunction, operators)
+{
+ PFUNCEXPECT("1==1", 1.0)
+ PFUNCEXPECT("1 *0 == 2-1-1", 1.0)
+ PFUNCEXPECT("0!=1", 1.0)
+ PFUNCEXPECT("0<1", 1.0)
+ PFUNCEXPECT("1>0", 1.0)
+ PFUNCEXPECT("0<=1", 1.0)
+ PFUNCEXPECT("1>=0", 1.0)
+ PFUNCEXPECT("1<=1", 1.0)
+ PFUNCEXPECT("1>=1", 1.0)
+ EXPECT_EQ(1.0, plib::pfunction<double>("0!=a", {"a"})({1.0}));
+}
+
+TEST(pfunction, func_if)
+{
+ PFUNCEXPECT("if(1>0, 2, 0)", 2.0)
+ PFUNCEXPECT("if(0>1, 2, 3)", 3.0)
+ PFUNCEXPECT("if(sin(1)>0, 2, 3)", 3.0) // fail
+ EXPECT_EQ( 1.0, plib::pfunction<double>("if(A2>2.5, 0-A1, (0.07-(0.005*A1))*if(A0>2.5,1,0-1))", {"A0","A1","A2"})({1.0,-1.0,3.0}));
+ EXPECT_EQ(-0.065, plib::pfunction<double>("if(A2>2.5, 0-A1, (0.07-(0.005*A1))*if(A0>2.5,1,0-1))", {"A0","A1","A2"})({1.0,1.0,1.0}));
+ EXPECT_EQ( 0.065, plib::pfunction<double>("if(A2>2.5, 0-A1, (0.07-(0.005*A1))*if(A0>2.5,1,0-1))", {"A0","A1","A2"})({3.0,1.0,1.0}));
+ //EXPECT(plib::pfunction<double>("if(A2>2.5, A1, if(A0>2.5,1,(0-1)))", {"A0","A1","A2"})({1.0,1.0,1.0}), -1.0)
+ //PFUNCEXPECT("-1>-2", 1.0)
+ EXPECT_TRUE(1.0 == plib::pfunction<double>("0!=a", {"a"})({1.0}));
+}
+