Skip to content

Commit

Permalink
Address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ahans committed Jan 28, 2024
1 parent 1d9ecd6 commit 2be14ce
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 60 deletions.
8 changes: 5 additions & 3 deletions exercises/practice/kindergarten-garden/.meta/example.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ static int student_to_index(const char *student)
static const char *students[] = { "Alice", "Bob", "Charlie", "David",
"Eve", "Fred", "Ginny", "Harriet",
"Ileana", "Joseph", "Kincaid", "Larry" };
for (size_t i = 0; i < sizeof(students); ++i) {
for (size_t i = 0; i < sizeof(students) / sizeof(students[0]); ++i) {
if (strcmp(students[i], student) == 0) {
return i;
}
Expand All @@ -34,13 +34,15 @@ static plant_t letter_to_plant(char letter)
exit(1);
}

void plants(const char *diagram, const char *student, plant_t *plants)
plants_t plants(const char *diagram, const char *student)
{
const char *rows[] = { diagram, strchr(diagram, '\n') + 1 };
const size_t student_col = student_to_index(student) * 2;
plants_t res;
for (int i = 0; i < 4; ++i) {
const size_t row_index = i / 2;
const size_t col_index = student_col + i % 2;
plants[i] = letter_to_plant(rows[row_index][col_index]);
res.plants[i] = letter_to_plant(rows[row_index][col_index]);
}
return res;
}
6 changes: 5 additions & 1 deletion exercises/practice/kindergarten-garden/.meta/example.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

typedef enum { CLOVER = 0, GRASS = 1, RADISHES = 2, VIOLETS = 3 } plant_t;

void plants(const char *diagram, const char *student, plant_t *plants);
typedef struct {
plant_t plants[4];
} plants_t;

plants_t plants(const char *diagram, const char *student);

#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "kindergarten_garden.h"
4 changes: 4 additions & 0 deletions exercises/practice/kindergarten-garden/kindergarten_garden.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@

typedef enum { CLOVER = 0, GRASS = 1, RADISHES = 2, VIOLETS = 3 } plant_t;

typedef struct {
plant_t plants[4];
} plants_t;

#endif
148 changes: 92 additions & 56 deletions exercises/practice/kindergarten-garden/test_kindergarten_garden.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,136 +11,172 @@ void tearDown(void)
}

static void test_kindergarten_garden(const char *diagram, const char *student,
const plant_t *expected)
const plants_t expected)
{
plant_t actual[4];
plants(diagram, student, actual);
const plants_t actual = plants(diagram, student);

TEST_ASSERT_EQUAL_INT_ARRAY(expected, actual, 4);
TEST_ASSERT_EQUAL_INT_ARRAY(expected.plants, actual.plants, 4);
}

static void test_partial_garden_garden_with_single_student(void)
{
const plant_t expected[] = { RADISHES, CLOVER, GRASS, GRASS };
test_kindergarten_garden("RC\nGG", "Alice", expected);
const plants_t expected = { .plants = { RADISHES, CLOVER, GRASS, GRASS } };
test_kindergarten_garden("RC\n"
"GG",
"Alice", expected);
}

static void test_partial_garden_different_garden_with_single_student(void)
{
const plant_t expected[] = { VIOLETS, CLOVER, RADISHES, CLOVER };
test_kindergarten_garden("VC\nRC", "Alice", expected);
TEST_IGNORE(); // delete this line to run test
const plants_t expected = { .plants = { VIOLETS, CLOVER, RADISHES,
CLOVER } };
test_kindergarten_garden("VC\n"
"RC",
"Alice", expected);
}

static void test_partial_garden_garden_with_two_students(void)
{
const plant_t expected[] = { CLOVER, GRASS, RADISHES, CLOVER };
test_kindergarten_garden("VVCG\nVVRC", "Bob", expected);
TEST_IGNORE();
const plants_t expected = { .plants = { CLOVER, GRASS, RADISHES, CLOVER } };
test_kindergarten_garden("VVCG\n"
"VVRC",
"Bob", expected);
}

static void test_partial_garden_second_student_s_garden(void)
{
const plant_t expected[] = { CLOVER, CLOVER, CLOVER, CLOVER };
test_kindergarten_garden("VVCCGG\nVVCCGG", "Bob", expected);
TEST_IGNORE();
const plants_t expected = { .plants = { CLOVER, CLOVER, CLOVER, CLOVER } };
test_kindergarten_garden("VVCCGG\n"
"VVCCGG",
"Bob", expected);
}

static void test_partial_garden_third_student_s_garden(void)
{
const plant_t expected[] = { GRASS, GRASS, GRASS, GRASS };
test_kindergarten_garden("VVCCGG\nVVCCGG", "Charlie", expected);
TEST_IGNORE();
const plants_t expected = { .plants = { GRASS, GRASS, GRASS, GRASS } };
test_kindergarten_garden("VVCCGG\n"
"VVCCGG",
"Charlie", expected);
}

static void test_full_garden_for_alice_first_student_s_garden(void)
{
const plant_t expected[] = { VIOLETS, RADISHES, VIOLETS, RADISHES };
test_kindergarten_garden(
"VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV", "Alice", expected);
TEST_IGNORE();
const plants_t expected = { .plants = { VIOLETS, RADISHES, VIOLETS,
RADISHES } };
test_kindergarten_garden("VRCGVVRVCGGCCGVRGCVCGCGV\n"
"VRCCCGCRRGVCGCRVVCVGCGCV",
"Alice", expected);
}

static void test_full_garden_for_bob_second_student_s_garden(void)
{
const plant_t expected[] = { CLOVER, GRASS, CLOVER, CLOVER };
test_kindergarten_garden(
"VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV", "Bob", expected);
TEST_IGNORE();
const plants_t expected = { .plants = { CLOVER, GRASS, CLOVER, CLOVER } };
test_kindergarten_garden("VRCGVVRVCGGCCGVRGCVCGCGV\n"
"VRCCCGCRRGVCGCRVVCVGCGCV",
"Bob", expected);
}

static void test_full_garden_for_charlie(void)
{
const plant_t expected[] = { VIOLETS, VIOLETS, CLOVER, GRASS };
test_kindergarten_garden(
"VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV", "Charlie",
expected);
TEST_IGNORE();
const plants_t expected = { .plants = { VIOLETS, VIOLETS, CLOVER, GRASS } };
test_kindergarten_garden("VRCGVVRVCGGCCGVRGCVCGCGV\n"
"VRCCCGCRRGVCGCRVVCVGCGCV",
"Charlie", expected);
}

static void test_full_garden_for_david(void)
{
const plant_t expected[] = { RADISHES, VIOLETS, CLOVER, RADISHES };
test_kindergarten_garden(
"VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV", "David", expected);
TEST_IGNORE();
const plants_t expected = { .plants = { RADISHES, VIOLETS, CLOVER,
RADISHES } };
test_kindergarten_garden("VRCGVVRVCGGCCGVRGCVCGCGV\n"
"VRCCCGCRRGVCGCRVVCVGCGCV",
"David", expected);
}

static void test_full_garden_for_eve(void)
{
const plant_t expected[] = { CLOVER, GRASS, RADISHES, GRASS };
test_kindergarten_garden(
"VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV", "Eve", expected);
TEST_IGNORE();
const plants_t expected = { .plants = { CLOVER, GRASS, RADISHES, GRASS } };
test_kindergarten_garden("VRCGVVRVCGGCCGVRGCVCGCGV\n"
"VRCCCGCRRGVCGCRVVCVGCGCV",
"Eve", expected);
}

static void test_full_garden_for_fred(void)
{
const plant_t expected[] = { GRASS, CLOVER, VIOLETS, CLOVER };
test_kindergarten_garden(
"VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV", "Fred", expected);
TEST_IGNORE();
const plants_t expected = { .plants = { GRASS, CLOVER, VIOLETS, CLOVER } };
test_kindergarten_garden("VRCGVVRVCGGCCGVRGCVCGCGV\n"
"VRCCCGCRRGVCGCRVVCVGCGCV",
"Fred", expected);
}

static void test_full_garden_for_ginny(void)
{
const plant_t expected[] = { CLOVER, GRASS, GRASS, CLOVER };
test_kindergarten_garden(
"VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV", "Ginny", expected);
TEST_IGNORE();
const plants_t expected = { .plants = { CLOVER, GRASS, GRASS, CLOVER } };
test_kindergarten_garden("VRCGVVRVCGGCCGVRGCVCGCGV\n"
"VRCCCGCRRGVCGCRVVCVGCGCV",
"Ginny", expected);
}

static void test_full_garden_for_harriet(void)
{
const plant_t expected[] = { VIOLETS, RADISHES, RADISHES, VIOLETS };
test_kindergarten_garden(
"VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV", "Harriet",
expected);
TEST_IGNORE();
const plants_t expected = { .plants = { VIOLETS, RADISHES, RADISHES,
VIOLETS } };
test_kindergarten_garden("VRCGVVRVCGGCCGVRGCVCGCGV\n"
"VRCCCGCRRGVCGCRVVCVGCGCV",
"Harriet", expected);
}

static void test_full_garden_for_ileana(void)
{
const plant_t expected[] = { GRASS, CLOVER, VIOLETS, CLOVER };
test_kindergarten_garden(
"VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV", "Ileana",
expected);
TEST_IGNORE();
const plants_t expected = { .plants = { GRASS, CLOVER, VIOLETS, CLOVER } };
test_kindergarten_garden("VRCGVVRVCGGCCGVRGCVCGCGV\n"
"VRCCCGCRRGVCGCRVVCVGCGCV",
"Ileana", expected);
}

static void test_full_garden_for_joseph(void)
{
const plant_t expected[] = { VIOLETS, CLOVER, VIOLETS, GRASS };
test_kindergarten_garden(
"VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV", "Joseph",
expected);
TEST_IGNORE();
const plants_t expected = { .plants = { VIOLETS, CLOVER, VIOLETS, GRASS } };
test_kindergarten_garden("VRCGVVRVCGGCCGVRGCVCGCGV\n"
"VRCCCGCRRGVCGCRVVCVGCGCV",
"Joseph", expected);
}

static void test_full_garden_for_kincaid_second_to_last_student_s_garden(void)
{
const plant_t expected[] = { GRASS, CLOVER, CLOVER, GRASS };
test_kindergarten_garden(
"VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV", "Kincaid",
expected);
TEST_IGNORE();
const plants_t expected = { .plants = { GRASS, CLOVER, CLOVER, GRASS } };
test_kindergarten_garden("VRCGVVRVCGGCCGVRGCVCGCGV\n"
"VRCCCGCRRGVCGCRVVCVGCGCV",
"Kincaid", expected);
}

static void test_full_garden_for_larry_last_student_s_garden(void)
{
const plant_t expected[] = { GRASS, VIOLETS, CLOVER, VIOLETS };
test_kindergarten_garden(
"VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV", "Larry", expected);
TEST_IGNORE();
const plants_t expected = { .plants = { GRASS, VIOLETS, CLOVER, VIOLETS } };
test_kindergarten_garden("VRCGVVRVCGGCCGVRGCVCGCGV\n"
"VRCCCGCRRGVCGCRVVCVGCGCV",
"Larry", expected);
}

int main(void)
{
UnityBegin("test_kindergarten-garden.c");
UnityBegin("test_kindergarten_garden.c");

RUN_TEST(test_partial_garden_garden_with_single_student);
RUN_TEST(test_partial_garden_different_garden_with_single_student);
Expand Down

0 comments on commit 2be14ce

Please sign in to comment.