1 //===----------- VariadicFunctionTest.cpp - VariadicFunction unit tests ---===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm/ADT/VariadicFunction.h"
10 #include "llvm/ADT/ArrayRef.h"
11 #include "gtest/gtest.h"
16 // Defines a variadic function StringCat() to join strings.
17 // StringCat()'s arguments and return value have class types.
18 std::string
StringCatImpl(ArrayRef
<const std::string
*> Args
) {
20 for (unsigned i
= 0, e
= Args
.size(); i
< e
; ++i
)
24 const VariadicFunction
<std::string
, std::string
, StringCatImpl
> StringCat
= {};
26 TEST(VariadicFunctionTest
, WorksForClassTypes
) {
27 EXPECT_EQ("", StringCat());
28 EXPECT_EQ("a", StringCat("a"));
29 EXPECT_EQ("abc", StringCat("a", "bc"));
30 EXPECT_EQ("0123456789abcdefghijklmnopqrstuv",
31 StringCat("0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
32 "a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
33 "k", "l", "m", "n", "o", "p", "q", "r", "s", "t",
37 // Defines a variadic function Sum(), whose arguments and return value
38 // have primitive types.
39 // The return type of SumImp() is deliberately different from its
40 // argument type, as we want to test that this works.
41 long SumImpl(ArrayRef
<const int *> Args
) {
43 for (unsigned i
= 0, e
= Args
.size(); i
< e
; ++i
)
47 const VariadicFunction
<long, int, SumImpl
> Sum
= {};
49 TEST(VariadicFunctionTest
, WorksForPrimitiveTypes
) {
52 EXPECT_EQ(12, Sum(10, 2));
53 EXPECT_EQ(1234567, Sum(1000000, 200000, 30000, 4000, 500, 60, 7));
56 // Appends an array of strings to dest and returns the number of
57 // characters appended.
58 int StringAppendImpl(std::string
*Dest
, ArrayRef
<const std::string
*> Args
) {
60 for (unsigned i
= 0, e
= Args
.size(); i
< e
; ++i
) {
61 Chars
+= Args
[i
]->size();
66 const VariadicFunction1
<int, std::string
*, std::string
,
67 StringAppendImpl
> StringAppend
= {};
69 TEST(VariadicFunction1Test
, Works
) {
71 EXPECT_EQ(0, StringAppend(&S0
));
74 std::string
S1("bin");
75 EXPECT_EQ(2, StringAppend(&S1
, "go"));
76 EXPECT_EQ("bingo", S1
);
78 std::string
S4("Fab4");
79 EXPECT_EQ(4 + 4 + 6 + 5,
80 StringAppend(&S4
, "John", "Paul", "George", "Ringo"));
81 EXPECT_EQ("Fab4JohnPaulGeorgeRingo", S4
);
84 // Counts how many optional arguments fall in the given range.
85 // Returns the result in *num_in_range. We make the return type void
86 // as we want to test that VariadicFunction* can handle it.
87 void CountInRangeImpl(int *NumInRange
, int Low
, int High
,
88 ArrayRef
<const int *> Args
) {
90 for (unsigned i
= 0, e
= Args
.size(); i
< e
; ++i
)
91 if (Low
<= *Args
[i
] && *Args
[i
] <= High
)
94 const VariadicFunction3
<void, int *, int, int, int,
95 CountInRangeImpl
> CountInRange
= {};
97 TEST(VariadicFunction3Test
, Works
) {
99 CountInRange(&N
, -100, 100);
102 CountInRange(&N
, -100, 100, 42);
105 CountInRange(&N
, -100, 100, 1, 999, -200, 42);