Skip to content

Commit 8ec15c1

Browse files
Copilotnunoplopes
andauthored
Exercise max_element raw-pointer rule
Co-authored-by: nunoplopes <2998477+nunoplopes@users.noreply.github.com>
1 parent a8fddb9 commit 8ec15c1

3 files changed

Lines changed: 32 additions & 15 deletions

File tree

‎tests/unit/max.cpp‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include <algorithm>
22
#include <cassert>
3-
#include <vector>
43

54
int main() {
65
int x1 = 1, x2 = 2, x3 = 10, x4 = 20;
@@ -11,8 +10,8 @@ int main() {
1110
int r4 = std::min(*p2, x3);
1211
int r5 = std::max(30, 40);
1312
assert(r1 + r2 + r3 + r4 + r5 == 56);
14-
const std::vector<int> values{1, 7, 7, 3};
15-
auto max = std::max_element(values.begin(), values.end());
16-
assert(max == values.begin() + 1);
13+
int values[] = {1, 7, 7, 3};
14+
int *max = std::max_element(values, values + 4);
15+
assert(max == values + 1);
1716
return 0;
1817
}

‎tests/unit/out/refcount/max.rs‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,25 @@ fn main_0() -> i32 {
5252
.read())
5353
};
5454
assert!((((((r1 + r2) + r3) + r4) + r5) == 56));
55-
let values: Value<Vec<i32>> = Rc::new(RefCell::new(vec![1, 7, 7, 3]));
55+
let values: Value<Box<[i32]>> = Rc::new(RefCell::new(Box::new([1, 7, 7, 3])));
5656
let max: Value<Ptr<i32>> = Rc::new(RefCell::new(
5757
({
58-
let ___first: Ptr<i32> = (values.as_pointer() as Ptr<i32>);
59-
let ___last: Ptr<i32> = (values.as_pointer() as Ptr<i32>).to_end();
60-
max_element_0(___first, ___last)
58+
let count = ((values.as_pointer() as Ptr<i32>).offset(4)).get_offset()
59+
- (values.as_pointer() as Ptr<i32>).get_offset();
60+
let max_index = PtrValueIter::new(&(values.as_pointer() as Ptr<i32>), count)
61+
.enumerate()
62+
.max_by(|(idx_a, val_a), (idx_b, val_b)| {
63+
val_a
64+
.partial_cmp(val_b)
65+
.unwrap_or(std::cmp::Ordering::Equal)
66+
.then_with(|| idx_b.cmp(idx_a))
67+
})
68+
.map(|(idx, _)| idx)
69+
.unwrap_or(0);
70+
(values.as_pointer() as Ptr<i32>) + max_index
6171
}),
6272
));
63-
assert!((*max.borrow()) == (values.as_pointer() as Ptr<i32>).offset(1_i64 as isize));
73+
assert!((*max.borrow()) == (values.as_pointer() as Ptr<i32>).offset(1));
6474
return 0;
6575
}
6676
pub fn __cpp2rust_init_globals() {}

‎tests/unit/out/unsafe/max.rs‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,21 @@ unsafe fn main_0() -> i32 {
4949
})
5050
};
5151
assert!(((((((r1) + (r2)) + (r3)) + (r4)) + (r5)) == (56)));
52-
let values: Vec<i32> = vec![1, 7, 7, 3];
53-
let mut max: *const i32 = (unsafe {
54-
let ___first: *const i32 = values.as_ptr();
55-
let ___last: *const i32 = values.as_ptr().add(values.len());
56-
max_element_0(___first, ___last)
52+
let mut values: [i32; 4] = [1, 7, 7, 3];
53+
let mut max: *mut i32 = (unsafe {
54+
let count = values.as_mut_ptr().add(4).offset_from(values.as_mut_ptr()) as usize;
55+
std::slice::from_raw_parts(values.as_mut_ptr(), count)
56+
.iter()
57+
.enumerate()
58+
.max_by(|(idx_a, x), (idx_b, y)| {
59+
x.partial_cmp(y)
60+
.unwrap_or(std::cmp::Ordering::Equal)
61+
.then_with(|| idx_b.cmp(idx_a))
62+
})
63+
.map(|(i, _)| values.as_mut_ptr().add(i))
64+
.unwrap_or(values.as_mut_ptr())
5765
});
58-
assert!(max == values.as_ptr().add(1_i64 as usize));
66+
assert!(max == values.as_mut_ptr().add(1));
5967
return 0;
6068
}
6169
pub unsafe fn __cpp2rust_init_globals() {}

0 commit comments

Comments
 (0)