auto_reset_unittest.cc 844 B

123456789101112131415161718192021222324252627282930313233
  1. // Copyright 2019 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 "base/auto_reset.h"
  5. #include <utility>
  6. #include "testing/gtest/include/gtest/gtest.h"
  7. namespace base {
  8. TEST(AutoReset, Move) {
  9. int value = 10;
  10. {
  11. AutoReset<int> resetter1{&value, 20};
  12. EXPECT_EQ(20, value);
  13. {
  14. value = 15;
  15. AutoReset<int> resetter2 = std::move(resetter1);
  16. // Moving to a new resetter does not change the value;
  17. EXPECT_EQ(15, value);
  18. }
  19. // Moved-to `resetter2` is out of scoped, and resets to the original value
  20. // that was in moved-from `resetter1`.
  21. EXPECT_EQ(10, value);
  22. value = 105;
  23. }
  24. // Moved-from `resetter1` does not reset to anything.
  25. EXPECT_EQ(105, value);
  26. }
  27. } // namespace base