From 808bb073bbc014dd94d6e344b60427a58cd32feb Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Tue, 8 Jan 2019 10:07:44 +0100 Subject: [PATCH] 10_PITF: random Fehler behoben --- src/10_PITF/Testat/Bank.cpp | 1 - src/10_PITF/Testat/BankAccount.cpp | 6 +++--- src/10_PITF/Testat/BankAccount.h | 2 +- src/10_PITF/Testat/CashFactory.cpp | 4 ++-- src/10_PITF/Testat/HeapObject.cpp | 6 ++++-- src/10_PITF/Testat/main.cpp | 30 +++++++++++++++++++----------- 6 files changed, 29 insertions(+), 20 deletions(-) diff --git a/src/10_PITF/Testat/Bank.cpp b/src/10_PITF/Testat/Bank.cpp index 60ba555..80f02ac 100644 --- a/src/10_PITF/Testat/Bank.cpp +++ b/src/10_PITF/Testat/Bank.cpp @@ -13,7 +13,6 @@ std::uniform_int_distribution dist(0, 9000); Bank::Bank(std::string name) : name(std::move(name)) {} -Bank::Bank(Bank *bank) : name(std::move(bank->getName())) {} void Bank::addAccount(BankAccount *account) { accounts.insert(std::move(std::make_unique(account))); diff --git a/src/10_PITF/Testat/BankAccount.cpp b/src/10_PITF/Testat/BankAccount.cpp index e5b3f3b..6576ecb 100644 --- a/src/10_PITF/Testat/BankAccount.cpp +++ b/src/10_PITF/Testat/BankAccount.cpp @@ -38,10 +38,10 @@ BankAccount *BankAccount::operator+(std::shared_ptr money) { return this; } -Money *BankAccount::operator-(int value) { - auto *cashFactory = new CashFactory(); +std::shared_ptr BankAccount::operator-(int value) { + auto cashFactory = CashFactory(); - return cashFactory->printCoin(value, CurrencyValue::USD).get(); + return cashFactory.printCoin(value, CurrencyValue::USD); } std::string BankAccount::getName() { diff --git a/src/10_PITF/Testat/BankAccount.h b/src/10_PITF/Testat/BankAccount.h index 79b3799..cfe95f3 100644 --- a/src/10_PITF/Testat/BankAccount.h +++ b/src/10_PITF/Testat/BankAccount.h @@ -29,7 +29,7 @@ namespace banking { BankAccount *operator+(std::shared_ptr money); - Money *operator-(int value); + std::shared_ptr operator-(int value); std::string getName(); diff --git a/src/10_PITF/Testat/CashFactory.cpp b/src/10_PITF/Testat/CashFactory.cpp index e1016fe..67ab9f6 100644 --- a/src/10_PITF/Testat/CashFactory.cpp +++ b/src/10_PITF/Testat/CashFactory.cpp @@ -18,7 +18,7 @@ std::shared_ptr CashFactory::printBill(int value, Currency currency) { serial = randomString(15); } usedSerials.push_back(serial); - return std::make_shared(new Bill(value, currency, serial)); + return std::make_shared(value, currency, serial); } } @@ -26,7 +26,7 @@ std::shared_ptr CashFactory::printCoin(int value, Currency currency) { auto * iter = std::find(std::begin(allowedCoins), std::end(allowedCoins), value); if(iter == std::end(allowedCoins)) { return nullptr; - }else return std::make_shared(new Coin(value, currency)); + }else return std::make_shared(value, currency); } std::string CashFactory::randomString(size_t length) { diff --git a/src/10_PITF/Testat/HeapObject.cpp b/src/10_PITF/Testat/HeapObject.cpp index b4b4ba3..625b16f 100644 --- a/src/10_PITF/Testat/HeapObject.cpp +++ b/src/10_PITF/Testat/HeapObject.cpp @@ -20,8 +20,10 @@ HeapObject::~HeapObject(){ dtorCount++; } -bool HeapObject::assertionsHold(){ - assert(ctorCount == newCount); // all objects have been allocated on heap +bool HeapObject::assertionsHold() { + //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 return true; } \ No newline at end of file diff --git a/src/10_PITF/Testat/main.cpp b/src/10_PITF/Testat/main.cpp index f9bdc00..4935978 100644 --- a/src/10_PITF/Testat/main.cpp +++ b/src/10_PITF/Testat/main.cpp @@ -35,17 +35,17 @@ std::ostream &operator<<(std::ostream &os, Cash cash) { return os; } -std::ostream &operator<<(std::ostream &os, Money * money) { +std::ostream &operator<<(std::ostream &os, std::shared_ptr money) { os << money->getValue() << " " << money->getCurrency(); return os; } -std::ostream &operator<<(std::ostream &os, Person *person) { +std::ostream &operator<<(std::ostream &os, std::shared_ptr person) { os << person->getName(); return os; } -std::ostream &operator<<(std::ostream &os, BankAccount* bankAccount) { +std::ostream &operator<<(std::ostream &os, std::shared_ptr bankAccount) { os << bankAccount->getOwner() << " " << bankAccount->getName() << ": " << bankAccount->getMoney(); return os; } @@ -56,15 +56,16 @@ bool sortMoney(std::shared_ptr const &a, std::shared_ptr const &b) std::set> banks; std::vector> moneyVector; + void setup() { - std::shared_ptr bank1 = std::make_shared(new Bank("Bank 1")); - std::shared_ptr bank2 = std::make_shared(new Bank("Bank 2")); - std::shared_ptr bank3 = std::make_shared(new Bank("Bank 3")); + std::shared_ptr bank1 = std::make_shared("Bank 1"); + std::shared_ptr bank2 = std::make_shared("Bank 2"); + std::shared_ptr bank3 = std::make_shared("Bank 3"); - std::shared_ptr max = std::make_shared(new Person("Max Müller")); - std::shared_ptr marius = std::make_shared(new Person("Marius Müller")); - std::shared_ptr alex = std::make_shared(new Person("Alex Maier")); + std::shared_ptr max = std::make_shared("Max Müller"); + std::shared_ptr marius = std::make_shared("Marius Müller"); + std::shared_ptr alex = std::make_shared("Alex Maier"); bank1->addAccount(new BankAccount(max, "Konto 1")); @@ -102,8 +103,6 @@ void simulate() { *bankAccount + coin; moneyVector.push_back(std::static_pointer_cast(coin)); } - Money *fee = *bankAccount - 10; - delete fee; } std::cout << bankAccount << std::endl; } @@ -130,12 +129,21 @@ void simulate() { } +void tearDown() { + banks.clear(); + moneyVector.clear(); +} + int main(int argc, char **argv) { std::cout << "+++++++++++++++++++++++++ " << "setup()" << " +++++++++++++++++++++++++" << std::endl; setup(); std::cout << "+++++++++++++++++++++++++ " << "simulate()" << " +++++++++++++++++++++++++" << std::endl; simulate(); + tearDown(); + + + HeapObject::assertionsHold(); return 0; } \ No newline at end of file