Files
allwpilib/wpilibc/src/test/native/cpp/DoubleSolenoidTest.cpp
Thad House 60ede67abd [hal, wpilib] Switch PCM to be a single object that is allowed to be duplicated (#3475)
Having PCM as a singleton is a problem, as multiple things need to use it, and that gets really ugly. This changes PCM's to be a reference counted object, that can be passed around and constructed from multiple places.

In Java, this is using a map to hold a data store with a ref count, and allocating new objects any time a duplicate is requested.

In C++, this uses a trick constructor to store a PCM instance in the data store itself. This instance can then be passed to base objects using std::shared_ptr's aliasing constructor, which means constructing a solenoid from a PCM is not allocating after the 1st one.

This did require removing sendable from PCM. A compressor class was added back in to act as sendable for the PCM.

After this change is finished, the only change RobotBuilder and Team Code would require is passing a module type to solenoid constructors.

Co-authored-by: sciencewhiz <sciencewhiz@users.noreply.github.com>
2021-09-16 18:50:27 -07:00

76 lines
2.5 KiB
C++

// 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 <hal/HAL.h>
#include "frc/DoubleSolenoid.h"
#include "frc/PneumaticsControlModule.h"
#include "frc/Solenoid.h"
#include "gtest/gtest.h"
namespace frc {
TEST(DoubleSolenoidTest, ValidInitialization) {
DoubleSolenoid solenoid{3, frc::PneumaticsModuleType::CTREPCM, 2, 3};
solenoid.Set(DoubleSolenoid::kReverse);
EXPECT_EQ(DoubleSolenoid::kReverse, solenoid.Get());
solenoid.Set(DoubleSolenoid::kForward);
EXPECT_EQ(DoubleSolenoid::kForward, solenoid.Get());
solenoid.Set(DoubleSolenoid::kOff);
EXPECT_EQ(DoubleSolenoid::kOff, solenoid.Get());
}
TEST(DoubleSolenoidTest, ThrowForwardPortAlreadyInitialized) {
// Single solenoid that is reused for forward port
Solenoid solenoid{5, frc::PneumaticsModuleType::CTREPCM, 2};
EXPECT_THROW(DoubleSolenoid(5, frc::PneumaticsModuleType::CTREPCM, 2, 3),
std::runtime_error);
}
TEST(DoubleSolenoidTest, ThrowReversePortAlreadyInitialized) {
// Single solenoid that is reused for forward port
Solenoid solenoid{6, frc::PneumaticsModuleType::CTREPCM, 3};
EXPECT_THROW(DoubleSolenoid(6, frc::PneumaticsModuleType::CTREPCM, 2, 3),
std::runtime_error);
}
TEST(DoubleSolenoidTest, ThrowBothPortsAlreadyInitialized) {
PneumaticsControlModule pcm{6};
// Single solenoid that is reused for forward port
Solenoid solenoid0(6, frc::PneumaticsModuleType::CTREPCM, 2);
Solenoid solenoid1(6, frc::PneumaticsModuleType::CTREPCM, 3);
EXPECT_THROW(DoubleSolenoid(6, frc::PneumaticsModuleType::CTREPCM, 2, 3),
std::runtime_error);
}
TEST(DoubleSolenoidTest, Toggle) {
DoubleSolenoid solenoid{4, frc::PneumaticsModuleType::CTREPCM, 2, 3};
// Bootstrap it into reverse
solenoid.Set(DoubleSolenoid::kReverse);
solenoid.Toggle();
EXPECT_EQ(DoubleSolenoid::kForward, solenoid.Get());
solenoid.Toggle();
EXPECT_EQ(DoubleSolenoid::kReverse, solenoid.Get());
// Of shouldn't do anything on toggle
solenoid.Set(DoubleSolenoid::kOff);
solenoid.Toggle();
EXPECT_EQ(DoubleSolenoid::kOff, solenoid.Get());
}
TEST(DoubleSolenoidTest, InvalidForwardPort) {
EXPECT_THROW(DoubleSolenoid(0, frc::PneumaticsModuleType::CTREPCM, 100, 1),
std::runtime_error);
}
TEST(DoubleSolenoidTest, InvalidReversePort) {
EXPECT_THROW(DoubleSolenoid(0, frc::PneumaticsModuleType::CTREPCM, 0, 100),
std::runtime_error);
}
} // namespace frc