00001
00003 #include <iostream>
00004 #include <istream>
00005 #include <locale>
00006 #include <ostream>
00007 #include <sstream>
00008 #include <stdexcept>
00009
00010 #include "test.hpp"
00011 #include "currency.hpp"
00012
00013 int main(int argc, char** argv)
00014 {
00015 if (argc >= 2)
00016 std::locale::global(std::locale(argv[1]));
00017 else
00018 std::locale::global(std::locale(""));
00019 std::cin.imbue(std::locale());
00020 std::cout.imbue(std::locale());
00021 typedef currency<long,4> currency4;
00022 typedef currency<long,2> currency2;
00023 currency2 c1;
00024 TEST(c1.value().value() == 0);
00025 currency2 c2(1L);
00026 TEST(c2.value().value() == 100);
00027 currency2 c3(3, 1416);
00028 TEST(c3.value().value() == 314);
00029 currency2 c4(6, 275);
00030 TEST(c4.value().value() == 628);
00031 currency2 c5(5, 279);
00032 TEST(c5.value().value() == 528);
00033 TEST(c3 + c3 == c1 + c4);
00034 TEST(c3 + c3 <= c1 + c4);
00035 TEST(c3 + c3 >= c1 + c4);
00036 TEST(c1 < c2);
00037 TEST(c1 <= c2);
00038 TEST(c1 != c2);
00039 TEST(c2 > c1);
00040 TEST(c2 >= c1);
00041 TEST(c2 != c1);
00042 TEST(c2 / 2L == currency2(0, 50));
00043
00044 TEST(c4 - c5 == c2);
00045 TEST(c3 * 2L == c4);
00046 TEST(c4 / 2L == c3);
00047 c5 += c2;
00048 TEST(c5 == c4);
00049 c5 /= 2L;
00050 TEST(c3 == c5);
00051 c3 *= 2L;
00052 TEST(c3 == c4);
00053 c3 -= c5;
00054 TEST(c3 == c5);
00055
00056 TEST(-c4 == c1 - c4);
00057 TEST(-(-c4) == c4);
00058 TEST(c3 + c5 == --c4 + c2);
00059 TEST(c3 + c5 == c4 + c2);
00060 TEST(c3 + c5 == c4-- + c2);
00061 TEST(c3 + c5 == c4 + c2 + c2);
00062 TEST(c3 + c5 == ++c4 + c2);
00063 TEST(c3 + c5 == c4 + c2);
00064 TEST(c3 + c5 == c4++ + c2);
00065 TEST(c3 + c5 == c4);
00066
00067 c2 *= 2L;
00068 TEST(c4 / c2 == rational<long>(314, 100));
00069 TEST((c4 /= 2L) == c5);
00070 TEST(c4 == c5);
00071
00072 TEST(c4.as_string() == "3.14");
00073 TEST(c4.integer() == 3);
00074 c4 += currency2(-1,8);
00075 TEST((c4 == currency2(2.06)));
00076 TEST(c4.integer() == 2);
00077 TEST(c4.round() == 2);
00078 c4 += c2 / 2L;
00079 TEST(c4.round() == 3);
00080
00081 TEST(c3.integer() == 3);
00082 TEST((-c3).integer() == -3);
00083 TEST(c3.fraction() == 14);
00084 TEST((-c3).fraction() == 14);
00085
00086 TEST(currency4(7,4999).round() == 7L);
00087 TEST(currency4(7,5000).round() == 8L);
00088 TEST(currency4(7,5001).round() == 8L);
00089 TEST(currency4(7,4999).round() == 7L);
00090 TEST(currency4(8,5000).round() == 8L);
00091 TEST(currency4(8,5001).round() == 9L);
00092
00093 TEST(currency4(123,2345500) == currency4(123,2346));
00094 TEST(currency4(123,2345501) == currency4(123,2346));
00095 TEST(currency4(123,2345499) == currency4(123,2345));
00096 TEST(currency4(123,2346500) == currency4(123,2346));
00097 TEST(currency4(123,2346501) == currency4(123,2347));
00098 TEST(currency4(123,2346499) == currency4(123,2346));
00099 TEST(currency4(123,2346400) == currency4(123,2346));
00100 TEST(currency4(123,2346600) == currency4(123,2347));
00101
00102 TEST(currency4(-3.14159265).value().value() == -31416L);
00103 TEST(currency4(123,456789).value().value() == 1234568L);
00104 TEST(currency4(123,4).value().value() == 1230004L);
00105 TEST(currency4(-10,1111).value().value() == -101111L);
00106
00107 std::ostringstream out;
00108 c3 = currency2(3, 14);
00109 out << std::showbase << c3 << " 3.14 " << currency2(-10,12) << " 3.00 421.40 end";
00110 currency2 c6;
00111 std::istringstream in(out.str());
00112 TEST(in >> c6);
00113 TEST(c6 == c3);
00114 c6 = currency2();
00115 TEST(in >> c6);
00116 TEST(c6 == c3);
00117 TEST(in >> c6);
00118 TEST(c6.value() == -1012L);
00119 TEST(in >> c6);
00120 TEST(c6.value() == 300L);
00121 TEST(in >> c6);
00122 TEST(c6.value() == 42140L);
00123 TEST(not (in >> c6));
00124
00125 TEST(currency2(31.59265) == currency2(31, 59));
00126 TEST(currency2(31.595) == currency2(31, 60));
00127
00128
00129 currency2 big(1234567, 89);
00130 TEST(big.as_string() == "1,234,567.89");
00131
00132 bool okay(false);
00133 try {
00134 currency2 c7(1, -1);
00135 } catch (std::invalid_argument const& ex) {
00136 okay = true;
00137 } catch (std::exception const& ex) {
00138 std::cerr << "wrong exception: " << ex.what() << '\n';
00139 }
00140 TEST(okay);
00141 }