-
Notifications
You must be signed in to change notification settings - Fork 1
/
OrderTests.cpp
170 lines (137 loc) · 5.07 KB
/
OrderTests.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <cstdint>
#include <ostream>
#include <stdexcept>
#include <string>
#include <vector>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
using namespace ::testing;
enum class Side
{
Buy,
Sell
};
std::ostream& operator<<(std::ostream& out, Side side)
{
switch (side)
{
case Side::Buy:
return out << "Buy";
case Side::Sell:
return out << "Sell";
}
return out << "Unknown side";
}
struct OrderInfo
{
static constexpr int InvalidOrderId = -1;
std::string order_id;
double price;
std::uint64_t volume;
Side side;
};
// We can create simple matchers by composing existing matchers and returning them.
Matcher<std::string> IsValidOrderId()
{
return Not(IsEmpty());
}
std::ostream& operator<<(std::ostream& out, const OrderInfo& order)
{
out << "OrderInfo[OrderId=" << order.order_id;
out << ", Price=" << order.price;
out << ", Volume=" << order.volume;
out << ", Side=" << order.side << "]";
return out;
}
struct Orders
{
std::string book;
std::vector<OrderInfo> orders;
};
class OrderTest : public Test
{
protected:
Orders getEvent(int eventId) const
{
// Obviously this is canned data, but let's not get distracted with fancy simulations...
switch (eventId)
{
case 1:
return {
"RDSA",
{
OrderInfo{.order_id = "event1-1", .price = 1.23, .volume = 100, .side = Side::Buy},
OrderInfo{.order_id = "event1-3", .price = 1.25, .volume = 300, .side = Side::Buy},
OrderInfo{.order_id = "event1-2", .price = 1.24, .volume = 200, .side = Side::Buy},
}};
case 2:
return {"RDSA", {}};
}
throw std::runtime_error("Event not received");
}
};
TEST_F(OrderTest, Event1OrdersAreSentOnTheCorrectBook)
{
// TODO: Use StrEq to ensure the book is "RDSA" in the following EXPECT_THAT assertion.
// EXPECT_THAT(getEvent(1).book, /* Fill me in */);
FAIL() << "This test needs to be implemented.";
}
TEST_F(OrderTest, Event1OrderIdsAreAllValid)
{
// EXAMPLE: GoogleTest has some great matchers for working with collections. E.g. we can ensure that each OrderInfo
// has a valid order id using `Each`.
EXPECT_THAT(getEvent(1).orders, Each(Field("order_id", &OrderInfo::order_id, IsValidOrderId())));
}
TEST_F(OrderTest, Event1ExpectedPrices)
{
// The collection based GoogleTest matchers are better than testing individual elements in a for loop as they
// express the intent of the testing better and produce better error messages that provide context. E.g. Using
// UnorderedElementsAre vs ElementsAre informs the developer as to whether the order matters or not.
// TODO: use EXPECT_THAT with the Field, UnorderedElementsAre, and appropriate floating point matchers to ensure
// that the prices are 1.23, 1.24, 1.25.
FAIL() << "This test needs to be implemented.";
}
TEST_F(OrderTest, Event1AllOrdersAreBuySide)
{
// TODO: You should be able to work this one out from the test name.
FAIL() << "This test needs to be implemented.";
}
TEST_F(OrderTest, Event1OrderIdsStartWithEventDash1)
{
// TODO: Ensure that all order ids for event 1 start with the text "event1-"
// EXPECT_THAT(getEvent().orders, /* Fill me in */);
FAIL() << "This test needs to be implemented.";
}
TEST_F(OrderTest, Event1ExpectedOrders)
{
// TODO: Write a *single* EXPECT_THAT statement that verifies the following orders. Note: You will need to use the
// AllOf matcher alongside UnorderedElementsAre. Note: the test should ensure that the price 1.23 is in the order
// with volume 100, i.e. separate tests for price and volume are not sufficient. The expected orders are:
// Price | Volume
// 1.23 | 100
// 1.24 | 200
// 1.25 | 300
FAIL() << "This test needs to be implemented.";
// TODO: Extension exercise:
// The above can be verbose. Can you write a simple matcher to remove the duplication? (Hint: IsValidOrderId shows
// *one* way of creating a custom matcher. There are other ways in the GoogleTest documentation.)
// Create a simple matcher and duplicate the above EXPECT_THAT statement you wrote above with your new matcher.
FAIL()
<< "The extension exercise needs to be implemented. Remove this if you are not going to complete the extension";
}
TEST_F(OrderTest, Event2OrdersAreEmpty)
{
// TODO: Use the *most* appropriate matcher to ensure the collection is empty.
FAIL() << "This test needs to be implemented.";
}
TEST_F(OrderTest, UnknownEventIdThrows)
{
// TODO: Find an appropriate assertion to ensure an exception of type std::runtime_error is thrown when an event
// does not exist. Use `getEvent(999)`.
FAIL() << "This test needs to be implemented.";
}
TEST_F(OrderTest, GettingKnownEventDoesNotThrow)
{
// TODO: Find an appropriate assertion to ensure that no exception is thrown when retrieving a known event.
FAIL() << "This test needs to be implemented.";
}