join_ut.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #include "join.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. #include <util/generic/vector.h>
  4. #include <util/stream/output.h>
  5. struct TCustomData {
  6. TVector<int> Ints;
  7. };
  8. TString ToString(const TCustomData& d) {
  9. return JoinSeq("__", d.Ints);
  10. }
  11. Y_UNIT_TEST_SUITE(JoinStringTest) {
  12. Y_UNIT_TEST(ScalarItems) {
  13. UNIT_ASSERT_EQUAL(Join(',', 10, 11.1, "foobar"), "10,11.1,foobar");
  14. UNIT_ASSERT_EQUAL(Join(", ", 10, 11.1, "foobar"), "10, 11.1, foobar");
  15. UNIT_ASSERT_EQUAL(Join(", ", 10, 11.1, TString("foobar")), "10, 11.1, foobar");
  16. UNIT_ASSERT_EQUAL(Join('#', 0, "a", "foobar", -1.4, TStringBuf("aaa")), "0#a#foobar#-1.4#aaa");
  17. UNIT_ASSERT_EQUAL(Join("", "", ""), "");
  18. UNIT_ASSERT_EQUAL(Join("", "a", "b", "c"), "abc");
  19. UNIT_ASSERT_EQUAL(Join("", "a", "b", "", "c"), "abc");
  20. UNIT_ASSERT_EQUAL(Join(" ", "a", "b", "", "c"), "a b c");
  21. }
  22. Y_UNIT_TEST(IntContainerItems) {
  23. int v[] = {1, 2, 3};
  24. TVector<int> vv(v, v + 3);
  25. UNIT_ASSERT_EQUAL(JoinSeq(" ", vv), "1 2 3");
  26. UNIT_ASSERT_EQUAL(JoinSeq(" ", vv), JoinRange(" ", vv.begin(), vv.end()));
  27. UNIT_ASSERT_EQUAL(JoinRange(" ", v, v + 2), "1 2");
  28. UNIT_ASSERT_EQUAL(JoinSeq(" ", {}), "");
  29. UNIT_ASSERT_EQUAL(JoinSeq(" ", {42}), "42");
  30. UNIT_ASSERT_EQUAL(JoinSeq(" ", {1, 2, 3}), "1 2 3");
  31. UNIT_ASSERT_VALUES_EQUAL(JoinSeq(" ", v), "1 2 3");
  32. }
  33. Y_UNIT_TEST(StrContainerItems) {
  34. // try various overloads and template type arguments
  35. static const char* const result = "1 22 333";
  36. static const char* const v[] = {"1", "22", "333"};
  37. TVector<const char*> vchar(v, v + sizeof(v) / sizeof(v[0]));
  38. TVector<TStringBuf> vbuf(v, v + sizeof(v) / sizeof(v[0]));
  39. TVector<TString> vstring(v, v + sizeof(v) / sizeof(v[0]));
  40. // ranges
  41. UNIT_ASSERT_EQUAL(JoinRange(" ", v, v + 3), result);
  42. UNIT_ASSERT_EQUAL(JoinRange(" ", vchar.begin(), vchar.end()), result);
  43. UNIT_ASSERT_EQUAL(JoinRange(" ", vbuf.begin(), vbuf.end()), result);
  44. UNIT_ASSERT_EQUAL(JoinRange(" ", vstring.begin(), vstring.end()), result);
  45. {
  46. TStringStream stream;
  47. stream << MakeRangeJoiner(" ", v, v + 3);
  48. UNIT_ASSERT_EQUAL(stream.Str(), result);
  49. }
  50. {
  51. TStringStream stream;
  52. stream << MakeRangeJoiner(" ", vchar.begin(), vchar.end());
  53. UNIT_ASSERT_EQUAL(stream.Str(), result);
  54. }
  55. {
  56. TStringStream stream;
  57. stream << MakeRangeJoiner(" ", vbuf.begin(), vbuf.end());
  58. UNIT_ASSERT_EQUAL(stream.Str(), result);
  59. }
  60. {
  61. TStringStream stream;
  62. stream << MakeRangeJoiner(" ", vstring.begin(), vstring.end());
  63. UNIT_ASSERT_EQUAL(stream.Str(), result);
  64. }
  65. // vectors
  66. UNIT_ASSERT_EQUAL(JoinSeq(" ", vchar), result);
  67. UNIT_ASSERT_EQUAL(JoinSeq(" ", vbuf), result);
  68. UNIT_ASSERT_EQUAL(JoinSeq(" ", vstring), result);
  69. {
  70. TStringStream stream;
  71. stream << MakeRangeJoiner(" ", vchar);
  72. UNIT_ASSERT_EQUAL(stream.Str(), result);
  73. }
  74. {
  75. TStringStream stream;
  76. stream << MakeRangeJoiner(" ", vbuf);
  77. UNIT_ASSERT_EQUAL(stream.Str(), result);
  78. }
  79. {
  80. TStringStream stream;
  81. stream << MakeRangeJoiner(" ", vstring);
  82. UNIT_ASSERT_EQUAL(stream.Str(), result);
  83. }
  84. // initializer lists with type deduction
  85. UNIT_ASSERT_EQUAL(JoinSeq(" ", {v[0], v[1], v[2]}), result);
  86. UNIT_ASSERT_EQUAL(JoinSeq(" ", {vchar[0], vchar[1], vchar[2]}), result);
  87. UNIT_ASSERT_EQUAL(JoinSeq(" ", {vbuf[0], vbuf[1], vbuf[2]}), result);
  88. UNIT_ASSERT_EQUAL(JoinSeq(" ", {vstring[0], vstring[1], vstring[2]}), result);
  89. {
  90. TStringStream stream;
  91. stream << MakeRangeJoiner(" ", {v[0], v[1], v[2]});
  92. UNIT_ASSERT_EQUAL(stream.Str(), result);
  93. }
  94. {
  95. TStringStream stream;
  96. stream << MakeRangeJoiner(" ", {vchar[0], vchar[1], vchar[2]});
  97. UNIT_ASSERT_EQUAL(stream.Str(), result);
  98. }
  99. {
  100. TStringStream stream;
  101. stream << MakeRangeJoiner(" ", {vbuf[0], vbuf[1], vbuf[2]});
  102. UNIT_ASSERT_EQUAL(stream.Str(), result);
  103. }
  104. {
  105. TStringStream stream;
  106. stream << MakeRangeJoiner(" ", {vstring[0], vstring[1], vstring[2]});
  107. UNIT_ASSERT_EQUAL(stream.Str(), result);
  108. }
  109. // initializer lists with explicit types
  110. UNIT_ASSERT_EQUAL(JoinSeq(" ", std::initializer_list<const char*>{v[0], v[1], v[2]}), result);
  111. UNIT_ASSERT_EQUAL(JoinSeq(" ", std::initializer_list<const char*>{vchar[0], vchar[1], vchar[2]}), result);
  112. UNIT_ASSERT_EQUAL(JoinSeq(" ", std::initializer_list<TStringBuf>{vbuf[0], vbuf[1], vbuf[2]}), result);
  113. UNIT_ASSERT_EQUAL(JoinSeq(" ", std::initializer_list<TString>{vstring[0], vstring[1], vstring[2]}), result);
  114. {
  115. TStringStream stream;
  116. stream << MakeRangeJoiner(" ", std::initializer_list<const char*>{v[0], v[1], v[2]});
  117. UNIT_ASSERT_EQUAL(stream.Str(), result);
  118. }
  119. {
  120. TStringStream stream;
  121. stream << MakeRangeJoiner(" ", std::initializer_list<const char*>{vchar[0], vchar[1], vchar[2]});
  122. UNIT_ASSERT_EQUAL(stream.Str(), result);
  123. }
  124. {
  125. TStringStream stream;
  126. stream << MakeRangeJoiner(" ", std::initializer_list<TStringBuf>{vbuf[0], vbuf[1], vbuf[2]});
  127. UNIT_ASSERT_EQUAL(stream.Str(), result);
  128. }
  129. {
  130. TStringStream stream;
  131. stream << MakeRangeJoiner(" ", std::initializer_list<TString>{vstring[0], vstring[1], vstring[2]});
  132. UNIT_ASSERT_EQUAL(stream.Str(), result);
  133. }
  134. // c-style array
  135. UNIT_ASSERT_VALUES_EQUAL(JoinSeq(" ", v), result);
  136. }
  137. Y_UNIT_TEST(CustomToString) {
  138. TCustomData d1{{1, 2, 3, 4, 5}};
  139. TCustomData d2{{0, -1, -2}};
  140. UNIT_ASSERT_EQUAL(Join(" ", d1, d2), "1__2__3__4__5 0__-1__-2");
  141. }
  142. Y_UNIT_TEST(JoinChars) {
  143. // Note that char delimeter is printed as single char string,
  144. // but joined char values are printed as their numeric codes! O_o
  145. UNIT_ASSERT_EQUAL(Join('a', 'a', 'a'), "97a97");
  146. UNIT_ASSERT_EQUAL(Join("a", "a", "a"), "aaa");
  147. }
  148. } // Y_UNIT_TEST_SUITE(JoinStringTest)