optimization_test.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright 2020 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "absl/base/optimization.h"
  15. #include "gtest/gtest.h"
  16. #include "absl/types/optional.h"
  17. namespace {
  18. // Tests for the ABSL_PREDICT_TRUE and ABSL_PREDICT_FALSE macros.
  19. // The tests only verify that the macros are functionally correct - i.e. code
  20. // behaves as if they weren't used. They don't try to check their impact on
  21. // optimization.
  22. TEST(PredictTest, PredictTrue) {
  23. EXPECT_TRUE(ABSL_PREDICT_TRUE(true));
  24. EXPECT_FALSE(ABSL_PREDICT_TRUE(false));
  25. EXPECT_TRUE(ABSL_PREDICT_TRUE(1 == 1));
  26. EXPECT_FALSE(ABSL_PREDICT_TRUE(1 == 2));
  27. if (ABSL_PREDICT_TRUE(false)) ADD_FAILURE();
  28. if (!ABSL_PREDICT_TRUE(true)) ADD_FAILURE();
  29. EXPECT_TRUE(ABSL_PREDICT_TRUE(true) && true);
  30. EXPECT_TRUE(ABSL_PREDICT_TRUE(true) || false);
  31. }
  32. TEST(PredictTest, PredictFalse) {
  33. EXPECT_TRUE(ABSL_PREDICT_FALSE(true));
  34. EXPECT_FALSE(ABSL_PREDICT_FALSE(false));
  35. EXPECT_TRUE(ABSL_PREDICT_FALSE(1 == 1));
  36. EXPECT_FALSE(ABSL_PREDICT_FALSE(1 == 2));
  37. if (ABSL_PREDICT_FALSE(false)) ADD_FAILURE();
  38. if (!ABSL_PREDICT_FALSE(true)) ADD_FAILURE();
  39. EXPECT_TRUE(ABSL_PREDICT_FALSE(true) && true);
  40. EXPECT_TRUE(ABSL_PREDICT_FALSE(true) || false);
  41. }
  42. TEST(PredictTest, OneEvaluation) {
  43. // Verify that the expression is only evaluated once.
  44. int x = 0;
  45. if (ABSL_PREDICT_TRUE((++x) == 0)) ADD_FAILURE();
  46. EXPECT_EQ(x, 1);
  47. if (ABSL_PREDICT_FALSE((++x) == 0)) ADD_FAILURE();
  48. EXPECT_EQ(x, 2);
  49. }
  50. TEST(PredictTest, OperatorOrder) {
  51. // Verify that operator order inside and outside the macro behaves well.
  52. // These would fail for a naive '#define ABSL_PREDICT_TRUE(x) x'
  53. EXPECT_TRUE(ABSL_PREDICT_TRUE(1 && 2) == true);
  54. EXPECT_TRUE(ABSL_PREDICT_FALSE(1 && 2) == true);
  55. EXPECT_TRUE(!ABSL_PREDICT_TRUE(1 == 2));
  56. EXPECT_TRUE(!ABSL_PREDICT_FALSE(1 == 2));
  57. }
  58. TEST(PredictTest, Pointer) {
  59. const int x = 3;
  60. const int *good_intptr = &x;
  61. const int *null_intptr = nullptr;
  62. EXPECT_TRUE(ABSL_PREDICT_TRUE(good_intptr));
  63. EXPECT_FALSE(ABSL_PREDICT_TRUE(null_intptr));
  64. EXPECT_TRUE(ABSL_PREDICT_FALSE(good_intptr));
  65. EXPECT_FALSE(ABSL_PREDICT_FALSE(null_intptr));
  66. }
  67. TEST(PredictTest, Optional) {
  68. // Note: An optional's truth value is the value's existence, not its truth.
  69. absl::optional<bool> has_value(false);
  70. absl::optional<bool> no_value;
  71. EXPECT_TRUE(ABSL_PREDICT_TRUE(has_value));
  72. EXPECT_FALSE(ABSL_PREDICT_TRUE(no_value));
  73. EXPECT_TRUE(ABSL_PREDICT_FALSE(has_value));
  74. EXPECT_FALSE(ABSL_PREDICT_FALSE(no_value));
  75. }
  76. class ImplictlyConvertibleToBool {
  77. public:
  78. explicit ImplictlyConvertibleToBool(bool value) : value_(value) {}
  79. operator bool() const { // NOLINT(google-explicit-constructor)
  80. return value_;
  81. }
  82. private:
  83. bool value_;
  84. };
  85. TEST(PredictTest, ImplicitBoolConversion) {
  86. const ImplictlyConvertibleToBool is_true(true);
  87. const ImplictlyConvertibleToBool is_false(false);
  88. if (!ABSL_PREDICT_TRUE(is_true)) ADD_FAILURE();
  89. if (ABSL_PREDICT_TRUE(is_false)) ADD_FAILURE();
  90. if (!ABSL_PREDICT_FALSE(is_true)) ADD_FAILURE();
  91. if (ABSL_PREDICT_FALSE(is_false)) ADD_FAILURE();
  92. }
  93. class ExplictlyConvertibleToBool {
  94. public:
  95. explicit ExplictlyConvertibleToBool(bool value) : value_(value) {}
  96. explicit operator bool() const { return value_; }
  97. private:
  98. bool value_;
  99. };
  100. TEST(PredictTest, ExplicitBoolConversion) {
  101. const ExplictlyConvertibleToBool is_true(true);
  102. const ExplictlyConvertibleToBool is_false(false);
  103. if (!ABSL_PREDICT_TRUE(is_true)) ADD_FAILURE();
  104. if (ABSL_PREDICT_TRUE(is_false)) ADD_FAILURE();
  105. if (!ABSL_PREDICT_FALSE(is_true)) ADD_FAILURE();
  106. if (ABSL_PREDICT_FALSE(is_false)) ADD_FAILURE();
  107. }
  108. } // namespace