blob: 68f8cb2a1e9a1dc06d3cdb132f27697a2c0e6251 [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/DriverStation.h" // NOLINT(build/include_order)
6
James Kuszmaulb13e13f2023-11-22 20:44:04 -08007#include <gtest/gtest.h>
James Kuszmaulcf324122023-01-14 14:07:17 -08008#include <hal/DriverStation.h>
Austin Schuh812d0d12021-11-04 20:16:48 -07009#include <units/math.h>
10#include <units/time.h>
11
Brian Silverman8fce7482020-01-05 13:18:21 -080012#include "TestBench.h"
13#include "frc/RobotController.h"
Brian Silverman8fce7482020-01-05 13:18:21 -080014
Austin Schuh812d0d12021-11-04 20:16:48 -070015#define EXPECT_NEAR_UNITS(val1, val2, eps) \
16 EXPECT_LE(units::math::abs(val1 - val2), eps)
Brian Silverman8fce7482020-01-05 13:18:21 -080017
18/**
19 * Test if the WaitForData function works
20 */
Austin Schuh812d0d12021-11-04 20:16:48 -070021TEST(DriverStationTest, WaitForData) {
22 units::microsecond_t initialTime(frc::RobotController::GetFPGATime());
Brian Silverman8fce7482020-01-05 13:18:21 -080023
James Kuszmaulcf324122023-01-14 14:07:17 -080024 wpi::Event waitEvent{true};
25 HAL_ProvideNewDataEventHandle(waitEvent.GetHandle());
26
Austin Schuh812d0d12021-11-04 20:16:48 -070027 // 20ms waiting intervals * 50 = 1s
Brian Silverman8fce7482020-01-05 13:18:21 -080028 for (int i = 0; i < 50; i++) {
James Kuszmaulcf324122023-01-14 14:07:17 -080029 wpi::WaitForObject(waitEvent.GetHandle());
Brian Silverman8fce7482020-01-05 13:18:21 -080030 }
31
James Kuszmaulcf324122023-01-14 14:07:17 -080032 HAL_RemoveNewDataEventHandle(waitEvent.GetHandle());
33
Austin Schuh812d0d12021-11-04 20:16:48 -070034 units::microsecond_t finalTime(frc::RobotController::GetFPGATime());
Brian Silverman8fce7482020-01-05 13:18:21 -080035
Austin Schuh812d0d12021-11-04 20:16:48 -070036 EXPECT_NEAR_UNITS(1_s, finalTime - initialTime, 200_ms);
Brian Silverman8fce7482020-01-05 13:18:21 -080037}