1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
#include "testing/testing.hpp"
#include "coding/traffic.hpp"
#include "geometry/mercator.hpp"
#include "geometry/point2d.hpp"
#include "base/logging.hpp"
#include "base/math.hpp"
#include <cstddef>
#include <cstdint>
#include <vector>
namespace traffic_test
{
using coding::TrafficGPSEncoder;
using std::vector;
double CalculateLength(vector<TrafficGPSEncoder::DataPoint> const & path)
{
double res = 0;
for (size_t i = 1; i < path.size(); ++i)
{
auto p1 = mercator::FromLatLon(path[i - 1].m_latLon.m_lat, path[i - 1].m_latLon.m_lon);
auto p2 = mercator::FromLatLon(path[i].m_latLon.m_lat, path[i].m_latLon.m_lon);
res += mercator::DistanceOnEarth(p1, p2);
}
return res;
}
void Test(vector<TrafficGPSEncoder::DataPoint> & points)
{
double constexpr kEps = 1e-5;
for (uint32_t version = 0; version <= TrafficGPSEncoder::kLatestVersion; ++version)
{
vector<uint8_t> buf;
MemWriter<decltype(buf)> memWriter(buf);
UNUSED_VALUE(TrafficGPSEncoder::SerializeDataPoints(version, memWriter, points));
vector<TrafficGPSEncoder::DataPoint> result;
MemReader memReader(buf.data(), buf.size());
ReaderSource<MemReader> src(memReader);
TrafficGPSEncoder::DeserializeDataPoints(version, src, result);
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));
}
if (version == TrafficGPSEncoder::kLatestVersion)
{
LOG(LINFO,
("path length =", CalculateLength(points), "num points =", points.size(), "compressed size =", buf.size()));
}
}
}
UNIT_TEST(Traffic_Serialization_Smoke)
{
vector<TrafficGPSEncoder::DataPoint> data = {
{0, ms::LatLon(0.0, 1.0), 1},
{0, ms::LatLon(0.0, 2.0), 2},
};
Test(data);
}
UNIT_TEST(Traffic_Serialization_EmptyPath)
{
vector<TrafficGPSEncoder::DataPoint> data;
Test(data);
}
UNIT_TEST(Traffic_Serialization_StraightLine100m)
{
vector<TrafficGPSEncoder::DataPoint> path = {
{0, ms::LatLon(0.0, 0.0), 1},
{0, ms::LatLon(0.0, 1e-3), 2},
};
Test(path);
}
UNIT_TEST(Traffic_Serialization_StraightLine50Km)
{
vector<TrafficGPSEncoder::DataPoint> path = {
{0, ms::LatLon(0.0, 0.0), 1},
{0, ms::LatLon(0.0, 0.5), 2},
};
Test(path);
}
UNIT_TEST(Traffic_Serialization_Zigzag500m)
{
vector<TrafficGPSEncoder::DataPoint> path;
for (size_t i = 0; i < 5; ++i)
{
double const x = i * 1e-3;
double const y = i % 2 == 0 ? 0 : 1e-3;
path.emplace_back(TrafficGPSEncoder::DataPoint(0, ms::LatLon(y, x), 3));
}
Test(path);
}
UNIT_TEST(Traffic_Serialization_Zigzag10Km)
{
vector<TrafficGPSEncoder::DataPoint> path;
for (size_t i = 0; i < 10; ++i)
{
double const x = i * 1e-2;
double const y = i % 2 == 0 ? 0 : 1e-2;
path.emplace_back(TrafficGPSEncoder::DataPoint(0, ms::LatLon(y, x), 0));
}
Test(path);
}
UNIT_TEST(Traffic_Serialization_Zigzag100Km)
{
vector<TrafficGPSEncoder::DataPoint> path;
for (size_t i = 0; i < 1000; ++i)
{
double const x = i * 1e-1;
double const y = i % 2 == 0 ? 0 : 1e-1;
path.emplace_back(TrafficGPSEncoder::DataPoint(0, ms::LatLon(y, x), 0));
}
Test(path);
}
UNIT_TEST(Traffic_Serialization_Circle20KmRadius)
{
vector<TrafficGPSEncoder::DataPoint> path;
size_t const n = 100;
for (size_t i = 0; i < n; ++i)
{
double const alpha = 2 * math::pi * i / n;
double const radius = 0.25;
double const x = radius * cos(alpha);
double const y = radius * sin(alpha);
path.emplace_back(TrafficGPSEncoder::DataPoint(0, ms::LatLon(y, x), 0));
}
Test(path);
}
UNIT_TEST(Traffic_Serialization_ExtremeLatLon)
{
vector<TrafficGPSEncoder::DataPoint> path = {
{0, ms::LatLon(-90, -180), 0},
{0, ms::LatLon(90, 180), 0},
};
Test(path);
}
} // namespace traffic_test
|