Skip to content

Commit 166826b

Browse files
authored
Merge pull request Laffini#14 from Laffini/Test-display-names
Test updates
2 parents 5ba2e77 + d3cbe8a commit 166826b

6 files changed

Lines changed: 91 additions & 17 deletions

File tree

src/main/java/net/laffyco/javamatchingengine/engine/OrderBook.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ private synchronized List<Trade> processLimitSell(final Order order) {
134134
if (n != 0 || currentPrice >= order.getPrice()) {
135135
// Traverse all matching orders.
136136
for (int i = 0; i >= 0; i++) {
137-
final Order buyOrder = this.buyOrders.get(i);
137+
final Order buyOrder = this.buyOrders.get(0);
138138

139139
// Fill entire order.
140140
if (buyOrder.getAmount() >= order.getAmount()) {
@@ -143,7 +143,7 @@ private synchronized List<Trade> processLimitSell(final Order order) {
143143
buyOrder.setAmount(
144144
buyOrder.getAmount() - order.getAmount());
145145
if (buyOrder.getAmount() == 0) {
146-
this.removeBuyOrder(i);
146+
this.removeBuyOrder(0);
147147
}
148148
this.setLastSalePrice(buyOrder.getPrice());
149149
return trades;
@@ -154,7 +154,7 @@ private synchronized List<Trade> processLimitSell(final Order order) {
154154
trades.add(new Trade(order.getId(), buyOrder.getId(),
155155
buyOrder.getAmount(), buyOrder.getPrice()));
156156
order.setAmount(order.getAmount() - buyOrder.getAmount());
157-
this.removeBuyOrder(i);
157+
this.removeBuyOrder(0);
158158
this.setLastSalePrice(buyOrder.getPrice());
159159
continue;
160160
}

src/test/java/net/laffyco/javamatchingengine/OrderControllerTests.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import javax.annotation.Resource;
1111

1212
import org.junit.jupiter.api.BeforeEach;
13+
import org.junit.jupiter.api.DisplayName;
1314
import org.junit.jupiter.api.Test;
1415
import org.mockito.InjectMocks;
1516
import org.mockito.Mock;
@@ -55,6 +56,7 @@ public void setUp() {
5556
* Get orders test.
5657
*/
5758
@Test
59+
@DisplayName("Get the orders")
5860
public void getOrders() {
5961
final Map<String, List<Order>> result = this.controller.getOrders();
6062

@@ -69,6 +71,7 @@ public void getOrders() {
6971
* Get order by ID test.
7072
*/
7173
@Test
74+
@DisplayName("Get an order by ID")
7275
public void getOrderById() {
7376
final Order found = Mockito.mock(Order.class);
7477
final String id = "testId";
@@ -83,6 +86,7 @@ public void getOrderById() {
8386
* Add order test.
8487
*/
8588
@Test
89+
@DisplayName("Add an order")
8690
public void addOrder() {
8791
final Side side = Side.BUY;
8892
final double amt = 10;
@@ -103,6 +107,7 @@ public void addOrder() {
103107
* Delete an order.
104108
*/
105109
@Test
110+
@DisplayName("Delete an order")
106111
public void deleteOrder() {
107112
final String id = "id";
108113
final Side side = Side.BUY;

src/test/java/net/laffyco/javamatchingengine/engine/CancelOrderTests.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import javax.annotation.Resource;
66

7+
import org.junit.jupiter.api.DisplayName;
78
import org.junit.jupiter.api.Test;
89
import org.mockito.InjectMocks;
910

@@ -27,6 +28,7 @@ public class CancelOrderTests extends MatchingEngineTest {
2728
*
2829
*/
2930
@Test
31+
@DisplayName("Attempt to cancel an order that doesn't exist")
3032
public void cancelNoCancellation() {
3133
assertEquals(this.orderBook.cancelOrder("test", Side.BUY), false);
3234
assertEquals(this.orderBook.cancelOrder("test", Side.SELL), false);
@@ -38,6 +40,7 @@ public void cancelNoCancellation() {
3840
* @throws InterruptedException
3941
*/
4042
@Test
43+
@DisplayName("Cancel a buy order with the side not given")
4144
public void cancelBuyOrder() throws InterruptedException {
4245

4346
final int amt = 1;
@@ -62,6 +65,7 @@ public void cancelBuyOrder() throws InterruptedException {
6265
*
6366
*/
6467
@Test
68+
@DisplayName("Cancel a buy order")
6569
public void cancelBuyOrderSideGiven() {
6670

6771
final int amt = 1;
@@ -87,6 +91,7 @@ public void cancelBuyOrderSideGiven() {
8791
* @throws InterruptedException
8892
*/
8993
@Test
94+
@DisplayName("Cancel a sell order with the side not given")
9095
public void cancelSellOrder() throws InterruptedException {
9196
final int amt = 1;
9297
final double price = 1;
@@ -110,6 +115,7 @@ public void cancelSellOrder() throws InterruptedException {
110115
*
111116
*/
112117
@Test
118+
@DisplayName("Cancel a sell order")
113119
public void cancelSellOrderSideGiven() {
114120
final int amt = 1;
115121
final double price = 1;

src/test/java/net/laffyco/javamatchingengine/engine/OrderBookTests.java

Lines changed: 71 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
44
import static org.junit.jupiter.api.Assertions.assertFalse;
5-
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
66
import java.util.List;
7-
87
import javax.annotation.Resource;
9-
8+
import org.junit.jupiter.api.BeforeEach;
9+
import org.junit.jupiter.api.DisplayName;
1010
import org.junit.jupiter.api.Test;
1111
import org.mockito.InjectMocks;
1212

@@ -31,19 +31,12 @@ public class OrderBookTests extends MatchingEngineTest {
3131
@Resource
3232
private OrderBook orderBook;
3333

34-
// /**
35-
// * Test setup.
36-
// */
37-
// @BeforeEach
38-
// public void setUp() {
39-
// this.orderBook = new OrderBook(new ArrayList<Order>(),
40-
// new ArrayList<Order>());
41-
// }
4234

4335
/**
4436
* Add a buy order, then add a matching sell order.
4537
*/
4638
@Test
39+
@DisplayName("Add a buy order, then add a matching sell order")
4740
public void buyThenSell() {
4841

4942
// Add buy order
@@ -78,6 +71,7 @@ public void buyThenSell() {
7871
* Add a sell order, then add a buy order.
7972
*/
8073
@Test
74+
@DisplayName("Add a sell order, then add a matching buy order")
8175
public void sellThenBuy() {
8276

8377
// Add sell order
@@ -112,6 +106,7 @@ public void sellThenBuy() {
112106
* Find order tests.
113107
*/
114108
@Test
109+
@DisplayName("Find orders")
115110
public void findOrder() {
116111

117112
// Can't find an order that hasn't been added to the book.
@@ -139,11 +134,76 @@ public void findOrder() {
139134
* Test that no orders are cancelled when an invalid side is provided.
140135
*/
141136
@Test
137+
@DisplayName("No orders are cancelled when an invalid side is provided")
142138
public void cancelTestInvalidSide() {
143139

144140
final String orderId = "";
145141

146142
final boolean result = this.orderBook.cancelOrder(orderId, null);
147143
assertFalse(result);
148144
}
145+
146+
/**
147+
* A buy order that fills two sell orders.
148+
*/
149+
@Test
150+
@DisplayName("A buy order that fills two sell orders")
151+
public void buyPartialFill() {
152+
153+
// Add two sell orders.
154+
this.orderBook.process(this.orders[1]);
155+
this.orderBook.process(this.orders[1]);
156+
157+
// Modify buy order to be twice the amount.
158+
final Order buyOrder = this.orders[0];
159+
buyOrder.setAmount(buyOrder.getAmount() * 2);
160+
161+
// Add the buy order.
162+
final List<Trade> trades = this.orderBook.process(buyOrder);
163+
164+
// Two trades should have taken place.
165+
assertTrue(trades.size() == 2);
166+
167+
// Order book should be empty.
168+
assertTrue(this.orderBook.getBuyOrders().isEmpty());
169+
assertTrue(this.orderBook.getSellOrders().isEmpty());
170+
171+
// The trades match the expected amt and price.
172+
for (final Trade trade : trades) {
173+
assertTrue(trade.getAmount() == buyOrder.getAmount());
174+
assertTrue(trade.getPrice() == buyOrder.getPrice());
175+
}
176+
}
177+
178+
/**
179+
* A buy order that fills two sell orders.
180+
*/
181+
@Test
182+
@DisplayName("A sell order that fills two buy orders")
183+
public void sellPartialFill() {
184+
185+
// Add two buy orders.
186+
this.orderBook.process(this.orders[0]);
187+
this.orderBook.process(this.orders[0]);
188+
189+
// Modify sell order to be twice the amount.
190+
final Order sellOrder = this.orders[1];
191+
sellOrder.setAmount(sellOrder.getAmount() * 2);
192+
193+
// Add the buy order.
194+
final List<Trade> trades = this.orderBook.process(sellOrder);
195+
196+
// Two trades should have taken place.
197+
assertTrue(trades.size() == 2);
198+
199+
// Order book should be empty.
200+
assertTrue(this.orderBook.getBuyOrders().isEmpty());
201+
assertTrue(this.orderBook.getSellOrders().isEmpty());
202+
203+
// The trades match the expected amt and price.
204+
for (final Trade trade : trades) {
205+
assertTrue(trade.getAmount() == sellOrder.getAmount());
206+
assertTrue(trade.getPrice() == sellOrder.getPrice());
207+
}
208+
}
149209
}

src/test/java/net/laffyco/javamatchingengine/engine/OrderTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package net.laffyco.javamatchingengine.engine;
22

33
import static org.junit.jupiter.api.Assertions.assertTrue;
4-
54
import java.util.Date;
6-
5+
import org.junit.jupiter.api.DisplayName;
76
import org.junit.jupiter.api.Test;
87

98
/**
@@ -20,6 +19,7 @@ public class OrderTests extends MatchingEngineTest {
2019
* Compare two orders with different prices.
2120
*/
2221
@Test
22+
@DisplayName("Compare two orders with different prices")
2323
public void comparePrice() {
2424
final double price2 = 3;
2525

src/test/java/net/laffyco/javamatchingengine/engine/SpreadTests.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
import static org.junit.jupiter.api.Assertions.assertEquals;
44

55
import java.util.Date;
6-
76
import javax.annotation.Resource;
87

8+
import org.junit.jupiter.api.DisplayName;
99
import org.junit.jupiter.api.Test;
1010
import org.mockito.InjectMocks;
1111

@@ -24,6 +24,7 @@ public class SpreadTests extends MatchingEngineTest {
2424
private OrderBook orderBook;
2525

2626
@Test
27+
@DisplayName("Get the spread when there are two orders")
2728
void spreadTwoOrders() {
2829
final Order[] orders = {
2930
new Order(2, 2.50, "sellOrder", Side.SELL, new Date()),
@@ -36,6 +37,7 @@ void spreadTwoOrders() {
3637
}
3738

3839
@Test
40+
@DisplayName("Get the spread when there are multiple orders")
3941
void spreadMultipleOrders() {
4042
final Order[] orders = {
4143
new Order(2, 2.50, "sellOrder", Side.SELL, new Date()),
@@ -50,6 +52,7 @@ void spreadMultipleOrders() {
5052
}
5153

5254
@Test
55+
@DisplayName("Attempt to get the spread when there are no orders")
5356
void spreadNoOrders() {
5457
assertEquals(this.orderBook.getSpread(), 0);
5558
}

0 commit comments

Comments
 (0)