Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions src/libbson/tests/json/bson_binary_vector/packed_bit.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,39 @@
{
"description": "Simple Vector PACKED_BIT",
"valid": true,
"vector": [127, 7],
"vector": [127, 8],
"dtype_hex": "0x10",
"dtype_alias": "PACKED_BIT",
"padding": 0,
"canonical_bson": "1600000005766563746F7200040000000910007F0700"
"canonical_bson": "1600000005766563746F7200040000000910007F0800"
},
{
"description": "Empty Vector PACKED_BIT",
"description": "PACKED_BIT with padding",
"valid": true,
"vector": [],
"vector": [127, 8],
"dtype_hex": "0x10",
"dtype_alias": "PACKED_BIT",
"padding": 0,
"canonical_bson": "1400000005766563746F72000200000009100000"
"padding": 3,
"canonical_bson": "1600000005766563746F7200040000000910037F0800"
},
{
"description": "PACKED_BIT with padding",
"valid": true,
"description": "PACKED_BIT with inconsistent padding",
"valid": false,
"vector": [127, 7],
"dtype_hex": "0x10",
"dtype_alias": "PACKED_BIT",
"padding": 3,
"canonical_bson": "1600000005766563746F7200040000000910037F0700"
},
{
"description": "Empty Vector PACKED_BIT",
"valid": true,
"vector": [],
"dtype_hex": "0x10",
"dtype_alias": "PACKED_BIT",
"padding": 0,
"canonical_bson": "1400000005766563746F72000200000009100000"
},
{
"description": "Overflow Vector PACKED_BIT",
"valid": false,
Expand Down
33 changes: 14 additions & 19 deletions src/libbson/tests/test-bson-vector.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,20 @@ append_vector_packed_bit_from_packed_array (
ASSERT (bson_iter_next (&copy_iter));
uint8_t packed_byte = (uint8_t) bson_iter_as_int64 (&copy_iter);
ASSERT (bson_vector_packed_bit_view_write_packed (view, &packed_byte, 1, i));

// Read back the packed byte, interpret any masking as a conversion failure.
uint8_t packed_byte_check;
ASSERT (bson_vector_packed_bit_view_read_packed (view, &packed_byte_check, 1, i));
if (packed_byte != packed_byte_check) {
bson_set_error (error,
TEST_ERROR_DOMAIN,
TEST_ERROR_CODE,
"byte at index %zu with value 0x%02X included write to masked bits (reads as 0x%02X)",
i,
packed_byte,
packed_byte_check);
return false;
}
}
return true;
} else {
Expand Down Expand Up @@ -159,17 +173,6 @@ test_bson_vector_json_case (vector_json_test_case_t *test_case)
bson_error_t vector_from_array_error;
bool vector_from_array_ok;

// (Spec test improvement TODO) Patch test cases that have unused bits set to '1' when '0' is required.
if (0 == strcmp ("PACKED_BIT with padding", test_case->test_description)) {
bson_iter_t iter;
ASSERT (bson_iter_init_find (&iter, &expected_bson, test_case->scenario_test_key));
uint32_t binary_len;
uint8_t *binary;
bson_iter_overwrite_binary (&iter, BSON_SUBTYPE_VECTOR, &binary_len, &binary);
ASSERT (binary_len > BSON_VECTOR_HEADER_LEN);
binary[binary_len - 1] &= (uint8_t) 0xFF << bson_vector_padding_from_header_byte_1 (binary[1]);
}

// Try a format conversion from array to the indicated vector format.
// The spec calls the first header byte "dtype" (combining the element type and element size fields)
if (0 == strcmp ("0x03", test_case->test_dtype_hex_str)) {
Expand Down Expand Up @@ -336,14 +339,6 @@ test_bson_vector_json_case (vector_json_test_case_t *test_case)
test_error ("test-vector array element %d has unexpected type, should be int.", (int) byte_count);
}

// (Spec test improvement TODO) Packed writes can't set unused bits to '1' in libbson, but the spec
// tests allow padding bits to take on undefined values. Modify the expected values to keep padding bits
// zeroed.
if (0 == strcmp ("PACKED_BIT with padding", test_case->test_description) &&
byte_count == bson_vector_packed_bit_const_view_length_bytes (actual_view) - 1u) {
expected_byte &= ((int64_t) 0xFF << *test_case->test_padding) & 0xFF;
}

// Note, the zero initializer is only needed due to a false positive -Wmaybe-uninitialized warning in
// uncommon configurations where the compiler does not have visibility into memcpy().
uint8_t actual_byte = 0;
Expand Down