t_balance.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. #define BOOST_TEST_DYN_LINK
  2. //#define BOOST_TEST_MODULE balance
  3. #include <boost/test/unit_test.hpp>
  4. #include <system.hh>
  5. #include "balance.h"
  6. using namespace ledger;
  7. struct balance_fixture {
  8. balance_fixture() {
  9. times_initialize();
  10. amount_t::initialize();
  11. // Cause the display precision for dollars to be initialized to 2.
  12. amount_t x1("$1.00");
  13. BOOST_CHECK(x1);
  14. amount_t::stream_fullstrings = true; // make reports from UnitTests accurate
  15. }
  16. ~balance_fixture()
  17. {
  18. amount_t::stream_fullstrings = false;
  19. amount_t::shutdown();
  20. times_shutdown();
  21. }
  22. };
  23. BOOST_FIXTURE_TEST_SUITE(balance, balance_fixture)
  24. BOOST_AUTO_TEST_CASE(testConstructors)
  25. {
  26. balance_t b0;
  27. balance_t b1(1.00);
  28. balance_t b2(123456UL);
  29. balance_t b3(12345L);
  30. balance_t b4(string ("EUR 123"));
  31. balance_t b5("$ 456");
  32. balance_t b6;
  33. balance_t b7(amount_t("$ 1.00"));
  34. balance_t b8(b7);
  35. BOOST_CHECK_EQUAL(balance_t(), b0);
  36. BOOST_CHECK_NE(balance_t("0"), b0);
  37. BOOST_CHECK_NE(balance_t("0.0"), b0);
  38. BOOST_CHECK_EQUAL(b2, 123456UL);
  39. BOOST_CHECK_EQUAL(b3, 12345L);
  40. BOOST_CHECK_EQUAL(b4, "EUR 123");
  41. BOOST_CHECK_EQUAL(b5, string("$ 456"));
  42. BOOST_CHECK_EQUAL(b7, b8);
  43. BOOST_CHECK_EQUAL(b8, amount_t("$ 1.00"));
  44. b5 = "euro 2345";
  45. b6 = string("DM -34532");
  46. b7 = amount_t("$ 1.00");
  47. b8 = b5;
  48. BOOST_CHECK_EQUAL(b5, b8);
  49. BOOST_CHECK(b0.valid());
  50. BOOST_CHECK(b1.valid());
  51. BOOST_CHECK(b2.valid());
  52. BOOST_CHECK(b3.valid());
  53. BOOST_CHECK(b4.valid());
  54. BOOST_CHECK(b5.valid());
  55. BOOST_CHECK(b6.valid());
  56. BOOST_CHECK(b7.valid());
  57. BOOST_CHECK(b8.valid());
  58. }
  59. BOOST_AUTO_TEST_CASE(testAddition)
  60. {
  61. amount_t a0;
  62. amount_t a1("$1");
  63. amount_t a2("2 EUR");
  64. amount_t a3("0.00 CAD");
  65. amount_t a4("$2");
  66. balance_t b0;
  67. balance_t b1(1.00);
  68. balance_t b2(2UL);
  69. balance_t b3(2L);
  70. balance_t b4;
  71. balance_t b5;
  72. b0 += b1;
  73. b2 += b3;
  74. b3 += a1;
  75. b3 += a2;
  76. b4 += b3;
  77. b5 += a1;
  78. b5 += a4;
  79. BOOST_CHECK_EQUAL(balance_t(1.00), b0);
  80. BOOST_CHECK_EQUAL(b3 += a3, b4);
  81. BOOST_CHECK_EQUAL(balance_t(4L), b2);
  82. BOOST_CHECK_EQUAL(balance_t() += amount_t("$3"), b5);
  83. BOOST_CHECK_THROW(b3 += a0, balance_error);
  84. BOOST_CHECK(b0.valid());
  85. BOOST_CHECK(b1.valid());
  86. BOOST_CHECK(b2.valid());
  87. BOOST_CHECK(b3.valid());
  88. BOOST_CHECK(b4.valid());
  89. BOOST_CHECK(b5.valid());
  90. }
  91. BOOST_AUTO_TEST_CASE(testSubtraction)
  92. {
  93. amount_t a0;
  94. amount_t a1("$1");
  95. amount_t a2("2 EUR");
  96. amount_t a3("0.00 CAD");
  97. amount_t a4("$2");
  98. balance_t b0;
  99. balance_t b1(1.00);
  100. balance_t b2(2UL);
  101. balance_t b3(2L);
  102. balance_t b4;
  103. balance_t b5;
  104. b0 -= b1;
  105. b2 -= b3;
  106. b3 -= a1;
  107. b3 -= a2;
  108. b4 = b3;
  109. b5 -= a1;
  110. b5 -= a4;
  111. BOOST_CHECK_EQUAL(balance_t(-1.00), b0);
  112. BOOST_CHECK_EQUAL(b3 -= a3, b4);
  113. BOOST_CHECK_EQUAL(balance_t(), b2);
  114. BOOST_CHECK_EQUAL(b3 -= b2, b3);
  115. BOOST_CHECK_EQUAL(balance_t() -= amount_t("$3"), b5);
  116. BOOST_CHECK_THROW(b3 -= a0, balance_error);
  117. BOOST_CHECK(b0.valid());
  118. BOOST_CHECK(b1.valid());
  119. BOOST_CHECK(b2.valid());
  120. BOOST_CHECK(b3.valid());
  121. BOOST_CHECK(b4.valid());
  122. BOOST_CHECK(b5.valid());
  123. }
  124. BOOST_AUTO_TEST_CASE(testEqaulity)
  125. {
  126. amount_t a0;
  127. amount_t a1("$1");
  128. amount_t a2("2 EUR");
  129. amount_t a3("0.00 CAD");
  130. balance_t b0;
  131. balance_t b1(1.00);
  132. balance_t b2(2UL);
  133. balance_t b3(2L);
  134. balance_t b4("EUR 2");
  135. balance_t b5("$-1");
  136. balance_t b6("0.00");
  137. balance_t b7("0.00");
  138. BOOST_CHECK(b2 == b3);
  139. BOOST_CHECK(b4 == a2);
  140. BOOST_CHECK(b1 == "1.00");
  141. BOOST_CHECK(b5 == amount_t("-$1"));
  142. BOOST_CHECK(!(b6 == "0"));
  143. BOOST_CHECK(!(b6 == a3));
  144. BOOST_CHECK(!(b6 == "0.00"));
  145. BOOST_CHECK(b6 == b7);
  146. b4 += b5;
  147. b5 += a2;
  148. BOOST_CHECK(b4 == b5);
  149. #pragma GCC diagnostic push
  150. #pragma GCC diagnostic ignored "-Wunused-value"
  151. BOOST_CHECK_THROW(b0 == a0, balance_error);
  152. #pragma GCC diagnostic pop
  153. BOOST_CHECK(b0.valid());
  154. BOOST_CHECK(b1.valid());
  155. BOOST_CHECK(b2.valid());
  156. BOOST_CHECK(b3.valid());
  157. BOOST_CHECK(b4.valid());
  158. BOOST_CHECK(b5.valid());
  159. BOOST_CHECK(b6.valid());
  160. BOOST_CHECK(b7.valid());
  161. }
  162. BOOST_AUTO_TEST_CASE(testMultiplication)
  163. {
  164. amount_t a0;
  165. amount_t a1("0.00");
  166. balance_t b0;
  167. balance_t b1(1.00);
  168. balance_t b2(2UL);
  169. balance_t b3("CAD -3");
  170. balance_t b4("EUR 4.99999");
  171. balance_t b5("$1");
  172. balance_t b6;
  173. BOOST_CHECK_EQUAL(b1 *= 2.00, amount_t(2.00));
  174. BOOST_CHECK_EQUAL(b2 *= 2L, amount_t(4L));
  175. BOOST_CHECK_EQUAL(b2 *= 2UL, amount_t(8UL));
  176. BOOST_CHECK_EQUAL(b3 *= amount_t("-8 CAD"), amount_t("CAD 24"));
  177. BOOST_CHECK_EQUAL(b0 *= 2UL, b0);
  178. BOOST_CHECK_EQUAL(b0 *= a1, a1);
  179. b6 += b3;
  180. b3 += b4;
  181. b3 += b5;
  182. b3 *= 2L;
  183. b6 *= 2L;
  184. b4 *= 2L;
  185. b5 *= 2L;
  186. b6 += b4;
  187. b6 += b5;
  188. BOOST_CHECK_EQUAL(b3, b6);
  189. BOOST_CHECK_THROW(b1 *= a0 , balance_error);
  190. BOOST_CHECK_THROW(b4 *= amount_t("1 CAD") , balance_error);
  191. BOOST_CHECK_THROW(b3 *= amount_t("1 CAD") , balance_error);
  192. BOOST_CHECK(b0.valid());
  193. BOOST_CHECK(b1.valid());
  194. BOOST_CHECK(b2.valid());
  195. BOOST_CHECK(b3.valid());
  196. BOOST_CHECK(b4.valid());
  197. BOOST_CHECK(b5.valid());
  198. BOOST_CHECK(b6.valid());
  199. }
  200. BOOST_AUTO_TEST_CASE(testDivision)
  201. {
  202. amount_t a0;
  203. amount_t a1("0.00");
  204. balance_t b0;
  205. balance_t b1(4.00);
  206. balance_t b2(4UL);
  207. balance_t b3("CAD -24");
  208. balance_t b4("EUR 4");
  209. balance_t b5("$2");
  210. balance_t b6;
  211. BOOST_CHECK_EQUAL(b1 /= 2.00, amount_t(2.00));
  212. BOOST_CHECK_EQUAL(b2 /= 2L, amount_t(2L));
  213. BOOST_CHECK_EQUAL(b2 /= 2UL, amount_t(1UL));
  214. BOOST_CHECK_EQUAL(b3 /= amount_t("-3 CAD"), amount_t("CAD 8"));
  215. BOOST_CHECK_EQUAL(b0 /= 2UL, b0);
  216. b6 += b3;
  217. b3 += b4;
  218. b3 += b5;
  219. b3 /= 2L;
  220. b6 /= 2L;
  221. b4 /= 2L;
  222. b5 /= 2L;
  223. b6 += b4;
  224. b6 += b5;
  225. BOOST_CHECK_EQUAL(b3, b6);
  226. BOOST_CHECK_THROW(b1 /= a0 , balance_error);
  227. BOOST_CHECK_THROW(b1 /= a1 , balance_error);
  228. BOOST_CHECK_THROW(b4 /= amount_t("1 CAD") , balance_error);
  229. BOOST_CHECK_THROW(b3 /= amount_t("1 CAD") , balance_error);
  230. BOOST_CHECK(b0.valid());
  231. BOOST_CHECK(b1.valid());
  232. BOOST_CHECK(b2.valid());
  233. BOOST_CHECK(b3.valid());
  234. BOOST_CHECK(b4.valid());
  235. BOOST_CHECK(b5.valid());
  236. BOOST_CHECK(b6.valid());
  237. }
  238. BOOST_AUTO_TEST_CASE(testNegation)
  239. {
  240. amount_t a1("0.00");
  241. amount_t a2("$ 123");
  242. amount_t a3("EUR 456");
  243. balance_t b0;
  244. balance_t b1;
  245. balance_t b2;
  246. balance_t b3;
  247. b1 += a1;
  248. b1 += a2;
  249. b1 += a3;
  250. b2 += -a1;
  251. b2 += -a2;
  252. b2 += -a3;
  253. b3 = -b1;
  254. BOOST_CHECK_EQUAL(b0.negated(), b0);
  255. BOOST_CHECK_EQUAL(b2, b3);
  256. BOOST_CHECK_EQUAL(b2, -b1);
  257. BOOST_CHECK_EQUAL(b2.negated(), b1);
  258. b2.in_place_negate();
  259. BOOST_CHECK_EQUAL(b2, b1);
  260. BOOST_CHECK_EQUAL(b1, -b3);
  261. BOOST_CHECK(b0.valid());
  262. BOOST_CHECK(b1.valid());
  263. BOOST_CHECK(b2.valid());
  264. BOOST_CHECK(b3.valid());
  265. }
  266. BOOST_AUTO_TEST_CASE(testAbs)
  267. {
  268. amount_t a1("0.00");
  269. amount_t a2("$ 123");
  270. amount_t a3("EUR 456");
  271. balance_t b0;
  272. balance_t b1;
  273. balance_t b2;
  274. b1 += a1;
  275. b1 += a2;
  276. b1 += a3;
  277. b2 += -a1;
  278. b2 += -a2;
  279. b2 += -a3;
  280. BOOST_CHECK_EQUAL(b0.abs(), b0);
  281. BOOST_CHECK_EQUAL(b2.abs(), b1.abs());
  282. BOOST_CHECK(b0.valid());
  283. BOOST_CHECK(b1.valid());
  284. BOOST_CHECK(b2.valid());
  285. }
  286. BOOST_AUTO_TEST_CASE(testCeiling)
  287. {
  288. amount_t a1("0.00");
  289. amount_t a2("$ 123.123");
  290. amount_t a3("EUR 456.56");
  291. amount_t a4(-a1);
  292. amount_t a5(-a2);
  293. amount_t a6(-a3);
  294. balance_t b0;
  295. balance_t b1;
  296. balance_t b2;
  297. balance_t b3;
  298. balance_t b4;
  299. b1 += a1;
  300. b1 += a2;
  301. b1 += a3;
  302. b2 += -a1;
  303. b2 += -a2;
  304. b2 += -a3;
  305. b3 += a1.ceilinged();
  306. b3 += a2.ceilinged();
  307. b3 += a3.ceilinged();
  308. b4 += a4.ceilinged();
  309. b4 += a5.ceilinged();
  310. b4 += a6.ceilinged();
  311. BOOST_CHECK_EQUAL(b0.ceilinged(), b0);
  312. BOOST_CHECK_EQUAL(b2.ceilinged(), b4);
  313. BOOST_CHECK_EQUAL(b1.ceilinged(), b3);
  314. b1.in_place_ceiling();
  315. BOOST_CHECK_EQUAL(b1, b3);
  316. BOOST_CHECK(b0.valid());
  317. BOOST_CHECK(b1.valid());
  318. BOOST_CHECK(b2.valid());
  319. BOOST_CHECK(b3.valid());
  320. BOOST_CHECK(b4.valid());
  321. }
  322. BOOST_AUTO_TEST_CASE(testFloor)
  323. {
  324. amount_t a1("0.00");
  325. amount_t a2("$ 123.123");
  326. amount_t a3("EUR 456.56");
  327. amount_t a4(-a1);
  328. amount_t a5(-a2);
  329. amount_t a6(-a3);
  330. balance_t b0;
  331. balance_t b1;
  332. balance_t b2;
  333. balance_t b3;
  334. balance_t b4;
  335. b1 += a1;
  336. b1 += a2;
  337. b1 += a3;
  338. b2 += -a1;
  339. b2 += -a2;
  340. b2 += -a3;
  341. b3 += a1.floored();
  342. b3 += a2.floored();
  343. b3 += a3.floored();
  344. b4 += a4.floored();
  345. b4 += a5.floored();
  346. b4 += a6.floored();
  347. BOOST_CHECK_EQUAL(b0.floored(), b0);
  348. BOOST_CHECK_EQUAL(b2.floored(), b4);
  349. BOOST_CHECK_EQUAL(b1.floored(), b3);
  350. b1.in_place_floor();
  351. BOOST_CHECK_EQUAL(b1, b3);
  352. BOOST_CHECK(b0.valid());
  353. BOOST_CHECK(b1.valid());
  354. BOOST_CHECK(b2.valid());
  355. BOOST_CHECK(b3.valid());
  356. BOOST_CHECK(b4.valid());
  357. }
  358. BOOST_AUTO_TEST_CASE(testRound)
  359. {
  360. amount_t a1("0.00");
  361. amount_t a2("$ 123.123");
  362. amount_t a3("EUR 456.567");
  363. amount_t a4("0.00");
  364. amount_t a5("$ 123.12");
  365. amount_t a6("EUR 456.57");
  366. balance_t b0;
  367. balance_t b1;
  368. balance_t b2;
  369. balance_t b3;
  370. balance_t b4;
  371. b1 += a1;
  372. b1 += a2;
  373. b1 += a3;
  374. b2 += a4;
  375. b2 += a5;
  376. b2 += a6;
  377. a1.in_place_roundto(2);
  378. a2.in_place_roundto(2);
  379. a3.in_place_roundto(2);
  380. a4.in_place_roundto(2);
  381. a5.in_place_roundto(2);
  382. a6.in_place_roundto(2);
  383. b3 += a1;
  384. b3 += a2;
  385. b3 += a3;
  386. b4 += a4;
  387. b4 += a5;
  388. b4 += a6;
  389. BOOST_CHECK_EQUAL(b0.rounded(), b0);
  390. BOOST_CHECK_EQUAL(b2.rounded(), b4);
  391. BOOST_CHECK_EQUAL(b1.rounded(), b4);
  392. b1.in_place_round();
  393. BOOST_CHECK_EQUAL(b1, b3);
  394. BOOST_CHECK(b0.valid());
  395. BOOST_CHECK(b1.valid());
  396. BOOST_CHECK(b2.valid());
  397. BOOST_CHECK(b3.valid());
  398. BOOST_CHECK(b4.valid());
  399. }
  400. BOOST_AUTO_TEST_CASE(testTruth)
  401. {
  402. amount_t a1("0.00");
  403. amount_t a2("$ 123");
  404. amount_t a3("EUR 456");
  405. balance_t b0;
  406. balance_t b1;
  407. b1 += a1;
  408. b1 += a2;
  409. b1 += a3;
  410. BOOST_CHECK(!b0);
  411. BOOST_CHECK(b1);
  412. BOOST_CHECK(b0.valid());
  413. BOOST_CHECK(b1.valid());
  414. }
  415. BOOST_AUTO_TEST_CASE(testForZero)
  416. {
  417. amount_t a1("0.00");
  418. amount_t a2("$ 123");
  419. amount_t a3("EUR 456");
  420. balance_t b0;
  421. balance_t b1;
  422. b1 += a1;
  423. b1 += a2;
  424. b1 += a3;
  425. BOOST_CHECK(b0.is_empty());
  426. BOOST_CHECK(b0.is_zero());
  427. BOOST_CHECK(b0.is_realzero());
  428. BOOST_CHECK(!b0.is_nonzero());
  429. BOOST_CHECK(!b1.is_empty());
  430. BOOST_CHECK(!b1.is_zero());
  431. BOOST_CHECK(!b1.is_realzero());
  432. BOOST_CHECK(b1.is_nonzero());
  433. BOOST_CHECK(b0.valid());
  434. BOOST_CHECK(b1.valid());
  435. }
  436. BOOST_AUTO_TEST_SUITE_END()