diff --git a/GildedRose/cpp/GildedRose.cc b/GildedRose/cpp/GildedRose.cc index c3ebb814..8df23e47 100644 --- a/GildedRose/cpp/GildedRose.cc +++ b/GildedRose/cpp/GildedRose.cc @@ -1,38 +1,71 @@ -#include +#include "GildedRose.h" -#include - -using namespace std; - -class Item -{ -public: - string name; - int sellIn; - int quality; - Item(string name, int sellIn, int quality) : name(name), sellIn(sellIn), quality(quality) - {} -}; - -class GildedRose -{ -public: - vector items; - GildedRose(vector items) : items (items) - {} +GildedRose::GildedRose(vector & items) : items(items) +{} - void updateQuality() +void GildedRose::updateQuality() +{ + for (int i = 0; i < items.size(); i++) { - for (int i = 0; i < items.size(); i++) + if (items[i].name != "Aged Brie" && items[i].name != "Backstage passes to a TAFKAL80ETC concert") { - if (items[i].name != "Aged Brie" && items[i].name != "Backstage passes to a TAFKAL80ETC concert") + if (items[i].quality > 0) { - if (items[i].quality > 0) + if (items[i].name != "Sulfuras, Hand of Ragnaros") { - if (items[i].name != "Sulfuras, Hand of Ragnaros") + items[i].quality = items[i].quality - 1; + } + } + } + else + { + if (items[i].quality < 50) + { + items[i].quality = items[i].quality + 1; + + if (items[i].name == "Backstage passes to a TAFKAL80ETC concert") + { + if (items[i].sellIn < 11) { - items[i].quality = items[i].quality - 1; + if (items[i].quality < 50) + { + items[i].quality = items[i].quality + 1; + } } + + if (items[i].sellIn < 6) + { + if (items[i].quality < 50) + { + items[i].quality = items[i].quality + 1; + } + } + } + } + } + + if (items[i].name != "Sulfuras, Hand of Ragnaros") + { + items[i].sellIn = items[i].sellIn - 1; + } + + if (items[i].sellIn < 0) + { + if (items[i].name != "Aged Brie") + { + if (items[i].name != "Backstage passes to a TAFKAL80ETC concert") + { + if (items[i].quality > 0) + { + if (items[i].name != "Sulfuras, Hand of Ragnaros") + { + items[i].quality = items[i].quality - 1; + } + } + } + else + { + items[i].quality = items[i].quality - items[i].quality; } } else @@ -40,84 +73,8 @@ public: if (items[i].quality < 50) { items[i].quality = items[i].quality + 1; - - if (items[i].name == "Backstage passes to a TAFKAL80ETC concert") - { - if (items[i].sellIn < 11) - { - if (items[i].quality < 50) - { - items[i].quality = items[i].quality + 1; - } - } - - if (items[i].sellIn < 6) - { - if (items[i].quality < 50) - { - items[i].quality = items[i].quality + 1; - } - } - } - } - } - - if (items[i].name != "Sulfuras, Hand of Ragnaros") - { - items[i].sellIn = items[i].sellIn - 1; - } - - if (items[i].sellIn < 0) - { - if (items[i].name != "Aged Brie") - { - if (items[i].name != "Backstage passes to a TAFKAL80ETC concert") - { - if (items[i].quality > 0) - { - if (items[i].name != "Sulfuras, Hand of Ragnaros") - { - items[i].quality = items[i].quality - 1; - } - } - } - else - { - items[i].quality = items[i].quality - items[i].quality; - } - } - else - { - if (items[i].quality < 50) - { - items[i].quality = items[i].quality + 1; - } } } } } -}; - - -void example() -{ - vector items; - items.push_back(Item("+5 Dexterity Vest", 10, 20)); - items.push_back(Item("Aged Brie", 2, 0)); - items.push_back(Item("Elixir of the Mongoose", 5, 7)); - items.push_back(Item("Sulfuras, Hand of Ragnaros", 0, 80)); - items.push_back(Item("Backstage passes to a TAFKAL80ETC concert", 15, 20)); - items.push_back(Item("Conjured Mana Cake", 3, 6)); - GildedRose app(items); - app.updateQuality(); } - - -TEST(GildedRoseTest, Foo) { - vector items; - items.push_back(Item("Foo", 0, 0)); - GildedRose app(items); - app.updateQuality(); - EXPECT_EQ("fixme", app.items[0].name); -} - diff --git a/GildedRose/cpp/GildedRose.h b/GildedRose/cpp/GildedRose.h new file mode 100644 index 00000000..8464f87b --- /dev/null +++ b/GildedRose/cpp/GildedRose.h @@ -0,0 +1,24 @@ +#include +#include + +using namespace std; + +class Item +{ +public: + string name; + int sellIn; + int quality; + Item(string name, int sellIn, int quality) : name(name), sellIn(sellIn), quality(quality) + {} +}; + +class GildedRose +{ +public: + vector & items; + GildedRose(vector & items); + + void updateQuality(); +}; + diff --git a/GildedRose/cpp/GildedRoseTextTests.cc b/GildedRose/cpp/GildedRoseTextTests.cc new file mode 100644 index 00000000..6a1366ed --- /dev/null +++ b/GildedRose/cpp/GildedRoseTextTests.cc @@ -0,0 +1,38 @@ +#include "GildedRose.h" + +#include + +using namespace std; + +ostream& operator<<(ostream& s, Item& item) +{ + s << item.name << ", " << item.sellIn << ", " << item.quality; + return s; +} + +int main() +{ + vector items; + items.push_back(Item("+5 Dexterity Vest", 10, 20)); + items.push_back(Item("Aged Brie", 2, 0)); + items.push_back(Item("Elixir of the Mongoose", 5, 7)); + items.push_back(Item("Sulfuras, Hand of Ragnaros", 0, 80)); + items.push_back(Item("Backstage passes to a TAFKAL80ETC concert", 15, 20)); + items.push_back(Item("Conjured Mana Cake", 3, 6)); + GildedRose app(items); + + cout << "OMGHAI!" << endl; + + for (int day = 0; day <= 30; day++) + { + cout << "-------- day " << day << " --------" << endl; + cout << "name, sellIn, quality" << endl; + for (vector::iterator i = items.begin(); i != items.end(); i++) + { + cout << *i << endl; + } + cout << endl; + + app.updateQuality(); + } +} diff --git a/GildedRose/cpp/GildedRoseUnitTests.cc b/GildedRose/cpp/GildedRoseUnitTests.cc new file mode 100644 index 00000000..7e72d830 --- /dev/null +++ b/GildedRose/cpp/GildedRoseUnitTests.cc @@ -0,0 +1,24 @@ +#include + +#include "GildedRose.h" + +TEST(GildedRoseTest, Foo) { + vector items; + items.push_back(Item("Foo", 0, 0)); + GildedRose app(items); + app.updateQuality(); + EXPECT_EQ("fixme", app.items[0].name); +} + +void example() +{ + vector items; + items.push_back(Item("+5 Dexterity Vest", 10, 20)); + items.push_back(Item("Aged Brie", 2, 0)); + items.push_back(Item("Elixir of the Mongoose", 5, 7)); + items.push_back(Item("Sulfuras, Hand of Ragnaros", 0, 80)); + items.push_back(Item("Backstage passes to a TAFKAL80ETC concert", 15, 20)); + items.push_back(Item("Conjured Mana Cake", 3, 6)); + GildedRose app(items); + app.updateQuality(); +} diff --git a/GildedRose/cpp/Makefile b/GildedRose/cpp/Makefile index e5e1d3e9..5ebc68d9 100644 --- a/GildedRose/cpp/Makefile +++ b/GildedRose/cpp/Makefile @@ -27,6 +27,8 @@ CXXFLAGS += -g -Wall -Wextra # created to the list. TESTS = GildedRose +TEXTTESTS = GildedRoseTextTests + # All Google Test headers. Usually you shouldn't change this # definition. GTEST_HEADERS = $(GTEST_DIR)/include/gtest/*.h \ @@ -34,10 +36,10 @@ GTEST_HEADERS = $(GTEST_DIR)/include/gtest/*.h \ # House-keeping build targets. -all : $(TESTS) +all : $(TESTS) $(TEXTTESTS) clean : - rm -f $(TESTS) gtest.a gtest_main.a *.o + rm -f $(TESTS) $(TEXTTESTS) gtest.a gtest_main.a *.o # Builds gtest.a and gtest_main.a. @@ -67,9 +69,18 @@ gtest_main.a : gtest-all.o gtest_main.o # gtest_main.a, depending on whether it defines its own main() # function. -GildedRose.o : $(USER_DIR)/GildedRose.cc \ - $(GTEST_HEADERS) - $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(USER_DIR)/GildedRose.cc +GildedRose.o : $(USER_DIR)/GildedRose.cc + $(CXX) -c $^ -GildedRose : GildedRose.o gtest_main.a +GildedRoseUnitTests.o : $(USER_DIR)/GildedRoseUnitTests.cc \ + $(GTEST_HEADERS) + $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(USER_DIR)/GildedRoseUnitTests.cc + +GildedRose : GildedRoseUnitTests.o GildedRose.o gtest_main.a $(CXX) $(CPPFLAGS) $(CXXFLAGS) -pthread $^ -o $@ + +GildedRoseTextTests.o : $(USER_DIR)/GildedRoseTextTests.cc + $(CXX) -c $^ + +GildedRoseTextTests : GildedRoseTextTests.o GildedRose.o + $(CXX) -pthread $^ -o $@