2022-09-23 14:48:59 -07:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
|
|
|
|
|
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
#include "wpi/scope"
|
|
|
|
|
|
|
|
|
|
TEST(ScopeExitTest, ScopeExit) {
|
|
|
|
|
int exitCount = 0;
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
wpi::scope_exit exit{[&] { ++exitCount; }};
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ(0, exitCount);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ(1, exitCount);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(ScopeExitTest, Release) {
|
|
|
|
|
int exitCount = 0;
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
wpi::scope_exit exit1{[&] { ++exitCount; }};
|
|
|
|
|
wpi::scope_exit exit2 = std::move(exit1);
|
2023-06-20 10:55:05 -07:00
|
|
|
// NOLINTNEXTLINE (clang-analyzer-cplusplus.Move)
|
2022-09-23 14:48:59 -07:00
|
|
|
wpi::scope_exit exit3 = std::move(exit1);
|
|
|
|
|
EXPECT_EQ(0, exitCount);
|
|
|
|
|
}
|
|
|
|
|
EXPECT_EQ(1, exitCount);
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
wpi::scope_exit exit{[&] { ++exitCount; }};
|
|
|
|
|
exit.release();
|
|
|
|
|
}
|
|
|
|
|
EXPECT_EQ(1, exitCount);
|
|
|
|
|
}
|