VariadicFunctionTest.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //===----------- VariadicFunctionTest.cpp - VariadicFunction unit tests ---===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "gtest/gtest.h"
  10. #include "llvm/ADT/ArrayRef.h"
  11. #include "llvm/ADT/VariadicFunction.h"
  12. using namespace llvm;
  13. namespace {
  14. // Defines a variadic function StringCat() to join strings.
  15. // StringCat()'s arguments and return value have class types.
  16. std::string StringCatImpl(ArrayRef<const std::string *> Args) {
  17. std::string S;
  18. for (unsigned i = 0, e = Args.size(); i < e; ++i)
  19. S += *Args[i];
  20. return S;
  21. }
  22. const VariadicFunction<std::string, std::string, StringCatImpl> StringCat = {};
  23. TEST(VariadicFunctionTest, WorksForClassTypes) {
  24. EXPECT_EQ("", StringCat());
  25. EXPECT_EQ("a", StringCat("a"));
  26. EXPECT_EQ("abc", StringCat("a", "bc"));
  27. EXPECT_EQ("0123456789abcdefghijklmnopqrstuv",
  28. StringCat("0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
  29. "a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
  30. "k", "l", "m", "n", "o", "p", "q", "r", "s", "t",
  31. "u", "v"));
  32. }
  33. // Defines a variadic function Sum(), whose arguments and return value
  34. // have primitive types.
  35. // The return type of SumImp() is deliberately different from its
  36. // argument type, as we want to test that this works.
  37. long SumImpl(ArrayRef<const int *> Args) {
  38. long Result = 0;
  39. for (unsigned i = 0, e = Args.size(); i < e; ++i)
  40. Result += *Args[i];
  41. return Result;
  42. }
  43. const VariadicFunction<long, int, SumImpl> Sum = {};
  44. TEST(VariadicFunctionTest, WorksForPrimitiveTypes) {
  45. EXPECT_EQ(0, Sum());
  46. EXPECT_EQ(1, Sum(1));
  47. EXPECT_EQ(12, Sum(10, 2));
  48. EXPECT_EQ(1234567, Sum(1000000, 200000, 30000, 4000, 500, 60, 7));
  49. }
  50. // Appends an array of strings to dest and returns the number of
  51. // characters appended.
  52. int StringAppendImpl(std::string *Dest, ArrayRef<const std::string *> Args) {
  53. int Chars = 0;
  54. for (unsigned i = 0, e = Args.size(); i < e; ++i) {
  55. Chars += Args[i]->size();
  56. *Dest += *Args[i];
  57. }
  58. return Chars;
  59. }
  60. const VariadicFunction1<int, std::string *, std::string,
  61. StringAppendImpl> StringAppend = {};
  62. TEST(VariadicFunction1Test, Works) {
  63. std::string S0("hi");
  64. EXPECT_EQ(0, StringAppend(&S0));
  65. EXPECT_EQ("hi", S0);
  66. std::string S1("bin");
  67. EXPECT_EQ(2, StringAppend(&S1, "go"));
  68. EXPECT_EQ("bingo", S1);
  69. std::string S4("Fab4");
  70. EXPECT_EQ(4 + 4 + 6 + 5,
  71. StringAppend(&S4, "John", "Paul", "George", "Ringo"));
  72. EXPECT_EQ("Fab4JohnPaulGeorgeRingo", S4);
  73. }
  74. // Counts how many optional arguments fall in the given range.
  75. // Returns the result in *num_in_range. We make the return type void
  76. // as we want to test that VariadicFunction* can handle it.
  77. void CountInRangeImpl(int *NumInRange, int Low, int High,
  78. ArrayRef<const int *> Args) {
  79. *NumInRange = 0;
  80. for (unsigned i = 0, e = Args.size(); i < e; ++i)
  81. if (Low <= *Args[i] && *Args[i] <= High)
  82. ++(*NumInRange);
  83. }
  84. const VariadicFunction3<void, int *, int, int, int,
  85. CountInRangeImpl> CountInRange = {};
  86. TEST(VariadicFunction3Test, Works) {
  87. int N = -1;
  88. CountInRange(&N, -100, 100);
  89. EXPECT_EQ(0, N);
  90. CountInRange(&N, -100, 100, 42);
  91. EXPECT_EQ(1, N);
  92. CountInRange(&N, -100, 100, 1, 999, -200, 42);
  93. EXPECT_EQ(2, N);
  94. }
  95. } // namespace