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