Skip to content

Add cbor_structurally_equal for encoding-level item comparison - #408

Merged
PJK merged 3 commits into
masterfrom
cbor-structurally-equal
Mar 22, 2026
Merged

PJK merged 3 commits into
masterfrom
cbor-structurally-equal

Conversation

@PJK

@PJK PJK commented Mar 22, 2026

Copy link
Copy Markdown
Owner

Summary

  • Adds cbor_structurally_equal(const cbor_item_t*, const cbor_item_t*) to common.h/common.c
  • 36 test cases in test/structurally_equal_test.c covering all eight CBOR major types
  • RST documentation in doc/source/api/item_types.rst

Design rationale

PR #96 proposed a cbor_equal function but left the semantics underspecified. RFC 8949 defines two distinct notions of equality:

  • Data-model equality (RFC 8949 §2.1, §5.6.1): encoding width is invisible; maps are unordered sets of key-value pairs; uint8(1) and uint64(1) are the same integer.
  • Structural equality: every byte of the encoding counts; uint8(1) ≠ uint16(1).

This PR implements structural equality under the name cbor_structurally_equal, which makes the semantics explicit and avoids surprising callers who might expect data-model behaviour from a function named cbor_equal. The function is extensively documented to explain what it does and does not compare, and to point callers toward the value accessors (cbor_get_uint64 etc.) for data-model comparisons.

Notable structural rules that differ from data-model equality:

Case cbor_structurally_equal
uint8(1) vs uint16(1) false — encoding width differs
float2(1.5) vs float4(1.5) false — float width differs
Definite array vs indefinite array (same elements) false
Indefinite bytestring, same bytes, different chunk split false
Maps with same pairs in different order false — positional comparison
Two NaN with different bit patterns false — bitwise float comparison

NULL inputs are programming errors; assertions fire in debug builds (consistent with the rest of the libcbor API).

Test plan

  • ctest -R structurally_equal_test passes (36/36)
  • bash clang-format.sh produces no diff
  • CI green

🤖 Generated with Claude Code

PJK and others added 3 commits March 22, 2026 17:44
Introduces cbor_structurally_equal(), which compares two cbor_item_t
values for structural (encoding-level) equality: every aspect of the
encoding is significant — integer width, float width, definite vs.
indefinite length, chunk boundaries in indefinite strings/bytestrings,
and positional order of map entries.

This is intentionally stricter than the RFC 8949 data-model equality
(where encoding width is invisible and maps are unordered). The
function is documented to make this distinction explicit.

Includes 36 test cases covering all eight major types and the key
edge cases (same value different width, definite vs. indefinite,
chunk structure, map ordering, NaN, cross-type).

Related to #96 (cbor_map_get and cbor_equal).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…rally_equal

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@PJK
PJK merged commit dd5676a into master Mar 22, 2026
16 checks passed
@PJK
PJK deleted the cbor-structurally-equal branch March 22, 2026 16:56
@codecov

codecov Bot commented Mar 22, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 88.23529% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 99.42%. Comparing base (dfe2297) to head (8aaabe6).
⚠️ Report is 16 commits behind head on master.

Files with missing lines Patch % Lines
src/cbor/common.c 88.23% 8 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##            master     #408      +/-   ##
===========================================
- Coverage   100.00%   99.42%   -0.58%     
===========================================
  Files           20       20              
  Lines         1663     1731      +68     
===========================================
+ Hits          1663     1721      +58     
- Misses           0       10      +10     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

⌂ 换网址