-
Notifications
You must be signed in to change notification settings - Fork 0
/
portfolio_test.cpp
61 lines (46 loc) · 1.77 KB
/
portfolio_test.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
#include <glog/logging.h>
#include "gtest/gtest.h"
#include "portfolio.h"
TEST(PortfolioTest, index) {
Portfolio folio(100.0, {"FOO", "BAR", "BAZ"});
EXPECT_EQ(folio.index("FOO"), 0);
EXPECT_EQ(folio.index("BAR"), 1);
EXPECT_EQ(folio.index("BAZ"), 2);
}
TEST(PortfolioTest, shares) {
Portfolio folio(100.0, {"FOO", "BAR"});
EXPECT_EQ(folio.shares(folio.index("BAR")), 0.0);
}
TEST(PortfolioTest, buy) {
Portfolio folio(100.0, {"FOO", "BAR"});
EXPECT_EQ(folio.shares(folio.index("BAR")), 0.0);
folio.buy(/*symbol_index=*/1, /*quantity=*/1, /*price=*/50.0);
EXPECT_EQ(folio.shares(folio.index("BAR")), 1.0);
folio.buy(/*symbol_index=*/1, /*quantity=*/1, /*price=*/50.0);
EXPECT_EQ(folio.shares(folio.index("BAR")), 2.0);
}
TEST(PortfolioTest, sell) {
Portfolio folio(100.0, {"FOO", "BAR"});
folio.buy(/*symbol_index=*/0, /*quantity=*/1, /*price=*/50.0);
EXPECT_EQ(folio.shares(folio.index("FOO")), 1.0);
folio.buy(/*symbol_index=*/1, /*quantity=*/1, /*price=*/50.0);
EXPECT_EQ(folio.shares(folio.index("BAR")), 1.0);
folio.sell(/*symbol_index=*/0, /*quantity=*/1, /*price=*/20.0);
EXPECT_EQ(folio.shares(folio.index("FOO")), 0.0);
folio.sell(/*symbol_index=*/1, /*quantity=*/1, /*price=*/10.0);
EXPECT_EQ(folio.shares(folio.index("BAR")), 0.0);
EXPECT_EQ(folio.cash(), 30.0);
}
TEST(PortfolioTest, value) {
Portfolio folio(110.0, {"FOO", "BAR"});
folio.buy(/*symbol_index=*/0, /*quantity=*/1, /*price=*/50.0);
folio.buy(/*symbol_index=*/1, /*quantity=*/1, /*price=*/50.0);
// Should be $10 cash, 1 share each.
EXPECT_EQ(folio.value({200.0, 3000.0}), 3210.0);
}
int main(int argc, char **argv) {
google::InitGoogleLogging(argv[0]);
::testing::InitGoogleTest(&argc, argv);
google::InstallFailureSignalHandler();
return RUN_ALL_TESTS();
}