Part 4. Checking for in-order data (15 points)

In this part, you’ll implement a new function and write a few more unit tests to make sure your code works.

Your task

Write a function

#![allow(unused)]
fn main() {
fn is_in_order(data: &[i32]) -> bool {
    todo!("Implement me")
}
}

that returns true if the elements of data are in ascending order and false otherwise. To do this, you’ll want to iterate over data using for x in data {} and check that each element is at least as large as the previous element. To keep track of the pervious element, you’ll need a mutable variable: let mut prev: i32;. You can either use i32::MIN as its initial value or the value of the first element in data. Either approach works.

Before implementing the function write the following unit tests that should pass once you implement the function.

  1. Empty vectors are always in order.
  2. Vectors containing a single element are always in order.
  3. Vectors with multiple elements, in order.
  4. Vectors with multiple elements, not in order.

Example

Inside your test module, you’ll want to add a new function per unit test. For example, the test for multiple elements not in order might look something like this.

#![allow(unused)]
fn main() {
fn is_in_order(data: &[i32]) -> bool { todo!() }
#[test]
fn is_in_order_multiple_out_of_order() {
    let test_vector = vec![10, -3, 8, 2, 25];
    assert!(!is_in_order(&test_vector));
}
}

Notice that this is asserting the negation of is_in_order() because of the !.

Implement is_in_order and make sure it passes your tests.

My output for $ cargo test looks like this.

running 7 tests
test test::is_in_order_empty ... ok
test test::is_in_order_multiple_out_of_order ... ok
test test::reversed_vec_empty ... ok
test test::is_in_order_multiple_in_order ... ok
test test::is_in_order_one ... ok
test test::reversed_vec_one ... ok
test test::reversed_vec_three ... ok

test result: ok. 7 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s