ported the C++ version in C

This commit is contained in:
Frederic Lepied 2012-08-17 11:31:01 +02:00
parent 8e10e60244
commit e48300e32e
7 changed files with 243 additions and 0 deletions

90
GildedRose/C/GildedRose.c Normal file
View File

@ -0,0 +1,90 @@
#include <string.h>
#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;
}
}
}
}
}

View File

@ -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);

View File

@ -0,0 +1,43 @@
#include <stdio.h>
#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;
}

View File

@ -0,0 +1,43 @@
#include <CppUTest/TestHarness.h>
#include <CppUTest/CommandLineTestRunner.h>
#include <CppUTestExt/MockSupport.h>
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);
}

51
GildedRose/C/Makefile Normal file
View File

@ -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}

5
GildedRose/C/README Normal file
View File

@ -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

2
GildedRose/C/run-once.sh Executable file
View File

@ -0,0 +1,2 @@
make
./GildedRoseTextTests