template_util_unittest.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. // Author: lar@google.com (Laramie Leavitt)
  31. //
  32. // These tests are really compile time tests.
  33. // If you try to step through this in a debugger
  34. // you will not see any evaluations, merely that
  35. // value is assigned true or false sequentially.
  36. #include <google/protobuf/stubs/template_util.h>
  37. #include <google/protobuf/testing/googletest.h>
  38. #include <gtest/gtest.h>
  39. namespace GOOGLE_NAMESPACE = google::protobuf::internal;
  40. namespace google {
  41. namespace protobuf {
  42. namespace internal {
  43. namespace {
  44. TEST(TemplateUtilTest, TestSize) {
  45. EXPECT_GT(sizeof(GOOGLE_NAMESPACE::big_), sizeof(GOOGLE_NAMESPACE::small_));
  46. }
  47. TEST(TemplateUtilTest, TestIntegralConstants) {
  48. // test the built-in types.
  49. EXPECT_TRUE(true_type::value);
  50. EXPECT_FALSE(false_type::value);
  51. typedef integral_constant<int, 1> one_type;
  52. EXPECT_EQ(1, one_type::value);
  53. }
  54. TEST(TemplateUtilTest, TestTemplateIf) {
  55. typedef if_<true, true_type, false_type>::type if_true;
  56. EXPECT_TRUE(if_true::value);
  57. typedef if_<false, true_type, false_type>::type if_false;
  58. EXPECT_FALSE(if_false::value);
  59. }
  60. TEST(TemplateUtilTest, TestTemplateTypeEquals) {
  61. // Check that the TemplateTypeEquals works correctly.
  62. bool value = false;
  63. // Test the same type is true.
  64. value = type_equals_<int, int>::value;
  65. EXPECT_TRUE(value);
  66. // Test different types are false.
  67. value = type_equals_<float, int>::value;
  68. EXPECT_FALSE(value);
  69. // Test type aliasing.
  70. typedef const int foo;
  71. value = type_equals_<const foo, const int>::value;
  72. EXPECT_TRUE(value);
  73. }
  74. TEST(TemplateUtilTest, TestTemplateAndOr) {
  75. // Check that the TemplateTypeEquals works correctly.
  76. bool value = false;
  77. // Yes && Yes == true.
  78. value = and_<true_, true_>::value;
  79. EXPECT_TRUE(value);
  80. // Yes && No == false.
  81. value = and_<true_, false_>::value;
  82. EXPECT_FALSE(value);
  83. // No && Yes == false.
  84. value = and_<false_, true_>::value;
  85. EXPECT_FALSE(value);
  86. // No && No == false.
  87. value = and_<false_, false_>::value;
  88. EXPECT_FALSE(value);
  89. // Yes || Yes == true.
  90. value = or_<true_, true_>::value;
  91. EXPECT_TRUE(value);
  92. // Yes || No == true.
  93. value = or_<true_, false_>::value;
  94. EXPECT_TRUE(value);
  95. // No || Yes == true.
  96. value = or_<false_, true_>::value;
  97. EXPECT_TRUE(value);
  98. // No || No == false.
  99. value = or_<false_, false_>::value;
  100. EXPECT_FALSE(value);
  101. }
  102. TEST(TemplateUtilTest, TestIdentity) {
  103. EXPECT_TRUE(
  104. (type_equals_<GOOGLE_NAMESPACE::identity_<int>::type, int>::value));
  105. EXPECT_TRUE(
  106. (type_equals_<GOOGLE_NAMESPACE::identity_<void>::type, void>::value));
  107. }
  108. } // anonymous namespace
  109. } // namespace internal
  110. } // namespace protobuf
  111. } // namespace google