Skip to content

Commit c6f3c57

Browse files
committed
[ci] fix ubuntu ci test errors
1 parent 73f9490 commit c6f3c57

File tree

3 files changed

+99
-30
lines changed

3 files changed

+99
-30
lines changed

include/nlohmann/detail/input/binary_reader.hpp

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2796,11 +2796,21 @@ class binary_reader
27962796
/*!
27972797
@brief Get index byte size from type marker for SOA
27982798
*/
2799-
static constexpr std::size_t soa_index_size(char_int_type m) noexcept
2799+
static std::size_t soa_index_size(char_int_type m) noexcept
28002800
{
2801-
return (m == 'U' || m == 'i' || m == 'B') ? 1 :
2802-
(m == 'u' || m == 'I') ? 2 :
2803-
(m == 'm' || m == 'l') ? 4 : 0;
2801+
if (m == 'U' || m == 'i' || m == 'B')
2802+
{
2803+
return 1;
2804+
}
2805+
if (m == 'u' || m == 'I')
2806+
{
2807+
return 2;
2808+
}
2809+
if (m == 'm' || m == 'l')
2810+
{
2811+
return 4;
2812+
}
2813+
return 0;
28042814
}
28052815

28062816
/*!
@@ -2846,9 +2856,11 @@ class binary_reader
28462856
return false;
28472857
}
28482858
if (idx >= f.str_dict.size())
2859+
{
28492860
return sax->parse_error(chars_read, get_token_string(),
28502861
parse_error::create(113, chars_read,
28512862
exception_message(input_format, "dict index out of range", "SOA"), nullptr));
2863+
}
28522864
string_t str = f.str_dict[idx];
28532865
return sax->string(str);
28542866
}
@@ -3199,14 +3211,16 @@ class binary_reader
31993211
static_cast<unsigned char>(b1));
32003212
const int exp = (half >> 10u) & 0x1Fu;
32013213
const unsigned mant = half & 0x3FFu;
3202-
double val;
3214+
double val = 0.0;
32033215
if (exp == 0)
32043216
{
32053217
val = std::ldexp(mant, -24);
32063218
}
32073219
else if (exp == 31)
3220+
{
32083221
val = (mant == 0) ? std::numeric_limits<double>::infinity()
32093222
: std::numeric_limits<double>::quiet_NaN();
3223+
}
32103224
else
32113225
{
32123226
val = std::ldexp(mant + 1024, exp - 25);
@@ -3230,20 +3244,28 @@ class binary_reader
32303244
if (is_row_major)
32313245
{
32323246
for (std::size_t ri = 0; ri < count; ++ri)
3247+
{
32333248
for (std::size_t fi = 0; fi < nf; ++fi)
3249+
{
32343250
if (JSON_HEDLEY_UNLIKELY(!read_field(fi)))
32353251
{
32363252
return false;
32373253
}
3254+
}
3255+
}
32383256
}
32393257
else
32403258
{
32413259
for (std::size_t fi = 0; fi < nf; ++fi)
3260+
{
32423261
for (std::size_t ri = 0; ri < count; ++ri)
3262+
{
32433263
if (JSON_HEDLEY_UNLIKELY(!read_field(fi)))
32443264
{
32453265
return false;
32463266
}
3267+
}
3268+
}
32473269
}
32483270

32493271
// Read offset tables and resolve strings

include/nlohmann/detail/output/binary_writer.hpp

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@ class binary_writer
7474
std::int32_t type_marker;
7575
bjdata_soa_string_encoding_t str_enc = bjdata_soa_string_encoding_t::fixed;
7676
std::size_t str_fixed_len = 0;
77-
std::vector<std::size_t> str_indices;
78-
std::vector<string_t> str_dict;
79-
std::vector<std::size_t> str_offsets;
80-
string_t str_buffer;
77+
std::vector<std::size_t> str_indices{};
78+
std::vector<string_t> str_dict{};
79+
std::vector<std::size_t> str_offsets{};
80+
string_t str_buffer{};
8181
};
8282

8383
public:
@@ -1933,16 +1933,40 @@ class binary_writer
19331933
}
19341934

19351935
// Calculate costs
1936-
double thresh = (threshold > 0) ? threshold : 0.3;
1936+
const double thresh = (threshold > 0) ? threshold : 0.3;
19371937
std::size_t fixed_cost = max_len * arr.size();
19381938

19391939
// Dict cost
1940-
std::size_t idx_size = unique_count <= 255 ? 1 : unique_count <= 65535 ? 2 : 4;
1941-
std::size_t dict_cost = idx_size * arr.size() + total_len + unique_count * 2;
1940+
std::size_t idx_size;
1941+
if (unique_count <= 255)
1942+
{
1943+
idx_size = 1;
1944+
}
1945+
else if (unique_count <= 65535)
1946+
{
1947+
idx_size = 2;
1948+
}
1949+
else
1950+
{
1951+
idx_size = 4;
1952+
}
1953+
std::size_t dict_cost = (idx_size * arr.size()) + total_len + (unique_count * 2);
19421954

19431955
// Offset cost
1944-
std::size_t off_size = total_len <= 255 ? 1 : total_len <= 65535 ? 2 : 4;
1945-
std::size_t offset_cost = arr.size() * off_size + (arr.size() + 1) * off_size + total_len;
1956+
std::size_t off_size;
1957+
if (total_len <= 255)
1958+
{
1959+
off_size = 1;
1960+
}
1961+
else if (total_len <= 65535)
1962+
{
1963+
off_size = 2;
1964+
}
1965+
else
1966+
{
1967+
off_size = 4;
1968+
}
1969+
std::size_t offset_cost = (arr.size() * off_size) + ((arr.size() + 1) * off_size) + total_len;
19461970

19471971
// Decision logic
19481972
if (unique_count <= static_cast<std::size_t>(static_cast<double>(arr.size()) * thresh) &&
@@ -2002,7 +2026,7 @@ class binary_writer
20022026
{
20032027
// [$U] or [$u] or [$m]
20042028
std::size_t max_offset = field.str_buffer.size();
2005-
char index_type = max_offset <= 255 ? 'U' : max_offset <= 65535 ? 'u' : 'm';
2029+
const char index_type = (max_offset <= 255) ? 'U' : (max_offset <= 65535) ? 'u' : 'm';
20062030

20072031
oa->write_character(to_char_type(0x5B)); // '['
20082032
oa->write_character(to_char_type(0x24)); // '$'

tests/src/unit-bjdata.cpp

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5214,24 +5214,32 @@ TEST_CASE("BJData SOA Variable-Length Strings")
52145214
std::vector<uint8_t> v =
52155215
{
52165216
'{', '$', '{',
5217-
'i', 3, 't', 'x', 't', '[', '$', 'u', ']',
5218-
'}', '#', 'i', 2,
5219-
// Payload (2 × uint16 for offsets 0 and 150):
5220-
0x00, 0x00, // offset 0 as uint16 LE
5221-
0x96, 0x00, // offset 150 as uint16 LE
5222-
// Offset table (3 × uint16):
5223-
0x00, 0x00, // 0
5224-
0x96, 0x00, // 150
5225-
0x2C, 0x01 // 300
5217+
'i', 3, 't', 'x', 't', '[', '$', 'u', ']', // field "txt" with uint16 offset encoding
5218+
'}', '#', 'i', 2, // 2 records
5219+
// Payload: 2 offset indices (uint16 each, referring to offset table)
5220+
0x00, 0x00, // record 0: offset index 0
5221+
0x01, 0x00, // record 1: offset index 1
5222+
// Offset table: 3 entries (start, middle, end)
5223+
0x00, 0x00, // offset 0
5224+
0x96, 0x00, // offset 150 (150 bytes)
5225+
0x2C, 0x01 // offset 300 (300 bytes total)
52265226
};
52275227

5228+
// Add 300 bytes of string data
52285229
for (int i = 0; i < 300; i++)
52295230
{
52305231
v.push_back('X');
52315232
}
52325233

52335234
const auto j = json::from_bjdata(v);
5234-
CHECK(j["txt"][0].get<std::string>().size() == 150);
5235+
CHECK(j.is_object());
5236+
CHECK(j.contains("txt"));
5237+
CHECK(j["txt"].is_array());
5238+
CHECK(j["txt"].size() == 2);
5239+
const auto str0_size = j["txt"][0].get<std::string>().size();
5240+
const auto str1_size = j["txt"][1].get<std::string>().size();
5241+
CHECK(str0_size == 150); // First string: bytes 0-149
5242+
CHECK(str1_size == 150); // Second string: bytes 150-299
52355243
}
52365244

52375245
SECTION("offset EOF errors")
@@ -6409,7 +6417,8 @@ TEST_CASE("SOA Coverage Tests")
64096417
json::bjdata_version_t::draft4,
64106418
json::bjdata_soa_format_t::col_major);
64116419
auto j2 = json::from_bjdata(v);
6412-
CHECK((j2["s"][0].get<std::string>().find("number_0") != std::string::npos));
6420+
const auto str_val = j2["s"][0].get<std::string>();
6421+
CHECK(str_val.find("number_0") != std::string::npos);
64136422
}
64146423
}
64156424

@@ -6437,7 +6446,6 @@ TEST_CASE("SOA Coverage Tests")
64376446

64386447
SECTION("offset with uint16 indices")
64396448
{
6440-
// Create strings totaling > 255 bytes to force uint16 offsets
64416449
json j = json::array();
64426450
std::string long_str(100, 'x');
64436451
for (int i = 0; i < 5; ++i)
@@ -6448,7 +6456,14 @@ TEST_CASE("SOA Coverage Tests")
64486456
json::bjdata_version_t::draft4,
64496457
json::bjdata_soa_format_t::col_major);
64506458
auto j2 = json::from_bjdata(v);
6451-
CHECK((j2["s"][0].get<std::string>().size() > 100));
6459+
6460+
// Column-major produces: {"s": [str0, str1, str2, str3, str4]}
6461+
CHECK(j2.is_object());
6462+
CHECK(j2.contains("s"));
6463+
CHECK(j2["s"].is_array());
6464+
CHECK(j2["s"].size() == 5);
6465+
const auto str_size = j2["s"][0].get<std::string>().size();
6466+
CHECK(str_size > 100);
64526467
}
64536468

64546469
SECTION("offset with uint32 indices")
@@ -6464,7 +6479,14 @@ TEST_CASE("SOA Coverage Tests")
64646479
json::bjdata_version_t::draft4,
64656480
json::bjdata_soa_format_t::col_major);
64666481
auto j2 = json::from_bjdata(v);
6467-
CHECK((j2["s"][0].get<std::string>().size() > 10000));
6482+
6483+
// Column-major produces: {"s": [str0, str1, ...]}
6484+
CHECK(j2.is_object());
6485+
CHECK(j2.contains("s"));
6486+
CHECK(j2["s"].is_array());
6487+
CHECK(j2["s"].size() == 10);
6488+
const auto str_size = j2["s"][0].get<std::string>().size();
6489+
CHECK(str_size > 10000);
64686490
}
64696491
}
64706492

@@ -6539,7 +6561,8 @@ TEST_CASE("SOA Coverage Tests")
65396561
json::bjdata_version_t::draft4,
65406562
json::bjdata_soa_format_t::col_major);
65416563
auto j2 = json::from_bjdata(v);
6542-
CHECK((j2["s"][0].get<std::string>().size() == 100));
6564+
const auto str_size = j2["s"][0].get<std::string>().size();
6565+
CHECK(str_size == 100);
65436566
}
65446567
}
65456568

0 commit comments

Comments
 (0)