upload
This commit is contained in:
441
test/test_record_io.cc
Normal file
441
test/test_record_io.cc
Normal file
@@ -0,0 +1,441 @@
|
||||
#include "vslam/record_io.h"
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <chrono>
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <fstream>
|
||||
#include <limits>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
#include "vslam/record_format.h"
|
||||
|
||||
using namespace vslam;
|
||||
|
||||
namespace {
|
||||
|
||||
std::string TempPath(const char* name) {
|
||||
return std::string("/tmp/vslam_test_") + name + "_" +
|
||||
std::to_string(::getpid()) + ".vsr";
|
||||
}
|
||||
|
||||
LaserScan MakeScan() {
|
||||
LaserScan s;
|
||||
s.header.frame_id = "laser_link";
|
||||
s.header.stamp = UnixTime::fromNSec(1737000000123456789LL);
|
||||
s.header.seq = 7;
|
||||
s.angle_min = -2.356194f;
|
||||
s.angle_max = 2.356194f;
|
||||
s.angle_increment = static_cast<float>(2.0 * 2.356194 / 1080.0);
|
||||
s.scan_time = 0.04f;
|
||||
s.time_increment = s.scan_time / 1081.0f;
|
||||
s.range_min = 0.05f;
|
||||
s.range_max = 40.0f;
|
||||
s.ranges.resize(1081);
|
||||
for (size_t i = 0; i < s.ranges.size(); ++i) {
|
||||
s.ranges[i] = 1.0f + static_cast<float>(i) * 0.001f;
|
||||
}
|
||||
// Ba ngu nghia phai song sot qua vong ghi/doc.
|
||||
s.ranges[0] = std::numeric_limits<float>::quiet_NaN();
|
||||
s.ranges[1] = std::numeric_limits<float>::infinity();
|
||||
s.ranges[2] = s.range_max + 1.0f;
|
||||
return s;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST(RecordIo, RoundTripScan) {
|
||||
const std::string path = TempPath("scan");
|
||||
const LaserScan orig = MakeScan();
|
||||
|
||||
{
|
||||
RecordWriterConfig c;
|
||||
c.path = path;
|
||||
c.config_json = R"({"test":true})";
|
||||
RecordWriter w(c);
|
||||
ASSERT_TRUE(w.Open().ok());
|
||||
std::string payload;
|
||||
EncodeScanPayload(orig, &payload);
|
||||
EXPECT_TRUE(w.Write(VsrStream::kScanRaw, orig.header.stamp, payload));
|
||||
w.Close();
|
||||
}
|
||||
|
||||
RecordReader r;
|
||||
ASSERT_TRUE(r.Open(path).ok());
|
||||
EXPECT_EQ(r.info().version, kVsrVersion);
|
||||
EXPECT_EQ(r.info().config_json, R"({"test":true})");
|
||||
|
||||
VsrChunkHeader h;
|
||||
std::string payload;
|
||||
ASSERT_TRUE(r.Next(&h, &payload));
|
||||
EXPECT_EQ(h.stream, VsrStream::kScanRaw);
|
||||
EXPECT_EQ(h.stamp_ns, 1737000000123456789ULL);
|
||||
|
||||
LaserScan back;
|
||||
ASSERT_TRUE(DecodeScanPayload(payload, &back));
|
||||
EXPECT_EQ(back.header.frame_id, orig.header.frame_id);
|
||||
EXPECT_EQ(back.header.seq, orig.header.seq);
|
||||
EXPECT_EQ(back.header.stamp, orig.header.stamp);
|
||||
EXPECT_FLOAT_EQ(back.angle_min, orig.angle_min);
|
||||
EXPECT_FLOAT_EQ(back.time_increment, orig.time_increment);
|
||||
ASSERT_EQ(back.ranges.size(), orig.ranges.size());
|
||||
|
||||
// Ba ngu nghia: NaN / inf / sentinel phai giu NGUYEN VEN.
|
||||
EXPECT_TRUE(std::isnan(back.ranges[0]));
|
||||
EXPECT_TRUE(std::isinf(back.ranges[1]));
|
||||
EXPECT_FLOAT_EQ(back.ranges[2], orig.range_max + 1.0f);
|
||||
for (size_t i = 3; i < orig.ranges.size(); ++i) {
|
||||
EXPECT_FLOAT_EQ(back.ranges[i], orig.ranges[i]) << "tia " << i;
|
||||
}
|
||||
|
||||
EXPECT_FALSE(r.Next(&h, &payload));
|
||||
EXPECT_FALSE(r.truncated()) << "het file binh thuong khong phai la cut";
|
||||
r.Close();
|
||||
std::remove(path.c_str());
|
||||
}
|
||||
|
||||
TEST(RecordIo, RoundTripImuVaEncoder) {
|
||||
const std::string path = TempPath("imu");
|
||||
{
|
||||
RecordWriterConfig c;
|
||||
c.path = path;
|
||||
RecordWriter w(c);
|
||||
ASSERT_TRUE(w.Open().ok());
|
||||
|
||||
Imu imu;
|
||||
imu.header.frame_id = "imu_link";
|
||||
imu.header.stamp = UnixTime::fromSec(100.5);
|
||||
imu.angular_velocity = Vector3{0.01, -0.02, 0.03};
|
||||
imu.linear_acceleration = Vector3{0.1, -0.2, 9.81};
|
||||
imu.angular_velocity_covariance[0] = 2.4e-7;
|
||||
std::string p1;
|
||||
EncodeImuPayload(imu, &p1);
|
||||
w.Write(VsrStream::kImu, imu.header.stamp, p1);
|
||||
|
||||
std::string p2;
|
||||
EncodeEncoderPayload(12.345, -6.789, &p2);
|
||||
w.Write(VsrStream::kEncoder, UnixTime::fromSec(100.6), p2);
|
||||
w.Close();
|
||||
}
|
||||
|
||||
RecordReader r;
|
||||
ASSERT_TRUE(r.Open(path).ok());
|
||||
|
||||
VsrChunkHeader h;
|
||||
std::string payload;
|
||||
ASSERT_TRUE(r.Next(&h, &payload));
|
||||
EXPECT_EQ(h.stream, VsrStream::kImu);
|
||||
Imu imu;
|
||||
ASSERT_TRUE(DecodeImuPayload(payload, &imu));
|
||||
EXPECT_DOUBLE_EQ(imu.angular_velocity.z, 0.03);
|
||||
EXPECT_DOUBLE_EQ(imu.linear_acceleration.z, 9.81);
|
||||
EXPECT_DOUBLE_EQ(imu.angular_velocity_covariance[0], 2.4e-7);
|
||||
// MCU khong gui orientation -> phai danh dau theo quy uoc sensor_msgs.
|
||||
EXPECT_LT(imu.orientation_covariance[0], 0.0);
|
||||
|
||||
ASSERT_TRUE(r.Next(&h, &payload));
|
||||
EXPECT_EQ(h.stream, VsrStream::kEncoder);
|
||||
double l = 0.0, rr = 0.0;
|
||||
ASSERT_TRUE(DecodeEncoderPayload(payload, &l, &rr));
|
||||
EXPECT_DOUBLE_EQ(l, 12.345);
|
||||
EXPECT_DOUBLE_EQ(rr, -6.789);
|
||||
|
||||
r.Close();
|
||||
std::remove(path.c_str());
|
||||
}
|
||||
|
||||
TEST(RecordIo, RoundTripImage) {
|
||||
const std::string path = TempPath("img");
|
||||
std::vector<uint8_t> pixels(320 * 240 * 2);
|
||||
for (size_t i = 0; i < pixels.size(); ++i) pixels[i] = static_cast<uint8_t>(i & 0xFF);
|
||||
|
||||
{
|
||||
RecordWriterConfig c;
|
||||
c.path = path;
|
||||
RecordWriter w(c);
|
||||
ASSERT_TRUE(w.Open().ok());
|
||||
|
||||
Image img;
|
||||
img.header.stamp = UnixTime::fromSec(50.0);
|
||||
img.header.frame_id = "camera_depth_optical_frame";
|
||||
img.width = 320;
|
||||
img.height = 240;
|
||||
img.step = 320 * 2;
|
||||
img.format = PixelFormat::kMono16;
|
||||
img.depth_scale = 0.001f;
|
||||
img.data = pixels.data();
|
||||
|
||||
std::string p;
|
||||
EncodeImagePayload(img, &p);
|
||||
w.Write(VsrStream::kDepth, img.header.stamp, p);
|
||||
w.Close();
|
||||
}
|
||||
|
||||
RecordReader r;
|
||||
ASSERT_TRUE(r.Open(path).ok());
|
||||
VsrChunkHeader h;
|
||||
std::string payload;
|
||||
ASSERT_TRUE(r.Next(&h, &payload));
|
||||
|
||||
Image back;
|
||||
std::vector<uint8_t> storage;
|
||||
ASSERT_TRUE(DecodeImagePayload(payload, &back, &storage));
|
||||
EXPECT_EQ(back.width, 320u);
|
||||
EXPECT_EQ(back.height, 240u);
|
||||
EXPECT_EQ(back.step, 640u);
|
||||
EXPECT_EQ(back.format, PixelFormat::kMono16);
|
||||
EXPECT_FLOAT_EQ(back.depth_scale, 0.001f);
|
||||
ASSERT_EQ(storage.size(), pixels.size());
|
||||
EXPECT_EQ(std::memcmp(back.data, pixels.data(), pixels.size()), 0);
|
||||
|
||||
r.Close();
|
||||
std::remove(path.c_str());
|
||||
}
|
||||
|
||||
TEST(RecordIo, NhieuChunkGiuDungThuTu) {
|
||||
const std::string path = TempPath("order");
|
||||
{
|
||||
RecordWriterConfig c;
|
||||
c.path = path;
|
||||
RecordWriter w(c);
|
||||
ASSERT_TRUE(w.Open().ok());
|
||||
for (int i = 0; i < 500; ++i) {
|
||||
std::string p;
|
||||
EncodeEncoderPayload(static_cast<double>(i), 0.0, &p);
|
||||
w.Write(VsrStream::kEncoder, UnixTime::fromSec(1.0 + i * 0.02), p);
|
||||
}
|
||||
w.Close();
|
||||
}
|
||||
|
||||
RecordReader r;
|
||||
ASSERT_TRUE(r.Open(path).ok());
|
||||
VsrChunkHeader h;
|
||||
std::string payload;
|
||||
int n = 0;
|
||||
double prev_stamp = -1.0;
|
||||
while (r.Next(&h, &payload)) {
|
||||
double l = 0.0, rr = 0.0;
|
||||
ASSERT_TRUE(DecodeEncoderPayload(payload, &l, &rr));
|
||||
EXPECT_DOUBLE_EQ(l, static_cast<double>(n));
|
||||
const double st = static_cast<double>(h.stamp_ns) * 1e-9;
|
||||
EXPECT_GT(st, prev_stamp);
|
||||
prev_stamp = st;
|
||||
n++;
|
||||
}
|
||||
EXPECT_EQ(n, 500);
|
||||
EXPECT_FALSE(r.truncated());
|
||||
EXPECT_EQ(r.chunks_read(), 500u);
|
||||
r.Close();
|
||||
std::remove(path.c_str());
|
||||
}
|
||||
|
||||
// --- File hong -------------------------------------------------------------
|
||||
|
||||
TEST(RecordIo, CrcSaiThiDungLaiChuKhongDocTiep) {
|
||||
// Mot the USB rut ra giua chung lam file hong tu diem nao do tro di. Doc
|
||||
// tiep chi cho ra rac, va rac do se di vao thuat toan ma khong ai biet.
|
||||
const std::string path = TempPath("crc");
|
||||
{
|
||||
RecordWriterConfig c;
|
||||
c.path = path;
|
||||
RecordWriter w(c);
|
||||
ASSERT_TRUE(w.Open().ok());
|
||||
for (int i = 0; i < 5; ++i) {
|
||||
std::string p;
|
||||
EncodeEncoderPayload(static_cast<double>(i), 0.0, &p);
|
||||
w.Write(VsrStream::kEncoder, UnixTime::fromSec(1.0 + i), p);
|
||||
}
|
||||
w.Close();
|
||||
}
|
||||
|
||||
// Lat mot bit trong payload cua chunk thu ba.
|
||||
{
|
||||
std::fstream f(path, std::ios::binary | std::ios::in | std::ios::out);
|
||||
ASSERT_TRUE(f.good());
|
||||
// header co dinh + config rong, roi 2 chunk (20 + 16 payload + 4 crc)
|
||||
const long off = static_cast<long>(kVsrHeaderFixedSize) + 2 * (20 + 16 + 4) + 25;
|
||||
f.seekg(off);
|
||||
char byte = 0;
|
||||
f.read(&byte, 1);
|
||||
byte ^= 0x01;
|
||||
f.seekp(off);
|
||||
f.write(&byte, 1);
|
||||
}
|
||||
|
||||
RecordReader r;
|
||||
ASSERT_TRUE(r.Open(path).ok());
|
||||
VsrChunkHeader h;
|
||||
std::string payload;
|
||||
int n = 0;
|
||||
while (r.Next(&h, &payload)) n++;
|
||||
|
||||
EXPECT_LT(n, 5) << "phai dung truoc chunk hong";
|
||||
EXPECT_TRUE(r.truncated()) << "phai bao la cut, khong im lang";
|
||||
r.Close();
|
||||
std::remove(path.c_str());
|
||||
}
|
||||
|
||||
TEST(RecordIo, FileCutGiuaChung) {
|
||||
const std::string path = TempPath("trunc");
|
||||
{
|
||||
RecordWriterConfig c;
|
||||
c.path = path;
|
||||
RecordWriter w(c);
|
||||
ASSERT_TRUE(w.Open().ok());
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
std::string p;
|
||||
EncodeEncoderPayload(static_cast<double>(i), 0.0, &p);
|
||||
w.Write(VsrStream::kEncoder, UnixTime::fromSec(1.0 + i), p);
|
||||
}
|
||||
w.Close();
|
||||
}
|
||||
|
||||
// Cat mat 10 byte cuoi -- giong the USB rut ra dot ngot.
|
||||
{
|
||||
std::ifstream in(path, std::ios::binary | std::ios::ate);
|
||||
const long size = static_cast<long>(in.tellg());
|
||||
in.close();
|
||||
ASSERT_EQ(::truncate(path.c_str(), size - 10), 0);
|
||||
}
|
||||
|
||||
RecordReader r;
|
||||
ASSERT_TRUE(r.Open(path).ok());
|
||||
VsrChunkHeader h;
|
||||
std::string payload;
|
||||
int n = 0;
|
||||
while (r.Next(&h, &payload)) n++;
|
||||
EXPECT_EQ(n, 9) << "9 chunk dau van doc duoc";
|
||||
EXPECT_TRUE(r.truncated());
|
||||
r.Close();
|
||||
std::remove(path.c_str());
|
||||
}
|
||||
|
||||
TEST(RecordIo, TuChoiFileKhongPhaiVsr) {
|
||||
const std::string path = TempPath("bad");
|
||||
{
|
||||
std::ofstream f(path, std::ios::binary);
|
||||
f << "day khong phai file vsr, chi la text thuong thoi";
|
||||
}
|
||||
RecordReader r;
|
||||
const StatusResponse st = r.Open(path);
|
||||
EXPECT_FALSE(st.ok());
|
||||
EXPECT_NE(st.message.find("magic"), std::string::npos);
|
||||
std::remove(path.c_str());
|
||||
}
|
||||
|
||||
TEST(RecordIo, TuChoiPhienBanKhacRoRang) {
|
||||
// Doi schema lam moi file expected/*.json cu vo gia tri. Tu choi ro rang
|
||||
// thay vi co doc va sinh du lieu vo nghia.
|
||||
const std::string path = TempPath("ver");
|
||||
{
|
||||
std::ofstream f(path, std::ios::binary);
|
||||
f.write(kVsrMagic, sizeof(kVsrMagic));
|
||||
const uint32_t bad_ver = 999;
|
||||
for (int i = 0; i < 4; ++i) f.put(static_cast<char>((bad_ver >> (8 * i)) & 0xFF));
|
||||
for (int i = 0; i < 4 + 8 + 4; ++i) f.put(0);
|
||||
}
|
||||
RecordReader r;
|
||||
const StatusResponse st = r.Open(path);
|
||||
EXPECT_FALSE(st.ok());
|
||||
EXPECT_NE(st.message.find("999"), std::string::npos);
|
||||
std::remove(path.c_str());
|
||||
}
|
||||
|
||||
TEST(RecordIo, KhongMoDuocDuongDanXauThiBao) {
|
||||
RecordWriterConfig c;
|
||||
c.path = "/khong/ton/tai/abc.vsr";
|
||||
RecordWriter w(c);
|
||||
EXPECT_FALSE(w.Open().ok());
|
||||
}
|
||||
|
||||
// --- Drop chu khong chan ---------------------------------------------------
|
||||
|
||||
TEST(RecordIo, HangDoiDayThiDropChuKhongChan) {
|
||||
// GHI LOG TUYET DOI KHONG DUOC NGHEN VONG AN TOAN. Mot the USB cham khong
|
||||
// duoc phep lam luong lidar_rx dung lai.
|
||||
const std::string path = TempPath("drop");
|
||||
RecordWriterConfig c;
|
||||
c.path = path;
|
||||
// Nho hon MOT chunk, nen moi chunk lon deu drop -- tat dinh, khong phu
|
||||
// thuoc vao viec luong ghi tieu thu nhanh hay cham.
|
||||
c.max_queue_bytes = 512;
|
||||
RecordWriter w(c);
|
||||
ASSERT_TRUE(w.Open().ok());
|
||||
|
||||
std::string big(2000, 'x');
|
||||
int dropped = 0;
|
||||
const auto t0 = std::chrono::steady_clock::now();
|
||||
for (int i = 0; i < 2000; ++i) {
|
||||
if (!w.Write(VsrStream::kEvent, UnixTime::fromSec(1.0 + i), big)) dropped++;
|
||||
}
|
||||
const auto dt = std::chrono::steady_clock::now() - t0;
|
||||
|
||||
EXPECT_EQ(dropped, 2000) << "chunk lon hon ca hang doi ma khong drop";
|
||||
// Khong duoc chan: 2000 lan ghi phai xong rat nhanh du hang doi day.
|
||||
EXPECT_LT(std::chrono::duration_cast<std::chrono::milliseconds>(dt).count(), 3000);
|
||||
w.Close();
|
||||
|
||||
const RecordWriterStats s = w.Stats();
|
||||
EXPECT_EQ(s.chunks_dropped, static_cast<uint64_t>(dropped));
|
||||
std::remove(path.c_str());
|
||||
}
|
||||
|
||||
TEST(RecordIo, BanGhiTuMoTaLoHongCuaChinhNo) {
|
||||
// Moi dot drop phai de lai mot su kien trong stream kEvent, de doc lai sau
|
||||
// sau thang van biet doan nao thieu du lieu va thieu bao nhieu.
|
||||
const std::string path = TempPath("selfdesc");
|
||||
{
|
||||
RecordWriterConfig c;
|
||||
c.path = path;
|
||||
// Chunk lon (1500 B) luon vuot hang doi 512 B -> drop tat dinh.
|
||||
// Chunk nho (10 B) thi lot -> no keo theo su kien tom tat so lan drop.
|
||||
c.max_queue_bytes = 512;
|
||||
RecordWriter w(c);
|
||||
ASSERT_TRUE(w.Open().ok());
|
||||
|
||||
const std::string big(1500, 'y');
|
||||
for (int i = 0; i < 50; ++i) {
|
||||
ASSERT_FALSE(w.Write(VsrStream::kEvent, UnixTime::fromSec(1.0 + i), big));
|
||||
}
|
||||
const std::string small(10, 'z');
|
||||
ASSERT_TRUE(w.Write(VsrStream::kEvent, UnixTime::fromSec(100.0), small));
|
||||
w.Close();
|
||||
}
|
||||
|
||||
RecordReader r;
|
||||
ASSERT_TRUE(r.Open(path).ok());
|
||||
VsrChunkHeader h;
|
||||
std::string payload;
|
||||
bool saw_drop_event = false;
|
||||
while (r.Next(&h, &payload)) {
|
||||
if (h.stream == VsrStream::kEvent &&
|
||||
payload.find("\"t\":\"drop\"") != std::string::npos) {
|
||||
saw_drop_event = true;
|
||||
// So lan drop phai duoc ghi lai chinh xac, khong phai chi "co drop".
|
||||
EXPECT_NE(payload.find("\"chunks\":50"), std::string::npos) << payload;
|
||||
}
|
||||
}
|
||||
EXPECT_TRUE(saw_drop_event) << "dot drop khong de lai dau vet nao";
|
||||
r.Close();
|
||||
std::remove(path.c_str());
|
||||
}
|
||||
|
||||
// --- crc32 -----------------------------------------------------------------
|
||||
|
||||
TEST(Crc32, KhopGiaTriChuan) {
|
||||
// Gia tri chuan cua CRC-32 IEEE cho "123456789".
|
||||
const std::string s = "123456789";
|
||||
EXPECT_EQ(Crc32(s.data(), s.size()), 0xCBF43926u);
|
||||
}
|
||||
|
||||
TEST(Crc32, DoiMotBitThiDoiKetQua) {
|
||||
std::string a(100, 'a');
|
||||
std::string b = a;
|
||||
b[50] ^= 0x01;
|
||||
EXPECT_NE(Crc32(a.data(), a.size()), Crc32(b.data(), b.size()));
|
||||
}
|
||||
Reference in New Issue
Block a user