utils_tests.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2015 Microsoft Corporation. All rights reserved.
  4. //
  5. // This code is licensed under the MIT License (MIT).
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  8. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  9. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  10. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  11. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  12. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  13. // THE SOFTWARE.
  14. //
  15. ///////////////////////////////////////////////////////////////////////////////
  16. #include <gtest/gtest.h>
  17. #include <algorithm> // for move
  18. #include <complex>
  19. #include <cstddef> // for std::ptrdiff_t
  20. #include <cstdint> // for uint32_t, int32_t
  21. #include <functional> // for reference_wrapper, _Bind_helper<>::type
  22. #include <gsl/narrow> // for narrow, narrowing_error
  23. #include <gsl/util> // finally, narrow_cast
  24. #include <limits> // for numeric_limits
  25. #include <type_traits> // for is_same
  26. using namespace gsl;
  27. namespace
  28. {
  29. void f(int& i) { i += 1; }
  30. static int j = 0;
  31. void g() { j += 1; }
  32. } // namespace
  33. TEST(utils_tests, sanity_check_for_gsl_index_typedef)
  34. {
  35. static_assert(std::is_same<gsl::index, std::ptrdiff_t>::value,
  36. "gsl::index represents wrong arithmetic type");
  37. }
  38. TEST(utils_tests, finally_lambda)
  39. {
  40. int i = 0;
  41. {
  42. auto _ = finally([&]() { f(i); });
  43. EXPECT_TRUE(i == 0);
  44. }
  45. EXPECT_TRUE(i == 1);
  46. }
  47. TEST(utils_tests, finally_lambda_move)
  48. {
  49. int i = 0;
  50. {
  51. auto _1 = finally([&]() { f(i); });
  52. {
  53. auto _2 = std::move(_1);
  54. EXPECT_TRUE(i == 0);
  55. }
  56. EXPECT_TRUE(i == 1);
  57. {
  58. auto _2 = std::move(_1);
  59. EXPECT_TRUE(i == 1);
  60. }
  61. EXPECT_TRUE(i == 1);
  62. }
  63. EXPECT_TRUE(i == 1);
  64. }
  65. TEST(utils_tests, finally_const_lvalue_lambda)
  66. {
  67. int i = 0;
  68. {
  69. const auto const_lvalue_lambda = [&]() { f(i); };
  70. auto _ = finally(const_lvalue_lambda);
  71. EXPECT_TRUE(i == 0);
  72. }
  73. EXPECT_TRUE(i == 1);
  74. }
  75. TEST(utils_tests, finally_mutable_lvalue_lambda)
  76. {
  77. int i = 0;
  78. {
  79. auto mutable_lvalue_lambda = [&]() { f(i); };
  80. auto _ = finally(mutable_lvalue_lambda);
  81. EXPECT_TRUE(i == 0);
  82. }
  83. EXPECT_TRUE(i == 1);
  84. }
  85. TEST(utils_tests, finally_function_with_bind)
  86. {
  87. int i = 0;
  88. {
  89. auto _ = finally([&i] { return f(i); });
  90. EXPECT_TRUE(i == 0);
  91. }
  92. EXPECT_TRUE(i == 1);
  93. }
  94. TEST(utils_tests, finally_function_ptr)
  95. {
  96. j = 0;
  97. {
  98. auto _ = finally(&g);
  99. EXPECT_TRUE(j == 0);
  100. }
  101. EXPECT_TRUE(j == 1);
  102. }
  103. TEST(utils_tests, finally_function)
  104. {
  105. j = 0;
  106. {
  107. auto _ = finally(g);
  108. EXPECT_TRUE(j == 0);
  109. }
  110. EXPECT_TRUE(j == 1);
  111. }
  112. TEST(utils_tests, narrow_cast)
  113. {
  114. int n = 120;
  115. char c = narrow_cast<char>(n);
  116. EXPECT_TRUE(c == 120);
  117. n = 300;
  118. unsigned char uc = narrow_cast<unsigned char>(n);
  119. EXPECT_TRUE(uc == 44);
  120. }
  121. #ifndef GSL_KERNEL_MODE
  122. TEST(utils_tests, narrow)
  123. {
  124. int n = 120;
  125. const char c = narrow<char>(n);
  126. EXPECT_TRUE(c == 120);
  127. n = 300;
  128. EXPECT_THROW(narrow<char>(n), narrowing_error);
  129. const auto int32_max = std::numeric_limits<int32_t>::max();
  130. const auto int32_min = std::numeric_limits<int32_t>::min();
  131. EXPECT_TRUE(narrow<uint32_t>(int32_t(0)) == 0);
  132. EXPECT_TRUE(narrow<uint32_t>(int32_t(1)) == 1);
  133. EXPECT_TRUE(narrow<uint32_t>(int32_max) == static_cast<uint32_t>(int32_max));
  134. EXPECT_THROW(narrow<uint32_t>(int32_t(-1)), narrowing_error);
  135. EXPECT_THROW(narrow<uint32_t>(int32_min), narrowing_error);
  136. n = -42;
  137. EXPECT_THROW(narrow<unsigned>(n), narrowing_error);
  138. EXPECT_TRUE(
  139. narrow<std::complex<float>>(std::complex<double>(4, 2)) == std::complex<float>(4, 2));
  140. EXPECT_THROW(narrow<std::complex<float>>(std::complex<double>(4.2)), narrowing_error);
  141. EXPECT_TRUE(narrow<int>(float(1)) == 1);
  142. }
  143. #endif // GSL_KERNEL_MODE