Project 2 - Fixed-Point Number Class

- Main Page
- Classes
- Files

- File List
- File Members

list4701.cpp
1 // Listing 47-1. Testing the fixed Class
2 #include <iostream>
3 #include <sstream>
4 #include <stdexcept>
5
6 #include "test.hpp"
7 #include "fixed.hpp"
8
9 int main()
10 {
11 fixed f1{};
12 test_equal(f1.value(), 0);
13 fixed f2{1};
14 test_equal(f2.value(), 10000);
15 fixed f3{3, 14162};
16 test_equal(f3.value(), 31416);
17 fixed f4{2, 14159265};
18 test_equal(f4.value(), 21416);
19 test_equal(f2 + f4, f1 + f3);
20 test(f2 + f4 <= f1 + f3);
21 test(f2 + f4 >= f1 + f3);
22 test(f1 < f2);
23 test(f1 <= f2);
24 test(f1 != f2);
25 test(f2 > f1);
26 test(f2 >= f1);
27 test(f2 != f1);
28
29 test_equal(f2 + f4, f3 - f1);
30 test_equal(f2 * f3, f3);
31 test_equal(f3 / f2, f3);
32 f4 += f2;
33 test_equal(f3, f4);
34 f4 -= f1;
35 test_equal(f3, f4);
36 f4 *= f2;
37 test_equal(f3, f4);
38 f4 /= f2;
39 test_equal(f3, f4);
40
41 test_equal(-f4, f1 - f4);
42 test_equal(-(-f4), f4);
43 --f4;
44 test_equal(f4 + 1, f3);
45 f4--;
46 test_equal(f4 + 2, f3);
47 ++f4;
48 test_equal(f4 + 1, f3);
49 f4++;
50 test_equal(f4, f3);
51 ++f3;
52 test_equal(++f4, f3);
53 test_equal(f4--, f3);
54 test_equal(f4++, --f3);
55 test_equal(--f4, f3);
56
57 test_equal(f4 / f3, f2);
58 test_equal(f4 - f3, f1);
59
60 test_equal(f4.to_string(), "3.1416");
61 test_equal(f4.integer(), 3);
62 f4 += fixed{0,4584};
63 test_equal(f4, 3.6);
64 test_equal(f4.integer(), 3);
65 test_equal(f4.round(), 4);
66
67 test_equal(f3.integer(), 3);
68 test_equal((-f3).integer(), -3);
69 test_equal(f3.fraction(), 1416);
70 test_equal((-f3).fraction(), 1416);
71
72 test_equal(fixed{7,4999}.round(), 7);
73 test_equal(fixed{7,5000}.round(), 8);
74 test_equal(fixed{7,5001}.round(), 8);
75 test_equal(fixed{7,4999}.round(), 7);
76 test_equal(fixed{8,5000}.round(), 8);
77 test_equal(fixed{8,5001}.round(), 9);
78
79 test_equal(fixed{123,2345500}, fixed(123,2346));
80 test_equal(fixed{123,2345501}, fixed(123,2346));
81 test_equal(fixed{123,2345499}, fixed(123,2345));
82 test_equal(fixed{123,2346500}, fixed(123,2346));
83 test_equal(fixed{123,2346501}, fixed(123,2347));
84 test_equal(fixed{123,2346499}, fixed(123,2346));
85 test_equal(fixed{123,2346400}, fixed(123,2346));
86 test_equal(fixed{123,2346600}, fixed(123,2347));
87
88 test_equal(fixed{-7,4999}.round(), -7);
89 test_equal(fixed{-7,5000}.round(), -8);
90 test_equal(fixed{-7,5001}.round(), -8);
91 test_equal(fixed{-7,4999}.round(), -7);
92 test_equal(fixed{-8,5000}.round(), -8);
93 test_equal(fixed{-8,5001}.round(), -9);
94
95 test_equal(fixed{-3.14159265}.value(), -31416);
96 test_equal(fixed{123,456789}.value(), 1234568);
97 test_equal(fixed{123,4}.value(), 1230004);
98 test_equal(fixed{-10,1111}.value(), -101111);
99
100 std::ostringstream out{};
101 out << f3 << " 3.14159265 " << fixed(-10,12) << " 3 421.4 end";
102 fixed f5{};
103 std::istringstream in{out.str()};
104 test(in >> f5);
105 test_equal(f5, f3);
106 test(in >> f5);
107 test_equal(f5, f3);
108 test(in >> f5);
109 test_equal(f5.value(), -100012);
110 test(in >> f5);
111 test_equal(f5.value(), 30000);
112 test(in >> f5);
113 test_equal(f5.value(), 4214000);
114 test(not (in >> f5));
115
116 test_equal(fixed{31.4159265}, fixed{31, 4159});
117 test_equal(fixed{31.41595}, fixed{31, 4160});
118
119 bool okay{false};
120 try {
121 fixed f6{1, -1};
122 } catch (std::invalid_argument const& ex) {
123 okay = true;
124 } catch (...) {
125 }
126 test(okay);
127 }
fixed
Implement a fixed-point number class. Values have places places after the decimal point...
Definition: fixed.hpp:37
fixed::value
value_type value() const
Return the internal value.
Definition: fixed.hpp:107
main
int main()
Definition: test.cpp:12
fixed.hpp
Fixed-point numbers.
fixed::round
value_type round() const
Definition: fixed.cpp:104