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