Add MapVector::rbegin(), MapVector::rend() to completment MapVector::begin(), MapVect...
[oota-llvm.git] / unittests / ADT / VariadicFunctionTest.cpp
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
10 #include "gtest/gtest.h"
11 #include "llvm/ADT/ArrayRef.h"
12 #include "llvm/ADT/VariadicFunction.h"
13
14 using namespace llvm;
15 namespace {
16
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) {
20   std::string S;
21   for (unsigned i = 0, e = Args.size(); i < e; ++i)
22     S += *Args[i];
23   return S;
24 }
25 const VariadicFunction<std::string, std::string, StringCatImpl> StringCat = {};
26
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",
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.
42 long SumImpl(ArrayRef<const int *> Args) {
43   long Result = 0;
44   for (unsigned i = 0, e = Args.size(); i < e; ++i)
45     Result += *Args[i];
46   return Result;
47 }
48 const VariadicFunction<long, int, SumImpl> Sum = {};
49
50 TEST(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.
59 int StringAppendImpl(std::string *Dest, ArrayRef<const std::string *> Args) {
60   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 }
67 const VariadicFunction1<int, std::string *, std::string,
68                         StringAppendImpl> StringAppend = {};
69
70 TEST(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.
88 void CountInRangeImpl(int *NumInRange, int Low, int High,
89                       ArrayRef<const int *> Args) {
90   *NumInRange = 0;
91   for (unsigned i = 0, e = Args.size(); i < e; ++i)
92     if (Low <= *Args[i] && *Args[i] <= High)
93       ++(*NumInRange);
94 }
95 const VariadicFunction3<void, int *, int, int, int,
96                         CountInRangeImpl> CountInRange = {};
97
98 TEST(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