From e48300e32e7889cd82341f0316a5b9f307910a17 Mon Sep 17 00:00:00 2001 From: Frederic Lepied Date: Fri, 17 Aug 2012 11:31:01 +0200 Subject: [PATCH] ported the C++ version in C --- GildedRose/C/GildedRose.c | 90 +++++++++++++++++++++++++++++ GildedRose/C/GildedRose.h | 9 +++ GildedRose/C/GildedRoseTextTests.c | 43 ++++++++++++++ GildedRose/C/GildedRoseUnitTests.cc | 43 ++++++++++++++ GildedRose/C/Makefile | 51 ++++++++++++++++ GildedRose/C/README | 5 ++ GildedRose/C/run-once.sh | 2 + 7 files changed, 243 insertions(+) create mode 100644 GildedRose/C/GildedRose.c create mode 100644 GildedRose/C/GildedRose.h create mode 100644 GildedRose/C/GildedRoseTextTests.c create mode 100644 GildedRose/C/GildedRoseUnitTests.cc create mode 100644 GildedRose/C/Makefile create mode 100644 GildedRose/C/README create mode 100755 GildedRose/C/run-once.sh diff --git a/GildedRose/C/GildedRose.c b/GildedRose/C/GildedRose.c new file mode 100644 index 00000000..afb97bbe --- /dev/null +++ b/GildedRose/C/GildedRose.c @@ -0,0 +1,90 @@ +#include +#include "GildedRose.h" + +Item* +init_item(Item* item, const char *name, int sellIn, int quality) +{ + item->sellIn = sellIn; + item->quality = quality; + item->name = strdup(name); + + return item; +} + +void update_quality(Item items[], int size) +{ + int i; + + for (i = 0; i < size; i++) + { + if (strcmp(items[i].name, "Aged Brie") && strcmp(items[i].name, "Backstage passes to a TAFKAL80ETC concert")) + { + if (items[i].quality > 0) + { + if (strcmp(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 (!strcmp(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 (strcmp(items[i].name, "Sulfuras, Hand of Ragnaros")) + { + items[i].sellIn = items[i].sellIn - 1; + } + + if (items[i].sellIn < 0) + { + if (strcmp(items[i].name, "Aged Brie")) + { + if (strcmp(items[i].name, "Backstage passes to a TAFKAL80ETC concert")) + { + if (items[i].quality > 0) + { + if (strcmp(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; + } + } + } + } +} diff --git a/GildedRose/C/GildedRose.h b/GildedRose/C/GildedRose.h new file mode 100644 index 00000000..78d54a08 --- /dev/null +++ b/GildedRose/C/GildedRose.h @@ -0,0 +1,9 @@ +typedef struct +{ + char *name; + int sellIn; + int quality; +} Item; + +extern Item* init_item(Item* item, const char *name, int sellIn, int quality); +extern void update_quality(Item items[], int size); diff --git a/GildedRose/C/GildedRoseTextTests.c b/GildedRose/C/GildedRoseTextTests.c new file mode 100644 index 00000000..d200ca0c --- /dev/null +++ b/GildedRose/C/GildedRoseTextTests.c @@ -0,0 +1,43 @@ +#include +#include "GildedRose.h" + +int +print_item(Item *item) +{ + return printf("%s, %d, %d\n", item->name, item->sellIn, item->quality); +} + +int main() +{ + Item items[9]; + int last = 0; + int day; + int index; + + init_item(items + last++, "+5 Dexterity Vest", 10, 20); + init_item(items + last++, "Aged Brie", 2, 0); + init_item(items + last++, "Elixir of the Mongoose", 5, 7); + init_item(items + last++, "Sulfuras, Hand of Ragnaros", 0, 80); + init_item(items + last++, "Sulfuras, Hand of Ragnaros", -1, 80); + init_item(items + last++, "Backstage passes to a TAFKAL80ETC concert", 15, 20); + init_item(items + last++, "Backstage passes to a TAFKAL80ETC concert", 10, 49); + init_item(items + last++, "Backstage passes to a TAFKAL80ETC concert", 5, 49); + // this Conjured item doesn't yet work properly + init_item(items + last++, "Conjured Mana Cake", 3, 6); + + puts("OMGHAI!"); + + for (day = 0; day <= 30; day++) + { + printf("-------- day %d --------\n", day); + printf("name, sellIn, quality\n"); + for(index = 0; index < last; index++) { + print_item(items + index); + } + + printf("\n"); + + update_quality(items, last); + } + return 0; +} diff --git a/GildedRose/C/GildedRoseUnitTests.cc b/GildedRose/C/GildedRoseUnitTests.cc new file mode 100644 index 00000000..6d06fdb7 --- /dev/null +++ b/GildedRose/C/GildedRoseUnitTests.cc @@ -0,0 +1,43 @@ +#include +#include +#include + +extern "C" { +#include "GildedRose.h" +} + +TEST_GROUP(TestGildedRoseGroup) +{ + void setup() { + } + void teardown() { + } +}; + +TEST(TestGildedRoseGroup, FirstTest) +{ + Item items[2]; + init_item(items, "Foo", 0, 0); + update_quality(items, 1); + STRCMP_EQUAL("fixme", items[0].name); +} + +void example() +{ + Item items[6]; + int last = 0; + + init_item(items + last++, "+5 Dexterity Vest", 10, 20); + init_item(items + last++, "Aged Brie", 2, 0); + init_item(items + last++, "Elixir of the Mongoose", 5, 7); + init_item(items + last++, "Sulfuras, Hand of Ragnaros", 0, 80); + init_item(items + last++, "Backstage passes to a TAFKAL80ETC concert", 15, 20); + init_item(items + last++, "Conjured Mana Cake", 3, 6); + update_quality(items, last); +} + +int +main(int ac, char** av) +{ + return CommandLineTestRunner::RunAllTests(ac, av); +} diff --git a/GildedRose/C/Makefile b/GildedRose/C/Makefile new file mode 100644 index 00000000..60d11871 --- /dev/null +++ b/GildedRose/C/Makefile @@ -0,0 +1,51 @@ +# Makefile for building the kata file with the Google Testing Framework +# +# SYNOPSIS: +# +# make [all] - makes everything. +# make TARGET - makes the given target. +# make clean - removes all files generated by make. + +# Please tweak the following variable definitions as needed by your +# project. + +# Points to the root of CppUTest, relative to where this file is. +# Remember to tweak this if you move this file. +CPPUTEST_HOME = CppUTest + +# Where to find user code. +USER_DIR = . + +# Flags passed to the preprocessor. +CPPFLAGS += -I$(CPPUTEST_HOME)/include + +# Flags passed to the C++ compiler. +CFLAGS += -g -Wall -Wextra + +LD_LIBRARIES = -L$(CPPUTEST_HOME)/lib -lCppUTest -lCppUTestExt + +# All tests produced by this Makefile. Remember to add new tests you +# created to the list. +TESTS = GildedRoseUnitTests + +TEXTTESTS = GildedRoseTextTests + +# House-keeping build targets. + +all : $(TESTS) $(TEXTTESTS) + +GildedRose.o : $(USER_DIR)/GildedRose.c + +GildedRoseUnitTests : $(USER_DIR)/GildedRoseUnitTests.cc GildedRose.o + $(CXX) $(CPPFLAGS) $(CFLAGS) -o $@ $(USER_DIR)/GildedRoseUnitTests.cc GildedRose.o $(LD_LIBRARIES) + +GildedRoseTextTests.o : $(USER_DIR)/GildedRoseTextTests.c + +GildedRoseTextTests : GildedRoseTextTests.o GildedRose.o + $(CC) $^ -o $@ + +clean : + rm -f $(TESTS) $(TEXTTESTS) *.o *~ + +check-syntax: + gcc $(CPPFLAGS) -o /dev/null -S ${CHK_SOURCES} diff --git a/GildedRose/C/README b/GildedRose/C/README new file mode 100644 index 00000000..2bc1f69b --- /dev/null +++ b/GildedRose/C/README @@ -0,0 +1,5 @@ +run-once.sh runs your tests once + +Assumptions: + - make and a C++ compiler (like gcc) is installed on your system and is in the PATH + - The CppUTest framework is in the directory CppUTest diff --git a/GildedRose/C/run-once.sh b/GildedRose/C/run-once.sh new file mode 100755 index 00000000..4f6b2303 --- /dev/null +++ b/GildedRose/C/run-once.sh @@ -0,0 +1,2 @@ +make +./GildedRoseTextTests