Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 1 | // 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. |
| 4 | |
| 5 | #include <hal/AnalogInput.h> |
| 6 | #include <hal/AnalogOutput.h> |
| 7 | #include <wpi/SmallVector.h> |
| 8 | |
| 9 | #include "CrossConnects.h" |
| 10 | #include "LifetimeWrappers.h" |
| 11 | #include "gtest/gtest.h" |
| 12 | |
| 13 | using namespace hlt; |
| 14 | |
| 15 | class AnalogCrossTest : public ::testing::TestWithParam<std::pair<int, int>> {}; |
| 16 | |
| 17 | TEST_P(AnalogCrossTest, AnalogCross) { |
| 18 | auto param = GetParam(); |
| 19 | |
| 20 | int32_t status = 0; |
| 21 | AnalogInputHandle input{param.first, &status}; |
| 22 | ASSERT_EQ(0, status); |
| 23 | AnalogOutputHandle output{param.second, &status}; |
| 24 | ASSERT_EQ(0, status); |
| 25 | |
| 26 | for (double i = 0; i < 5; i += 0.1) { |
| 27 | HAL_SetAnalogOutput(output, i, &status); |
| 28 | ASSERT_EQ(0, status); |
| 29 | usleep(1000); |
| 30 | ASSERT_NEAR(i, HAL_GetAnalogVoltage(input, &status), 0.01); |
| 31 | ASSERT_EQ(0, status); |
| 32 | } |
| 33 | |
| 34 | for (double i = 5; i > 0; i -= 0.1) { |
| 35 | HAL_SetAnalogOutput(output, i, &status); |
| 36 | ASSERT_EQ(0, status); |
| 37 | usleep(1000); |
| 38 | ASSERT_NEAR(i, HAL_GetAnalogVoltage(input, &status), 0.01); |
| 39 | ASSERT_EQ(0, status); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | TEST(AnalogInputTest, AllocateAll) { |
| 44 | wpi::SmallVector<AnalogInputHandle, 21> analogHandles; |
| 45 | for (int i = 0; i < HAL_GetNumAnalogInputs(); i++) { |
| 46 | int32_t status = 0; |
| 47 | analogHandles.emplace_back(AnalogInputHandle(i, &status)); |
| 48 | ASSERT_EQ(status, 0); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | TEST(AnalogInputTest, MultipleAllocateFails) { |
| 53 | int32_t status = 0; |
| 54 | AnalogInputHandle handle(0, &status); |
| 55 | ASSERT_NE(handle, HAL_kInvalidHandle); |
| 56 | ASSERT_EQ(status, 0); |
| 57 | |
| 58 | AnalogInputHandle handle2(0, &status); |
| 59 | ASSERT_EQ(handle2, HAL_kInvalidHandle); |
| 60 | ASSERT_LAST_ERROR_STATUS(status, RESOURCE_IS_ALLOCATED); |
| 61 | } |
| 62 | |
| 63 | TEST(AnalogInputTest, OverAllocateFails) { |
| 64 | int32_t status = 0; |
| 65 | AnalogInputHandle handle(HAL_GetNumAnalogInputs(), &status); |
| 66 | ASSERT_EQ(handle, HAL_kInvalidHandle); |
| 67 | ASSERT_LAST_ERROR_STATUS(status, RESOURCE_OUT_OF_RANGE); |
| 68 | } |
| 69 | |
| 70 | TEST(AnalogInputTest, UnderAllocateFails) { |
| 71 | int32_t status = 0; |
| 72 | AnalogInputHandle handle(-1, &status); |
| 73 | ASSERT_EQ(handle, HAL_kInvalidHandle); |
| 74 | ASSERT_LAST_ERROR_STATUS(status, RESOURCE_OUT_OF_RANGE); |
| 75 | } |
| 76 | |
| 77 | TEST(AnalogOutputTest, AllocateAll) { |
| 78 | wpi::SmallVector<AnalogOutputHandle, 21> analogHandles; |
| 79 | for (int i = 0; i < HAL_GetNumAnalogOutputs(); i++) { |
| 80 | int32_t status = 0; |
| 81 | analogHandles.emplace_back(AnalogOutputHandle(i, &status)); |
| 82 | ASSERT_EQ(status, 0); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | TEST(AnalogOutputTest, MultipleAllocateFails) { |
| 87 | int32_t status = 0; |
| 88 | AnalogOutputHandle handle(0, &status); |
| 89 | ASSERT_NE(handle, HAL_kInvalidHandle); |
| 90 | ASSERT_EQ(status, 0); |
| 91 | |
| 92 | AnalogOutputHandle handle2(0, &status); |
| 93 | ASSERT_EQ(handle2, HAL_kInvalidHandle); |
| 94 | ASSERT_LAST_ERROR_STATUS(status, RESOURCE_IS_ALLOCATED); |
| 95 | } |
| 96 | |
| 97 | TEST(AnalogOutputTest, OverAllocateFails) { |
| 98 | int32_t status = 0; |
| 99 | AnalogOutputHandle handle(HAL_GetNumAnalogOutputs(), &status); |
| 100 | ASSERT_EQ(handle, HAL_kInvalidHandle); |
| 101 | ASSERT_LAST_ERROR_STATUS(status, RESOURCE_OUT_OF_RANGE); |
| 102 | } |
| 103 | |
| 104 | TEST(AnalogOutputTest, UnderAllocateFails) { |
| 105 | int32_t status = 0; |
| 106 | AnalogOutputHandle handle(-1, &status); |
| 107 | ASSERT_EQ(handle, HAL_kInvalidHandle); |
| 108 | ASSERT_LAST_ERROR_STATUS(status, RESOURCE_OUT_OF_RANGE); |
| 109 | } |
| 110 | |
| 111 | INSTANTIATE_TEST_SUITE_P(AnalogCrossConnectsTests, AnalogCrossTest, |
| 112 | ::testing::ValuesIn(AnalogCrossConnects)); |