]> git.stg.codes - stg.git/blob - tests/test_fee_charge_rules.cpp
Use std::lock_guard instead of STG_LOCKER.
[stg.git] / tests / test_fee_charge_rules.cpp
1 #define BOOST_TEST_MODULE STGFeeChargeRules
2
3 #include "stg/admin.h"
4 #include "stg/user_property.h"
5 #include "user_impl.h"
6
7 #include "testsettings.h"
8 #include "testtariffs.h"
9 #include "teststore.h"
10 #include "testservices.h"
11
12 #pragma GCC diagnostic push
13 #pragma GCC diagnostic ignored "-Wold-style-cast"
14 #pragma GCC diagnostic ignored "-Wunused-parameter"
15 #pragma GCC diagnostic ignored "-Wsign-compare"
16 #pragma GCC diagnostic ignored "-Wparentheses"
17 #include <boost/test/unit_test.hpp>
18 #pragma GCC diagnostic pop
19
20 volatile time_t stgTime = 0;
21
22 namespace
23 {
24
25 class Settings : public TestSettings
26 {
27     public:
28         Settings(unsigned feeChargeType)
29             : m_feeChargeType(feeChargeType)
30         {}
31
32         unsigned GetFeeChargeType() const { return m_feeChargeType; }
33
34     private:
35         unsigned m_feeChargeType;
36 };
37
38 }
39
40 BOOST_AUTO_TEST_SUITE(FeeChargeRules)
41
42 BOOST_AUTO_TEST_CASE(ClassicRules)
43 {
44     Settings settings(0);
45     TestTariffs tariffs;
46     STG::Admin admin(STG::Priv(0xFFFF), {}, {});
47     TestStore store;
48     TestServices services;
49     STG::UserImpl user(&settings, &store, &tariffs, &admin, NULL, services);
50
51     STG::UserProperty<double>& cash = user.GetProperties().cash;
52     STG::UserProperty<std::string>& tariffName = user.GetProperties().tariffName;
53
54     BOOST_CHECK_EQUAL(user.GetProperties().cash, 0);
55     cash = 100;
56     BOOST_CHECK_EQUAL(user.GetProperties().cash, 100);
57
58     tariffs.SetFee(50);
59     tariffName = "test";
60     BOOST_CHECK_EQUAL(user.GetProperties().tariffName.ConstData(), "test");
61     user.ProcessDayFee();
62     BOOST_CHECK_EQUAL(user.GetProperties().cash, 50);
63     user.ProcessDayFee();
64     BOOST_CHECK_EQUAL(user.GetProperties().cash, 0);
65     user.ProcessDayFee();
66     BOOST_CHECK_EQUAL(user.GetProperties().cash, -50);
67     user.ProcessDayFee();
68     BOOST_CHECK_EQUAL(user.GetProperties().cash, -100);
69 }
70
71 BOOST_AUTO_TEST_CASE(PositiveCashRules)
72 {
73     Settings settings(1);
74     TestTariffs tariffs;
75     STG::Admin admin(STG::Priv(0xFFFF), {}, {});
76     TestStore store;
77     TestServices services;
78     STG::UserImpl user(&settings, &store, &tariffs, &admin, NULL, services);
79
80     STG::UserProperty<double> & cash(user.GetProperties().cash);
81     STG::UserProperty<double> & credit(user.GetProperties().credit);
82     STG::UserProperty<std::string> & tariffName(user.GetProperties().tariffName);
83
84     BOOST_CHECK_EQUAL(user.GetProperties().cash, 0);
85     BOOST_CHECK_EQUAL(user.GetProperties().credit, 0);
86     cash = 100;
87     BOOST_CHECK_EQUAL(user.GetProperties().cash, 100);
88
89     tariffs.SetFee(50);
90     tariffName = "test";
91     BOOST_CHECK_EQUAL(user.GetProperties().tariffName.ConstData(), "test");
92     user.ProcessDayFee();
93     BOOST_CHECK_EQUAL(user.GetProperties().cash, 50);
94     user.ProcessDayFee();
95     BOOST_CHECK_EQUAL(user.GetProperties().cash, 0);
96     user.ProcessDayFee();
97     BOOST_CHECK_EQUAL(user.GetProperties().cash, -50);
98     user.ProcessDayFee();
99     BOOST_CHECK_EQUAL(user.GetProperties().cash, -50);
100     cash = 49;
101     BOOST_CHECK_EQUAL(user.GetProperties().cash, 49);
102     user.ProcessDayFee();
103     BOOST_CHECK_EQUAL(user.GetProperties().cash, -1);
104     user.ProcessDayFee();
105     BOOST_CHECK_EQUAL(user.GetProperties().cash, -1);
106     credit = 50;
107     BOOST_CHECK_EQUAL(user.GetProperties().credit, 50);
108     user.ProcessDayFee();
109     BOOST_CHECK_EQUAL(user.GetProperties().cash, -51);
110     user.ProcessDayFee();
111     BOOST_CHECK_EQUAL(user.GetProperties().cash, -51);
112 }
113
114 BOOST_AUTO_TEST_CASE(GreaterThanFeeRules)
115 {
116     Settings settings(2);
117     TestTariffs tariffs;
118     STG::Admin admin(STG::Priv(0xFFFF), {}, {});
119     TestStore store;
120     TestServices services;
121     STG::UserImpl user(&settings, &store, &tariffs, &admin, NULL, services);
122
123     STG::UserProperty<double> & cash(user.GetProperties().cash);
124     STG::UserProperty<double> & credit(user.GetProperties().credit);
125     STG::UserProperty<std::string> & tariffName(user.GetProperties().tariffName);
126
127     BOOST_CHECK_EQUAL(user.GetProperties().cash, 0);
128     cash = 100;
129     BOOST_CHECK_EQUAL(user.GetProperties().cash, 100);
130
131     tariffs.SetFee(50);
132     tariffName = "test";
133     BOOST_CHECK_EQUAL(user.GetProperties().tariffName.ConstData(), "test");
134     user.ProcessDayFee();
135     BOOST_CHECK_EQUAL(user.GetProperties().cash, 50);
136     user.ProcessDayFee();
137     BOOST_CHECK_EQUAL(user.GetProperties().cash, 0);
138     user.ProcessDayFee();
139     BOOST_CHECK_EQUAL(user.GetProperties().cash, 0);
140     cash = 50;
141     BOOST_CHECK_EQUAL(user.GetProperties().cash, 50);
142     tariffs.SetFee(51);
143     user.ProcessDayFee();
144     BOOST_CHECK_EQUAL(user.GetProperties().cash, 50);
145     cash = 0;
146     BOOST_CHECK_EQUAL(user.GetProperties().cash, 0);
147     credit = 51;
148     BOOST_CHECK_EQUAL(user.GetProperties().credit, 51);
149     user.ProcessDayFee();
150     BOOST_CHECK_EQUAL(user.GetProperties().cash, -51);
151     user.ProcessDayFee();
152     BOOST_CHECK_EQUAL(user.GetProperties().cash, -51);
153 }
154
155 BOOST_AUTO_TEST_SUITE_END()