1 /*
2  Tests of variadic function emulation macros.
3 
4  Copyright (c) 2012-2014, Victor Zverovich
5  All rights reserved.
6 
7  Redistribution and use in source and binary forms, with or without
8  modification, are permitted provided that the following conditions are met:
9 
10  1. Redistributions of source code must retain the above copyright notice, this
11     list of conditions and the following disclaimer.
12  2. Redistributions in binary form must reproduce the above copyright notice,
13     this list of conditions and the following disclaimer in the documentation
14     and/or other materials provided with the distribution.
15 
16  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <utility>
29 #include <gtest/gtest.h>
30 
31 #define FMT_USE_VARIADIC_TEMPLATES 0
32 #include "fmt/format.h"
33 
34 #define IDENTITY(x) x
35 
TEST(UtilTest,Gen)36 TEST(UtilTest, Gen) {
37   int values[] = {FMT_GEN(10, IDENTITY)};
38   for (int i = 0; i < 10; ++i)
39     EXPECT_EQ(i, values[i]);
40 }
41 
42 #define MAKE_PAIR(x, y) std::make_pair(x, y)
43 
TEST(UtilTest,ForEach)44 TEST(UtilTest, ForEach) {
45   std::pair<char, int> values[] = {
46       FMT_FOR_EACH10(MAKE_PAIR, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j')
47   };
48   for (int i = 0; i < 10; ++i) {
49     EXPECT_EQ('a' + i, values[i].first);
50     EXPECT_EQ(i, values[i].second);
51   }
52 }
53 
TEST(UtilTest,NArg)54 TEST(UtilTest, NArg) {
55   EXPECT_EQ(1, FMT_NARG(a));
56   EXPECT_EQ(2, FMT_NARG(a, b));
57   EXPECT_EQ(3, FMT_NARG(a, b, c));
58   EXPECT_EQ(4, FMT_NARG(a, b, c, d));
59   EXPECT_EQ(5, FMT_NARG(a, b, c, d, e));
60   EXPECT_EQ(6, FMT_NARG(a, b, c, d, e, f));
61   EXPECT_EQ(7, FMT_NARG(a, b, c, d, e, f, g));
62   EXPECT_EQ(8, FMT_NARG(a, b, c, d, e, f, g, h));
63   EXPECT_EQ(9, FMT_NARG(a, b, c, d, e, f, g, h, i));
64   EXPECT_EQ(10, FMT_NARG(a, b, c, d, e, f, g, h, i, j));
65 }
66 
67 int result;
68 
69 #define MAKE_TEST(func) \
70   void func(const char *, const fmt::ArgList &args) { \
71     result = 0; \
72     for (unsigned i = 0; args[i].type; ++i) \
73       result += args[i].int_value; \
74   }
75 
76 MAKE_TEST(test_func)
77 
78 typedef char Char;
79 FMT_WRAP1(test_func, const char *, 1)
80 
TEST(UtilTest,Wrap1)81 TEST(UtilTest, Wrap1) {
82   result = 0;
83   test_func("", 42);
84   EXPECT_EQ(42, result);
85 }
86 
87 MAKE_TEST(test_variadic_void)
FMT_VARIADIC_VOID(test_variadic_void,const char *)88 FMT_VARIADIC_VOID(test_variadic_void, const char *)
89 
90 TEST(UtilTest, VariadicVoid) {
91   result = 0;
92   test_variadic_void("", 10, 20, 30, 40, 50, 60, 70, 80, 90, 100);
93   EXPECT_EQ(550, result);
94 }
95 
96 template <int>
97 struct S {};
98 
99 #define GET_TYPE(n) S<n>
100 
101 int test_variadic(FMT_GEN(10, GET_TYPE), const fmt::ArgList &args) { \
102   int result = 0; \
103   for (unsigned i = 0; args[i].type; ++i) \
104     result += args[i].int_value; \
105   return result;
106 }
FMT_VARIADIC(int,test_variadic,S<0>,S<1>,S<2>,S<3>,S<4>,S<5>,S<6>,S<7>,S<8>,S<9>)107 FMT_VARIADIC(int, test_variadic,
108     S<0>, S<1>, S<2>, S<3>, S<4>, S<5>, S<6>, S<7>, S<8>, S<9>)
109 
110 #define MAKE_ARG(n) S<n>()
111 
112 TEST(UtilTest, Variadic) {
113   EXPECT_EQ(550, test_variadic(FMT_GEN(10, MAKE_ARG),
114       10, 20, 30, 40, 50, 60, 70, 80, 90, 100));
115 }
116