One place raw sensor readings live, shared by everything that needs them
Sensor data entered the navigation runtime and disappeared into the costmap. At 0.05 m with inflation the costmap has destroyed ray order and object shape, so a dock detector fitting lines to find a V plate cannot reconstruct what the laser saw; two 0.2 m wings on that grid are one blob. A reinforcement-learning local planner wants the raw scan for the same reason. Neither had a route to it: ActionContext held only TF, and LocalPlanner::initialize takes only a costmap. The store keeps readings raw. Anything that wants filtering filters before publishing here; the store does not know a filter exists. That split is deliberate - a costmap wants outliers removed, a detector wants the true shape, and a store deciding for both would be wrong for one of them. Sharing is by shared_ptr to an immutable reading. Producers build a new one and swap the pointer rather than writing in place, so a reader copies the pointer under the mutex in tens of nanoseconds and then walks a thousand ranges with nobody able to change them underneath. Two consumers reading one topic share one copy rather than taking one each, which is what makes a megabyte depth cloud affordable to share. A bare pointer into the store would instead force every reader to hold the lock for the whole read, putting the sensor input path behind however long a detector takes. The store keeps its own mutex for the same reason: NavigationServer's already covers five kinds of data, and readers running at control-loop rate do not belong in that queue. Readers order on an arrival counter, not on the stamp. A stamp can go backwards - host restart, a switch to simulated time, a replayed bag - and a reader filtering on stamp then treats every later reading as old and freezes for good, silently. The stamp is still kept, for TF lookups and for age, which are different jobs. A consumer faster than its sensor gets null on the ticks with nothing new rather than the same reading handed back to be counted twice. The interface is type-erased because boost::dll-loaded plugins cross it. One virtual per message type would mean a new virtual per sensor, reshuffling the vtable and silently breaking every .so not rebuilt in the same pass - dlsym plus reinterpret_cast catches none of that at compile time. So identity travels as a string, payload as shared_ptr<const void>, and five virtuals cover every message type that exists or will. The typed sugar is a header-only template on the caller's side. typeid is avoided deliberately: comparing type_index across a .so depends on visibility and on how the linker merges typeinfo, and it fails quietly. Adding a message type is one line of a list that generates both the trait and the YAML short-name table, so a type can never be castable without being nameable, or the reverse. A read with the wrong identity is refused and logged once, never cast. static_pointer_cast to the wrong type reads garbage instead of crashing and surfaces somewhere unrelated; that check is the only thing standing between the erasure and it. 22 of the 24 robot_sensor_msgs types are registered. Imu and TimeReference are not, because their headers do not compile here at all - Imu.h still carries the unported ROS tail specialising ros::message_operations::Printer, and TimeReference.h declares its field as ros::Time. Both are faults in robot_sensor_msgs, and fixing them makes each one line here. Config is refused at boot rather than at first use, and an undeclared topic is dropped rather than added on the fly: an undeclared depth cloud would otherwise cost megabytes a frame and the config would stop describing what the process holds. A store with nothing declared stays silent - warning once per topic about a feature nobody switched on is how a working system starts printing errors after an upgrade. SensorConsumer is the side channel for a local planner: widening LocalPlanner::initialize would break every planner in the workspace at once for the sake of the one that wants a scan, so a planner that wants sensor data inherits this as well and the runtime finds out with a dynamic_cast.
This commit is contained in:
97
test/config/sensor_store_params.yaml
Normal file
97
test/config/sensor_store_params.yaml
Normal file
@@ -0,0 +1,97 @@
|
||||
# =================================================================================================
|
||||
# Cây config của test — KHÔNG phải bản chạy thật. Mỗi khối là một ca kiểm riêng.
|
||||
# =================================================================================================
|
||||
|
||||
# Ca thường: hai lidar, giữ bản mới nhất.
|
||||
sensor_store:
|
||||
sensors:
|
||||
- topic: /f_scan
|
||||
data_type: LaserScan # dạng ngắn
|
||||
capacity: 1
|
||||
max_age: 0.5 # [s]
|
||||
|
||||
- topic: /b_scan
|
||||
data_type: robot_sensor_msgs/LaserScan # dạng đầy đủ, phải tương đương
|
||||
capacity: 1
|
||||
max_age: 0.5
|
||||
|
||||
# Thiếu capacity và max_age -> phải rơi về default (1 và 0.5), không được từ chối.
|
||||
sensor_store_defaults:
|
||||
sensors:
|
||||
- topic: /f_scan
|
||||
data_type: LaserScan
|
||||
|
||||
# Stack frame: capacity > 1 cho consumer cần nhiều frame liên tiếp.
|
||||
sensor_store_stack:
|
||||
sensors:
|
||||
- topic: /f_scan
|
||||
data_type: LaserScan
|
||||
capacity: 5
|
||||
max_age: 0.5
|
||||
|
||||
# Nhiều loại cảm biến cùng lúc — kho không ưu ái loại nào.
|
||||
sensor_store_mixed:
|
||||
sensors:
|
||||
- topic: /f_scan
|
||||
data_type: LaserScan
|
||||
capacity: 1
|
||||
max_age: 0.5
|
||||
|
||||
- topic: /camera/depth/data
|
||||
data_type: DepthCameraData
|
||||
capacity: 1
|
||||
max_age: 0.3 # [s] depth chậm hơn lidar nhưng stale thì nguy hơn
|
||||
|
||||
- topic: /front_cloud
|
||||
data_type: PointCloud2
|
||||
capacity: 1
|
||||
max_age: 0.4
|
||||
|
||||
# Không khai sensor nào: hợp lệ, kho rỗng. Runtime không có consumer nào thì đây là câu trả lời
|
||||
# đúng, không phải lý do để không khởi động.
|
||||
sensor_store_empty:
|
||||
other_key: 1
|
||||
|
||||
# --- Các khối dưới đây đều PHẢI bị từ chối lúc boot -----------------------------------------------
|
||||
|
||||
# data_type không có thật.
|
||||
sensor_store_bad_type:
|
||||
sensors:
|
||||
- topic: /f_scan
|
||||
data_type: LazerScan
|
||||
|
||||
# thiếu khoá data_type.
|
||||
sensor_store_missing_type:
|
||||
sensors:
|
||||
- topic: /f_scan
|
||||
capacity: 1
|
||||
|
||||
# thiếu khoá topic.
|
||||
sensor_store_missing_topic:
|
||||
sensors:
|
||||
- data_type: LaserScan
|
||||
|
||||
# capacity < 1: không giữ bản nào thì consumer không bao giờ đọc được gì.
|
||||
sensor_store_bad_capacity:
|
||||
sensors:
|
||||
- topic: /f_scan
|
||||
data_type: LaserScan
|
||||
capacity: 0
|
||||
|
||||
# max_age <= 0: mọi bản đều stale ngay khi tới.
|
||||
sensor_store_bad_max_age:
|
||||
sensors:
|
||||
- topic: /f_scan
|
||||
data_type: LaserScan
|
||||
max_age: 0.0
|
||||
|
||||
# Một topic khai hai lần: một trong hai là config chết, và cái nào thắng chỉ do thứ tự khai.
|
||||
sensor_store_duplicate_topic:
|
||||
sensors:
|
||||
- topic: /f_scan
|
||||
data_type: LaserScan
|
||||
capacity: 1
|
||||
|
||||
- topic: /f_scan
|
||||
data_type: LaserScan
|
||||
capacity: 5
|
||||
187
test/sensor_store_config_test.cpp
Normal file
187
test/sensor_store_config_test.cpp
Normal file
@@ -0,0 +1,187 @@
|
||||
/*********************************************************************
|
||||
*
|
||||
* Software License Agreement (BSD License)
|
||||
*
|
||||
* Kiểm việc nạp khối `sensor_store` từ YAML.
|
||||
*
|
||||
* Mọi ca hỏng ở đây đều phải nổ LÚC BOOT. Config sai mà chỉ lộ ra giữa ca là kiểu lỗi tệ nhất: robot
|
||||
* đã ở ngoài hiện trường, và triệu chứng chỉ là một consumer im lặng không nhận được gì.
|
||||
*
|
||||
* Author: DuongTD
|
||||
*********************************************************************/
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <cstdlib>
|
||||
#include <string>
|
||||
|
||||
#include <robot/node_handle.h>
|
||||
|
||||
#include <robot_sensor_ports/sensor_store.h>
|
||||
#include <robot_sensor_ports/sensor_store_config.h>
|
||||
|
||||
namespace
|
||||
{
|
||||
using robot_sensor_ports::SensorStore;
|
||||
using robot_sensor_ports::SensorStoreConfig;
|
||||
|
||||
bool load(const std::string& ns, SensorStoreConfig& config, std::string& error)
|
||||
{
|
||||
robot::NodeHandle nh;
|
||||
return SensorStoreConfig::load(nh, ns, config, error);
|
||||
}
|
||||
|
||||
TEST(SensorStoreConfig, ReadsTheTopicsAsWritten)
|
||||
{
|
||||
SensorStoreConfig config;
|
||||
std::string error;
|
||||
|
||||
ASSERT_TRUE(load("sensor_store", config, error)) << error;
|
||||
ASSERT_EQ(config.sensors.size(), 2u);
|
||||
|
||||
EXPECT_EQ(config.sensors[0].topic, "/f_scan");
|
||||
EXPECT_EQ(config.sensors[0].capacity, 1u);
|
||||
EXPECT_DOUBLE_EQ(config.sensors[0].max_age, 0.5);
|
||||
EXPECT_EQ(config.sensors[1].topic, "/b_scan");
|
||||
}
|
||||
|
||||
TEST(SensorStoreConfig, TheShortAndFullFormsMeanTheSameThing)
|
||||
{
|
||||
SensorStoreConfig config;
|
||||
std::string error;
|
||||
|
||||
ASSERT_TRUE(load("sensor_store", config, error)) << error;
|
||||
ASSERT_EQ(config.sensors.size(), 2u);
|
||||
|
||||
// `/f_scan` khai dạng ngắn, `/b_scan` khai dạng đầy đủ. Người vận hành gõ ngắn cho nhanh, mọi
|
||||
// tầng dưới dòng này chỉ thấy một dạng duy nhất.
|
||||
EXPECT_EQ(config.sensors[0].data_type, "robot_sensor_msgs/LaserScan");
|
||||
EXPECT_EQ(config.sensors[1].data_type, "robot_sensor_msgs/LaserScan");
|
||||
}
|
||||
|
||||
TEST(SensorStoreConfig, FallsBackToDefaultsForTheOptionalKeys)
|
||||
{
|
||||
SensorStoreConfig config;
|
||||
std::string error;
|
||||
|
||||
ASSERT_TRUE(load("sensor_store_defaults", config, error)) << error;
|
||||
ASSERT_EQ(config.sensors.size(), 1u);
|
||||
|
||||
EXPECT_EQ(config.sensors[0].capacity, 1u);
|
||||
EXPECT_DOUBLE_EQ(config.sensors[0].max_age, 0.5);
|
||||
}
|
||||
|
||||
TEST(SensorStoreConfig, ReadsAFrameStackDepth)
|
||||
{
|
||||
SensorStoreConfig config;
|
||||
std::string error;
|
||||
|
||||
ASSERT_TRUE(load("sensor_store_stack", config, error)) << error;
|
||||
ASSERT_EQ(config.sensors.size(), 1u);
|
||||
EXPECT_EQ(config.sensors[0].capacity, 5u);
|
||||
}
|
||||
|
||||
TEST(SensorStoreConfig, HandlesEveryKindOfSensorTheSameWay)
|
||||
{
|
||||
SensorStoreConfig config;
|
||||
std::string error;
|
||||
|
||||
ASSERT_TRUE(load("sensor_store_mixed", config, error)) << error;
|
||||
ASSERT_EQ(config.sensors.size(), 3u);
|
||||
|
||||
EXPECT_EQ(config.sensors[0].data_type, "robot_sensor_msgs/LaserScan");
|
||||
EXPECT_EQ(config.sensors[1].data_type, "robot_sensor_msgs/DepthCameraData");
|
||||
EXPECT_EQ(config.sensors[2].data_type, "robot_sensor_msgs/PointCloud2");
|
||||
|
||||
// Và kho nhận được nguyên khối đó, không loại nào phải xử lý riêng.
|
||||
SensorStore store;
|
||||
ASSERT_TRUE(store.configure(config, error)) << error;
|
||||
EXPECT_EQ(store.inventory().size(), 3u);
|
||||
}
|
||||
|
||||
TEST(SensorStoreConfig, NoSensorSectionIsFineAndLeavesTheStoreEmpty)
|
||||
{
|
||||
SensorStoreConfig config;
|
||||
std::string error;
|
||||
|
||||
// Runtime không có consumer nào của dữ liệu thô thì không giữ gì. Đó là câu trả lời, không phải
|
||||
// lỗi, nên tuyệt đối không được chặn robot khởi động.
|
||||
ASSERT_TRUE(load("sensor_store_empty", config, error)) << error;
|
||||
EXPECT_TRUE(config.sensors.empty());
|
||||
}
|
||||
|
||||
TEST(SensorStoreConfig, RejectsADataTypeThatDoesNotExist)
|
||||
{
|
||||
SensorStoreConfig config;
|
||||
std::string error;
|
||||
|
||||
EXPECT_FALSE(load("sensor_store_bad_type", config, error));
|
||||
EXPECT_NE(error.find("LazerScan"), std::string::npos);
|
||||
// Lỗi phải chỉ ra được entry nào, và kèm danh sách tên thật.
|
||||
EXPECT_NE(error.find("/f_scan"), std::string::npos);
|
||||
EXPECT_NE(error.find("robot_sensor_msgs/LaserScan"), std::string::npos);
|
||||
}
|
||||
|
||||
TEST(SensorStoreConfig, RejectsAnEntryMissingItsKeys)
|
||||
{
|
||||
SensorStoreConfig config;
|
||||
std::string error;
|
||||
|
||||
EXPECT_FALSE(load("sensor_store_missing_type", config, error));
|
||||
EXPECT_NE(error.find("data_type"), std::string::npos);
|
||||
|
||||
EXPECT_FALSE(load("sensor_store_missing_topic", config, error));
|
||||
EXPECT_NE(error.find("topic"), std::string::npos);
|
||||
}
|
||||
|
||||
TEST(SensorStoreConfig, RejectsACapacityThatCanNeverHoldAnything)
|
||||
{
|
||||
SensorStoreConfig config;
|
||||
std::string error;
|
||||
|
||||
EXPECT_FALSE(load("sensor_store_bad_capacity", config, error));
|
||||
EXPECT_NE(error.find("capacity"), std::string::npos);
|
||||
}
|
||||
|
||||
TEST(SensorStoreConfig, RejectsAMaxAgeThatMakesEveryReadingStale)
|
||||
{
|
||||
SensorStoreConfig config;
|
||||
std::string error;
|
||||
|
||||
EXPECT_FALSE(load("sensor_store_bad_max_age", config, error));
|
||||
EXPECT_NE(error.find("max_age"), std::string::npos);
|
||||
}
|
||||
|
||||
TEST(SensorStoreConfig, RejectsTheSameTopicDeclaredTwice)
|
||||
{
|
||||
SensorStoreConfig config;
|
||||
std::string error;
|
||||
|
||||
// Một trong hai khai là config chết, và cái nào thắng chỉ do thứ tự viết. Nói ra thay vì chọn.
|
||||
EXPECT_FALSE(load("sensor_store_duplicate_topic", config, error));
|
||||
EXPECT_NE(error.find("/f_scan"), std::string::npos);
|
||||
}
|
||||
|
||||
TEST(SensorStoreConfig, ARejectedLoadLeavesThePreviousConfigAlone)
|
||||
{
|
||||
SensorStoreConfig config;
|
||||
std::string error;
|
||||
|
||||
ASSERT_TRUE(load("sensor_store", config, error)) << error;
|
||||
ASSERT_EQ(config.sensors.size(), 2u);
|
||||
|
||||
// Người gọi bỏ qua giá trị trả về vẫn còn config cũ, không phải một cây nửa vời.
|
||||
EXPECT_FALSE(load("sensor_store_bad_type", config, error));
|
||||
EXPECT_EQ(config.sensors.size(), 2u);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
#ifdef ROBOT_SENSOR_PORTS_TEST_CONFIG_DIR
|
||||
setenv("PNKX_NAV_CORE_CONFIG_DIR", ROBOT_SENSOR_PORTS_TEST_CONFIG_DIR, 0);
|
||||
#endif
|
||||
|
||||
testing::InitGoogleTest(&argc, argv);
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
||||
527
test/sensor_store_test.cpp
Normal file
527
test/sensor_store_test.cpp
Normal file
@@ -0,0 +1,527 @@
|
||||
/*********************************************************************
|
||||
*
|
||||
* Software License Agreement (BSD License)
|
||||
*
|
||||
* Kiểm kho cảm biến dùng chung.
|
||||
*
|
||||
* Bốn nhóm, theo đúng thứ tự rủi ro:
|
||||
* 1. con trỏ — hai reader có dùng chung một bản không, bản cũ có sống đủ lâu không;
|
||||
* 2. cursor — "chưa có gì mới" phải phân biệt được với "có bản mới trùng giá trị";
|
||||
* 3. từ chối — topic lạ và sai kiểu phải trả false, KHÔNG bao giờ cast bừa;
|
||||
* 4. ring — capacity và thứ tự cũ-trước-mới.
|
||||
*
|
||||
* Author: DuongTD
|
||||
*********************************************************************/
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <robot_sensor_ports/sensor_store.h>
|
||||
#include <robot_sensor_ports/sensor_types.h>
|
||||
|
||||
namespace
|
||||
{
|
||||
using robot_sensor_ports::SensorEntryConfig;
|
||||
using robot_sensor_ports::SensorInfo;
|
||||
using robot_sensor_ports::SensorSample;
|
||||
using robot_sensor_ports::SensorStore;
|
||||
using robot_sensor_ports::SensorStoreConfig;
|
||||
|
||||
using LaserScan = robot_sensor_msgs::LaserScan;
|
||||
using PointCloud2 = robot_sensor_msgs::PointCloud2;
|
||||
|
||||
SensorEntryConfig entry(const std::string& topic, const std::string& data_type,
|
||||
std::size_t capacity = 1, double max_age = 0.5)
|
||||
{
|
||||
SensorEntryConfig e;
|
||||
e.topic = topic;
|
||||
e.data_type = data_type;
|
||||
e.capacity = capacity;
|
||||
e.max_age = max_age;
|
||||
return e;
|
||||
}
|
||||
|
||||
LaserScan makeScan(float first_range)
|
||||
{
|
||||
LaserScan scan;
|
||||
scan.angle_min = -1.57f;
|
||||
scan.angle_max = 1.57f;
|
||||
scan.angle_increment = 0.01f;
|
||||
scan.range_min = 0.05f;
|
||||
scan.range_max = 10.0f;
|
||||
scan.ranges.assign(315, 3.0f);
|
||||
scan.ranges[0] = first_range;
|
||||
return scan;
|
||||
}
|
||||
|
||||
/// Kho với một topic laser, dựng sẵn cho phần lớn test.
|
||||
class StoreRig
|
||||
{
|
||||
public:
|
||||
explicit StoreRig(std::size_t capacity = 1)
|
||||
{
|
||||
SensorStoreConfig config;
|
||||
config.sensors.push_back(entry("/f_scan", "robot_sensor_msgs/LaserScan", capacity));
|
||||
std::string error;
|
||||
ok_ = store_.configure(config, error);
|
||||
error_ = error;
|
||||
}
|
||||
|
||||
bool ok_ = false;
|
||||
std::string error_;
|
||||
SensorStore store_;
|
||||
};
|
||||
|
||||
// ================================================================================================
|
||||
// 1. Chia sẻ bằng con trỏ
|
||||
// ================================================================================================
|
||||
|
||||
TEST(SensorStore, TwoReadersShareOneCopyOfTheReading)
|
||||
{
|
||||
StoreRig rig;
|
||||
ASSERT_TRUE(rig.ok_) << rig.error_;
|
||||
|
||||
ASSERT_TRUE(robot_sensor_ports::put(rig.store_, "/f_scan", makeScan(1.0f), robot::Time(10.0)));
|
||||
|
||||
uint64_t cursor_a = 0;
|
||||
uint64_t cursor_b = 0;
|
||||
auto a = robot_sensor_ports::latest<LaserScan>(rig.store_, "/f_scan", cursor_a);
|
||||
auto b = robot_sensor_ports::latest<LaserScan>(rig.store_, "/f_scan", cursor_b);
|
||||
|
||||
ASSERT_TRUE(a);
|
||||
ASSERT_TRUE(b);
|
||||
// Cùng một object, không phải hai bản copy. Đây là thứ khiến một depth cloud hàng MB chia được
|
||||
// cho nhiều consumer mà không nhân lên.
|
||||
EXPECT_EQ(a.get(), b.get());
|
||||
EXPECT_FLOAT_EQ(a->ranges[0], 1.0f);
|
||||
}
|
||||
|
||||
TEST(SensorStore, AReadingStaysValidAfterItHasBeenReplaced)
|
||||
{
|
||||
StoreRig rig;
|
||||
ASSERT_TRUE(rig.ok_) << rig.error_;
|
||||
|
||||
ASSERT_TRUE(robot_sensor_ports::put(rig.store_, "/f_scan", makeScan(1.0f), robot::Time(10.0)));
|
||||
|
||||
uint64_t cursor = 0;
|
||||
auto held = robot_sensor_ports::latest<LaserScan>(rig.store_, "/f_scan", cursor);
|
||||
ASSERT_TRUE(held);
|
||||
|
||||
// Bản mới đè lên slot trong lúc reader vẫn đang cầm bản cũ. Producer tráo con trỏ chứ không sửa
|
||||
// tại chỗ, nên bản đang cầm không được đổi một byte nào.
|
||||
ASSERT_TRUE(robot_sensor_ports::put(rig.store_, "/f_scan", makeScan(9.0f), robot::Time(10.1)));
|
||||
|
||||
EXPECT_FLOAT_EQ(held->ranges[0], 1.0f);
|
||||
}
|
||||
|
||||
// ================================================================================================
|
||||
// 2. Cursor
|
||||
// ================================================================================================
|
||||
|
||||
TEST(SensorStore, SaysNothingIsNewUntilSomethingIs)
|
||||
{
|
||||
StoreRig rig;
|
||||
ASSERT_TRUE(rig.ok_) << rig.error_;
|
||||
|
||||
uint64_t cursor = 0;
|
||||
|
||||
// Chưa có gì.
|
||||
EXPECT_FALSE(robot_sensor_ports::latest<LaserScan>(rig.store_, "/f_scan", cursor));
|
||||
|
||||
ASSERT_TRUE(robot_sensor_ports::put(rig.store_, "/f_scan", makeScan(1.0f), robot::Time(10.0)));
|
||||
ASSERT_TRUE(robot_sensor_ports::latest<LaserScan>(rig.store_, "/f_scan", cursor));
|
||||
|
||||
// Đọc lại ngay: control loop 20 Hz đọc cảm biến 15 Hz, một phần tư số tick rơi vào đây và phải
|
||||
// biết là không có gì mới, chứ không được xử lý lại đúng scan cũ rồi đếm hai lần.
|
||||
EXPECT_FALSE(robot_sensor_ports::latest<LaserScan>(rig.store_, "/f_scan", cursor));
|
||||
}
|
||||
|
||||
TEST(SensorStore, ANewReadingWithIdenticalContentStillCountsAsNew)
|
||||
{
|
||||
StoreRig rig;
|
||||
ASSERT_TRUE(rig.ok_) << rig.error_;
|
||||
|
||||
uint64_t cursor = 0;
|
||||
ASSERT_TRUE(robot_sensor_ports::put(rig.store_, "/f_scan", makeScan(1.0f), robot::Time(10.0)));
|
||||
ASSERT_TRUE(robot_sensor_ports::latest<LaserScan>(rig.store_, "/f_scan", cursor));
|
||||
|
||||
// Cùng nội dung, cùng stamp. Thứ tự đến mới là thứ quyết định, nên đây vẫn là bản mới.
|
||||
ASSERT_TRUE(robot_sensor_ports::put(rig.store_, "/f_scan", makeScan(1.0f), robot::Time(10.0)));
|
||||
EXPECT_TRUE(robot_sensor_ports::latest<LaserScan>(rig.store_, "/f_scan", cursor));
|
||||
}
|
||||
|
||||
TEST(SensorStore, AStampGoingBackwardsDoesNotFreezeTheReader)
|
||||
{
|
||||
StoreRig rig;
|
||||
ASSERT_TRUE(rig.ok_) << rig.error_;
|
||||
|
||||
uint64_t cursor = 0;
|
||||
ASSERT_TRUE(robot_sensor_ports::put(rig.store_, "/f_scan", makeScan(1.0f), robot::Time(100.0)));
|
||||
ASSERT_TRUE(robot_sensor_ports::latest<LaserScan>(rig.store_, "/f_scan", cursor));
|
||||
|
||||
// Host restart, đổi sang sim time, replay bag — stamp lùi hẳn về. Lọc theo stamp thì reader coi
|
||||
// mọi bản sau đó là cũ và đứng im vĩnh viễn mà không log gì. Lọc theo thứ tự đến thì không.
|
||||
ASSERT_TRUE(robot_sensor_ports::put(rig.store_, "/f_scan", makeScan(2.0f), robot::Time(1.0)));
|
||||
|
||||
auto fresh = robot_sensor_ports::latest<LaserScan>(rig.store_, "/f_scan", cursor);
|
||||
ASSERT_TRUE(fresh);
|
||||
EXPECT_FLOAT_EQ(fresh->ranges[0], 2.0f);
|
||||
}
|
||||
|
||||
// ================================================================================================
|
||||
// 3. Từ chối
|
||||
// ================================================================================================
|
||||
|
||||
TEST(SensorStore, DropsATopicNobodyDeclared)
|
||||
{
|
||||
StoreRig rig;
|
||||
ASSERT_TRUE(rig.ok_) << rig.error_;
|
||||
|
||||
// Không tự thêm slot: một depth cloud không ai khai sẽ ngốn hàng MB mỗi frame, và config thôi
|
||||
// không còn trả lời được câu "tiến trình này đang giữ những gì".
|
||||
EXPECT_FALSE(robot_sensor_ports::put(rig.store_, "/nobody", makeScan(1.0f), robot::Time(10.0)));
|
||||
|
||||
uint64_t cursor = 0;
|
||||
EXPECT_FALSE(robot_sensor_ports::latest<LaserScan>(rig.store_, "/nobody", cursor));
|
||||
}
|
||||
|
||||
TEST(SensorStore, AnUnconfiguredStoreIsSilent)
|
||||
{
|
||||
// Runtime chưa khai `sensor_store` thì không có kho. Cảnh báo mỗi topic một lần về một tính năng
|
||||
// chưa ai bật chính là cách một hệ đang chạy bỗng in lỗi sau khi nâng cấp.
|
||||
SensorStore store;
|
||||
EXPECT_FALSE(store.configured());
|
||||
EXPECT_FALSE(robot_sensor_ports::put(store, "/f_scan", makeScan(1.0f), robot::Time(10.0)));
|
||||
EXPECT_TRUE(store.inventory().empty());
|
||||
}
|
||||
|
||||
TEST(SensorStore, RefusesAPutOfTheWrongType)
|
||||
{
|
||||
StoreRig rig;
|
||||
ASSERT_TRUE(rig.ok_) << rig.error_;
|
||||
|
||||
PointCloud2 cloud;
|
||||
cloud.width = 4;
|
||||
|
||||
EXPECT_FALSE(robot_sensor_ports::put(rig.store_, "/f_scan", cloud, robot::Time(10.0)));
|
||||
|
||||
SensorInfo info;
|
||||
ASSERT_TRUE(rig.store_.describe("/f_scan", info));
|
||||
EXPECT_EQ(info.rejected, 1u);
|
||||
EXPECT_EQ(info.size, 0u);
|
||||
}
|
||||
|
||||
TEST(SensorStore, RefusesAReadOfTheWrongType)
|
||||
{
|
||||
StoreRig rig;
|
||||
ASSERT_TRUE(rig.ok_) << rig.error_;
|
||||
|
||||
ASSERT_TRUE(robot_sensor_ports::put(rig.store_, "/f_scan", makeScan(1.0f), robot::Time(10.0)));
|
||||
|
||||
// Đây là hàng rào duy nhất giữa type erasure và UB: cast sai kiểu đọc ra rác chứ không crash,
|
||||
// và hỏng ở một chỗ hoàn toàn khác.
|
||||
uint64_t cursor = 0;
|
||||
EXPECT_FALSE(robot_sensor_ports::latest<PointCloud2>(rig.store_, "/f_scan", cursor));
|
||||
EXPECT_EQ(cursor, 0u);
|
||||
|
||||
// Đọc đúng kiểu vẫn phải chạy sau đó.
|
||||
EXPECT_TRUE(robot_sensor_ports::latest<LaserScan>(rig.store_, "/f_scan", cursor));
|
||||
}
|
||||
|
||||
TEST(SensorStore, HasChecksTheTypeNotJustTheName)
|
||||
{
|
||||
StoreRig rig;
|
||||
ASSERT_TRUE(rig.ok_) << rig.error_;
|
||||
|
||||
EXPECT_TRUE(robot_sensor_ports::has<LaserScan>(rig.store_, "/f_scan"));
|
||||
EXPECT_FALSE(robot_sensor_ports::has<PointCloud2>(rig.store_, "/f_scan"));
|
||||
EXPECT_FALSE(robot_sensor_ports::has<LaserScan>(rig.store_, "/nobody"));
|
||||
}
|
||||
|
||||
TEST(SensorStore, RefusesAConfigItCannotHonour)
|
||||
{
|
||||
SensorStore store;
|
||||
std::string error;
|
||||
|
||||
SensorStoreConfig bad_capacity;
|
||||
bad_capacity.sensors.push_back(entry("/f_scan", "robot_sensor_msgs/LaserScan", 0));
|
||||
EXPECT_FALSE(store.configure(bad_capacity, error));
|
||||
EXPECT_NE(error.find("capacity"), std::string::npos);
|
||||
|
||||
SensorStoreConfig bad_age;
|
||||
bad_age.sensors.push_back(entry("/f_scan", "robot_sensor_msgs/LaserScan", 1, 0.0));
|
||||
EXPECT_FALSE(store.configure(bad_age, error));
|
||||
EXPECT_NE(error.find("max_age"), std::string::npos);
|
||||
|
||||
SensorStoreConfig empty_topic;
|
||||
empty_topic.sensors.push_back(entry("", "robot_sensor_msgs/LaserScan"));
|
||||
EXPECT_FALSE(store.configure(empty_topic, error));
|
||||
|
||||
// Config hỏng không được để lại kho ở trạng thái nửa vời.
|
||||
EXPECT_FALSE(store.configured());
|
||||
}
|
||||
|
||||
// ================================================================================================
|
||||
// 4. Ring
|
||||
// ================================================================================================
|
||||
|
||||
TEST(SensorStore, KeepsOnlyTheNewestWhenCapacityIsOne)
|
||||
{
|
||||
StoreRig rig(1);
|
||||
ASSERT_TRUE(rig.ok_) << rig.error_;
|
||||
|
||||
for (int i = 1; i <= 4; ++i)
|
||||
{
|
||||
ASSERT_TRUE(robot_sensor_ports::put(rig.store_, "/f_scan", makeScan(static_cast<float>(i)),
|
||||
robot::Time(10.0 + 0.1 * i)));
|
||||
}
|
||||
|
||||
const auto frames = robot_sensor_ports::history<LaserScan>(rig.store_, "/f_scan", 4);
|
||||
ASSERT_EQ(frames.size(), 1u);
|
||||
EXPECT_FLOAT_EQ(frames[0]->ranges[0], 4.0f);
|
||||
}
|
||||
|
||||
TEST(SensorStore, HandsBackAFrameStackOldestFirst)
|
||||
{
|
||||
StoreRig rig(3);
|
||||
ASSERT_TRUE(rig.ok_) << rig.error_;
|
||||
|
||||
for (int i = 1; i <= 5; ++i)
|
||||
{
|
||||
ASSERT_TRUE(robot_sensor_ports::put(rig.store_, "/f_scan", makeScan(static_cast<float>(i)),
|
||||
robot::Time(10.0 + 0.1 * i)));
|
||||
}
|
||||
|
||||
const auto frames = robot_sensor_ports::history<LaserScan>(rig.store_, "/f_scan", 3);
|
||||
ASSERT_EQ(frames.size(), 3u);
|
||||
|
||||
// Cũ trước, mới sau — đúng thứ tự đã xảy ra, để bên xếp frame không phải tự đảo (và không phải
|
||||
// nhớ đảo ở mọi chỗ gọi).
|
||||
EXPECT_FLOAT_EQ(frames[0]->ranges[0], 3.0f);
|
||||
EXPECT_FLOAT_EQ(frames[1]->ranges[0], 4.0f);
|
||||
EXPECT_FLOAT_EQ(frames[2]->ranges[0], 5.0f);
|
||||
}
|
||||
|
||||
TEST(SensorStore, GivesWhatItHasWhenTheStackIsNotFullYet)
|
||||
{
|
||||
StoreRig rig(5);
|
||||
ASSERT_TRUE(rig.ok_) << rig.error_;
|
||||
|
||||
ASSERT_TRUE(robot_sensor_ports::put(rig.store_, "/f_scan", makeScan(1.0f), robot::Time(10.0)));
|
||||
ASSERT_TRUE(robot_sensor_ports::put(rig.store_, "/f_scan", makeScan(2.0f), robot::Time(10.1)));
|
||||
|
||||
const auto frames = robot_sensor_ports::history<LaserScan>(rig.store_, "/f_scan", 5);
|
||||
EXPECT_EQ(frames.size(), 2u);
|
||||
}
|
||||
|
||||
TEST(SensorStore, HistoryOfAnEmptyOrUnknownTopicIsEmpty)
|
||||
{
|
||||
StoreRig rig(3);
|
||||
ASSERT_TRUE(rig.ok_) << rig.error_;
|
||||
|
||||
EXPECT_TRUE(robot_sensor_ports::history<LaserScan>(rig.store_, "/f_scan", 3).empty());
|
||||
EXPECT_TRUE(robot_sensor_ports::history<LaserScan>(rig.store_, "/nobody", 3).empty());
|
||||
}
|
||||
|
||||
TEST(SensorStore, ClearTopicForgetsOneTopicButKeepsItDeclared)
|
||||
{
|
||||
SensorStore store;
|
||||
SensorStoreConfig config;
|
||||
config.sensors.push_back(entry("/f_scan", "robot_sensor_msgs/LaserScan"));
|
||||
config.sensors.push_back(entry("/b_scan", "robot_sensor_msgs/LaserScan"));
|
||||
|
||||
std::string error;
|
||||
ASSERT_TRUE(store.configure(config, error)) << error;
|
||||
|
||||
ASSERT_TRUE(robot_sensor_ports::put(store, "/f_scan", makeScan(1.0f), robot::Time(10.0)));
|
||||
ASSERT_TRUE(robot_sensor_ports::put(store, "/b_scan", makeScan(2.0f), robot::Time(10.0)));
|
||||
|
||||
EXPECT_TRUE(store.clearTopic("/f_scan"));
|
||||
EXPECT_FALSE(store.clearTopic("/nobody"));
|
||||
|
||||
uint64_t cursor = 0;
|
||||
EXPECT_FALSE(robot_sensor_ports::latest<LaserScan>(store, "/f_scan", cursor));
|
||||
// Bỏ dữ liệu không phải bỏ khai báo — topic vẫn nhận được bản mới.
|
||||
EXPECT_TRUE(robot_sensor_ports::has<LaserScan>(store, "/f_scan"));
|
||||
// Và topic khác không bị đụng tới.
|
||||
uint64_t other = 0;
|
||||
EXPECT_TRUE(robot_sensor_ports::latest<LaserScan>(store, "/b_scan", other));
|
||||
}
|
||||
|
||||
TEST(SensorStore, ClearOfTypeOnlyTouchesThatKind)
|
||||
{
|
||||
SensorStore store;
|
||||
SensorStoreConfig config;
|
||||
config.sensors.push_back(entry("/f_scan", "robot_sensor_msgs/LaserScan"));
|
||||
config.sensors.push_back(entry("/cloud", "robot_sensor_msgs/PointCloud2"));
|
||||
|
||||
std::string error;
|
||||
ASSERT_TRUE(store.configure(config, error)) << error;
|
||||
|
||||
PointCloud2 cloud;
|
||||
cloud.width = 4;
|
||||
ASSERT_TRUE(robot_sensor_ports::put(store, "/f_scan", makeScan(1.0f), robot::Time(10.0)));
|
||||
ASSERT_TRUE(robot_sensor_ports::put(store, "/cloud", cloud, robot::Time(10.0)));
|
||||
|
||||
// Host bỏ dữ liệu theo từng LOẠI (removeAllLaserScans), còn kho đánh chỉ mục theo topic — đây là
|
||||
// chỗ dịch giữa hai cách.
|
||||
EXPECT_EQ(store.clearOfType(robot_sensor_ports::SensorType<LaserScan>::id()), 1u);
|
||||
|
||||
uint64_t scan_cursor = 0;
|
||||
uint64_t cloud_cursor = 0;
|
||||
EXPECT_FALSE(robot_sensor_ports::latest<LaserScan>(store, "/f_scan", scan_cursor));
|
||||
EXPECT_TRUE(robot_sensor_ports::latest<PointCloud2>(store, "/cloud", cloud_cursor));
|
||||
}
|
||||
|
||||
TEST(SensorStore, ClearingDoesNotRewindCursors)
|
||||
{
|
||||
StoreRig rig;
|
||||
ASSERT_TRUE(rig.ok_) << rig.error_;
|
||||
|
||||
uint64_t cursor = 0;
|
||||
ASSERT_TRUE(robot_sensor_ports::put(rig.store_, "/f_scan", makeScan(1.0f), robot::Time(10.0)));
|
||||
ASSERT_TRUE(robot_sensor_ports::latest<LaserScan>(rig.store_, "/f_scan", cursor));
|
||||
|
||||
rig.store_.clearTopic("/f_scan");
|
||||
|
||||
// Số đếm không quay về 0, nếu không cursor cũ sẽ trông mới hơn dữ liệu vừa tới và reader đó đứng
|
||||
// im vĩnh viễn.
|
||||
ASSERT_TRUE(robot_sensor_ports::put(rig.store_, "/f_scan", makeScan(3.0f), robot::Time(11.0)));
|
||||
auto fresh = robot_sensor_ports::latest<LaserScan>(rig.store_, "/f_scan", cursor);
|
||||
ASSERT_TRUE(fresh);
|
||||
EXPECT_FLOAT_EQ(fresh->ranges[0], 3.0f);
|
||||
}
|
||||
|
||||
// ================================================================================================
|
||||
// Chẩn đoán
|
||||
// ================================================================================================
|
||||
|
||||
TEST(SensorStore, InventoryNamesEveryTopicSoAMistypedOneCanBeFound)
|
||||
{
|
||||
SensorStore store;
|
||||
SensorStoreConfig config;
|
||||
config.sensors.push_back(entry("/f_scan", "robot_sensor_msgs/LaserScan"));
|
||||
config.sensors.push_back(entry("/b_scan", "robot_sensor_msgs/LaserScan"));
|
||||
|
||||
std::string error;
|
||||
ASSERT_TRUE(store.configure(config, error)) << error;
|
||||
|
||||
const std::vector<SensorInfo> all = store.inventory();
|
||||
ASSERT_EQ(all.size(), 2u);
|
||||
|
||||
// Đây là thứ biến "action hết giờ" thành "ông xin /front_scan, kho có /f_scan và /b_scan".
|
||||
EXPECT_EQ(all[0].topic, "/b_scan");
|
||||
EXPECT_EQ(all[1].topic, "/f_scan");
|
||||
EXPECT_EQ(all[0].data_type, "robot_sensor_msgs/LaserScan");
|
||||
EXPECT_EQ(all[0].sequence, 0u);
|
||||
}
|
||||
|
||||
TEST(SensorStore, DescribeCarriesTheFreshnessPolicySoConsumersNeedNotRedeclareIt)
|
||||
{
|
||||
SensorStore store;
|
||||
SensorStoreConfig config;
|
||||
config.sensors.push_back(entry("/f_scan", "robot_sensor_msgs/LaserScan", 2, 0.3));
|
||||
|
||||
std::string error;
|
||||
ASSERT_TRUE(store.configure(config, error)) << error;
|
||||
|
||||
ASSERT_TRUE(robot_sensor_ports::put(store, "/f_scan", makeScan(1.0f), robot::Time(10.0)));
|
||||
|
||||
SensorInfo info;
|
||||
ASSERT_TRUE(store.describe("/f_scan", info));
|
||||
EXPECT_DOUBLE_EQ(info.max_age, 0.3);
|
||||
EXPECT_EQ(info.capacity, 2u);
|
||||
EXPECT_EQ(info.size, 1u);
|
||||
EXPECT_EQ(info.sequence, 1u);
|
||||
EXPECT_DOUBLE_EQ(info.stamp.toSec(), 10.0);
|
||||
|
||||
EXPECT_FALSE(store.describe("/nobody", info));
|
||||
}
|
||||
|
||||
TEST(SensorStore, MeasuresTheRateFromStampsAndIgnoresAGap)
|
||||
{
|
||||
StoreRig rig;
|
||||
ASSERT_TRUE(rig.ok_) << rig.error_;
|
||||
|
||||
// 10 Hz đều.
|
||||
for (int i = 0; i < 10; ++i)
|
||||
{
|
||||
ASSERT_TRUE(robot_sensor_ports::put(rig.store_, "/f_scan", makeScan(1.0f),
|
||||
robot::Time(10.0 + 0.1 * i)));
|
||||
}
|
||||
|
||||
SensorInfo info;
|
||||
ASSERT_TRUE(rig.store_.describe("/f_scan", info));
|
||||
EXPECT_NEAR(info.rate_hz, 10.0, 0.5);
|
||||
|
||||
// Cảm biến chết 30 giây rồi sống lại. Trung bình cả khoảng nghỉ vào là báo một con số chưa từng
|
||||
// xảy ra, nên khoảng đó bị bỏ qua và con số cũ giữ nguyên.
|
||||
ASSERT_TRUE(robot_sensor_ports::put(rig.store_, "/f_scan", makeScan(1.0f), robot::Time(40.0)));
|
||||
ASSERT_TRUE(rig.store_.describe("/f_scan", info));
|
||||
EXPECT_NEAR(info.rate_hz, 10.0, 0.5);
|
||||
}
|
||||
|
||||
TEST(SensorStore, ClearDropsTheDataButKeepsCursorsMonotonic)
|
||||
{
|
||||
StoreRig rig;
|
||||
ASSERT_TRUE(rig.ok_) << rig.error_;
|
||||
|
||||
uint64_t cursor = 0;
|
||||
ASSERT_TRUE(robot_sensor_ports::put(rig.store_, "/f_scan", makeScan(1.0f), robot::Time(10.0)));
|
||||
ASSERT_TRUE(robot_sensor_ports::latest<LaserScan>(rig.store_, "/f_scan", cursor));
|
||||
|
||||
rig.store_.clear();
|
||||
|
||||
SensorInfo info;
|
||||
ASSERT_TRUE(rig.store_.describe("/f_scan", info));
|
||||
EXPECT_EQ(info.size, 0u);
|
||||
EXPECT_TRUE(rig.store_.configured());
|
||||
|
||||
// Đếm tiếp chứ không quay về 0: reset số đếm sẽ làm cursor cũ trông mới hơn dữ liệu vừa tới, và
|
||||
// reader đó đứng im vĩnh viễn.
|
||||
ASSERT_TRUE(robot_sensor_ports::put(rig.store_, "/f_scan", makeScan(2.0f), robot::Time(11.0)));
|
||||
auto fresh = robot_sensor_ports::latest<LaserScan>(rig.store_, "/f_scan", cursor);
|
||||
ASSERT_TRUE(fresh);
|
||||
EXPECT_FLOAT_EQ(fresh->ranges[0], 2.0f);
|
||||
}
|
||||
|
||||
TEST(SensorStore, ZeroCopyPutSharesTheCallersObject)
|
||||
{
|
||||
StoreRig rig;
|
||||
ASSERT_TRUE(rig.ok_) << rig.error_;
|
||||
|
||||
// Đường dành cho producer đã cầm sẵn ConstPtr — addDepthCameraData nhận đúng dạng đó, copy một
|
||||
// megabyte để cất là phí thuần tuý.
|
||||
boost::shared_ptr<const LaserScan> owned = boost::make_shared<const LaserScan>(makeScan(7.0f));
|
||||
ASSERT_TRUE(robot_sensor_ports::put(rig.store_, "/f_scan", owned, robot::Time(10.0)));
|
||||
|
||||
uint64_t cursor = 0;
|
||||
auto read_back = robot_sensor_ports::latest<LaserScan>(rig.store_, "/f_scan", cursor);
|
||||
ASSERT_TRUE(read_back);
|
||||
EXPECT_EQ(read_back.get(), owned.get());
|
||||
}
|
||||
|
||||
TEST(SensorStore, ReadingThroughTheReadOnlyPortWorksTheSame)
|
||||
{
|
||||
StoreRig rig;
|
||||
ASSERT_TRUE(rig.ok_) << rig.error_;
|
||||
|
||||
ASSERT_TRUE(robot_sensor_ports::put(rig.store_, "/f_scan", makeScan(5.0f), robot::Time(10.0)));
|
||||
|
||||
// Đây là thứ plugin thật sự cầm: cổng chỉ-đọc, không xoá được kho, không thay được topic nào.
|
||||
const robot_sensor_ports::SensorReader& reader = rig.store_;
|
||||
|
||||
uint64_t cursor = 0;
|
||||
auto scan = robot_sensor_ports::latest<LaserScan>(reader, "/f_scan", cursor);
|
||||
ASSERT_TRUE(scan);
|
||||
EXPECT_FLOAT_EQ(scan->ranges[0], 5.0f);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
testing::InitGoogleTest(&argc, argv);
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
||||
136
test/sensor_type_test.cpp
Normal file
136
test/sensor_type_test.cpp
Normal file
@@ -0,0 +1,136 @@
|
||||
/*********************************************************************
|
||||
*
|
||||
* Software License Agreement (BSD License)
|
||||
*
|
||||
* Kiểm định danh kiểu và bảng tên ngắn.
|
||||
*
|
||||
* Trọng tâm là hai đường từ chối: tên không có thật, và tên ngắn bị hai package cùng dùng. Cái thứ
|
||||
* hai là lý do bảng này tồn tại thay vì so chuỗi thẳng — đoán bừa giữa hai kiểu nghĩa là cất một
|
||||
* message khác hẳn thứ người vận hành định cất.
|
||||
*
|
||||
* Author: DuongTD
|
||||
*********************************************************************/
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <robot_sensor_ports/sensor_types.h>
|
||||
|
||||
namespace
|
||||
{
|
||||
using robot_sensor_ports::SensorType;
|
||||
using robot_sensor_ports::TypeNames;
|
||||
|
||||
/// Một kiểu giả để dựng ca đụng tên ngắn. Trùng nửa sau với robot_sensor_msgs/Temperature.
|
||||
struct FakeTemperature
|
||||
{
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace robot_sensor_ports
|
||||
{
|
||||
ROBOT_SENSOR_PORTS_REGISTER_TYPE(::FakeTemperature, "test_msgs/Temperature")
|
||||
} // namespace robot_sensor_ports
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
TEST(SensorType, GivesTheFullIdentityAtCompileTime)
|
||||
{
|
||||
EXPECT_STREQ(SensorType<robot_sensor_msgs::LaserScan>::id(), "robot_sensor_msgs/LaserScan");
|
||||
EXPECT_STREQ(SensorType<robot_sensor_msgs::PointCloud2>::id(), "robot_sensor_msgs/PointCloud2");
|
||||
EXPECT_STREQ(SensorType<robot_sensor_msgs::DepthCameraData>::id(),
|
||||
"robot_sensor_msgs/DepthCameraData");
|
||||
}
|
||||
|
||||
TEST(TypeNames, ResolvesTheShortFormAnOperatorTypes)
|
||||
{
|
||||
std::string full;
|
||||
std::string error;
|
||||
|
||||
ASSERT_TRUE(TypeNames::resolve("LaserScan", full, error)) << error;
|
||||
EXPECT_EQ(full, "robot_sensor_msgs/LaserScan");
|
||||
}
|
||||
|
||||
TEST(TypeNames, AcceptsTheFullFormToo)
|
||||
{
|
||||
std::string full;
|
||||
std::string error;
|
||||
|
||||
ASSERT_TRUE(TypeNames::resolve("robot_sensor_msgs/DepthCameraData", full, error)) << error;
|
||||
EXPECT_EQ(full, "robot_sensor_msgs/DepthCameraData");
|
||||
}
|
||||
|
||||
TEST(TypeNames, RejectsAnUnknownNameAndListsWhatExists)
|
||||
{
|
||||
std::string full;
|
||||
std::string error;
|
||||
|
||||
EXPECT_FALSE(TypeNames::resolve("LazerScan", full, error));
|
||||
EXPECT_NE(error.find("unknown"), std::string::npos);
|
||||
// Người gõ sai cần thấy danh sách tên thật ngay tại dòng lỗi, không phải đi tìm trong header.
|
||||
EXPECT_NE(error.find("robot_sensor_msgs/LaserScan"), std::string::npos);
|
||||
}
|
||||
|
||||
TEST(TypeNames, RejectsAnEmptyName)
|
||||
{
|
||||
std::string full;
|
||||
std::string error;
|
||||
|
||||
EXPECT_FALSE(TypeNames::resolve("", full, error));
|
||||
}
|
||||
|
||||
TEST(TypeNames, RefusesToGuessWhenTwoPackagesShareAShortName)
|
||||
{
|
||||
TypeNames::add("test_msgs/Temperature");
|
||||
|
||||
std::string full;
|
||||
std::string error;
|
||||
|
||||
// `Temperature` giờ khớp cả robot_sensor_msgs lẫn test_msgs. Chọn một cái là cất nhầm kiểu trong
|
||||
// im lặng.
|
||||
EXPECT_FALSE(TypeNames::resolve("Temperature", full, error));
|
||||
EXPECT_NE(error.find("ambiguous"), std::string::npos);
|
||||
EXPECT_NE(error.find("robot_sensor_msgs/Temperature"), std::string::npos);
|
||||
EXPECT_NE(error.find("test_msgs/Temperature"), std::string::npos);
|
||||
}
|
||||
|
||||
TEST(TypeNames, TheFullFormStillWorksWhenTheShortOneIsAmbiguous)
|
||||
{
|
||||
TypeNames::add("test_msgs/Temperature");
|
||||
|
||||
std::string full;
|
||||
std::string error;
|
||||
|
||||
// Người đã gõ dạng đầy đủ thì không có gì để đoán — phải nhận, kể cả khi nửa sau bị trùng.
|
||||
ASSERT_TRUE(TypeNames::resolve("robot_sensor_msgs/Temperature", full, error)) << error;
|
||||
EXPECT_EQ(full, "robot_sensor_msgs/Temperature");
|
||||
}
|
||||
|
||||
TEST(TypeNames, KnowsEveryTypeInTheBuiltInList)
|
||||
{
|
||||
const std::vector<std::string> all = TypeNames::all();
|
||||
|
||||
// 22 loại của robot_sensor_msgs — hai loại còn lại (Imu, TimeReference) có header hỏng sẵn trong
|
||||
// chính robot_sensor_msgs, xem sensor_types.h. Danh sách sinh ra bảng này cũng sinh ra các
|
||||
// specialisation của SensorType, nên hai bên không thể lệch nhau — test này canh cả hai.
|
||||
EXPECT_GE(all.size(), 22u);
|
||||
|
||||
for (const char* expected : {"robot_sensor_msgs/LaserScan", "robot_sensor_msgs/PointCloud2",
|
||||
"robot_sensor_msgs/DepthCameraData", "robot_sensor_msgs/CameraInfo",
|
||||
"robot_sensor_msgs/JointState", "robot_sensor_msgs/Image"})
|
||||
{
|
||||
EXPECT_NE(std::find(all.begin(), all.end(), expected), all.end()) << expected;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
testing::InitGoogleTest(&argc, argv);
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
||||
Reference in New Issue
Block a user