1 #include "test_config.h"
2 #include "ace/CDR_Base.h"
3 #include "ace/OS_NS_string.h"
5 #ifndef ACE_LACKS_IOSTREAM_TOTALLY
9 #define TEST_EQUAL(LHS, RHS) if (!((LHS) == (RHS))) { \
11 ACE_ERROR ((LM_ERROR, "Test assertion FAILED " #LHS " != " #RHS \
18 void log (ACE_OSTREAM_TYPE
*os
, const char *label
, const char *expected
,
19 const ACE_CDR::Fixed
&f
)
21 #ifdef ACE_LACKS_IOSTREAM_TOTALLY
22 ACE_OS::fprintf (os
, "%s: expected %s got ", label
, expected
);
24 *os
<< label
<< ": expected " << expected
<< " got ";
27 #ifdef ACE_LACKS_IOSTREAM_TOTALLY
28 ACE_OS::fputc ('\n', os
);
35 #define EXPECT(STR, OBJ) \
37 char buffer[Fixed::MAX_STRING_SIZE]; \
38 OBJ.to_string (buffer, sizeof buffer); \
39 if (ACE_OS::strcmp (STR, buffer)) { \
41 ACE_ERROR ((LM_ERROR, "FAILED conversion to string at line %l\n")); \
42 log (os, #OBJ, STR, OBJ); \
46 int run_main (int, ACE_TCHAR
*[])
48 ACE_START_TEST (ACE_TEXT ("CDR_Fixed_Test"));
50 using Fixed
= ACE_CDR::Fixed
;
51 using LongLong
= ACE_CDR::LongLong
;
52 using ULongLong
= ACE_CDR::ULongLong
;
53 using LongDouble
= ACE_CDR::LongDouble
;
55 const Fixed f1
= Fixed::from_integer (LongLong (-1234567890l)),
56 f2
= Fixed::from_integer (ULongLong (987654321ul)),
57 f3
= Fixed::from_string ("612578912487901265.90125789");
59 TEST_EQUAL (LongLong (-1234567890l), LongLong (f1
));
60 TEST_EQUAL (LongLong (987654321), LongLong (f2
));
61 TEST_EQUAL (LongLong (612578912487901265ull), LongLong (f3
));
63 TEST_EQUAL (0, f1
.fixed_scale ());
64 TEST_EQUAL (10, f1
.fixed_digits ());
65 TEST_EQUAL (0, f2
.fixed_scale ());
66 TEST_EQUAL (9, f2
.fixed_digits ());
67 TEST_EQUAL (8, f3
.fixed_scale ());
68 TEST_EQUAL (26, f3
.fixed_digits ());
70 ACE_OSTREAM_TYPE
*const os
= ace_file_stream::instance ()->output_file ();
72 EXPECT ("-1234567890", f1
);
73 EXPECT ("987654321", f2
);
74 EXPECT ("612578912487901265.90125789", f3
);
76 Fixed f1_scaled
= f1
/ Fixed::from_integer (LongLong (100));
77 const Fixed f4
= Fixed::from_string ("-12345678.9");
78 TEST_EQUAL (f1_scaled
, f4
);
80 EXPECT ("-12345678.9", f4
);
82 const Fixed f5
= Fixed::from_string ("0.00000000"),
83 f6
= Fixed::from_string ("0.000000000");
84 EXPECT ("0.00000000", f5
);
85 EXPECT ("0.000000000", f6
);
86 TEST_EQUAL (8, f5
.fixed_scale ()); // these are not "normalized" since the
87 TEST_EQUAL (9, f6
.fixed_scale ()); // caller explicitly requested this scale
89 Fixed f7
= Fixed::from_string ("0.1");
90 EXPECT ("0", f7
.round (0));
91 EXPECT ("0", f7
.truncate (0));
93 f7
= Fixed::from_string ("0.05");
94 EXPECT ("0.1", f7
.round (1));
95 EXPECT ("0.0", f7
.truncate (1));
97 f7
= Fixed::from_string ("-0.005");
98 EXPECT ("-0.01", f7
.round (2));
99 EXPECT ("0.00", f7
.truncate (2));
102 ACE_CDR_LONG_DOUBLE_ASSIGNMENT (ld1
, 1234.45);
103 const Fixed f8
= Fixed::from_floating (ld1
);
104 TEST_EQUAL (LongLong (1234), LongLong (f8
));
107 ACE_CDR_LONG_DOUBLE_ASSIGNMENT (ld2
, -0.3125);
108 const Fixed f9
= Fixed::from_floating (ld2
);
109 EXPECT ("-0.3125", f9
);
112 ACE_CDR_LONG_DOUBLE_ASSIGNMENT (ld3
, 0.125);
113 const Fixed f10
= Fixed::from_floating (ld3
);
114 EXPECT ("0.125", f10
);
116 LongDouble ldzero
= ACE_CDR_LONG_DOUBLE_INITIALIZER
;
117 const Fixed fzero
= Fixed::from_floating (ldzero
);
119 ACE_CDR_LONG_DOUBLE_ASSIGNMENT(ldzero
, 0);
120 const Fixed fzero2
= Fixed::from_floating(ldzero
);
123 Fixed f11
= Fixed::from_integer (LongLong (-1l));
124 TEST_EQUAL (LongLong (-1), LongLong (f11
));
125 TEST_EQUAL (false, !f11
);
127 TEST_EQUAL (true, !f11
);
128 TEST_EQUAL (LongLong (0), LongLong (f11
));
129 TEST_EQUAL (Fixed::from_integer (), f11
);
130 f11
+= Fixed::from_string ("0.124357891");
132 TEST_EQUAL (Fixed::from_string ("1.124357891"), f11
);
135 TEST_EQUAL (Fixed::from_string ("-0.875642109"), f11
);
137 TEST_EQUAL (true, Fixed::from_integer (LongLong (-4)) < Fixed::from_integer (LongLong (2)));
138 TEST_EQUAL (true, Fixed::from_integer (LongLong (-4)) < Fixed::from_integer (LongLong (-2)));
139 TEST_EQUAL (false, Fixed::from_integer (LongLong (4)) < Fixed::from_integer (LongLong (-2)));
140 TEST_EQUAL (false, Fixed::from_integer (LongLong (4)) < Fixed::from_integer (LongLong (2)));
141 TEST_EQUAL (true, Fixed::from_string ("2.17") < Fixed::from_string ("3.142"));
142 TEST_EQUAL (true, Fixed::from_string ("10.1") < Fixed::from_string ("100"));
144 TEST_EQUAL (Fixed::from_string ("0000.000"), f5
);
145 TEST_EQUAL (Fixed::from_string ("-0.00500"), f7
);
146 TEST_EQUAL (f11
, f11
);
149 TEST_EQUAL (++f12
, f11
);
152 TEST_EQUAL (--f13
, f12
);
154 Fixed f14
= Fixed::from_integer (LongLong (9));
155 TEST_EQUAL (1, f14
.fixed_digits ());
157 TEST_EQUAL (Fixed::from_integer (LongLong (10)), f14
);
159 TEST_EQUAL (Fixed::from_string ("778.33"),
160 Fixed::from_string ("12.9") + Fixed::from_string ("765.43"));
162 Fixed full
= Fixed::from_string ("9876543210987654321098765432109"),
164 full
+= Fixed::from_string ("0.99");
165 TEST_EQUAL (before
, full
); // no rounding
168 const Fixed f15
= Fixed::from_string ("9876543210987654321098765432119");
169 TEST_EQUAL (f15
, f14
);
171 Fixed f16
= Fixed::from_string ("123.4567890123456789012345678901");
172 f16
+= Fixed::from_integer (LongLong (9876));
173 const Fixed f17
= Fixed::from_string ("9999.456789012345678901234567890");
174 TEST_EQUAL (f17
, f16
);
176 Fixed f18
= Fixed::from_string ("197854191");
177 f18
-= Fixed::from_string ("123546789");
178 EXPECT ("74307402", f18
);
179 TEST_EQUAL (8, f18
.fixed_digits ());
181 Fixed f19
= Fixed::from_integer (LongLong (9));
182 f19
-= Fixed::from_integer (LongLong (10));
185 Fixed f20
= Fixed::from_integer (LongLong (99));
186 f20
+= Fixed::from_integer (LongLong (99));
187 EXPECT ("198", f20
); // carry extra digit
189 Fixed f21
= Fixed::from_string ("7.532");
190 f21
-= Fixed::from_string ("4.91");
191 EXPECT ("2.622", f21
);
193 Fixed f22
= Fixed::from_integer (LongLong (-99)) * Fixed::from_integer (LongLong (-9));
196 Fixed f23
= Fixed::from_integer (LongLong (9)) * Fixed::from_integer (LongLong (-99));
197 EXPECT ("-891", f23
);
199 Fixed f24
= Fixed::from_string ("-3.4") * Fixed::from_string ("5.67");
200 EXPECT ("-19.278", f24
);
202 Fixed f25
= Fixed::from_string ("9999999999.1234567890") // 10.10
203 * Fixed::from_string ("876543219087654321.9876543210");// 18.10
204 EXPECT ("8765432190108215212037174200.146", f25
); // 28.3
206 // 1234567890123456789012345678901
207 Fixed f26
= Fixed::from_string ("0.0000000000000000000000000000001")
208 * Fixed::from_string ("0.1");
209 EXPECT ( "0.0000000000000000000000000000000", f26
);
211 Fixed f27
= Fixed::from_string ("817459124");
212 f27
/= Fixed::from_string ("0.001");
213 EXPECT ("817459124000", f27
);
214 f27
/= Fixed::from_integer (LongLong (1000));
215 EXPECT ("817459124", f27
);
216 f27
/= Fixed::from_integer (LongLong (-2));
217 EXPECT ("-408729562", f27
);
219 Fixed f28
= Fixed::from_string ("200000000");
220 f28
/= Fixed::from_string ("500");
221 EXPECT ("400000", f28
);
222 TEST_EQUAL (6, f28
.fixed_digits ());
224 Fixed f29
= Fixed::from_integer (LongLong (1)) / Fixed::from_integer (LongLong (-3));
225 EXPECT ("-0.333333333333333333333333333333", f29
); //TODO: one more 3
227 Fixed f30
= Fixed::from_string("-9999752.0000") / Fixed::from_string("-4999876.00");