[LLVM][NFC] remove unused fields
[llvm-core.git] / unittests / ADT / VariadicFunctionTest.cpp
blob56cf85a9ca2ae39462606d062542d804b18459f0
1 //===----------- VariadicFunctionTest.cpp - VariadicFunction unit tests ---===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
9 #include "llvm/ADT/VariadicFunction.h"
10 #include "llvm/ADT/ArrayRef.h"
11 #include "gtest/gtest.h"
13 using namespace llvm;
14 namespace {
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) {
19 std::string S;
20 for (unsigned i = 0, e = Args.size(); i < e; ++i)
21 S += *Args[i];
22 return S;
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",
34 "u", "v"));
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) {
42 long Result = 0;
43 for (unsigned i = 0, e = Args.size(); i < e; ++i)
44 Result += *Args[i];
45 return Result;
47 const VariadicFunction<long, int, SumImpl> Sum = {};
49 TEST(VariadicFunctionTest, WorksForPrimitiveTypes) {
50 EXPECT_EQ(0, Sum());
51 EXPECT_EQ(1, Sum(1));
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) {
59 int Chars = 0;
60 for (unsigned i = 0, e = Args.size(); i < e; ++i) {
61 Chars += Args[i]->size();
62 *Dest += *Args[i];
64 return Chars;
66 const VariadicFunction1<int, std::string *, std::string,
67 StringAppendImpl> StringAppend = {};
69 TEST(VariadicFunction1Test, Works) {
70 std::string S0("hi");
71 EXPECT_EQ(0, StringAppend(&S0));
72 EXPECT_EQ("hi", 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) {
89 *NumInRange = 0;
90 for (unsigned i = 0, e = Args.size(); i < e; ++i)
91 if (Low <= *Args[i] && *Args[i] <= High)
92 ++(*NumInRange);
94 const VariadicFunction3<void, int *, int, int, int,
95 CountInRangeImpl> CountInRange = {};
97 TEST(VariadicFunction3Test, Works) {
98 int N = -1;
99 CountInRange(&N, -100, 100);
100 EXPECT_EQ(0, N);
102 CountInRange(&N, -100, 100, 42);
103 EXPECT_EQ(1, N);
105 CountInRange(&N, -100, 100, 1, 999, -200, 42);
106 EXPECT_EQ(2, N);
109 } // namespace