1818#include " s2/encoded_s2cell_id_vector.h"
1919
2020#include < vector>
21+
2122#include < gtest/gtest.h>
2223#include " absl/memory/memory.h"
24+ #include " s2/s2cell_id.h"
2325#include " s2/s2loop.h"
2426#include " s2/s2pointutil.h"
2527#include " s2/s2shape_index.h"
@@ -31,6 +33,7 @@ using s2textformat::MakeCellIdOrDie;
3133using std::vector;
3234
3335namespace s2coding {
36+ namespace {
3437
3538// Encodes the given vector and returns the corresponding
3639// EncodedS2CellIdVector (which points into the Encoder's data buffer).
@@ -139,6 +142,30 @@ TEST(EncodedS2CellIdVector, OneByteRangeWithBaseValue) {
139142 0x0100500000000000 , 0x0100330000000000 }, 9 );
140143}
141144
145+ TEST (EncodedS2CellIdVector, MaxShiftRange) {
146+ const std::vector<uint8> bytes = {
147+ (31 << 3 ) // 31 -> add 29 to bytes[1].
148+ + 1 , // Number of encoded cell IDs.
149+ 27 , // 27+29 is the maximum supported shift.
150+ 1 , 0 // Encoded cell ID. Not important.
151+ };
152+ Decoder decoder (bytes.data (), bytes.size ());
153+ EncodedS2CellIdVector cell_ids;
154+ EXPECT_TRUE (cell_ids.Init (&decoder));
155+ }
156+
157+ TEST (EncodedS2CellIdVector, ShiftOutOfRange) {
158+ const std::vector<uint8> bytes = {
159+ (31 << 3 ) // 31 -> add 29 to bytes[1].
160+ + 1 , // Number of encoded cell IDs.
161+ 28 , // 28+29 is greater than the maximum supported shift of 56.
162+ 1 , 0 // Encoded cell ID. Not important.
163+ };
164+ Decoder decoder (bytes.data (), bytes.size ());
165+ EncodedS2CellIdVector cell_ids;
166+ EXPECT_FALSE (cell_ids.Init (&decoder));
167+ }
168+
142169TEST (EncodedS2CellIdVector, SixFaceCells) {
143170 vector<S2CellId> ids;
144171 for (int face = 0 ; face < 6 ; ++face) {
@@ -229,4 +256,5 @@ TEST(EncodedS2CellIdVector, LowerBoundLimits) {
229256 EXPECT_EQ (2 , cell_ids.lower_bound (S2CellId::Sentinel ()));
230257}
231258
259+ } // namespace
232260} // namespace s2coding
0 commit comments