Skip to content

Commit

Permalink
new get_highest_priority function:
Browse files Browse the repository at this point in the history
- created fn
- created tests
- created small doc
  • Loading branch information
toyota-corolla0 committed Oct 4, 2023
1 parent 7792540 commit d66a394
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/orderbook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,16 @@ where
result
}

/// Returns the `OrderID` of the next to be fulfilled order by side
pub fn get_highest_priority_order(&self, side: Side) -> Option<OrderID> {
let shared_order = match side {
Side::Buy => self.buy_side.get_highest_priority(),
Side::Sell => self.sell_side.get_highest_priority()
};

shared_order.map(|o| o.borrow().id)
}

fn get_priority(&mut self) -> u64 {
self.priority += 1;
self.priority
Expand Down
38 changes: 38 additions & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,44 @@ fn general1() {
);
}

#[test]
fn get_highest_priority_order1() {
let mut ob = OrderBook::new();

assert_eq!(
ob.process_limit_order(1, Side::Buy, Decimal::from(20), Decimal::from(5))
.unwrap()
.len(),
0
);
assert_eq!(
ob.process_limit_order(2, Side::Buy, Decimal::from(15), Decimal::from(3))
.unwrap()
.len(),
0
);
assert_eq!(
ob.process_limit_order(3, Side::Sell, Decimal::from(75), Decimal::from(10))
.unwrap()
.len(),
0
);
assert_eq!(
ob.process_limit_order(4, Side::Sell, Decimal::from(50), Decimal::from(4))
.unwrap()
.len(),
0
);

assert_eq!(ob.get_highest_priority_order(Side::Buy), Some(1));
assert_eq!(ob.get_highest_priority_order(Side::Sell), Some(4));

_ = ob.cancel_order(3);
_ = ob.cancel_order(4);

assert_eq!(ob.get_highest_priority_order(Side::Sell), None);
}

#[test]
fn print() {
let mut ob = OrderBook::new();
Expand Down

0 comments on commit d66a394

Please sign in to comment.