FixedPointString.cpp 3.1 KB

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