Repo created

This commit is contained in:
Fr4nz D13trich 2025-11-22 13:58:55 +01:00
parent 4af19165ec
commit 68073add76
12458 changed files with 12350765 additions and 2 deletions

View file

@ -0,0 +1,16 @@
project(tracking_tests)
set(
SRC
archival_reporter_tests.cpp
protocol_test.cpp
reporter_test.cpp
)
omim_add_test(${PROJECT_NAME} ${SRC})
omim_link_libraries(${PROJECT_NAME}
platform_tests_support
tracking
routing
)

View file

@ -0,0 +1,164 @@
#include "testing/testing.hpp"
#include "tracking/archival_file.hpp"
#include "tracking/archive.hpp"
#include "platform/platform.hpp"
#include "coding/file_reader.hpp"
#include "coding/file_writer.hpp"
#include "geometry/latlon.hpp"
#include "base/assert.hpp"
#include "base/file_name_utils.hpp"
#include "base/logging.hpp"
#include "base/stl_helpers.hpp"
#include "base/timer.hpp"
#include <chrono>
#include <random>
using namespace tracking;
namespace
{
constexpr size_t kItemsForDump = 2 * 60 * 60; // 2 hours of travelling
constexpr double kAccuracyEps = 1e-4;
location::GpsInfo GetStartingPoint()
{
location::GpsInfo start;
start.m_timestamp = 1573227904;
start.m_horizontalAccuracy = 5;
start.m_latitude = 11.67281;
start.m_longitude = 5.22804;
return start;
}
void UpdateSpeedGroup(traffic::SpeedGroup & sg)
{
static std::random_device randomDevice;
static std::mt19937 gen(randomDevice());
static std::uniform_int_distribution<> dis(0, base::Underlying(traffic::SpeedGroup::Count));
sg = static_cast<traffic::SpeedGroup>(dis(gen));
}
void UpdateLocation(location::GpsInfo & loc)
{
static std::random_device randomDevice;
static std::mt19937 gen(randomDevice());
static std::uniform_int_distribution<> dis(0, 50);
loc.m_latitude += dis(gen) * 0.0001;
loc.m_longitude += dis(gen) * 0.0001;
CHECK_GREATER_OR_EQUAL(loc.m_latitude, ms::LatLon::kMinLat, ());
CHECK_LESS_OR_EQUAL(loc.m_latitude, ms::LatLon::kMaxLat, ());
CHECK_GREATER_OR_EQUAL(loc.m_longitude, ms::LatLon::kMinLon, ());
CHECK_LESS_OR_EQUAL(loc.m_longitude, ms::LatLon::kMaxLon, ());
double constexpr kMinIntervalBetweenLocationsS = 3.0;
loc.m_timestamp += kMinIntervalBetweenLocationsS + dis(gen);
}
UNIT_TEST(PacketCar_OperationsConsistency)
{
BasicArchive<PacketCar> archive(kItemsForDump, 1.0 /* m_minDelaySeconds */);
location::GpsInfo point = GetStartingPoint();
traffic::SpeedGroup sg = traffic::SpeedGroup::G0;
base::HighResTimer timer;
for (size_t i = 0; i < kItemsForDump; ++i)
{
archive.Add(point, sg);
UpdateLocation(point);
UpdateSpeedGroup(sg);
}
auto const track = archive.Extract();
LOG(LINFO, ("Duration of dumping", timer.ElapsedMilliseconds(), "ms"));
timer.Reset();
std::string const fileName = "archival_reporter_car.track";
{
FileWriter writer(fileName);
CHECK(archive.Write(writer), ());
}
LOG(LINFO, ("Duration of serializing", timer.ElapsedMilliseconds(), "ms"));
uint64_t sizeBytes;
CHECK(GetPlatform().GetFileSizeByFullPath(fileName, sizeBytes), ());
LOG(LINFO, ("File size", sizeBytes, "bytes"));
FileReader reader(fileName);
ReaderSource<FileReader> src(reader);
CHECK(archive.Read(src), ());
CHECK_EQUAL(kItemsForDump, archive.Size(), ());
auto const newTrack = archive.Extract();
for (size_t i = 0; i < kItemsForDump; ++i)
{
TEST_ALMOST_EQUAL_ABS(track[i].m_lat, newTrack[i].m_lat, kAccuracyEps, ("failed index", i));
TEST_ALMOST_EQUAL_ABS(track[i].m_lon, newTrack[i].m_lon, kAccuracyEps, ("failed index", i));
CHECK_EQUAL(track[i].m_timestamp, newTrack[i].m_timestamp, ("failed index", i));
CHECK_EQUAL(track[i].m_speedGroup, newTrack[i].m_speedGroup, ("failed index", i));
}
}
UNIT_TEST(PacketPedestrianBicycle_OperationsConsistency)
{
BasicArchive<Packet> archive(kItemsForDump, 3.0 /* m_minDelaySeconds */);
location::GpsInfo point = GetStartingPoint();
for (size_t i = 0; i < kItemsForDump; ++i)
{
archive.Add(point);
UpdateLocation(point);
}
auto const track = archive.Extract();
std::string const fileName = "archival_reporter_pedestrian_bicycle.track";
{
FileWriter writer(fileName);
CHECK(archive.Write(writer), (fileName));
}
uint64_t sizeBytes;
CHECK(GetPlatform().GetFileSizeByFullPath(fileName, sizeBytes), (fileName, sizeBytes));
LOG(LINFO, ("File size", sizeBytes, "bytes"));
FileReader reader(fileName);
ReaderSource<FileReader> src(reader);
CHECK(archive.Read(src), ());
CHECK_EQUAL(kItemsForDump, archive.Size(), ());
auto const newTrack = archive.Extract();
for (size_t i = 0; i < kItemsForDump; ++i)
{
TEST_ALMOST_EQUAL_ABS(track[i].m_lat, newTrack[i].m_lat, kAccuracyEps, ("failed index", i));
TEST_ALMOST_EQUAL_ABS(track[i].m_lon, newTrack[i].m_lon, kAccuracyEps, ("failed index", i));
CHECK_EQUAL(track[i].m_timestamp, newTrack[i].m_timestamp, ("failed index", i));
}
}
UNIT_TEST(ArchiveName_Create)
{
routing::RouterType const routerType = routing::RouterType::Pedestrian;
uint32_t const version = 1;
std::chrono::seconds const timestamp(1573635326);
std::string const filename = tracking::archival_file::GetArchiveFilename(version, timestamp, routerType);
CHECK_EQUAL(filename, "1_1573635326_1.track", ());
}
UNIT_TEST(ArchiveName_Extract)
{
std::string const filename = "1_1573635326_2.track";
auto const meta = tracking::archival_file::ParseArchiveFilename(filename);
CHECK_EQUAL(meta.m_protocolVersion, 1, ());
CHECK_EQUAL(meta.m_timestamp, 1573635326, ());
CHECK_EQUAL(meta.m_trackType, routing::RouterType::Bicycle, ());
}
} // namespace

View file

@ -0,0 +1,142 @@
#include "testing/testing.hpp"
#include "tracking/protocol.hpp"
#include <string>
using namespace std;
using namespace tracking;
UNIT_TEST(Protocol_CreateAuthPacket)
{
auto packet = Protocol::CreateAuthPacket("ABC");
TEST_EQUAL(packet.size(), 7, ());
TEST_EQUAL(Protocol::PacketType(packet[0]), Protocol::PacketType::CurrentAuth, ());
TEST_EQUAL(packet[1], 0x00, ());
TEST_EQUAL(packet[2], 0x00, ());
TEST_EQUAL(packet[3], 0x03, ());
TEST_EQUAL(packet[4], 'A', ());
TEST_EQUAL(packet[5], 'B', ());
TEST_EQUAL(packet[6], 'C', ());
}
UNIT_TEST(Protocol_DecodeHeader)
{
string id_str("ABC");
auto packet = Protocol::CreateAuthPacket(id_str);
TEST_EQUAL(packet.size(), 7, ());
TEST_EQUAL(Protocol::PacketType(packet[0]), Protocol::PacketType::CurrentAuth, ());
{
auto header = Protocol::DecodeHeader(packet);
TEST_EQUAL(header.first, Protocol::PacketType::CurrentAuth, ());
TEST_EQUAL(header.second, id_str.size(), ());
}
{
auto header = Protocol::DecodeHeader({});
TEST_EQUAL(header.first, Protocol::PacketType::Error, ());
TEST_EQUAL(header.second, 0, ());
}
{
auto header = Protocol::DecodeHeader({7, 9});
TEST_EQUAL(header.first, Protocol::PacketType::Error, ());
TEST_EQUAL(header.second, 2, ());
}
}
UNIT_TEST(Protocol_CreateDataPacket)
{
using Container = Protocol::DataElementsCirc;
Container buffer(5);
buffer.push_back(Container::value_type(1, ms::LatLon(10, 10), 1));
buffer.push_back(Container::value_type(2, ms::LatLon(15, 15), 2));
auto packetV0 = Protocol::CreateDataPacket(buffer, Protocol::PacketType::DataV0);
TEST_EQUAL(packetV0.size(), 26, ());
TEST_EQUAL(Protocol::PacketType(packetV0[0]), Protocol::PacketType::DataV0, ());
TEST_EQUAL(packetV0[1], 0x00, ());
TEST_EQUAL(packetV0[2], 0x00, ());
TEST_EQUAL(packetV0[3], 22, ());
TEST_EQUAL(packetV0[4], 1, ());
TEST_EQUAL(packetV0[5], 227, ());
TEST_EQUAL(packetV0[6], 241, ());
auto packetV1 = Protocol::CreateDataPacket(buffer, Protocol::PacketType::DataV1);
TEST_EQUAL(packetV1.size(), 28, ());
TEST_EQUAL(Protocol::PacketType(packetV1[0]), Protocol::PacketType::DataV1, ());
TEST_EQUAL(packetV1[1], 0x00, ());
TEST_EQUAL(packetV1[2], 0x00, ());
TEST_EQUAL(packetV1[3], 24, ());
TEST_EQUAL(packetV1[4], 1, ());
TEST_EQUAL(packetV1[5], 227, ());
TEST_EQUAL(packetV1[6], 241, ());
}
UNIT_TEST(Protocol_DecodeAuthPacket)
{
auto packet = Protocol::CreateAuthPacket("ABC");
TEST_EQUAL(packet.size(), 7, ());
TEST_EQUAL(Protocol::PacketType(packet[0]), Protocol::PacketType::CurrentAuth, ());
auto payload = vector<uint8_t>(begin(packet) + sizeof(uint32_t /* header */), end(packet));
auto result = Protocol::DecodeAuthPacket(Protocol::PacketType::CurrentAuth, payload);
TEST_EQUAL(result, "ABC", ());
}
template <typename Container>
void DecodeDataPacketVersionTest(Container const & points, Protocol::PacketType version)
{
double const kEps = 1e-5;
auto packet = Protocol::CreateDataPacket(points, version);
TEST_GREATER(packet.size(), 0, ());
TEST_EQUAL(Protocol::PacketType(packet[0]), version, ());
auto payload = vector<uint8_t>(begin(packet) + sizeof(uint32_t /* header */), end(packet));
Container result = Protocol::DecodeDataPacket(version, payload);
TEST_EQUAL(points.size(), result.size(), ());
for (size_t i = 0; i < points.size(); ++i)
{
TEST_EQUAL(points[i].m_timestamp, result[i].m_timestamp, (points[i].m_timestamp, result[i].m_timestamp));
TEST(AlmostEqualAbsOrRel(points[i].m_latLon.m_lat, result[i].m_latLon.m_lat, kEps),
(points[i].m_latLon.m_lat, result[i].m_latLon.m_lat));
TEST(AlmostEqualAbsOrRel(points[i].m_latLon.m_lon, result[i].m_latLon.m_lon, kEps),
(points[i].m_latLon.m_lon, result[i].m_latLon.m_lon));
}
}
UNIT_TEST(Protocol_DecodeDataPacket)
{
using Container = Protocol::DataElementsVec;
Container points;
points.push_back(Container::value_type(1, ms::LatLon(10, 10), 1));
points.push_back(Container::value_type(2, ms::LatLon(15, 15), 2));
DecodeDataPacketVersionTest(points, Protocol::PacketType::DataV0);
DecodeDataPacketVersionTest(points, Protocol::PacketType::DataV1);
}
UNIT_TEST(Protocol_DecodeWrongDataPacket)
{
vector<vector<uint8_t>> payloads = {
vector<uint8_t>{},
vector<uint8_t>{0x25},
vector<uint8_t>{0x0},
vector<uint8_t>{0x0, 0x0, 0x23, 0xFF},
vector<uint8_t>{0xFF, 0x1, 0x23, 0xFF, 0x1, 0x0, 0x27, 0x63, 0x32, 0x9, 0xFF},
};
for (auto const packetType : {Protocol::PacketType::DataV0, Protocol::PacketType::DataV1})
{
for (auto const & payload : payloads)
{
auto result = Protocol::DecodeDataPacket(packetType, payload);
TEST(result.empty(), (packetType, payload));
}
}
}

View file

@ -0,0 +1,106 @@
#include "testing/testing.hpp"
#include "tracking/protocol.hpp"
#include "tracking/reporter.hpp"
#include "coding/traffic.hpp"
#include "platform/location.hpp"
#include "platform/platform_tests_support/test_socket.hpp"
#include "platform/socket.hpp"
#include "base/math.hpp"
#include "base/thread.hpp"
#include <chrono>
#include <cmath>
#include <cstdint>
#include <memory>
#include <vector>
using namespace std;
using namespace std::chrono;
using namespace tracking;
using namespace platform::tests_support;
namespace
{
void TransferLocation(Reporter & reporter, TestSocket & testSocket, double timestamp, double latidute,
double longtitude)
{
location::GpsInfo gpsInfo;
gpsInfo.m_timestamp = timestamp;
gpsInfo.m_latitude = latidute;
gpsInfo.m_longitude = longtitude;
gpsInfo.m_horizontalAccuracy = 1.0;
reporter.AddLocation(gpsInfo, traffic::SpeedGroup::Unknown);
using Packet = tracking::Protocol::PacketType;
vector<uint8_t> buffer;
size_t readSize = 0;
size_t attempts = 3;
do
{
readSize = testSocket.ReadServer(buffer);
if (attempts-- && readSize == 0)
continue;
switch (Packet(buffer[0]))
{
case Packet::CurrentAuth:
{
buffer.clear();
testSocket.WriteServer(tracking::Protocol::kOk);
break;
}
case Packet::Error:
case Packet::DataV0:
case Packet::DataV1:
{
readSize = 0;
break;
}
}
}
while (readSize);
TEST(!buffer.empty(), ());
vector<coding::TrafficGPSEncoder::DataPoint> points;
MemReader memReader(buffer.data(), buffer.size());
ReaderSource<MemReader> src(memReader);
src.Skip(sizeof(uint32_t /* header */));
coding::TrafficGPSEncoder::DeserializeDataPoints(coding::TrafficGPSEncoder::kLatestVersion, src, points);
TEST_EQUAL(points.size(), 1, ());
auto const & point = points[0];
TEST_EQUAL(point.m_timestamp, timestamp, ());
TEST(AlmostEqualAbs(point.m_latLon.m_lat, latidute, 0.001), ());
TEST(AlmostEqualAbs(point.m_latLon.m_lon, longtitude, 0.001), ());
}
} // namespace
UNIT_TEST(Reporter_Smoke)
{
{
unique_ptr<platform::Socket> socket;
Reporter reporter(std::move(socket), "localhost", 0, milliseconds(10) /* pushDelay */);
}
{
auto socket = make_unique<TestSocket>();
Reporter reporter(std::move(socket), "localhost", 0, milliseconds(10) /* pushDelay */);
}
{
auto socket = platform::CreateSocket();
Reporter reporter(std::move(socket), "localhost", 0, milliseconds(10) /* pushDelay */);
}
}
UNIT_TEST(Reporter_TransferLocations)
{
auto socket = make_unique<TestSocket>();
TestSocket & testSocket = *socket.get();
Reporter reporter(std::move(socket), "localhost", 0, milliseconds(10) /* pushDelay */);
TransferLocation(reporter, testSocket, 1.0, 2.0, 3.0);
TransferLocation(reporter, testSocket, 4.0, 5.0, 6.0);
TransferLocation(reporter, testSocket, 7.0, 8.0, 9.0);
}