scoped_generic_unittest.cc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // Copyright 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include <vector>
  5. #include "butil/scoped_generic.h"
  6. #include <gtest/gtest.h>
  7. namespace butil {
  8. namespace {
  9. struct IntTraits {
  10. IntTraits(std::vector<int>* freed) : freed_ints(freed) {}
  11. static int InvalidValue() {
  12. return -1;
  13. }
  14. void Free(int value) {
  15. freed_ints->push_back(value);
  16. }
  17. std::vector<int>* freed_ints;
  18. };
  19. typedef ScopedGeneric<int, IntTraits> ScopedInt;
  20. } // namespace
  21. TEST(ScopedGenericTest, ScopedGeneric) {
  22. std::vector<int> values_freed;
  23. IntTraits traits(&values_freed);
  24. // Invalid case, delete should not be called.
  25. {
  26. ScopedInt a(IntTraits::InvalidValue(), traits);
  27. }
  28. EXPECT_TRUE(values_freed.empty());
  29. // Simple deleting case.
  30. static const int kFirst = 0;
  31. {
  32. ScopedInt a(kFirst, traits);
  33. }
  34. ASSERT_EQ(1u, values_freed.size());
  35. ASSERT_EQ(kFirst, values_freed[0]);
  36. values_freed.clear();
  37. // Release should return the right value and leave the object empty.
  38. {
  39. ScopedInt a(kFirst, traits);
  40. EXPECT_EQ(kFirst, a.release());
  41. ScopedInt b(IntTraits::InvalidValue(), traits);
  42. EXPECT_EQ(IntTraits::InvalidValue(), b.release());
  43. }
  44. ASSERT_TRUE(values_freed.empty());
  45. // Reset should free the old value, then the new one should go away when
  46. // it goes out of scope.
  47. static const int kSecond = 1;
  48. {
  49. ScopedInt b(kFirst, traits);
  50. b.reset(kSecond);
  51. ASSERT_EQ(1u, values_freed.size());
  52. ASSERT_EQ(kFirst, values_freed[0]);
  53. }
  54. ASSERT_EQ(2u, values_freed.size());
  55. ASSERT_EQ(kSecond, values_freed[1]);
  56. values_freed.clear();
  57. // Swap.
  58. {
  59. ScopedInt a(kFirst, traits);
  60. ScopedInt b(kSecond, traits);
  61. a.swap(b);
  62. EXPECT_TRUE(values_freed.empty()); // Nothing should be freed.
  63. EXPECT_EQ(kSecond, a.get());
  64. EXPECT_EQ(kFirst, b.get());
  65. }
  66. // Values should be deleted in the opposite order.
  67. ASSERT_EQ(2u, values_freed.size());
  68. EXPECT_EQ(kFirst, values_freed[0]);
  69. EXPECT_EQ(kSecond, values_freed[1]);
  70. values_freed.clear();
  71. // Pass.
  72. {
  73. ScopedInt a(kFirst, traits);
  74. ScopedInt b(a.Pass());
  75. EXPECT_TRUE(values_freed.empty()); // Nothing should be freed.
  76. ASSERT_EQ(IntTraits::InvalidValue(), a.get());
  77. ASSERT_EQ(kFirst, b.get());
  78. }
  79. ASSERT_EQ(1u, values_freed.size());
  80. ASSERT_EQ(kFirst, values_freed[0]);
  81. }
  82. TEST(ScopedGenericTest, Operators) {
  83. std::vector<int> values_freed;
  84. IntTraits traits(&values_freed);
  85. static const int kFirst = 0;
  86. static const int kSecond = 1;
  87. {
  88. ScopedInt a(kFirst, traits);
  89. EXPECT_TRUE(a == kFirst);
  90. EXPECT_FALSE(a != kFirst);
  91. EXPECT_FALSE(a == kSecond);
  92. EXPECT_TRUE(a != kSecond);
  93. EXPECT_TRUE(kFirst == a);
  94. EXPECT_FALSE(kFirst != a);
  95. EXPECT_FALSE(kSecond == a);
  96. EXPECT_TRUE(kSecond != a);
  97. }
  98. // is_valid().
  99. {
  100. ScopedInt a(kFirst, traits);
  101. EXPECT_TRUE(a.is_valid());
  102. a.reset();
  103. EXPECT_FALSE(a.is_valid());
  104. }
  105. }
  106. // Cheesy manual "no compile" test for manually validating changes.
  107. #if 0
  108. TEST(ScopedGenericTest, NoCompile) {
  109. // Assignment shouldn't work.
  110. /*{
  111. ScopedInt a(kFirst, traits);
  112. ScopedInt b(a);
  113. }*/
  114. // Comparison shouldn't work.
  115. /*{
  116. ScopedInt a(kFirst, traits);
  117. ScopedInt b(kFirst, traits);
  118. if (a == b) {
  119. }
  120. }*/
  121. // Implicit conversion to bool shouldn't work.
  122. /*{
  123. ScopedInt a(kFirst, traits);
  124. bool result = a;
  125. }*/
  126. }
  127. #endif
  128. } // namespace butil