mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-20 00:51:42 +00:00
[ntcore] Add UnitTopic<T> (C++ only) (#4497)
This avoids the need for explicit value() calls (as compared to using DoubleTopic). The unit name is published as the "unit" property. Implementation note: the test needs to be in wpilibc because ntcore does not depend on wpimath.
This commit is contained in:
45
wpilibc/src/test/native/cpp/UnitNetworkTablesTest.cpp
Normal file
45
wpilibc/src/test/native/cpp/UnitNetworkTablesTest.cpp
Normal file
@@ -0,0 +1,45 @@
|
||||
// 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 <networktables/DoubleTopic.h>
|
||||
#include <networktables/NetworkTableInstance.h>
|
||||
#include <networktables/UnitTopic.h>
|
||||
#include <units/length.h>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
class UnitNetworkTablesTest : public ::testing::Test {
|
||||
public:
|
||||
UnitNetworkTablesTest() : inst{nt::NetworkTableInstance::Create()} {}
|
||||
~UnitNetworkTablesTest() override { nt::NetworkTableInstance::Destroy(inst); }
|
||||
nt::NetworkTableInstance inst;
|
||||
};
|
||||
|
||||
TEST_F(UnitNetworkTablesTest, Publish) {
|
||||
auto topic = nt::UnitTopic<units::meter_t>{inst.GetTopic("meterTest")};
|
||||
auto pub = topic.Publish();
|
||||
pub.Set(2_m);
|
||||
ASSERT_EQ(topic.GetProperty("unit"), "meter");
|
||||
ASSERT_TRUE(topic.IsMatchingUnit());
|
||||
}
|
||||
|
||||
TEST_F(UnitNetworkTablesTest, SubscribeDouble) {
|
||||
auto topic = nt::UnitTopic<units::meter_t>{inst.GetTopic("meterTest")};
|
||||
auto pub = topic.Publish();
|
||||
auto sub = inst.GetDoubleTopic("meterTest").Subscribe(0);
|
||||
ASSERT_EQ(sub.Get(), 0);
|
||||
ASSERT_EQ(sub.Get(3), 3);
|
||||
pub.Set(2_m);
|
||||
ASSERT_EQ(sub.Get(), 2);
|
||||
}
|
||||
|
||||
TEST_F(UnitNetworkTablesTest, SubscribeUnit) {
|
||||
auto topic = nt::UnitTopic<units::meter_t>{inst.GetTopic("meterTest")};
|
||||
auto pub = topic.Publish();
|
||||
auto sub = topic.Subscribe(0_m);
|
||||
ASSERT_EQ(sub.Get(), 0_m);
|
||||
ASSERT_EQ(sub.Get(3_m), 3_m);
|
||||
pub.Set(2_m);
|
||||
ASSERT_EQ(sub.Get(), 2_m);
|
||||
}
|
||||
Reference in New Issue
Block a user