blob: 03697175337de6647b4b300381489db7fe3a2407 [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 Kuszmaulcf324122023-01-14 14:07:17 -08007#include <hal/DriverStation.h>
Austin Schuh812d0d12021-11-04 20:16:48 -07008#include <units/math.h>
9#include <units/time.h>
10
Brian Silverman8fce7482020-01-05 13:18:21 -080011#include "TestBench.h"
12#include "frc/RobotController.h"
13#include "gtest/gtest.h"
14
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}