1 // Copyright 2005 Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 // ----
31 // Author: lar@google.com (Laramie Leavitt)
32 //
33 // These tests are really compile time tests.
34 // If you try to step through this in a debugger
35 // you will not see any evaluations, merely that
36 // value is assigned true or false sequentially.
37
38 #include <google/protobuf/stubs/template_util.h>
39
40 #include <google/protobuf/testing/googletest.h>
41 #include <gtest/gtest.h>
42
43 namespace GOOGLE_NAMESPACE = google::protobuf::internal;
44
45 namespace google {
46 namespace protobuf {
47 namespace internal {
48 namespace {
49
TEST(TemplateUtilTest,TestSize)50 TEST(TemplateUtilTest, TestSize) {
51 EXPECT_GT(sizeof(GOOGLE_NAMESPACE::big_), sizeof(GOOGLE_NAMESPACE::small_));
52 }
53
TEST(TemplateUtilTest,TestIntegralConstants)54 TEST(TemplateUtilTest, TestIntegralConstants) {
55 // test the built-in types.
56 EXPECT_TRUE(true_type::value);
57 EXPECT_FALSE(false_type::value);
58
59 typedef integral_constant<int, 1> one_type;
60 EXPECT_EQ(1, one_type::value);
61 }
62
TEST(TemplateUtilTest,TestTemplateIf)63 TEST(TemplateUtilTest, TestTemplateIf) {
64 typedef if_<true, true_type, false_type>::type if_true;
65 EXPECT_TRUE(if_true::value);
66
67 typedef if_<false, true_type, false_type>::type if_false;
68 EXPECT_FALSE(if_false::value);
69 }
70
TEST(TemplateUtilTest,TestTemplateTypeEquals)71 TEST(TemplateUtilTest, TestTemplateTypeEquals) {
72 // Check that the TemplateTypeEquals works correctly.
73 bool value = false;
74
75 // Test the same type is true.
76 value = type_equals_<int, int>::value;
77 EXPECT_TRUE(value);
78
79 // Test different types are false.
80 value = type_equals_<float, int>::value;
81 EXPECT_FALSE(value);
82
83 // Test type aliasing.
84 typedef const int foo;
85 value = type_equals_<const foo, const int>::value;
86 EXPECT_TRUE(value);
87 }
88
TEST(TemplateUtilTest,TestTemplateAndOr)89 TEST(TemplateUtilTest, TestTemplateAndOr) {
90 // Check that the TemplateTypeEquals works correctly.
91 bool value = false;
92
93 // Yes && Yes == true.
94 value = and_<true_, true_>::value;
95 EXPECT_TRUE(value);
96 // Yes && No == false.
97 value = and_<true_, false_>::value;
98 EXPECT_FALSE(value);
99 // No && Yes == false.
100 value = and_<false_, true_>::value;
101 EXPECT_FALSE(value);
102 // No && No == false.
103 value = and_<false_, false_>::value;
104 EXPECT_FALSE(value);
105
106 // Yes || Yes == true.
107 value = or_<true_, true_>::value;
108 EXPECT_TRUE(value);
109 // Yes || No == true.
110 value = or_<true_, false_>::value;
111 EXPECT_TRUE(value);
112 // No || Yes == true.
113 value = or_<false_, true_>::value;
114 EXPECT_TRUE(value);
115 // No || No == false.
116 value = or_<false_, false_>::value;
117 EXPECT_FALSE(value);
118 }
119
TEST(TemplateUtilTest,TestIdentity)120 TEST(TemplateUtilTest, TestIdentity) {
121 EXPECT_TRUE(
122 (type_equals_<GOOGLE_NAMESPACE::identity_<int>::type, int>::value));
123 EXPECT_TRUE(
124 (type_equals_<GOOGLE_NAMESPACE::identity_<void>::type, void>::value));
125 }
126
127 } // anonymous namespace
128 } // namespace internal
129 } // namespace protobuf
130 } // namespace google
131