The Code that leads to the the unordered_map:
class APortfolio : public Test
{
public:
static const string SAMSUNG;
static const date ArbitraryDate;
Portfolio portfolio_;
// How to code helper methods that abstract common functionality
// and allow for updatable tests, without having to change old tests
// to reflect changes to the class under test.
void Purchase(const string &symbol, int shareCount,
const date &transactionDate = APortfolio::ArbitraryDate)
{
portfolio_.Purchase(symbol, shareCount, transactionDate);
}
void Sell(const string &symbol, int shareCount,
const date &transactionDate = APortfolio::ArbitraryDate)
{
portfolio_.Sell(symbol, shareCount, transactionDate);
}
void ASSERT_PURCHASE(PurchaseRecord &purchase, int shareCount,
const date &transactionDate)
{
ASSERT_THAT(purchase.ShareCount, Eq(shareCount));
ASSERT_THAT(purchase.Date, Eq(transactionDate));
}
};
const string APortfolio::SAMSUNG("SSNLF");
const date APortfolio::ArbitraryDate(2014, Sep, 5);
TEST_F(APortfolio, ReducesShareCountOfSymboleOnSell)
{
Purchase(SAMSUNG, 30);
Sell(SAMSUNG, 13);
ASSERT_THAT(portfolio_.ShareCount(SAMSUNG), Eq(30u - 13));
}
And the methods that handle the generation and assignment for each key:
void Portfolio::addPurchaseRecord(const string &symbol, int shareCount, const date &date)
{
if (!containsSymbol(symbol));
initializePurchaseRecords(symbol);
add(symbol, { shareCount, date });
}
bool Portfolio::containsSymbol(const std::string &symbol) const
{
return holdings_.find(symbol) == holdings_.end();
}
void Portfolio::initializePurchaseRecords(const string &symbol)
{
holdings_[symbol] = Holding();
}
void Portfolio::add(const string &symbol, PurchaseRecord &&record)
{
holdings_[symbol].Add(record);
}
When
Purchase()and
Sell()are called the holdings_ unordered_map are reinitialized each time. All of the sample code for checking for a key in an unordered_map, I've found on Stack Overflow, matches what I have. Any idea what's going on here?
This post has been edited by zerophase: 24 August 2014 - 08:48 PM

New Topic/Question
Reply


MultiQuote



|