Project 3 - Currency Type
test.cpp
Go to the documentation of this file.
1 
3 #include <iostream>
4 #include <istream>
5 #include <locale>
6 #include <ostream>
7 #include <sstream>
8 #include <stdexcept>
9 
10 #include "test.hpp"
11 #include "currency.hpp"
12 
13 int main(int argc, char** argv)
14 {
15  if (argc >= 2)
16  std::locale::global(std::locale(argv[1]));
17  else
18  std::locale::global(std::locale(""));
19  std::cin.imbue(std::locale());
20  std::cout.imbue(std::locale());
21  typedef currency<long,4> currency4;
22  typedef currency<long,2> currency2;
23  currency2 c1;
24  TEST(c1.value().value() == 0);
25  currency2 c2(1L);
26  TEST(c2.value().value() == 100);
27  currency2 c3(3, 1416);
28  TEST(c3.value().value() == 314);
29  currency2 c4(6, 275);
30  TEST(c4.value().value() == 628);
31  currency2 c5(5, 279);
32  TEST(c5.value().value() == 528);
33  TEST(c3 + c3 == c1 + c4);
34  TEST(c3 + c3 <= c1 + c4);
35  TEST(c3 + c3 >= c1 + c4);
36  TEST(c1 < c2);
37  TEST(c1 <= c2);
38  TEST(c1 != c2);
39  TEST(c2 > c1);
40  TEST(c2 >= c1);
41  TEST(c2 != c1);
42  TEST(c2 / 2L == currency2(0, 50));
43 
44  TEST(c4 - c5 == c2);
45  TEST(c3 * 2L == c4);
46  TEST(c4 / 2L == c3);
47  c5 += c2;
48  TEST(c5 == c4);
49  c5 /= 2L;
50  TEST(c3 == c5);
51  c3 *= 2L;
52  TEST(c3 == c4);
53  c3 -= c5;
54  TEST(c3 == c5);
55 
56  TEST(-c4 == c1 - c4);
57  TEST(-(-c4) == c4);
58  TEST(c3 + c5 == --c4 + c2);
59  TEST(c3 + c5 == c4 + c2);
60  TEST(c3 + c5 == c4-- + c2);
61  TEST(c3 + c5 == c4 + c2 + c2);
62  TEST(c3 + c5 == ++c4 + c2);
63  TEST(c3 + c5 == c4 + c2);
64  TEST(c3 + c5 == c4++ + c2);
65  TEST(c3 + c5 == c4);
66 
67  c2 *= 2L;
68  TEST(c4 / c2 == rational<long>(314, 100));
69  TEST((c4 /= 2L) == c5);
70  TEST(c4 == c5);
71 
72  TEST(c4.as_string() == "3.14");
73  TEST(c4.integer() == 3);
74  c4 += currency2(-1,8);
75  TEST((c4 == currency2(2.06)));
76  TEST(c4.integer() == 2);
77  TEST(c4.round() == 2);
78  c4 += c2 / 2L;
79  TEST(c4.round() == 3);
80 
81  TEST(c3.integer() == 3);
82  TEST((-c3).integer() == -3);
83  TEST(c3.fraction() == 14);
84  TEST((-c3).fraction() == 14);
85 
86  TEST(currency4(7,4999).round() == 7L);
87  TEST(currency4(7,5000).round() == 8L);
88  TEST(currency4(7,5001).round() == 8L);
89  TEST(currency4(7,4999).round() == 7L);
90  TEST(currency4(8,5000).round() == 8L);
91  TEST(currency4(8,5001).round() == 9L);
92 
93  TEST(currency4(123,2345500) == currency4(123,2346));
94  TEST(currency4(123,2345501) == currency4(123,2346));
95  TEST(currency4(123,2345499) == currency4(123,2345));
96  TEST(currency4(123,2346500) == currency4(123,2346));
97  TEST(currency4(123,2346501) == currency4(123,2347));
98  TEST(currency4(123,2346499) == currency4(123,2346));
99  TEST(currency4(123,2346400) == currency4(123,2346));
100  TEST(currency4(123,2346600) == currency4(123,2347));
101 
102  TEST(currency4(-3.14159265).value().value() == -31416L);
103  TEST(currency4(123,456789).value().value() == 1234568L);
104  TEST(currency4(123,4).value().value() == 1230004L);
105  TEST(currency4(-10,1111).value().value() == -101111L);
106 
107  std::ostringstream out;
108  c3 = currency2(3, 14);
109  out << std::showbase << c3 << " 3.14 " << currency2(-10,12) << " 3.00 421.40 end";
110  currency2 c6;
111  std::istringstream in(out.str());
112  TEST(in >> c6);
113  TEST(c6 == c3);
114  c6 = currency2();
115  TEST(in >> c6);
116  TEST(c6 == c3);
117  TEST(in >> c6);
118  TEST(c6.value() == -1012L);
119  TEST(in >> c6);
120  TEST(c6.value() == 300L);
121  TEST(in >> c6);
122  TEST(c6.value() == 42140L);
123  TEST(not (in >> c6));
124 
125  TEST(currency2(31.59265) == currency2(31, 59));
126  TEST(currency2(31.595) == currency2(31, 60));
127 
128  // Adjust the following test to match your native locale.
129  currency2 big(1234567, 89);
130  TEST(big.as_string() == "1,234,567.89");
131 
132  bool okay(false);
133  try {
134  currency2 c7(1, -1);
135  } catch (std::invalid_argument const& ex) {
136  okay = true;
137  } catch (std::exception const& ex) {
138  std::cerr << "wrong exception: " << ex.what() << '\n';
139  }
140  TEST(okay);
141 }
Represent a rational number (fraction) as a numerator and denominator.
Definition: rational.hpp:16
int main(int argc, char **argv)
Definition: test.cpp:13