blob: eaaaf7e0faf035951d446be9bb6781adcd2ddd97 [file] [log] [blame]
Austin Schuh812d0d12021-11-04 20:16:48 -07001// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
Brian Silverman8fce7482020-01-05 13:18:21 -08004
5#include "frc/Notifier.h" // NOLINT(build/include_order)
6
Austin Schuh812d0d12021-11-04 20:16:48 -07007#include <fmt/core.h>
Brian Silverman8fce7482020-01-05 13:18:21 -08008
Brian Silverman8fce7482020-01-05 13:18:21 -08009#include "frc/Timer.h"
10#include "gtest/gtest.h"
11
Austin Schuh812d0d12021-11-04 20:16:48 -070012TEST(NotifierTest, StartPeriodicAndStop) {
13 uint32_t counter = 0;
Brian Silverman8fce7482020-01-05 13:18:21 -080014
Austin Schuh812d0d12021-11-04 20:16:48 -070015 frc::Notifier notifier{[&] { ++counter; }};
16 notifier.StartPeriodic(1_s);
Brian Silverman8fce7482020-01-05 13:18:21 -080017
Austin Schuh812d0d12021-11-04 20:16:48 -070018 frc::Wait(10.5_s);
Brian Silverman8fce7482020-01-05 13:18:21 -080019
Austin Schuh812d0d12021-11-04 20:16:48 -070020 notifier.Stop();
21 EXPECT_EQ(10u, counter) << "Received " << counter
22 << " notifications in 10.5 seconds\n";
23 fmt::print("Received {} notifications in 10.5 seconds\n", counter);
Brian Silverman8fce7482020-01-05 13:18:21 -080024
Austin Schuh812d0d12021-11-04 20:16:48 -070025 frc::Wait(3_s);
Brian Silverman8fce7482020-01-05 13:18:21 -080026
Austin Schuh812d0d12021-11-04 20:16:48 -070027 EXPECT_EQ(10u, counter) << "Received " << counter - 10
28 << " notifications in 3 seconds\n";
29 fmt::print("Received {} notifications in 3 seconds\n", counter - 10);
30}
Brian Silverman8fce7482020-01-05 13:18:21 -080031
Austin Schuh812d0d12021-11-04 20:16:48 -070032TEST(NotifierTest, StartSingle) {
33 uint32_t counter = 0;
34
35 frc::Notifier notifier{[&] { ++counter; }};
36 notifier.StartSingle(1_s);
37
38 frc::Wait(10.5_s);
39
40 EXPECT_EQ(1u, counter) << "Received " << counter
41 << " notifications in 10.5 seconds\n";
42 fmt::print("Received {} notifications in 10.5 seconds\n", counter);
Brian Silverman8fce7482020-01-05 13:18:21 -080043}