diff --git a/wpilibc/src/test/native/cpp/NotifierTest.cpp b/wpilibc/src/test/native/cpp/NotifierTest.cpp new file mode 100644 index 0000000000..05e2e87688 --- /dev/null +++ b/wpilibc/src/test/native/cpp/NotifierTest.cpp @@ -0,0 +1,52 @@ +// 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 + +#include "frc/Notifier.h" +#include "frc/simulation/SimHooks.h" +#include "gtest/gtest.h" + +using namespace frc; + +namespace { + +class NotifierTest : public ::testing::Test { + protected: + void SetUp() override { + sim::PauseTiming(); + sim::RestartTiming(); + } + + void TearDown() override { sim::ResumeTiming(); } +}; + +} // namespace + +TEST_F(NotifierTest, StartPeriodicAndStop) { + std::atomic counter{0}; + + Notifier notifier{[&] { ++counter; }}; + notifier.StartPeriodic(1_s); + + sim::StepTiming(10.5_s); + + notifier.Stop(); + EXPECT_EQ(10u, counter); + + sim::StepTiming(3_s); + + EXPECT_EQ(10u, counter); +} + +TEST_F(NotifierTest, StartSingle) { + std::atomic counter{0}; + + Notifier notifier{[&] { ++counter; }}; + notifier.StartSingle(1_s); + + sim::StepTiming(10.5_s); + + EXPECT_EQ(1u, counter); +} diff --git a/wpilibj/src/test/java/edu/wpi/first/wpilibj/NotifierTest.java b/wpilibj/src/test/java/edu/wpi/first/wpilibj/NotifierTest.java new file mode 100644 index 0000000000..d6e16b634b --- /dev/null +++ b/wpilibj/src/test/java/edu/wpi/first/wpilibj/NotifierTest.java @@ -0,0 +1,63 @@ +// 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. + +package edu.wpi.first.wpilibj; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import edu.wpi.first.hal.HAL; +import edu.wpi.first.wpilibj.simulation.SimHooks; +import java.util.concurrent.atomic.AtomicInteger; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.parallel.ResourceLock; + +/** Tests to see if the Notifier is working properly. */ +class NotifierTest { + @BeforeEach + void setup() { + HAL.initialize(500, 0); + SimHooks.pauseTiming(); + SimHooks.restartTiming(); + } + + @AfterEach + void cleanup() { + SimHooks.resumeTiming(); + } + + @Test + @ResourceLock("timing") + void testStartPeriodicAndStop() { + AtomicInteger counter = new AtomicInteger(); + Notifier notifier = new Notifier(counter::getAndIncrement); + notifier.startPeriodic(1.0); + + SimHooks.stepTiming(10); + + notifier.stop(); + assertEquals(10, counter.get()); + + SimHooks.stepTiming(3.0); + + assertEquals(10, counter.get()); + + notifier.close(); + } + + @Test + @ResourceLock("timing") + void testStartSingle() { + AtomicInteger counter = new AtomicInteger(); + Notifier notifier = new Notifier(counter::getAndIncrement); + notifier.startSingle(1.0); + + SimHooks.stepTiming(10.5); + + assertEquals(1, counter.get()); + + notifier.close(); + } +}