Skip to content

Commit

Permalink
Fix test_retain_autoreleased on GNUStep
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Jan 11, 2022
1 parent 411d995 commit 419086c
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions objc2-foundation/tests/objc_id_retain_autoreleased.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,31 @@ fn create_data(bytes: &[u8]) -> Id<NSData, Shared> {

#[test]
fn test_retain_autoreleased() {
#[cfg(gnustep)]
unsafe {
objc2::__gnustep_hack::get_class_to_force_linkage()
};

autoreleasepool(|_| {
let data = create_data(b"12");
// The autorelease-return-mechanism has to "warm up" somehow? At least
// for some reason the first time this is used it fails.
assert_eq!(retain_count(&data), 2);
assert_eq!(retain_count(&data), if cfg!(gnustep) { 1 } else { 2 });

// When compiled in release mode / with optimizations enabled,
// subsequent usage of `retain_autoreleased` will succeed in retaining
// the autoreleased value!
let expected_retain_count = if cfg!(debug_assertions) { 2 } else { 1 };
let expected = if cfg!(all(debug_assertions, not(gnustep))) {
2
} else {
1
};

let data = create_data(b"34");
assert_eq!(retain_count(&data), expected_retain_count);
assert_eq!(retain_count(&data), expected);

let data = create_data(b"56");
assert_eq!(retain_count(&data), expected_retain_count);
assert_eq!(retain_count(&data), expected);

// Here we manually clean up the autorelease, so it will always be 1.
let data = autoreleasepool(|_| create_data(b"78"));
Expand Down

0 comments on commit 419086c

Please sign in to comment.