Skip to content

Rewrote Guid.CompareTo#271

Merged
josesimoes merged 1 commit into
nanoframework:developfrom
josesimoes:guid-fixes
Apr 27, 2026
Merged

Rewrote Guid.CompareTo#271
josesimoes merged 1 commit into
nanoframework:developfrom
josesimoes:guid-fixes

Conversation

@josesimoes

@josesimoes josesimoes commented Apr 27, 2026

Copy link
Copy Markdown
Member

Description

  • Comparison now uses unsigned arithmetic.
  • Add new unit tests to properly cover CompareTo.

Motivation and Context

  • Fixes issues in Guid comparison. It was comparing the packed storage words, not the logical Guid components. Also, it was using signed subtraction to determine ordering which can overflow and flip the sign.

How Has This Been Tested?

  • New unit tests added to cover this fix.
  • [tested against nanoclr buildId 61138]

Screenshots

Types of changes

  • Improvement (non-breaking change that improves a feature, code or algorithm)
  • Bug fix (non-breaking change which fixes an issue with code or algorithm)
  • New feature (non-breaking change which adds functionality to code)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Config and build (change in the configuration and build system, has no impact on code or features)
  • Dependencies (update dependencies and changes associated, has no impact on code or features)
  • Unit Tests (add new Unit Test(s) or improved existing one(s), has no impact on code or features)
  • Documentation (changes or updates in the documentation, has no impact on code or features)

Checklist:

  • My code follows the code style of this project (only if there are changes in source code).
  • My changes require an update to the documentation (there are changes that require the docs website to be updated).
  • I have updated the documentation accordingly (the changes require an update on the docs in this repo).
  • I have read the CONTRIBUTING document.
  • I have tested everything locally and all new and existing tests passed (only if there are changes in source code).
  • I have added new tests to cover my changes.

@coderabbitai

coderabbitai Bot commented Apr 27, 2026

Copy link
Copy Markdown
📝 Walkthrough

Walkthrough

The PR modifies Guid comparison functionality by rewriting the CompareTo method implementation to use deterministic unsigned lexicographic comparison with byte/word reordering, and extends test coverage with additional test cases while replacing implicit var declarations with explicit types in existing tests.

Changes

Cohort / File(s) Summary
Core Implementation
nanoFramework.CoreLibrary/System/Guid.cs
Rewrites CompareTo(Guid other) method signature (parameter renamed to value) and implementation: replaces simple per-index comparison loop with deterministic unsigned lexicographic comparison over normalized internal array components, including byte/word reordering for elements 1–3, returning only -1, 1, or 0.
Test Coverage Expansion
Tests/NFUnitTestTypes/UnitTestGuid.cs
Replaces multiple var declarations with explicit types (Guid, string, byte[]) throughout existing tests; adds three new [TestMethod]s for Guid.CompareTo covering less-than, greater-than, and component-wise ordering scenarios with boundary testing.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the main change—rewriting the Guid.CompareTo method—which aligns with the primary focus of the changeset.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description check ✅ Passed The pull request description clearly relates to the changeset by explaining the bug fix in Guid.CompareTo (unsigned arithmetic, correct component comparison) and documenting the new unit tests added.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@josesimoes

Copy link
Copy Markdown
Member Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Apr 27, 2026

Copy link
Copy Markdown
✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@nanoFramework.CoreLibrary/System/Guid.cs`:
- Around line 183-235: The CompareTo implementation uses repeated null checks
and duplicated byte-swap logic; replace the eight inline "thisData == null ? 0u
: ..." checks by initializing _data once like other methods (use _data ??= new
int[4]) at the start of CompareTo, and extract the repeated 16-bit and 32-bit
byte-swap code into private helpers (e.g., a SwapUInt16/SwapUInt32 or SwapBytes
method) and call those from CompareTo to remove duplication and match idiomatic
null-handling used by Equals/operator==/ToByteArray/GetHashCode.

In `@Tests/NFUnitTestTypes/UnitTestGuid.cs`:
- Around line 142-175: Tests assert exact CompareTo return values which tie
tests to implementation details; update assertions in Guid_CompareTo_GreaterThan
and Guid_CompareTo_OrdersByComponents to check only the sign (greater than 0 or
less than 0) instead of exact -1/1, and make Guid_CompareTo_LessThan use the
same sign-style checks for consistency; locate the failing assertions in methods
Guid_CompareTo_GreaterThan, Guid_CompareTo_OrdersByComponents and replace
Assert.AreEqual(1, ...)/Assert.AreEqual(-1, ...) with Assert.IsTrue(... >
0)/Assert.IsTrue(... < 0) (or Assert.IsTrue(... != 0) where appropriate) so
tests validate the IComparable contract rather than exact magnitude.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository: nanoframework/coderabbit/.coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: bdd9ed98-759a-4f97-9c24-22367943ea85

📥 Commits

Reviewing files that changed from the base of the PR and between 1474d8c and 9019ed3.

📒 Files selected for processing (2)
  • Tests/NFUnitTestTypes/UnitTestGuid.cs
  • nanoFramework.CoreLibrary/System/Guid.cs

Comment thread nanoFramework.CoreLibrary/System/Guid.cs Outdated
Comment thread Tests/NFUnitTestTypes/UnitTestGuid.cs
- Comparison now uses unsigned arithmetic.
- Add new unit tests to properly cover CompareTo.
- Bump native version assembly to 100.22.0.4.
@sonarqubecloud

Copy link
Copy Markdown

@josesimoes josesimoes merged commit 779da80 into nanoframework:develop Apr 27, 2026
7 checks passed
@josesimoes josesimoes deleted the guid-fixes branch April 27, 2026 11:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants