1 | #include "clang/AST/Type.h" |
2 | #include "llvm/ADT/APSInt.h" |
3 | #include "llvm/ADT/SmallString.h" |
4 | #include "gtest/gtest.h" |
5 | |
6 | using clang::FixedPointValueToString; |
7 | using llvm::APSInt; |
8 | using llvm::SmallString; |
9 | |
10 | namespace { |
11 | |
12 | TEST(FixedPointString, DifferentTypes) { |
13 | SmallString<64> S; |
14 | FixedPointValueToString(Str&: S, Val: APSInt::get(X: 320), Scale: 7); |
15 | ASSERT_STREQ(S.c_str(), "2.5" ); |
16 | |
17 | S.clear(); |
18 | FixedPointValueToString(Str&: S, Val: APSInt::get(X: 0), Scale: 7); |
19 | ASSERT_STREQ(S.c_str(), "0.0" ); |
20 | |
21 | // signed short _Accum |
22 | S.clear(); |
23 | FixedPointValueToString(Str&: S, Val: APSInt::getMaxValue(numBits: 16, /*Unsigned=*/false), Scale: 7); |
24 | ASSERT_STREQ(S.c_str(), "255.9921875" ); |
25 | |
26 | // signed _Accum |
27 | S.clear(); |
28 | FixedPointValueToString(Str&: S, Val: APSInt::getMaxValue(numBits: 32, /*Unsigned=*/false), Scale: 15); |
29 | ASSERT_STREQ(S.c_str(), "65535.999969482421875" ); |
30 | |
31 | // signed long _Accum |
32 | S.clear(); |
33 | FixedPointValueToString(Str&: S, Val: APSInt::getMaxValue(numBits: 64, /*Unsigned=*/false), Scale: 31); |
34 | ASSERT_STREQ(S.c_str(), "4294967295.9999999995343387126922607421875" ); |
35 | |
36 | // unsigned short _Accum |
37 | S.clear(); |
38 | FixedPointValueToString(Str&: S, Val: APSInt::getMaxValue(numBits: 16, /*Unsigned=*/true), Scale: 8); |
39 | ASSERT_STREQ(S.c_str(), "255.99609375" ); |
40 | |
41 | // unsigned _Accum |
42 | S.clear(); |
43 | FixedPointValueToString(Str&: S, Val: APSInt::getMaxValue(numBits: 32, /*Unsigned=*/true), Scale: 16); |
44 | ASSERT_STREQ(S.c_str(), "65535.9999847412109375" ); |
45 | |
46 | // unsigned long _Accum |
47 | S.clear(); |
48 | FixedPointValueToString(Str&: S, Val: APSInt::getMaxValue(numBits: 64, /*Unsigned=*/true), Scale: 32); |
49 | ASSERT_STREQ(S.c_str(), "4294967295.99999999976716935634613037109375" ); |
50 | |
51 | // signed short _Fract |
52 | S.clear(); |
53 | FixedPointValueToString(Str&: S, Val: APSInt::getMaxValue(numBits: 8, /*Unsigned=*/false), Scale: 7); |
54 | ASSERT_STREQ(S.c_str(), "0.9921875" ); |
55 | |
56 | // signed _Fract |
57 | S.clear(); |
58 | FixedPointValueToString(Str&: S, Val: APSInt::getMaxValue(numBits: 16, /*Unsigned=*/false), Scale: 15); |
59 | ASSERT_STREQ(S.c_str(), "0.999969482421875" ); |
60 | |
61 | // signed long _Fract |
62 | S.clear(); |
63 | FixedPointValueToString(Str&: S, Val: APSInt::getMaxValue(numBits: 32, /*Unsigned=*/false), Scale: 31); |
64 | ASSERT_STREQ(S.c_str(), "0.9999999995343387126922607421875" ); |
65 | |
66 | // unsigned short _Fract |
67 | S.clear(); |
68 | FixedPointValueToString(Str&: S, Val: APSInt::getMaxValue(numBits: 8, /*Unsigned=*/true), Scale: 8); |
69 | ASSERT_STREQ(S.c_str(), "0.99609375" ); |
70 | |
71 | // unsigned _Fract |
72 | S.clear(); |
73 | FixedPointValueToString(Str&: S, Val: APSInt::getMaxValue(numBits: 16, /*Unsigned=*/true), Scale: 16); |
74 | ASSERT_STREQ(S.c_str(), "0.9999847412109375" ); |
75 | |
76 | // unsigned long _Fract |
77 | S.clear(); |
78 | FixedPointValueToString(Str&: S, Val: APSInt::getMaxValue(numBits: 32, /*Unsigned=*/true), Scale: 32); |
79 | ASSERT_STREQ(S.c_str(), "0.99999999976716935634613037109375" ); |
80 | } |
81 | |
82 | TEST(FixedPointString, Negative) { |
83 | SmallString<64> S; |
84 | FixedPointValueToString(Str&: S, Val: APSInt::get(X: -320), Scale: 7); |
85 | ASSERT_STREQ(S.c_str(), "-2.5" ); |
86 | |
87 | S.clear(); |
88 | FixedPointValueToString(Str&: S, Val: APSInt::get(X: -64), Scale: 7); |
89 | ASSERT_STREQ(S.c_str(), "-0.5" ); |
90 | |
91 | // signed short _Accum |
92 | S.clear(); |
93 | FixedPointValueToString(Str&: S, Val: APSInt::getMinValue(numBits: 16, /*Unsigned=*/false), Scale: 7); |
94 | ASSERT_STREQ(S.c_str(), "-256.0" ); |
95 | |
96 | // signed _Accum |
97 | S.clear(); |
98 | FixedPointValueToString(Str&: S, Val: APSInt::getMinValue(numBits: 32, /*Unsigned=*/false), Scale: 15); |
99 | ASSERT_STREQ(S.c_str(), "-65536.0" ); |
100 | |
101 | // signed long _Accum |
102 | S.clear(); |
103 | FixedPointValueToString(Str&: S, Val: APSInt::getMinValue(numBits: 64, /*Unsigned=*/false), Scale: 31); |
104 | ASSERT_STREQ(S.c_str(), "-4294967296.0" ); |
105 | } |
106 | |
107 | } // namespace |
108 | |