Skip to content

Commit

Permalink
[#110] Use iterator based algorithm; add tests for partial ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
elfenpiff committed Feb 8, 2024
1 parent eec8168 commit 8972a62
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
17 changes: 8 additions & 9 deletions iceoryx2-bb/container/src/byte_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,14 @@ impl<const CAPACITY: usize, const CAPACITY_OTHER: usize>
PartialOrd<FixedSizeByteString<CAPACITY_OTHER>> for FixedSizeByteString<CAPACITY>
{
fn partial_cmp(&self, other: &FixedSizeByteString<CAPACITY_OTHER>) -> Option<Ordering> {
for i in 0..std::cmp::min(self.len, other.len) {
match unsafe { *self.data[i].as_ptr() }.cmp(unsafe { &*other.data[i].as_ptr() }) {
Ordering::Less => return Some(Ordering::Less),
Ordering::Greater => return Some(Ordering::Greater),
_ => (),
}
}

Some(self.len.cmp(&other.len))
Some(
self.data[..self.len]
.iter()
.zip(other.data[..other.len].iter())
.map(|(lhs, rhs)| unsafe { lhs.assume_init_read().cmp(rhs.assume_init_ref()) })
.find(|&ord| ord != Ordering::Equal)
.unwrap_or(self.len.cmp(&other.len)),
)
}
}

Expand Down
15 changes: 14 additions & 1 deletion iceoryx2-bb/container/tests/byte_string_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ use iceoryx2_bb_container::byte_string::*;
use iceoryx2_bb_testing::assert_that;

const SUT_CAPACITY: usize = 129;
const SUT_CAPACITY_ALT: usize = 65;
type Sut = FixedSizeByteString<SUT_CAPACITY>;
type SutAlt = FixedSizeByteString<SUT_CAPACITY_ALT>;

#[test]
fn fixed_size_byte_string_new_string_is_empty() {
Expand Down Expand Up @@ -345,7 +347,7 @@ fn fixed_size_byte_string_strip_suffix_works() {
}

#[test]
fn fixed_size_byte_ordering_works() {
fn fixed_size_byte_string_ordering_works() {
unsafe {
assert_that!(Sut::new_unchecked(b"fuubla").cmp(&Sut::new_unchecked(b"fuubla")), eq std::cmp::Ordering::Equal );
assert_that!(Sut::new_unchecked(b"fuubla").cmp(&Sut::new_unchecked(b"fuvbla")), eq std::cmp::Ordering::Less );
Expand All @@ -354,3 +356,14 @@ fn fixed_size_byte_ordering_works() {
assert_that!(Sut::new_unchecked(b"fuubla").cmp(&Sut::new_unchecked(b"fuu")), eq std::cmp::Ordering::Greater );
}
}

#[test]
fn fixed_size_byte_string_partial_ordering_works() {
unsafe {
assert_that!(SutAlt::new_unchecked(b"darth_fuubla").partial_cmp(&Sut::new_unchecked(b"darth_fuubla")), eq Some(std::cmp::Ordering::Equal ));
assert_that!(SutAlt::new_unchecked(b"darth_fuubla").partial_cmp(&Sut::new_unchecked(b"darth_fuvbla")), eq Some(std::cmp::Ordering::Less ));
assert_that!(SutAlt::new_unchecked(b"darth_fuubla").partial_cmp(&Sut::new_unchecked(b"darth_fuubaa")), eq Some(std::cmp::Ordering::Greater ));
assert_that!(SutAlt::new_unchecked(b"darth_fuubla").partial_cmp(&Sut::new_unchecked(b"darth_fuubla123")), eq Some(std::cmp::Ordering::Less ));
assert_that!(SutAlt::new_unchecked(b"darth_fuubla").partial_cmp(&Sut::new_unchecked(b"darth_fuu")), eq Some(std::cmp::Ordering::Greater ));
}
}

0 comments on commit 8972a62

Please sign in to comment.