From 3886ef930e3fd54894b8798ab17ef4d3bc0b995a Mon Sep 17 00:00:00 2001 From: Maxim Mamontov Date: Mon, 8 Nov 2010 12:35:28 +0200 Subject: [PATCH 1/1] =?utf8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D1=8F?= =?utf8?q?=D0=B5=D0=BC=20=D0=BA=D0=B0=D1=82=D0=B0=D0=BB=D0=BE=D0=B3=20?= =?utf8?q?=D1=81=20=D1=82=D0=B5=D1=81=D1=82=D0=B0=D0=BC=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- tests/Makefile | 28 +++ tests/main.cpp | 22 +++ tests/raw_ip_packet_old.h | 146 ++++++++++++++++ tests/test_admin_conf.cpp | 222 ++++++++++++++++++++++++ tests/test_raw_ip.cpp | 110 ++++++++++++ tests/test_tariff.cpp | 347 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 875 insertions(+) create mode 100644 tests/Makefile create mode 100644 tests/main.cpp create mode 100644 tests/raw_ip_packet_old.h create mode 100644 tests/test_admin_conf.cpp create mode 100644 tests/test_raw_ip.cpp create mode 100644 tests/test_tariff.cpp diff --git a/tests/Makefile b/tests/Makefile new file mode 100644 index 00000000..a924540f --- /dev/null +++ b/tests/Makefile @@ -0,0 +1,28 @@ +CXXFLAGS+=-g3 -Wall -W -pedantic -DLINUX -I../include -I../projects/stargazer +LIBS=-lpthread +PROG=tests + +SOURCES=main.cpp \ + test_raw_ip.cpp \ + test_admin_conf.cpp \ + test_tariff.cpp \ + ../projects/stargazer/tariff.cpp + +all: $(PROG) + +$(PROG): $(subst .cpp,.o,$(SOURCES)) + $(CXX) $(LDFLAGS) $^ $(LIBS) -o $@ + +clean: + rm -f *.o *.d + +ifneq ($(MAKECMDGOALS),distclean) +ifneq ($(MAKECMDGOALS),clean) +-include $(subst .cpp,.d,$(SOURCES)) +endif +endif + +%.d: %.cpp + @$(CC) -MM $(CXXFLAGS) $< > $@.$$$$; \ + sed 's,\($*\).o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \ + rm -f $@.$$$$ diff --git a/tests/main.cpp b/tests/main.cpp new file mode 100644 index 00000000..f561cb16 --- /dev/null +++ b/tests/main.cpp @@ -0,0 +1,22 @@ +#include +#include +#include + +using std::exception; +using std::cerr; +using std::endl; + +namespace tut +{ + test_runner_singleton runner; +} + +int main() +{ + tut::reporter reporter; + tut::runner.get().set_callback(&reporter); + + tut::runner.get().run_tests(); + + return !reporter.all_ok(); +} diff --git a/tests/raw_ip_packet_old.h b/tests/raw_ip_packet_old.h new file mode 100644 index 00000000..60af8ace --- /dev/null +++ b/tests/raw_ip_packet_old.h @@ -0,0 +1,146 @@ +#ifndef RAW_IP_PACKET_OLD_H +#define RAW_IP_PACKET_OLD_H + +#include // for htons +//#include // for struct ip + +#include + +#include "stg_const.h" + +#define IPv4 (2) + +enum { pcktSizeOLD = 68 }; //60(max) ip + 8 udp or tcp (part of tcp or udp header to ports) +//----------------------------------------------------------------------------- +struct RAW_PACKET_OLD +{ + RAW_PACKET_OLD() + : dataLen(-1) + { + memset(pckt, 0, pcktSizeOLD); + } + + RAW_PACKET_OLD(const RAW_PACKET_OLD & rp) + : dataLen(rp.dataLen) + { + memcpy(pckt, rp.pckt, pcktSizeOLD); + } + +uint16_t GetIPVersion() const; +uint8_t GetHeaderLen() const; +uint8_t GetProto() const; +uint32_t GetLen() const; +uint32_t GetSrcIP() const; +uint32_t GetDstIP() const; +uint16_t GetSrcPort() const; +uint16_t GetDstPort() const; + +uint8_t pckt[pcktSizeOLD]; // îÁÞÁÌÏ ÐÁËÅÔÁ ÚÁÈ×ÁÞÅÎÎÏÇÏ ÉÚ ÓÅÔÉ +int32_t dataLen; // äÌÉÎÁ IP ÐÁËÅÔÁ. åÓÌÉ -1, ÔÏ ÉÓÐÏÌØÚÏ×ÁÔØ ÄÌÉÎÕ ÉÚ ÚÁÇÏÌÏ×ËÁ ÓÁÍÏÇÏ ÐÁËÅÔÁ. +}; +//----------------------------------------------------------------------------- +inline uint16_t RAW_PACKET_OLD::GetIPVersion() const +{ +return pckt[0] >> 4; +} +//----------------------------------------------------------------------------- +inline uint8_t RAW_PACKET_OLD::GetHeaderLen() const +{ +return (pckt[0] & 0x0F) * 4; +} +//----------------------------------------------------------------------------- +inline uint8_t RAW_PACKET_OLD::GetProto() const +{ +return pckt[9]; +} +//----------------------------------------------------------------------------- +inline uint32_t RAW_PACKET_OLD::GetLen() const +{ +if (dataLen != -1) + return dataLen; +return ntohs(*(uint16_t*)(pckt + 2)); +} +//----------------------------------------------------------------------------- +inline uint32_t RAW_PACKET_OLD::GetSrcIP() const +{ +return *(uint32_t*)(pckt + 12); +} +//----------------------------------------------------------------------------- +inline uint32_t RAW_PACKET_OLD::GetDstIP() const +{ +return *(uint32_t*)(pckt + 16); +} +//----------------------------------------------------------------------------- +inline uint16_t RAW_PACKET_OLD::GetSrcPort() const +{ +if (GetProto() == 1) // for icmp proto return port 0 + return 0; +return ntohs(*((uint16_t*)(pckt + GetHeaderLen()))); +} +//----------------------------------------------------------------------------- +inline uint16_t RAW_PACKET_OLD::GetDstPort() const +{ +if (GetProto() == 1) // for icmp proto return port 0 + return 0; +return ntohs(*((uint16_t*)(pckt + GetHeaderLen() + 2))); +} +//----------------------------------------------------------------------------- +inline bool operator==(const RAW_PACKET_OLD & lhs, const RAW_PACKET_OLD & rhs) +{ +if (lhs.GetSrcIP() != rhs.GetSrcIP()) + return false; + +if (lhs.GetDstIP() != rhs.GetDstIP()) + return false; + +if (lhs.GetSrcPort() != rhs.GetSrcPort()) + return false; + +if (lhs.GetDstPort() != rhs.GetDstPort()) + return false; + +if (lhs.GetProto() != rhs.GetProto()) + return false; + +return true; +} +//----------------------------------------------------------------------------- +inline bool operator<(const RAW_PACKET_OLD & lhs, const RAW_PACKET_OLD & rhs) +{ +if (lhs.GetSrcIP() < rhs.GetSrcIP()) + return true; +if (lhs.GetSrcIP() > rhs.GetSrcIP()) + return false; + +if (lhs.GetDstIP() < rhs.GetDstIP()) + return true; +if (lhs.GetDstIP() > rhs.GetDstIP()) + return false; + +if (lhs.GetSrcPort() < rhs.GetSrcPort()) + return true; +if (lhs.GetSrcPort() > rhs.GetSrcPort()) + return false; + +if (lhs.GetDstPort() < rhs.GetDstPort()) + return true; +if (lhs.GetDstPort() > rhs.GetDstPort()) + return false; + +if (lhs.GetProto() < rhs.GetProto()) + return true; + +/* +Last compare + +if (lhs.GetProto() > rhs.GetProto()) + return false; + +don't needed +*/ + +return false; +} +//----------------------------------------------------------------------------- + +#endif diff --git a/tests/test_admin_conf.cpp b/tests/test_admin_conf.cpp new file mode 100644 index 00000000..51913d5c --- /dev/null +++ b/tests/test_admin_conf.cpp @@ -0,0 +1,222 @@ +#include + +#include + +#include "admin_conf.h" + +namespace tut +{ + struct priv_data { + enum { + MIX2 = 0x06C6, // 2103210 + ONES = 0x1555, + MIX3 = 0x1B1B, // 3210321 + TWOS = 0x2AAA, + MIX1 = 0x24E4, // 0123012 + THREES = 0x3FFF + }; + }; + + typedef test_group tg; + tg priv_test_group("PRIV tests group"); + + typedef tg::object testobject; + + template<> + template<> + void testobject::test<1>() + { + set_test_name("Check default constructor"); + + PRIV zero; + + ensure("zero.userStat == 0", zero.userStat == 0); + ensure("zero.userConf == 0", zero.userConf == 0); + ensure("zero.userCash == 0", zero.userCash == 0); + ensure("zero.userPasswd == 0", zero.userPasswd == 0); + ensure("zero.userAddDel == 0", zero.userAddDel == 0); + ensure("zero.adminChg == 0", zero.adminChg == 0); + ensure("zero.tariffChg == 0", zero.tariffChg == 0); + + ensure("zero.ToInt() == 0", zero.ToInt() == 0); + } + + template<> + template<> + void testobject::test<2>() + { + set_test_name("Check uint16_t conversions"); + + for (uint16_t i = 0; i < 4; ++i) { + + // 'i' is extra trash in high bits + + PRIV priv1(ONES | (i << 0x0E)); // All 1 + + ensure_equals("priv1.userStat == 1", priv1.userStat, 1); + ensure_equals("priv1.userConf == 1", priv1.userConf, 1); + ensure_equals("priv1.userCash == 1", priv1.userCash, 1); + ensure_equals("priv1.userPasswd == 1", priv1.userPasswd, 1); + ensure_equals("priv1.userAddDel == 1", priv1.userAddDel, 1); + ensure_equals("priv1.adminChg == 1", priv1.adminChg, 1); + ensure_equals("priv1.tariffChg == 1", priv1.tariffChg, 1); + + ensure_equals("priv1.ToInt() == 0x1555", priv1.ToInt(), static_cast(ONES)); + + PRIV priv2(TWOS | (i << 0x0E)); // All 2 + + ensure_equals("priv2.userStat == 2", priv2.userStat, 2); + ensure_equals("priv2.userConf == 2", priv2.userConf, 2); + ensure_equals("priv2.userCash == 2", priv2.userCash, 2); + ensure_equals("priv2.userPasswd == 2", priv2.userPasswd, 2); + ensure_equals("priv2.userAddDel == 2", priv2.userAddDel, 2); + ensure_equals("priv2.adminChg == 2", priv2.adminChg, 2); + ensure_equals("priv2.tariffChg == 2", priv2.tariffChg, 2); + + ensure_equals("priv2.ToInt() = 0x2AAA", priv2.ToInt(), static_cast(TWOS)); + + PRIV priv3(THREES | (i << 0x0E)); // All 3 + + ensure_equals("priv3.userStat == 3", priv3.userStat, 3); + ensure_equals("priv3.userConf == 3", priv3.userConf, 3); + ensure_equals("priv3.userCash == 3", priv3.userCash, 3); + ensure_equals("priv3.userPasswd == 3", priv3.userPasswd, 3); + ensure_equals("priv3.userAddDel == 3", priv3.userAddDel, 3); + ensure_equals("priv3.adminChg == 3", priv3.adminChg, 3); + ensure_equals("priv3.tariffChg == 3", priv3.tariffChg, 3); + + ensure_equals("priv2.ToInt() = 0x3FFF", priv3.ToInt(), static_cast(THREES)); + + PRIV pm1(MIX1 | (i << 0x0E)); // 0123012 + + ensure_equals("pm1.userStat == 0", pm1.userStat, 0); + ensure_equals("pm1.userConf == 1", pm1.userConf, 1); + ensure_equals("pm1.userCash == 2", pm1.userCash, 2); + ensure_equals("pm1.userPasswd == 3", pm1.userPasswd, 3); + ensure_equals("pm1.userAddDel == 0", pm1.userAddDel, 0); + ensure_equals("pm1.adminChg == 1", pm1.adminChg, 1); + ensure_equals("pm1.tariffChg == 2", pm1.tariffChg, 2); + + ensure_equals("pm1.ToInt() = 0x24E4", pm1.ToInt(), static_cast(MIX1)); + + PRIV pm2(MIX2 | (i << 0x0E)); // 0123012 + + ensure_equals("pm2.userStat == 2", pm2.userStat, 2); + ensure_equals("pm2.userConf == 1", pm2.userConf, 1); + ensure_equals("pm2.userCash == 0", pm2.userCash, 0); + ensure_equals("pm2.userPasswd == 3", pm2.userPasswd, 3); + ensure_equals("pm2.userAddDel == 2", pm2.userAddDel, 2); + ensure_equals("pm2.adminChg == 1", pm2.adminChg, 1); + ensure_equals("pm2.tariffChg == 0", pm2.tariffChg, 0); + + ensure_equals("pm2.ToInt() = 0x06C6", pm2.ToInt(), static_cast(MIX2)); + + PRIV pm3(MIX3 | (i << 0x0E)); // 3210321 + + ensure_equals("pm3.userStat == 3", pm3.userStat, 3); + ensure_equals("pm3.userConf == 2", pm3.userConf, 2); + ensure_equals("pm3.userCash == 1", pm3.userCash, 1); + ensure_equals("pm3.userPasswd == 0", pm3.userPasswd, 0); + ensure_equals("pm3.userAddDel == 3", pm3.userAddDel, 3); + ensure_equals("pm3.adminChg == 2", pm3.adminChg, 2); + ensure_equals("pm3.tariffChg == 1", pm3.tariffChg, 1); + + ensure_equals("pm3.ToInt() = 0x1B1B", pm3.ToInt(), static_cast(MIX3)); + + } + + } + + template<> + template<> + void testobject::test<3>() + { + set_test_name("Check explicit uint16_t conversions"); + + for (uint16_t i = 0; i < 4; ++i) { + + // 'i' is extra trash in high bits + + PRIV priv1; + priv1.FromInt(ONES | (i << 0x0E)); // All 1 + + ensure_equals("priv1.userStat == 1", priv1.userStat, 1); + ensure_equals("priv1.userConf == 1", priv1.userConf, 1); + ensure_equals("priv1.userCash == 1", priv1.userCash, 1); + ensure_equals("priv1.userPasswd == 1", priv1.userPasswd, 1); + ensure_equals("priv1.userAddDel == 1", priv1.userAddDel, 1); + ensure_equals("priv1.adminChg == 1", priv1.adminChg, 1); + ensure_equals("priv1.tariffChg == 1", priv1.tariffChg, 1); + + ensure_equals("priv1.ToInt() == 0x1555", priv1.ToInt(), static_cast(ONES)); + + PRIV priv2; + priv2.FromInt(TWOS | (i << 0x0E)); // All 2 + + ensure_equals("priv2.userStat == 2", priv2.userStat, 2); + ensure_equals("priv2.userConf == 2", priv2.userConf, 2); + ensure_equals("priv2.userCash == 2", priv2.userCash, 2); + ensure_equals("priv2.userPasswd == 2", priv2.userPasswd, 2); + ensure_equals("priv2.userAddDel == 2", priv2.userAddDel, 2); + ensure_equals("priv2.adminChg == 2", priv2.adminChg, 2); + ensure_equals("priv2.tariffChg == 2", priv2.tariffChg, 2); + + ensure_equals("priv2.ToInt() = 0x2AAA", priv2.ToInt(), static_cast(TWOS)); + + PRIV priv3; + priv3.FromInt(THREES | (i << 0x0E)); // All 3 + + ensure_equals("priv3.userStat == 3", priv3.userStat, 3); + ensure_equals("priv3.userConf == 3", priv3.userConf, 3); + ensure_equals("priv3.userCash == 3", priv3.userCash, 3); + ensure_equals("priv3.userPasswd == 3", priv3.userPasswd, 3); + ensure_equals("priv3.userAddDel == 3", priv3.userAddDel, 3); + ensure_equals("priv3.adminChg == 3", priv3.adminChg, 3); + ensure_equals("priv3.tariffChg == 3", priv3.tariffChg, 3); + + ensure_equals("priv2.ToInt() = 0x3FFF", priv3.ToInt(), static_cast(THREES)); + + PRIV pm1; + pm1.FromInt(MIX1 | (i << 0x0E)); // 0123012 + + ensure_equals("pm1.userStat == 0", pm1.userStat, 0); + ensure_equals("pm1.userConf == 1", pm1.userConf, 1); + ensure_equals("pm1.userCash == 2", pm1.userCash, 2); + ensure_equals("pm1.userPasswd == 3", pm1.userPasswd, 3); + ensure_equals("pm1.userAddDel == 0", pm1.userAddDel, 0); + ensure_equals("pm1.adminChg == 1", pm1.adminChg, 1); + ensure_equals("pm1.tariffChg == 2", pm1.tariffChg, 2); + + ensure_equals("pm1.ToInt() = 0x24E4", pm1.ToInt(), static_cast(MIX1)); + + PRIV pm2; + pm2.FromInt(MIX2 | (i << 0x0E)); // 0123012 + + ensure_equals("pm2.userStat == 2", pm2.userStat, 2); + ensure_equals("pm2.userConf == 1", pm2.userConf, 1); + ensure_equals("pm2.userCash == 0", pm2.userCash, 0); + ensure_equals("pm2.userPasswd == 3", pm2.userPasswd, 3); + ensure_equals("pm2.userAddDel == 2", pm2.userAddDel, 2); + ensure_equals("pm2.adminChg == 1", pm2.adminChg, 1); + ensure_equals("pm2.tariffChg == 0", pm2.tariffChg, 0); + + ensure_equals("pm2.ToInt() = 0x06C6", pm2.ToInt(), static_cast(MIX2)); + + PRIV pm3; + pm3.FromInt(MIX3 | (i << 0x0E)); // 3210321 + + ensure_equals("pm3.userStat == 3", pm3.userStat, 3); + ensure_equals("pm3.userConf == 2", pm3.userConf, 2); + ensure_equals("pm3.userCash == 1", pm3.userCash, 1); + ensure_equals("pm3.userPasswd == 0", pm3.userPasswd, 0); + ensure_equals("pm3.userAddDel == 3", pm3.userAddDel, 3); + ensure_equals("pm3.adminChg == 2", pm3.adminChg, 2); + ensure_equals("pm3.tariffChg == 1", pm3.tariffChg, 1); + + ensure_equals("pm3.ToInt() = 0x1B1B", pm3.ToInt(), static_cast(MIX3)); + + } + + } + +} diff --git a/tests/test_raw_ip.cpp b/tests/test_raw_ip.cpp new file mode 100644 index 00000000..6e147686 --- /dev/null +++ b/tests/test_raw_ip.cpp @@ -0,0 +1,110 @@ +#include +#include +#include + +#include +#include +#include + +#include + +#include "raw_ip_packet_old.h" +#include "raw_ip_packet.h" + +#ifndef ITERATIONS +#define ITERATIONS 1000 +#endif + +void genVector(uint8_t * buf); + +std::ostream & operator<<(std::ostream & stream, const RAW_PACKET & p); + +namespace tut +{ + struct rp_data { + }; + + typedef test_group tg; + tg rp_test_group("RAW_PACKET tests group"); + + typedef tg::object testobject; + + template<> + template<> + void testobject::test<1>() + { + set_test_name("Check structure consistency"); + + RAW_PACKET rp; + rp.ipHeader.ip_v = 4; + rp.ipHeader.ip_hl = 5; + rp.ipHeader.ip_tos = 0; + rp.ipHeader.ip_len = htons(40); // 20 of header + 20 of data + rp.ipHeader.ip_p = 6; + rp.ipHeader.ip_src.s_addr = inet_addr("192.168.0.1"); + rp.ipHeader.ip_dst.s_addr = inet_addr("192.168.0.101"); + rp.sPort = htons(80); + rp.dPort = htons(38546); + + ensure_equals("IP header size (explicitly)", sizeof(rp.ipHeader), 20); + ensure_equals("IP version", rp.GetIPVersion(), 4); + ensure_equals("IP header size (with options)", rp.GetHeaderLen(), 20); + ensure_equals("Underlying protocol version", rp.GetProto(), 6); + ensure_equals("Packet length", rp.GetLen(), 40); + ensure_equals("Source IP address", rp.GetSrcIP(), inet_addr("192.168.0.1")); + ensure_equals("Destination IP address", rp.GetDstIP(), inet_addr("192.168.0.101")); + ensure_equals("Source port number", rp.GetSrcPort(), 80); + ensure_equals("Destination port number", rp.GetDstPort(), 38546); + } + + template<> + template<> + void testobject::test<2>() + { + srand(time(NULL)); + for (size_t i = 0; i < ITERATIONS; ++i) { + RAW_PACKET_OLD p1; + RAW_PACKET p2; + RAW_PACKET p3; + + uint8_t buf[68]; + genVector(buf); + + memcpy(p1.pckt, buf, 68); + memcpy(p2.pckt, buf, 68); + memcpy(p3.pckt, buf, 68); + + ensure_equals("IP versions", p1.GetIPVersion(), p2.GetIPVersion()); + ensure_equals("IP headers length", p1.GetHeaderLen(), p2.GetHeaderLen()); + ensure_equals("Protocols", p1.GetProto(), p2.GetProto()); + ensure_equals("Source IPs", p1.GetSrcIP(), p2.GetSrcIP()); + ensure_equals("Destination IPs", p1.GetDstIP(), p2.GetDstIP()); + ensure_equals("Source ports", p1.GetSrcPort(), p2.GetSrcPort()); + ensure_equals("Destination ports", p1.GetDstPort(), p2.GetDstPort()); + + ensure_equals("Self equallity", p2, p3); + ensure_equals("Reverse self equallity", p3, p2); + } + } +} + +inline +void genVector(uint8_t * buf) +{ + for (size_t i = 0; i < 68; ++i) { + buf[i] = rand() % 256; + } + buf[0] = (buf[0] & 0xF0) | 0x05; // Fix header length +} + +std::ostream & operator<<(std::ostream & stream, const RAW_PACKET & p) +{ + stream.unsetf(std::ios::dec); + stream.setf(std::ios::hex); + for (size_t i = 0; i < sizeof(p.pckt); ++i) { + stream << static_cast(p.pckt[i]); + } + stream.unsetf(std::ios::hex); + stream.setf(std::ios::dec); + return stream; +} diff --git a/tests/test_tariff.cpp b/tests/test_tariff.cpp new file mode 100644 index 00000000..8e4a25a3 --- /dev/null +++ b/tests/test_tariff.cpp @@ -0,0 +1,347 @@ +#include + +#include "tariff_conf.h" +#include "tariff.h" + +namespace tut +{ + struct tariff_data { + }; + + typedef test_group tg; + tg tariff_test_group("TARIFF tests group"); + + typedef tg::object testobject; + + template<> + template<> + void testobject::test<1>() + { + set_test_name("Check construction"); + + TARIFF_DATA td("test"); + td.tariffConf.fee = 1; + td.tariffConf.free = 2; + td.tariffConf.traffType = TRAFF_UP_DOWN; + td.tariffConf.passiveCost = 4; + td.dirPrice[0].mDay = 30; + td.dirPrice[0].hDay = 9; + td.dirPrice[0].mNight = 30; + td.dirPrice[0].hNight = 21; + td.dirPrice[0].priceDayA = 0; + td.dirPrice[0].priceDayB = 1; + td.dirPrice[0].priceNightA = 2; + td.dirPrice[0].priceNightB = 3; + td.dirPrice[0].threshold = 4; + td.dirPrice[0].singlePrice = 0; + td.dirPrice[0].noDiscount = 0; + TARIFF tariff(td); + + ensure("freeMb = 2", tariff.GetFreeMb() == td.tariffConf.free); + ensure("passiveCost = 4", tariff.GetPassiveCost() == td.tariffConf.passiveCost); + ensure("fee = 1", tariff.GetFee() == td.tariffConf.fee); + ensure("free (alias of freeMb) = 2", tariff.GetFree() == td.tariffConf.free); + ensure("name = \"test\"'", tariff.GetName() == td.tariffConf.name); + ensure("traffType = TRAFF_UP_DOWN", tariff.GetTraffType() == td.tariffConf.traffType); + ensure("threshold[0] = 4", tariff.GetThreshold(0) == td.dirPrice[0].threshold); + ensure_equals("traffByType(6, 0) = 6", tariff.GetTraffByType(6, 0), 6); + ensure_equals("traffByType(5, 1) = 6", tariff.GetTraffByType(5, 1), 6); + ensure_equals("traffByType(4, 2) = 6", tariff.GetTraffByType(4, 2), 6); + ensure_equals("traffByType(3, 3) = 6", tariff.GetTraffByType(3, 3), 6); + ensure_equals("traffByType(2, 4) = 6", tariff.GetTraffByType(2, 4), 6); + ensure_equals("traffByType(1, 5) = 6", tariff.GetTraffByType(1, 5), 6); + ensure_equals("traffByType(0, 6) = 6", tariff.GetTraffByType(0, 6), 6); + } + + template<> + template<> + void testobject::test<2>() + { + set_test_name("Check traff types"); + + TARIFF_DATA td("test"); + td.tariffConf.fee = 1; + td.tariffConf.free = 2; + td.tariffConf.traffType = TRAFF_UP; + td.tariffConf.passiveCost = 4; + td.dirPrice[0].mDay = 30; + td.dirPrice[0].hDay = 9; + td.dirPrice[0].mNight = 30; + td.dirPrice[0].hNight = 21; + td.dirPrice[0].priceDayA = 0; + td.dirPrice[0].priceDayB = 1; + td.dirPrice[0].priceNightA = 2; + td.dirPrice[0].priceNightB = 3; + td.dirPrice[0].threshold = 4; + td.dirPrice[0].singlePrice = 0; + td.dirPrice[0].noDiscount = 0; + TARIFF tariff(td); + + ensure("traffType = TRAFF_UP", tariff.GetTraffType() == TRAFF_UP); + ensure_equals("traffByType(6, 0) = 6 for UP", tariff.GetTraffByType(6, 0), 6); + ensure_equals("traffByType(5, 1) = 5 for UP", tariff.GetTraffByType(5, 1), 5); + ensure_equals("traffByType(4, 2) = 4 for UP", tariff.GetTraffByType(4, 2), 4); + ensure_equals("traffByType(3, 3) = 3 for UP", tariff.GetTraffByType(3, 3), 3); + ensure_equals("traffByType(2, 4) = 2 for UP", tariff.GetTraffByType(2, 4), 2); + ensure_equals("traffByType(1, 5) = 1 for UP", tariff.GetTraffByType(1, 5), 1); + ensure_equals("traffByType(0, 6) = 0 for UP", tariff.GetTraffByType(0, 6), 0); + + td.tariffConf.traffType = TRAFF_DOWN; + tariff = td; + + ensure("traffType = TRAFF_DOWN", tariff.GetTraffType() == TRAFF_DOWN); + ensure_equals("traffByType(6, 0) = 0 for DOWN", tariff.GetTraffByType(6, 0), 0); + ensure_equals("traffByType(5, 1) = 1 for DOWN", tariff.GetTraffByType(5, 1), 1); + ensure_equals("traffByType(4, 2) = 2 for DOWN", tariff.GetTraffByType(4, 2), 2); + ensure_equals("traffByType(3, 3) = 3 for DOWN", tariff.GetTraffByType(3, 3), 3); + ensure_equals("traffByType(2, 4) = 4 for DOWN", tariff.GetTraffByType(2, 4), 4); + ensure_equals("traffByType(1, 5) = 5 for DOWN", tariff.GetTraffByType(1, 5), 5); + ensure_equals("traffByType(0, 6) = 6 for DOWN", tariff.GetTraffByType(0, 6), 6); + + td.tariffConf.traffType = TRAFF_MAX; + tariff = td; + + ensure("traffType = TRAFF_MAX", tariff.GetTraffType() == TRAFF_MAX); + ensure_equals("traffByType(6, 0) = 6 for MAX", tariff.GetTraffByType(6, 0), 6); + ensure_equals("traffByType(5, 1) = 5 for MAX", tariff.GetTraffByType(5, 1), 5); + ensure_equals("traffByType(4, 2) = 4 for MAX", tariff.GetTraffByType(4, 2), 4); + ensure_equals("traffByType(3, 3) = 3 for MAX", tariff.GetTraffByType(3, 3), 3); + ensure_equals("traffByType(2, 4) = 4 for MAX", tariff.GetTraffByType(2, 4), 4); + ensure_equals("traffByType(1, 5) = 5 for MAX", tariff.GetTraffByType(1, 5), 5); + ensure_equals("traffByType(0, 6) = 6 for MAX", tariff.GetTraffByType(0, 6), 6); + + td.tariffConf.traffType = TRAFF_UP_DOWN; + tariff = td; + + ensure("traffType = TRAFF_UP_DOWN", tariff.GetTraffType() == TRAFF_UP_DOWN); + ensure_equals("traffByType(6, 0) = 6 for UP_DOWN", tariff.GetTraffByType(6, 0), 6); + ensure_equals("traffByType(5, 1) = 6 for UP_DOWN", tariff.GetTraffByType(5, 1), 6); + ensure_equals("traffByType(4, 2) = 6 for UP_DOWN", tariff.GetTraffByType(4, 2), 6); + ensure_equals("traffByType(3, 3) = 6 for UP_DOWN", tariff.GetTraffByType(3, 3), 6); + ensure_equals("traffByType(2, 4) = 6 for UP_DOWN", tariff.GetTraffByType(2, 4), 6); + ensure_equals("traffByType(1, 5) = 6 for UP_DOWN", tariff.GetTraffByType(1, 5), 6); + ensure_equals("traffByType(0, 6) = 6 for UP_DOWN", tariff.GetTraffByType(0, 6), 6); + } + + template<> + template<> + void testobject::test<3>() + { + set_test_name("Check normal interval prices"); + + TARIFF_DATA td("test"); + td.tariffConf.fee = 1; + td.tariffConf.free = 2; + td.tariffConf.traffType = TRAFF_UP_DOWN; + td.tariffConf.passiveCost = 4; + td.dirPrice[0].mDay = 30; + td.dirPrice[0].hDay = 9; + td.dirPrice[0].mNight = 30; + td.dirPrice[0].hNight = 21; + td.dirPrice[0].priceDayA = 0; + td.dirPrice[0].priceDayB = 1; + td.dirPrice[0].priceNightA = 2; + td.dirPrice[0].priceNightB = 3; + td.dirPrice[0].threshold = 4; + td.dirPrice[0].singlePrice = 0; + td.dirPrice[0].noDiscount = 0; + TARIFF tariff(td); + + ensure_equals("0000 == 0", tariff.GetPriceWithTraffType(0, 0 * 1024 * 1024, 0, 1286461245), 0); // Near 17:30, 0 < 4 DA + ensure_equals("0001 == 0", tariff.GetPriceWithTraffType(0, 6 * 1024 * 1024, 0, 1286461245), 1); // Near 17:30, 6 > 4 DB + ensure_equals("0010 == 0", tariff.GetPriceWithTraffType(0, 0 * 1024 * 1024, 0, 1286479245), 2); // Near 22:30, 0 < 4 NA + ensure_equals("0011 == 0", tariff.GetPriceWithTraffType(0, 6 * 1024 * 1024, 0, 1286479245), 3); // Near 22:30, 6 > 4 NB + + td.dirPrice[0].singlePrice = 1; + tariff = td; + + ensure_equals("0100 == 0", tariff.GetPriceWithTraffType(0, 0 * 1024 * 1024, 0, 1286461245), 0); // Near 17:30, 0 < 4 DA + ensure_equals("0101 == 0", tariff.GetPriceWithTraffType(0, 6 * 1024 * 1024, 0, 1286461245), 1); // Near 17:30, 6 > 4 DB + ensure_equals("0110 == 0", tariff.GetPriceWithTraffType(0, 0 * 1024 * 1024, 0, 1286479245), 0); // Near 22:30, 0 < 4 DA + ensure_equals("0111 == 0", tariff.GetPriceWithTraffType(0, 6 * 1024 * 1024, 0, 1286479245), 1); // Near 22:30, 6 > 4 DB + + td.dirPrice[0].singlePrice = 0; + td.dirPrice[0].noDiscount = 1; + tariff = td; + + ensure_equals("1000 == 0", tariff.GetPriceWithTraffType(0, 0 * 1024 * 1024, 0, 1286461245), 0); // Near 17:30, 0 < 4 DA + ensure_equals("1001 == 0", tariff.GetPriceWithTraffType(0, 6 * 1024 * 1024, 0, 1286461245), 0); // Near 17:30, 6 > 4 DA + ensure_equals("1010 == 0", tariff.GetPriceWithTraffType(0, 0 * 1024 * 1024, 0, 1286479245), 2); // Near 22:30, 0 < 4 NA + ensure_equals("1011 == 0", tariff.GetPriceWithTraffType(0, 6 * 1024 * 1024, 0, 1286479245), 2); // Near 22:30, 6 > 4 NA + + td.dirPrice[0].singlePrice = 1; + td.dirPrice[0].noDiscount = 1; + tariff = td; + + ensure_equals("1100 == 0", tariff.GetPriceWithTraffType(0, 0 * 1024 * 1024, 0, 1286461245), 0); // Near 17:30, 0 < 4 DA + ensure_equals("1101 == 0", tariff.GetPriceWithTraffType(0, 6 * 1024 * 1024, 0, 1286461245), 0); // Near 17:30, 6 > 4 DA + ensure_equals("1110 == 0", tariff.GetPriceWithTraffType(0, 0 * 1024 * 1024, 0, 1286479245), 0); // Near 22:30, 0 < 4 DA + ensure_equals("1111 == 0", tariff.GetPriceWithTraffType(0, 6 * 1024 * 1024, 0, 1286479245), 0); // Near 22:30, 6 > 4 DA + } + + template<> + template<> + void testobject::test<4>() + { + set_test_name("Check construction for day-night inversion"); + + TARIFF_DATA td("test"); + td.tariffConf.fee = 1; + td.tariffConf.free = 2; + td.tariffConf.traffType = TRAFF_UP_DOWN; + td.tariffConf.passiveCost = 4; + td.dirPrice[0].mDay = 30; + td.dirPrice[0].hDay = 21; + td.dirPrice[0].mNight = 30; + td.dirPrice[0].hNight = 9; + td.dirPrice[0].priceDayA = 0; + td.dirPrice[0].priceDayB = 1; + td.dirPrice[0].priceNightA = 2; + td.dirPrice[0].priceNightB = 3; + td.dirPrice[0].threshold = 4; + td.dirPrice[0].singlePrice = 0; + td.dirPrice[0].noDiscount = 0; + TARIFF tariff(td); + + ensure("freeMb = 2", tariff.GetFreeMb() == td.tariffConf.free); + ensure("passiveCost = 4", tariff.GetPassiveCost() == td.tariffConf.passiveCost); + ensure("fee = 1", tariff.GetFee() == td.tariffConf.fee); + ensure("free (alias of freeMb) = 2", tariff.GetFree() == td.tariffConf.free); + ensure("name = \"test\"'", tariff.GetName() == td.tariffConf.name); + ensure("traffType = TRAFF_UP_DOWN", tariff.GetTraffType() == td.tariffConf.traffType); + ensure("threshold[0] = 4", tariff.GetThreshold(0) == td.dirPrice[0].threshold); + ensure_equals("traffByType(6, 0) = 6", tariff.GetTraffByType(6, 0), 6); + ensure_equals("traffByType(5, 1) = 6", tariff.GetTraffByType(5, 1), 6); + ensure_equals("traffByType(4, 2) = 6", tariff.GetTraffByType(4, 2), 6); + ensure_equals("traffByType(3, 3) = 6", tariff.GetTraffByType(3, 3), 6); + ensure_equals("traffByType(2, 4) = 6", tariff.GetTraffByType(2, 4), 6); + ensure_equals("traffByType(1, 5) = 6", tariff.GetTraffByType(1, 5), 6); + ensure_equals("traffByType(0, 6) = 6", tariff.GetTraffByType(0, 6), 6); + } + + template<> + template<> + void testobject::test<5>() + { + set_test_name("Check traff types for day-night inversion"); + + TARIFF_DATA td("test"); + td.tariffConf.fee = 1; + td.tariffConf.free = 2; + td.tariffConf.traffType = TRAFF_UP; + td.tariffConf.passiveCost = 4; + td.dirPrice[0].mDay = 30; + td.dirPrice[0].hDay = 21; + td.dirPrice[0].mNight = 30; + td.dirPrice[0].hNight = 9; + td.dirPrice[0].priceDayA = 0; + td.dirPrice[0].priceDayB = 1; + td.dirPrice[0].priceNightA = 2; + td.dirPrice[0].priceNightB = 3; + td.dirPrice[0].threshold = 4; + td.dirPrice[0].singlePrice = 0; + td.dirPrice[0].noDiscount = 0; + TARIFF tariff(td); + + ensure("traffType = TRAFF_UP", tariff.GetTraffType() == TRAFF_UP); + ensure_equals("traffByType(6, 0) = 6 for UP", tariff.GetTraffByType(6, 0), 6); + ensure_equals("traffByType(5, 1) = 5 for UP", tariff.GetTraffByType(5, 1), 5); + ensure_equals("traffByType(4, 2) = 4 for UP", tariff.GetTraffByType(4, 2), 4); + ensure_equals("traffByType(3, 3) = 3 for UP", tariff.GetTraffByType(3, 3), 3); + ensure_equals("traffByType(2, 4) = 2 for UP", tariff.GetTraffByType(2, 4), 2); + ensure_equals("traffByType(1, 5) = 1 for UP", tariff.GetTraffByType(1, 5), 1); + ensure_equals("traffByType(0, 6) = 0 for UP", tariff.GetTraffByType(0, 6), 0); + + td.tariffConf.traffType = TRAFF_DOWN; + tariff = td; + + ensure("traffType = TRAFF_DOWN", tariff.GetTraffType() == TRAFF_DOWN); + ensure_equals("traffByType(6, 0) = 0 for DOWN", tariff.GetTraffByType(6, 0), 0); + ensure_equals("traffByType(5, 1) = 1 for DOWN", tariff.GetTraffByType(5, 1), 1); + ensure_equals("traffByType(4, 2) = 2 for DOWN", tariff.GetTraffByType(4, 2), 2); + ensure_equals("traffByType(3, 3) = 3 for DOWN", tariff.GetTraffByType(3, 3), 3); + ensure_equals("traffByType(2, 4) = 4 for DOWN", tariff.GetTraffByType(2, 4), 4); + ensure_equals("traffByType(1, 5) = 5 for DOWN", tariff.GetTraffByType(1, 5), 5); + ensure_equals("traffByType(0, 6) = 6 for DOWN", tariff.GetTraffByType(0, 6), 6); + + td.tariffConf.traffType = TRAFF_MAX; + tariff = td; + + ensure("traffType = TRAFF_MAX", tariff.GetTraffType() == TRAFF_MAX); + ensure_equals("traffByType(6, 0) = 6 for MAX", tariff.GetTraffByType(6, 0), 6); + ensure_equals("traffByType(5, 1) = 5 for MAX", tariff.GetTraffByType(5, 1), 5); + ensure_equals("traffByType(4, 2) = 4 for MAX", tariff.GetTraffByType(4, 2), 4); + ensure_equals("traffByType(3, 3) = 3 for MAX", tariff.GetTraffByType(3, 3), 3); + ensure_equals("traffByType(2, 4) = 4 for MAX", tariff.GetTraffByType(2, 4), 4); + ensure_equals("traffByType(1, 5) = 5 for MAX", tariff.GetTraffByType(1, 5), 5); + ensure_equals("traffByType(0, 6) = 6 for MAX", tariff.GetTraffByType(0, 6), 6); + + td.tariffConf.traffType = TRAFF_UP_DOWN; + tariff = td; + + ensure("traffType = TRAFF_UP_DOWN", tariff.GetTraffType() == TRAFF_UP_DOWN); + ensure_equals("traffByType(6, 0) = 6 for UP_DOWN", tariff.GetTraffByType(6, 0), 6); + ensure_equals("traffByType(5, 1) = 6 for UP_DOWN", tariff.GetTraffByType(5, 1), 6); + ensure_equals("traffByType(4, 2) = 6 for UP_DOWN", tariff.GetTraffByType(4, 2), 6); + ensure_equals("traffByType(3, 3) = 6 for UP_DOWN", tariff.GetTraffByType(3, 3), 6); + ensure_equals("traffByType(2, 4) = 6 for UP_DOWN", tariff.GetTraffByType(2, 4), 6); + ensure_equals("traffByType(1, 5) = 6 for UP_DOWN", tariff.GetTraffByType(1, 5), 6); + ensure_equals("traffByType(0, 6) = 6 for UP_DOWN", tariff.GetTraffByType(0, 6), 6); + } + + template<> + template<> + void testobject::test<6>() + { + set_test_name("Check normal interval prices for day-night inversion"); + + TARIFF_DATA td("test"); + td.tariffConf.fee = 1; + td.tariffConf.free = 2; + td.tariffConf.traffType = TRAFF_UP_DOWN; + td.tariffConf.passiveCost = 4; + td.dirPrice[0].mDay = 30; + td.dirPrice[0].hDay = 21; + td.dirPrice[0].mNight = 30; + td.dirPrice[0].hNight = 9; + td.dirPrice[0].priceDayA = 0; + td.dirPrice[0].priceDayB = 1; + td.dirPrice[0].priceNightA = 2; + td.dirPrice[0].priceNightB = 3; + td.dirPrice[0].threshold = 4; + td.dirPrice[0].singlePrice = 0; + td.dirPrice[0].noDiscount = 0; + TARIFF tariff(td); + + ensure_equals("0000 == 0", tariff.GetPriceWithTraffType(0, 0 * 1024 * 1024, 0, 1286461245), 2); // Near 17:30, 0 < 4 NA + ensure_equals("0001 == 0", tariff.GetPriceWithTraffType(0, 6 * 1024 * 1024, 0, 1286461245), 3); // Near 17:30, 6 > 4 NB + ensure_equals("0010 == 0", tariff.GetPriceWithTraffType(0, 0 * 1024 * 1024, 0, 1286479245), 0); // Near 22:30, 0 < 4 DA + ensure_equals("0011 == 0", tariff.GetPriceWithTraffType(0, 6 * 1024 * 1024, 0, 1286479245), 1); // Near 22:30, 6 > 4 DB + + td.dirPrice[0].singlePrice = 1; + tariff = td; + + ensure_equals("0100 == 0", tariff.GetPriceWithTraffType(0, 0 * 1024 * 1024, 0, 1286461245), 0); // Near 17:30, 0 < 4 DA (ignore night) + ensure_equals("0101 == 0", tariff.GetPriceWithTraffType(0, 6 * 1024 * 1024, 0, 1286461245), 1); // Near 17:30, 6 > 4 DB (ignore night) + ensure_equals("0110 == 0", tariff.GetPriceWithTraffType(0, 0 * 1024 * 1024, 0, 1286479245), 0); // Near 22:30, 0 < 4 DA (ignore night) + ensure_equals("0111 == 0", tariff.GetPriceWithTraffType(0, 6 * 1024 * 1024, 0, 1286479245), 1); // Near 22:30, 6 > 4 DB (ignore night) + + td.dirPrice[0].singlePrice = 0; + td.dirPrice[0].noDiscount = 1; + tariff = td; + + ensure_equals("1000 == 0", tariff.GetPriceWithTraffType(0, 0 * 1024 * 1024, 0, 1286461245), 2); // Near 17:30, 0 < 4 NA + ensure_equals("1001 == 0", tariff.GetPriceWithTraffType(0, 6 * 1024 * 1024, 0, 1286461245), 2); // Near 17:30, 6 > 4 NA + ensure_equals("1010 == 0", tariff.GetPriceWithTraffType(0, 0 * 1024 * 1024, 0, 1286479245), 0); // Near 22:30, 0 < 4 DA + ensure_equals("1011 == 0", tariff.GetPriceWithTraffType(0, 6 * 1024 * 1024, 0, 1286479245), 0); // Near 22:30, 6 > 4 DA + + td.dirPrice[0].singlePrice = 1; + td.dirPrice[0].noDiscount = 1; + tariff = td; + + ensure_equals("1100 == 0", tariff.GetPriceWithTraffType(0, 0 * 1024 * 1024, 0, 1286461245), 0); // Near 17:30, 0 < 4 DA (ignore night) + ensure_equals("1101 == 0", tariff.GetPriceWithTraffType(0, 6 * 1024 * 1024, 0, 1286461245), 0); // Near 17:30, 6 > 4 DA (ignore night) + ensure_equals("1110 == 0", tariff.GetPriceWithTraffType(0, 0 * 1024 * 1024, 0, 1286479245), 0); // Near 22:30, 0 < 4 DA (ignore night) + ensure_equals("1111 == 0", tariff.GetPriceWithTraffType(0, 6 * 1024 * 1024, 0, 1286479245), 0); // Near 22:30, 6 > 4 DA (ignore night) + } +} -- 2.44.2