From 9e86f5e86612c4f61b27e5ada86ac9bdebcbc272 Mon Sep 17 00:00:00 2001 From: couriersud Date: Sun, 5 Jul 2020 11:40:22 +0200 Subject: 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. --- src/lib/netlist/tests/test_pfunction.cpp | 43 ++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/lib/netlist/tests/test_pfunction.cpp (limited to 'src/lib/netlist/tests/test_pfunction.cpp') 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(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("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("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("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("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("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("0!=a", {"a"})({1.0})); +} + -- cgit v1.2.3-70-g09d2