template_util_unittest.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. // These tests are really compile time tests.
  32. // If you try to step through this in a debugger
  33. // you will not see any evaluations, merely that
  34. // value is assigned true or false sequentially.
  35. #include <sparsehash/internal/sparseconfig.h>
  36. #include <config.h>
  37. #include <sparsehash/template_util.h>
  38. #include "testutil.h"
  39. using namespace GOOGLE_NAMESPACE;
  40. namespace {
  41. TEST(TemplateUtilTest, TestSize) {
  42. EXPECT_GT(sizeof(GOOGLE_NAMESPACE::big_), sizeof(GOOGLE_NAMESPACE::small_));
  43. }
  44. TEST(TemplateUtilTest, TestIntegralConstants) {
  45. // test the built-in types.
  46. EXPECT_TRUE(true_type::value);
  47. EXPECT_FALSE(false_type::value);
  48. typedef integral_constant<int, 1> one_type;
  49. EXPECT_EQ(1, one_type::value);
  50. }
  51. TEST(TemplateUtilTest, TestTemplateIf) {
  52. typedef if_<true, true_type, false_type>::type if_true;
  53. EXPECT_TRUE(if_true::value);
  54. typedef if_<false, true_type, false_type>::type if_false;
  55. EXPECT_FALSE(if_false::value);
  56. }
  57. TEST(TemplateUtilTest, TestTemplateTypeEquals) {
  58. // Check that the TemplateTypeEquals works correctly.
  59. bool value = false;
  60. // Test the same type is true.
  61. value = type_equals_<int, int>::value;
  62. EXPECT_TRUE(value);
  63. // Test different types are false.
  64. value = type_equals_<float, int>::value;
  65. EXPECT_FALSE(value);
  66. // Test type aliasing.
  67. typedef const int foo;
  68. value = type_equals_<const foo, const int>::value;
  69. EXPECT_TRUE(value);
  70. }
  71. TEST(TemplateUtilTest, TestTemplateAndOr) {
  72. // Check that the TemplateTypeEquals works correctly.
  73. bool value = false;
  74. // Yes && Yes == true.
  75. value = and_<true_, true_>::value;
  76. EXPECT_TRUE(value);
  77. // Yes && No == false.
  78. value = and_<true_, false_>::value;
  79. EXPECT_FALSE(value);
  80. // No && Yes == false.
  81. value = and_<false_, true_>::value;
  82. EXPECT_FALSE(value);
  83. // No && No == false.
  84. value = and_<false_, false_>::value;
  85. EXPECT_FALSE(value);
  86. // Yes || Yes == true.
  87. value = or_<true_, true_>::value;
  88. EXPECT_TRUE(value);
  89. // Yes || No == true.
  90. value = or_<true_, false_>::value;
  91. EXPECT_TRUE(value);
  92. // No || Yes == true.
  93. value = or_<false_, true_>::value;
  94. EXPECT_TRUE(value);
  95. // No || No == false.
  96. value = or_<false_, false_>::value;
  97. EXPECT_FALSE(value);
  98. }
  99. TEST(TemplateUtilTest, TestIdentity) {
  100. EXPECT_TRUE(
  101. (type_equals_<GOOGLE_NAMESPACE::identity_<int>::type, int>::value));
  102. EXPECT_TRUE(
  103. (type_equals_<GOOGLE_NAMESPACE::identity_<void>::type, void>::value));
  104. }
  105. } // namespace
  106. #include <iostream>
  107. int main(int, char **) {
  108. // All the work is done in the static constructors. If they don't
  109. // die, the tests have all passed.
  110. std::cout << "PASS\n";
  111. return 0;
  112. }