blob: cde31205966cfcba6ba0ab1f3218bd6f6443b843 [file] [log] [blame]
Chandler Carruthd916ce32011-12-16 08:58:59 +00001//===----------- 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
10#include "gtest/gtest.h"
11#include "llvm/ADT/ArrayRef.h"
12#include "llvm/ADT/VariadicFunction.h"
13
14using namespace llvm;
15namespace {
16
17// Defines a variadic function StringCat() to join strings.
18// StringCat()'s arguments and return value have class types.
Chandler Carruthdb21f4c2011-12-16 09:37:55 +000019std::string StringCatImpl(ArrayRef<const std::string *> Args) {
Chandler Carruthd916ce32011-12-16 08:58:59 +000020 std::string S;
21 for (unsigned i = 0, e = Args.size(); i < e; ++i)
22 S += *Args[i];
23 return S;
24}
Chandler Carruth1243cdd2011-12-17 10:20:15 +000025const VariadicFunction<std::string, std::string, StringCatImpl> StringCat = {};
Chandler Carruthd916ce32011-12-16 08:58:59 +000026
27TEST(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",
35 "u", "v"));
36}
37
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.
Chandler Carruthdb21f4c2011-12-16 09:37:55 +000042long SumImpl(ArrayRef<const int *> Args) {
Chandler Carruthd916ce32011-12-16 08:58:59 +000043 long Result = 0;
44 for (unsigned i = 0, e = Args.size(); i < e; ++i)
45 Result += *Args[i];
46 return Result;
47}
Chandler Carruth1243cdd2011-12-17 10:20:15 +000048const VariadicFunction<long, int, SumImpl> Sum = {};
Chandler Carruthd916ce32011-12-16 08:58:59 +000049
50TEST(VariadicFunctionTest, WorksForPrimitiveTypes) {
51 EXPECT_EQ(0, Sum());
52 EXPECT_EQ(1, Sum(1));
53 EXPECT_EQ(12, Sum(10, 2));
54 EXPECT_EQ(1234567, Sum(1000000, 200000, 30000, 4000, 500, 60, 7));
55}
56
57// Appends an array of strings to dest and returns the number of
58// characters appended.
Chandler Carruthdb21f4c2011-12-16 09:37:55 +000059int StringAppendImpl(std::string *Dest, ArrayRef<const std::string *> Args) {
Chandler Carruthd916ce32011-12-16 08:58:59 +000060 int Chars = 0;
61 for (unsigned i = 0, e = Args.size(); i < e; ++i) {
62 Chars += Args[i]->size();
63 *Dest += *Args[i];
64 }
65 return Chars;
66}
Chandler Carruthdb21f4c2011-12-16 09:37:55 +000067const VariadicFunction1<int, std::string *, std::string,
Chandler Carruth1243cdd2011-12-17 10:20:15 +000068 StringAppendImpl> StringAppend = {};
Chandler Carruthd916ce32011-12-16 08:58:59 +000069
70TEST(VariadicFunction1Test, Works) {
71 std::string S0("hi");
72 EXPECT_EQ(0, StringAppend(&S0));
73 EXPECT_EQ("hi", S0);
74
75 std::string S1("bin");
76 EXPECT_EQ(2, StringAppend(&S1, "go"));
77 EXPECT_EQ("bingo", S1);
78
79 std::string S4("Fab4");
80 EXPECT_EQ(4 + 4 + 6 + 5,
81 StringAppend(&S4, "John", "Paul", "George", "Ringo"));
82 EXPECT_EQ("Fab4JohnPaulGeorgeRingo", S4);
83}
84
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.
Chandler Carruthdb21f4c2011-12-16 09:37:55 +000088void CountInRangeImpl(int *NumInRange, int Low, int High,
89 ArrayRef<const int *> Args) {
Chandler Carruthd916ce32011-12-16 08:58:59 +000090 *NumInRange = 0;
91 for (unsigned i = 0, e = Args.size(); i < e; ++i)
92 if (Low <= *Args[i] && *Args[i] <= High)
93 ++(*NumInRange);
94}
Chandler Carruthdb21f4c2011-12-16 09:37:55 +000095const VariadicFunction3<void, int *, int, int, int,
Chandler Carruth1243cdd2011-12-17 10:20:15 +000096 CountInRangeImpl> CountInRange = {};
Chandler Carruthd916ce32011-12-16 08:58:59 +000097
98TEST(VariadicFunction3Test, Works) {
99 int N = -1;
100 CountInRange(&N, -100, 100);
101 EXPECT_EQ(0, N);
102
103 CountInRange(&N, -100, 100, 42);
104 EXPECT_EQ(1, N);
105
106 CountInRange(&N, -100, 100, 1, 999, -200, 42);
107 EXPECT_EQ(2, N);
108}
109
110} // namespace