preprocessor_ut.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #include <library/cpp/testing/gtest/gtest.h>
  2. #include <library/cpp/yt/misc/preprocessor.h>
  3. namespace NYT {
  4. namespace {
  5. ////////////////////////////////////////////////////////////////////////////////
  6. TEST(TPreprocessorTest, Concatenation)
  7. {
  8. EXPECT_EQ(12, PP_CONCAT(1, 2));
  9. EXPECT_STREQ("FooBar", PP_STRINGIZE(PP_CONCAT(Foo, Bar)));
  10. }
  11. TEST(TPreprocessorTest, Stringize)
  12. {
  13. EXPECT_STREQ(PP_STRINGIZE(123456), "123456");
  14. EXPECT_STREQ(PP_STRINGIZE(FooBar), "FooBar");
  15. EXPECT_STREQ(PP_STRINGIZE(T::XYZ), "T::XYZ");
  16. }
  17. TEST(TPreprocessorTest, Count)
  18. {
  19. EXPECT_EQ(0, PP_COUNT());
  20. EXPECT_EQ(1, PP_COUNT((0)));
  21. EXPECT_EQ(2, PP_COUNT((0)(0)));
  22. EXPECT_EQ(3, PP_COUNT((0)(0)(0)));
  23. EXPECT_EQ(4, PP_COUNT((0)(0)(0)(0)));
  24. EXPECT_EQ(5, PP_COUNT((0)(0)(0)(0)(0)));
  25. }
  26. TEST(TPreprocessorTest, Kill)
  27. {
  28. EXPECT_STREQ("PP_NIL (0)", PP_STRINGIZE(PP_NIL PP_KILL((0), 0)));
  29. EXPECT_STREQ("PP_NIL", PP_STRINGIZE(PP_NIL PP_KILL((0), 1)));
  30. EXPECT_STREQ("PP_NIL (0)(1)(2)", PP_STRINGIZE(PP_NIL PP_KILL((0)(1)(2), 0)));
  31. EXPECT_STREQ("PP_NIL (1)(2)", PP_STRINGIZE(PP_NIL PP_KILL((0)(1)(2), 1)));
  32. EXPECT_STREQ("PP_NIL (2)", PP_STRINGIZE(PP_NIL PP_KILL((0)(1)(2), 2)));
  33. EXPECT_STREQ("PP_NIL", PP_STRINGIZE(PP_NIL PP_KILL((0)(1)(2), 3)));
  34. }
  35. TEST(TPreprocessorTest, Head)
  36. {
  37. EXPECT_STREQ("0", PP_STRINGIZE(PP_HEAD((0))));
  38. EXPECT_STREQ("0", PP_STRINGIZE(PP_HEAD((0)(1))));
  39. EXPECT_STREQ("0", PP_STRINGIZE(PP_HEAD((0)(1)(2))));
  40. }
  41. TEST(TPreprocessorTest, Tail)
  42. {
  43. EXPECT_STREQ("PP_NIL", PP_STRINGIZE(PP_NIL PP_TAIL((0))));
  44. EXPECT_STREQ("PP_NIL (1)", PP_STRINGIZE(PP_NIL PP_TAIL((0)(1))));
  45. EXPECT_STREQ("PP_NIL (1)(2)", PP_STRINGIZE(PP_NIL PP_TAIL((0)(1)(2))));
  46. }
  47. TEST(TPreprocessorTest, ForEach)
  48. {
  49. EXPECT_STREQ(
  50. "\"Foo\" \"Bar\" \"Spam\" \"Ham\"",
  51. PP_STRINGIZE(PP_FOR_EACH(PP_STRINGIZE, (Foo)(Bar)(Spam)(Ham)))
  52. );
  53. #define my_functor(x) +x+
  54. EXPECT_STREQ(
  55. "+1+ +2+ +3+",
  56. PP_STRINGIZE(PP_FOR_EACH(my_functor, (1)(2)(3)))
  57. );
  58. #undef my_functor
  59. }
  60. TEST(TPreprocessorTest, MakeSingleton)
  61. {
  62. EXPECT_EQ(1, PP_ELEMENT((1), 0));
  63. EXPECT_EQ(1, PP_ELEMENT((1)(2), 0));
  64. EXPECT_EQ(2, PP_ELEMENT((1)(2), 1));
  65. EXPECT_EQ(1, PP_ELEMENT((1)(2)(3), 0));
  66. EXPECT_EQ(2, PP_ELEMENT((1)(2)(3), 1));
  67. EXPECT_EQ(3, PP_ELEMENT((1)(2)(3), 2));
  68. EXPECT_EQ(1, PP_ELEMENT((1)(2)(3)(4), 0));
  69. EXPECT_EQ(2, PP_ELEMENT((1)(2)(3)(4), 1));
  70. EXPECT_EQ(3, PP_ELEMENT((1)(2)(3)(4), 2));
  71. EXPECT_EQ(4, PP_ELEMENT((1)(2)(3)(4), 3));
  72. }
  73. TEST(TPreprocessorTest, Conditional)
  74. {
  75. EXPECT_EQ(1, PP_IF(PP_TRUE, 1, 2));
  76. EXPECT_EQ(2, PP_IF(PP_FALSE, 1, 2));
  77. }
  78. TEST(TPreprocessorTest, IsSequence)
  79. {
  80. EXPECT_STREQ("PP_FALSE", PP_STRINGIZE(PP_IS_SEQUENCE( 0 )));
  81. EXPECT_STREQ("PP_TRUE", PP_STRINGIZE(PP_IS_SEQUENCE((0) )));
  82. EXPECT_STREQ("PP_TRUE", PP_STRINGIZE(PP_IS_SEQUENCE((0)(0))));
  83. EXPECT_STREQ("PP_FALSE", PP_STRINGIZE(PP_IS_SEQUENCE(PP_NIL)));
  84. }
  85. TEST(TPreprocessorTest, Deparen)
  86. {
  87. EXPECT_STREQ("a", PP_STRINGIZE(PP_DEPAREN(a)));
  88. EXPECT_STREQ("a", PP_STRINGIZE(PP_DEPAREN((a))));
  89. EXPECT_STREQ("( a, b)", PP_STRINGIZE((PP_DEPAREN(a, b))));
  90. EXPECT_STREQ("( a, b)", PP_STRINGIZE((PP_DEPAREN((a, b)))));
  91. }
  92. ////////////////////////////////////////////////////////////////////////////////
  93. } // namespace
  94. } // namespace NYT