Skip to content

Commit

Permalink
cxx-qt-lib: fix incorrect cmp order
Browse files Browse the repository at this point in the history
This meant that QString and QDateTime had reversed
Ord results.
  • Loading branch information
ahayzen-kdab authored and vimpostor committed Oct 13, 2023
1 parent 8feba93 commit c42cf2c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
26 changes: 25 additions & 1 deletion crates/cxx-qt-lib/src/core/qdatetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ impl PartialOrd for QDateTime {

impl Ord for QDateTime {
fn cmp(&self, other: &Self) -> Ordering {
0.cmp(&ffi::qdatetime_cmp(self, other))
ffi::qdatetime_cmp(self, other).cmp(&0)
}
}

Expand Down Expand Up @@ -489,6 +489,30 @@ unsafe impl ExternType for QDateTime {
type Kind = cxx::kind::Trivial;
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn test_ordering() {
let qdatetime_a = QDateTime::from_date_and_time_time_zone(
&QDate::new(2023, 1, 1),
&QTime::new(1, 1, 1, 1),
&ffi::QTimeZone::utc(),
);
let qdatetime_b = QDateTime::from_date_and_time_time_zone(
&QDate::new(2023, 2, 2),
&QTime::new(2, 2, 2, 2),
&ffi::QTimeZone::utc(),
);

assert!(qdatetime_a < qdatetime_b);
assert_eq!(qdatetime_a.cmp(&qdatetime_b), Ordering::Less);
assert_eq!(qdatetime_b.cmp(&qdatetime_a), Ordering::Greater);
assert_eq!(qdatetime_a.cmp(&qdatetime_a), Ordering::Equal);
}
}

#[cfg(test)]
#[cfg(feature = "chrono")]
mod test_chrono {
Expand Down
33 changes: 31 additions & 2 deletions crates/cxx-qt-lib/src/core/qstring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ impl PartialOrd for QString {

impl Ord for QString {
fn cmp(&self, other: &Self) -> Ordering {
0.cmp(&ffi::qstring_cmp(self, other))
ffi::qstring_cmp(self, other).cmp(&0)
}
}

Expand Down Expand Up @@ -291,7 +291,7 @@ impl QString {
/// Lexically compares this string with the other string and
/// returns if this string is less than, equal to, or greater than the other string.
pub fn compare(&self, other: &QString, cs: ffi::CaseSensitivity) -> Ordering {
0.cmp(&self.compare_i32(other, cs))
self.compare_i32(other, cs).cmp(&0)
}

/// Returns the index position of the first occurrence of the string str in this string,
Expand Down Expand Up @@ -381,3 +381,32 @@ unsafe impl ExternType for QString {
type Id = type_id!("QString");
type Kind = cxx::kind::Trivial;
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn test_ordering() {
let qstring_a = QString::from("a");
let qstring_b = QString::from("b");

assert!(qstring_a < qstring_b);
assert_eq!(qstring_a.cmp(&qstring_b), Ordering::Less);
assert_eq!(qstring_b.cmp(&qstring_a), Ordering::Greater);
assert_eq!(qstring_a.cmp(&qstring_a), Ordering::Equal);

assert_eq!(
qstring_a.compare(&qstring_b, crate::CaseSensitivity::CaseInsensitive),
Ordering::Less
);
assert_eq!(
qstring_b.compare(&qstring_a, crate::CaseSensitivity::CaseInsensitive),
Ordering::Greater
);
assert_eq!(
qstring_a.compare(&qstring_a, crate::CaseSensitivity::CaseInsensitive),
Ordering::Equal
);
}
}

0 comments on commit c42cf2c

Please sign in to comment.