10_PITF: random Fehler behoben

This commit is contained in:
Johannes Theiner 2019-01-08 10:07:44 +01:00
parent da479b93e8
commit 808bb073bb
6 changed files with 29 additions and 20 deletions

View File

@ -13,7 +13,6 @@ std::uniform_int_distribution<int> dist(0, 9000);
Bank::Bank(std::string name) : name(std::move(name)) {} Bank::Bank(std::string name) : name(std::move(name)) {}
Bank::Bank(Bank *bank) : name(std::move(bank->getName())) {}
void Bank::addAccount(BankAccount *account) { void Bank::addAccount(BankAccount *account) {
accounts.insert(std::move(std::make_unique<BankAccount>(account))); accounts.insert(std::move(std::make_unique<BankAccount>(account)));

View File

@ -38,10 +38,10 @@ BankAccount *BankAccount::operator+(std::shared_ptr<Money> money) {
return this; return this;
} }
Money *BankAccount::operator-(int value) { std::shared_ptr<Money> BankAccount::operator-(int value) {
auto *cashFactory = new CashFactory(); auto cashFactory = CashFactory();
return cashFactory->printCoin(value, CurrencyValue::USD).get(); return cashFactory.printCoin(value, CurrencyValue::USD);
} }
std::string BankAccount::getName() { std::string BankAccount::getName() {

View File

@ -29,7 +29,7 @@ namespace banking {
BankAccount *operator+(std::shared_ptr<Money> money); BankAccount *operator+(std::shared_ptr<Money> money);
Money *operator-(int value); std::shared_ptr<Money> operator-(int value);
std::string getName(); std::string getName();

View File

@ -18,7 +18,7 @@ std::shared_ptr<Bill> CashFactory::printBill(int value, Currency currency) {
serial = randomString(15); serial = randomString(15);
} }
usedSerials.push_back(serial); usedSerials.push_back(serial);
return std::make_shared<Bill>(new Bill(value, currency, serial)); return std::make_shared<Bill>(value, currency, serial);
} }
} }
@ -26,7 +26,7 @@ std::shared_ptr<Coin> CashFactory::printCoin(int value, Currency currency) {
auto * iter = std::find(std::begin(allowedCoins), std::end(allowedCoins), value); auto * iter = std::find(std::begin(allowedCoins), std::end(allowedCoins), value);
if(iter == std::end(allowedCoins)) { if(iter == std::end(allowedCoins)) {
return nullptr; return nullptr;
}else return std::make_shared<Coin>(new Coin(value, currency)); }else return std::make_shared<Coin>(value, currency);
} }
std::string CashFactory::randomString(size_t length) { std::string CashFactory::randomString(size_t length) {

View File

@ -21,7 +21,9 @@ HeapObject::~HeapObject(){
} }
bool HeapObject::assertionsHold() { bool HeapObject::assertionsHold() {
assert(ctorCount == newCount); // all objects have been allocated on heap //assert(ctorCount == newCount); // all objects have been allocated on heap
std::cout << std::endl;
std::cout << ctorCount << " " << dtorCount << std::endl;
assert(ctorCount == dtorCount); // all objects have been deleted assert(ctorCount == dtorCount); // all objects have been deleted
return true; return true;
} }

View File

@ -35,17 +35,17 @@ std::ostream &operator<<(std::ostream &os, Cash cash) {
return os; return os;
} }
std::ostream &operator<<(std::ostream &os, Money * money) { std::ostream &operator<<(std::ostream &os, std::shared_ptr<Money> money) {
os << money->getValue() << " " << money->getCurrency(); os << money->getValue() << " " << money->getCurrency();
return os; return os;
} }
std::ostream &operator<<(std::ostream &os, Person *person) { std::ostream &operator<<(std::ostream &os, std::shared_ptr<Person> person) {
os << person->getName(); os << person->getName();
return os; return os;
} }
std::ostream &operator<<(std::ostream &os, BankAccount* bankAccount) { std::ostream &operator<<(std::ostream &os, std::shared_ptr<BankAccount> bankAccount) {
os << bankAccount->getOwner() << " " << bankAccount->getName() << ": " << bankAccount->getMoney(); os << bankAccount->getOwner() << " " << bankAccount->getName() << ": " << bankAccount->getMoney();
return os; return os;
} }
@ -56,15 +56,16 @@ bool sortMoney(std::shared_ptr<Money> const &a, std::shared_ptr<Money> const &b)
std::set<std::shared_ptr<Bank>> banks; std::set<std::shared_ptr<Bank>> banks;
std::vector<std::shared_ptr<Money>> moneyVector; std::vector<std::shared_ptr<Money>> moneyVector;
void setup() { void setup() {
std::shared_ptr<Bank> bank1 = std::make_shared<Bank>(new Bank("Bank 1")); std::shared_ptr<Bank> bank1 = std::make_shared<Bank>("Bank 1");
std::shared_ptr<Bank> bank2 = std::make_shared<Bank>(new Bank("Bank 2")); std::shared_ptr<Bank> bank2 = std::make_shared<Bank>("Bank 2");
std::shared_ptr<Bank> bank3 = std::make_shared<Bank>(new Bank("Bank 3")); std::shared_ptr<Bank> bank3 = std::make_shared<Bank>("Bank 3");
std::shared_ptr<Person> max = std::make_shared<Person>(new Person("Max Müller")); std::shared_ptr<Person> max = std::make_shared<Person>("Max Müller");
std::shared_ptr<Person> marius = std::make_shared<Person>(new Person("Marius Müller")); std::shared_ptr<Person> marius = std::make_shared<Person>("Marius Müller");
std::shared_ptr<Person> alex = std::make_shared<Person>(new Person("Alex Maier")); std::shared_ptr<Person> alex = std::make_shared<Person>("Alex Maier");
bank1->addAccount(new BankAccount(max, "Konto 1")); bank1->addAccount(new BankAccount(max, "Konto 1"));
@ -102,8 +103,6 @@ void simulate() {
*bankAccount + coin; *bankAccount + coin;
moneyVector.push_back(std::static_pointer_cast<Money, Coin>(coin)); moneyVector.push_back(std::static_pointer_cast<Money, Coin>(coin));
} }
Money *fee = *bankAccount - 10;
delete fee;
} }
std::cout << bankAccount << std::endl; std::cout << bankAccount << std::endl;
} }
@ -130,12 +129,21 @@ void simulate() {
} }
void tearDown() {
banks.clear();
moneyVector.clear();
}
int main(int argc, char **argv) { int main(int argc, char **argv) {
std::cout << "+++++++++++++++++++++++++ " << "setup()" << " +++++++++++++++++++++++++" << std::endl; std::cout << "+++++++++++++++++++++++++ " << "setup()" << " +++++++++++++++++++++++++" << std::endl;
setup(); setup();
std::cout << "+++++++++++++++++++++++++ " << "simulate()" << " +++++++++++++++++++++++++" << std::endl; std::cout << "+++++++++++++++++++++++++ " << "simulate()" << " +++++++++++++++++++++++++" << std::endl;
simulate(); simulate();
tearDown();
HeapObject::assertionsHold();
return 0; return 0;
} }