This commit is contained in:
trung09122002
2026-09-23 13:36:48 +07:00
parent dbcbf84dc3
commit 0a1b1b13f1
38 changed files with 2331 additions and 138 deletions

View File

@@ -129,53 +129,146 @@ TEST(BinaryCodec, MapFullNenTotTrenBanDoThua) {
EXPECT_EQ(out.data, m.data);
}
TEST(BinaryCodec, MapPatchChiGuiVungDoi) {
OccupancyGrid a;
a.width = 100;
a.height = 100;
a.resolution = 0.05f;
a.data.assign(10000u, -1);
// --- Ban do voxel 3D -------------------------------------------------------
OccupancyGrid b = a;
for (uint32_t y = 50; y < 55; ++y) {
for (uint32_t x = 20; x < 30; ++x) b.data[y * b.width + x] = 100;
namespace {
VoxelIndex VK(int x, int y, int z) {
VoxelIndex k;
k.x = static_cast<int16_t>(x);
k.y = static_cast<int16_t>(y);
k.z = static_cast<int16_t>(z);
return k;
}
VoxelGrid MakeGrid(size_t n, uint32_t epoch = 7) {
VoxelGrid g;
g.resolution = 0.10f;
g.epoch = epoch;
for (size_t i = 0; i < n; ++i) {
g.cells.push_back(VK(static_cast<int>(i), 0, 1));
}
std::string patch;
ASSERT_TRUE(EncodeMapPatch(b, a, UnixTime::fromSec(1.0), &patch));
BinaryHeader h;
ASSERT_TRUE(DecodeHeader(patch, 0, &h));
EXPECT_EQ(h.type, BinaryType::kMapPatch);
// Vung bao la 10x5 = 50 o, nen patch phai nho hon nhieu so voi full.
std::string full;
EncodeMapFull(b, UnixTime::fromSec(1.0), &full);
EXPECT_LT(patch.size(), full.size());
return g;
}
TEST(BinaryCodec, MapPatchTuChoiKhiKichThuocDoi) {
OccupancyGrid a;
a.width = 10; a.height = 10; a.resolution = 0.05f;
a.data.assign(100u, -1);
} // namespace
OccupancyGrid b = a;
b.width = 20; b.height = 10;
b.data.assign(200u, -1);
TEST(BinaryCodec, VoxelFullRoundTripMotManh) {
VoxelGrid g = MakeGrid(3);
g.cells[1] = VK(-5, -32768, 32767); // bien cua i16, ca hai dau
std::string patch;
// Kich thuoc doi -> khong va duoc, phai gui MAP_FULL.
EXPECT_FALSE(EncodeMapPatch(b, a, UnixTime::fromSec(1.0), &patch));
std::string buf;
EncodeVoxelFull(g, UnixTime::fromSec(2.0), 0, g.cells.size(), 0, 1, &buf);
VoxelChunkInfo info;
std::vector<VoxelIndex> out;
ASSERT_TRUE(DecodeVoxelFull(buf, &info, &out));
EXPECT_EQ(info.epoch, 7u);
EXPECT_EQ(info.total, 3u);
EXPECT_EQ(info.chunk_index, 0);
EXPECT_EQ(info.chunk_count, 1);
ASSERT_EQ(out.size(), 3u);
// Dau AM phai song sot qua duong truyen -- day la bug kinh dien cua i16.
EXPECT_EQ(out[1].x, -5);
EXPECT_EQ(out[1].y, -32768);
EXPECT_EQ(out[1].z, 32767);
}
TEST(BinaryCodec, MapPatchTuChoiKhiKhongCoGiDoi) {
OccupancyGrid a;
a.width = 10; a.height = 10; a.resolution = 0.05f;
a.data.assign(100u, -1);
TEST(BinaryCodec, VoxelFullChiaManhVaRapLai) {
const VoxelGrid g = MakeGrid(1000);
// 64 byte/manh -> 32 byte con lai sau hai header -> 5 o moi manh.
const uint32_t chunk_bytes = 16 + 32 + 5 * 6;
const size_t per = VoxelCellsPerChunk(chunk_bytes);
ASSERT_EQ(per, 5u);
const uint16_t chunks = VoxelChunkCount(g, chunk_bytes);
EXPECT_EQ(chunks, 200);
std::string patch;
EXPECT_FALSE(EncodeMapPatch(a, a, UnixTime::fromSec(1.0), &patch));
std::vector<VoxelIndex> rap;
for (uint16_t i = 0; i < chunks; ++i) {
std::string buf;
EncodeVoxelFull(g, UnixTime::fromSec(1.0), static_cast<size_t>(i) * per, per,
i, chunks, &buf);
VoxelChunkInfo info;
ASSERT_TRUE(DecodeVoxelFull(buf, &info, &rap));
EXPECT_EQ(info.total, 1000u);
EXPECT_EQ(info.chunk_count, chunks);
}
ASSERT_EQ(rap.size(), 1000u);
for (size_t i = 0; i < rap.size(); ++i) EXPECT_EQ(rap[i].x, static_cast<int>(i));
}
TEST(BinaryCodec, VoxelFullManhCuoiNganHon) {
const VoxelGrid g = MakeGrid(7);
const uint32_t chunk_bytes = 16 + 32 + 5 * 6; // 5 o/manh -> 5 + 2
EXPECT_EQ(VoxelChunkCount(g, chunk_bytes), 2);
std::string buf;
EncodeVoxelFull(g, UnixTime::fromSec(1.0), 5, 5, 1, 2, &buf); // xin 5, con 2
VoxelChunkInfo info;
std::vector<VoxelIndex> out;
ASSERT_TRUE(DecodeVoxelFull(buf, &info, &out));
EXPECT_EQ(out.size(), 2u) << "khong duoc doc qua duoi mang";
EXPECT_EQ(info.total, 7u);
}
TEST(BinaryCodec, VoxelFullBanDoRongVanCoMotManh) {
// Client phai nhan duoc MOT khung de biet ban do gio rong, khong thi no giu
// mai tap cu.
const VoxelGrid g = MakeGrid(0);
EXPECT_EQ(VoxelChunkCount(g, 16384), 1);
std::string buf;
EncodeVoxelFull(g, UnixTime::fromSec(1.0), 0, 0, 0, 1, &buf);
VoxelChunkInfo info;
std::vector<VoxelIndex> out;
ASSERT_TRUE(DecodeVoxelFull(buf, &info, &out));
EXPECT_EQ(info.total, 0u);
EXPECT_TRUE(out.empty());
}
TEST(BinaryCodec, VoxelDeltaRoundTrip) {
const std::vector<VoxelIndex> added = {VK(1, 2, 3), VK(-1, -2, -3)};
const std::vector<VoxelIndex> removed = {VK(9, 9, 9)};
std::string buf;
EncodeVoxelDelta(7, 42, added, removed, UnixTime::fromSec(3.0), &buf);
EXPECT_EQ(buf.size(), 16u + 16u + 3u * 6u);
uint32_t epoch = 0, seq = 0;
std::vector<VoxelIndex> a, r;
ASSERT_TRUE(DecodeVoxelDelta(buf, &epoch, &seq, &a, &r));
EXPECT_EQ(epoch, 7u);
EXPECT_EQ(seq, 42u);
ASSERT_EQ(a.size(), 2u);
EXPECT_EQ(a[1].x, -1);
EXPECT_EQ(a[1].z, -3);
ASSERT_EQ(r.size(), 1u);
EXPECT_EQ(r[0].y, 9);
}
TEST(BinaryCodec, VoxelDeltaRongVanHopLe) {
std::string buf;
EncodeVoxelDelta(1, 0, {}, {}, UnixTime::fromSec(1.0), &buf);
uint32_t epoch = 0, seq = 0;
std::vector<VoxelIndex> a, r;
ASSERT_TRUE(DecodeVoxelDelta(buf, &epoch, &seq, &a, &r));
EXPECT_TRUE(a.empty());
EXPECT_TRUE(r.empty());
}
TEST(BinaryCodec, VoxelKhungCutBiTuChoi) {
// Khung bi cat giua chung phai tra false chu khong doc qua duoi bo dem.
VoxelGrid g = MakeGrid(10);
std::string buf;
EncodeVoxelFull(g, UnixTime::fromSec(1.0), 0, 10, 0, 1, &buf);
buf.resize(buf.size() - 7);
VoxelChunkInfo info;
std::vector<VoxelIndex> out;
EXPECT_FALSE(DecodeVoxelFull(buf, &info, &out));
}
TEST(BinaryCodec, CloudGiamMauVaBoNaN) {
PointCloudXYZ c;
for (int i = 0; i < 10000; ++i) {