From e343dda1b944c5f7ae970f3c4626e0febc8ca222 Mon Sep 17 00:00:00 2001 From: Elahi Concha Date: Wed, 30 Oct 2024 14:31:32 +0100 Subject: [PATCH 1/4] Updated grade-school tests --- .../practice/grade-school/.meta/tests.toml | 88 +++++++-- .../grade-school/grade_school_test.cpp | 177 +++++++++++++----- 2 files changed, 203 insertions(+), 62 deletions(-) diff --git a/exercises/practice/grade-school/.meta/tests.toml b/exercises/practice/grade-school/.meta/tests.toml index eada129dd..50c9e2e59 100644 --- a/exercises/practice/grade-school/.meta/tests.toml +++ b/exercises/practice/grade-school/.meta/tests.toml @@ -1,24 +1,86 @@ -# This is an auto-generated file. Regular comments will be removed when this -# file is regenerated. Regenerating will not touch any manually added keys, -# so comments can be added in a "comment" key. +# This is an auto-generated file. +# +# Regenerating this file via `configlet sync` will: +# - Recreate every `description` key/value pair +# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications +# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) +# - Preserve any other key/value pair +# +# As user-added comments (using the # character) will be removed when this file +# is regenerated, comments can be added via a `comment` key. + +[a3f0fb58-f240-4723-8ddc-e644666b85cc] +description = "Roster is empty when no student is added" + +[9337267f-7793-4b90-9b4a-8e3978408824] +description = "Add a student" [6d0a30e4-1b4e-472e-8e20-c41702125667] -description = "Adding a student adds them to the sorted roster" +description = "Student is added to the roster" + +[73c3ca75-0c16-40d7-82f5-ed8fe17a8e4a] +description = "Adding multiple students in the same grade in the roster" [233be705-dd58-4968-889d-fb3c7954c9cc] -description = "Adding more student adds them to the sorted roster" +description = "Multiple students in the same grade are added to the roster" + +[87c871c1-6bde-4413-9c44-73d59a259d83] +description = "Cannot add student to same grade in the roster more than once" + +[c125dab7-2a53-492f-a99a-56ad511940d8] +description = "A student can't be in two different grades" +include = false + +[a0c7b9b8-0e89-47f8-8b4a-c50f885e79d1] +description = "A student can only be added to the same grade in the roster once" +include = false +reimplements = "c125dab7-2a53-492f-a99a-56ad511940d8" + +[d7982c4f-1602-49f6-a651-620f2614243a] +description = "Student not added to same grade in the roster more than once" +reimplements = "a0c7b9b8-0e89-47f8-8b4a-c50f885e79d1" + +[e70d5d8f-43a9-41fd-94a4-1ea0fa338056] +description = "Adding students in multiple grades" [75a51579-d1d7-407c-a2f8-2166e984e8ab] -description = "Adding students to different grades adds them to the same sorted roster" +description = "Students in multiple grades are added to the roster" -[a3f0fb58-f240-4723-8ddc-e644666b85cc] -description = "Roster returns an empty list if there are no students enrolled" +[7df542f1-57ce-433c-b249-ff77028ec479] +description = "Cannot add same student to multiple grades in the roster" -[180a8ff9-5b94-43fc-9db1-d46b4a8c93b6] -description = "Student names with grades are displayed in the same sorted roster" +[6a03b61e-1211-4783-a3cc-fc7f773fba3f] +description = "A student cannot be added to more than one grade in the sorted roster" +include = false +reimplements = "c125dab7-2a53-492f-a99a-56ad511940d8" -[1bfbcef1-e4a3-49e8-8d22-f6f9f386187e] -description = "Grade returns the students in that grade in alphabetical order" +[c7ec1c5e-9ab7-4d3b-be5c-29f2f7a237c5] +description = "Student not added to multiple grades in the roster" +reimplements = "6a03b61e-1211-4783-a3cc-fc7f773fba3f" + +[d9af4f19-1ba1-48e7-94d0-dabda4e5aba6] +description = "Students are sorted by grades in the roster" + +[d9fb5bea-f5aa-4524-9d61-c158d8906807] +description = "Students are sorted by name in the roster" + +[180a8ff9-5b94-43fc-9db1-d46b4a8c93b6] +description = "Students are sorted by grades and then by name in the roster" [5e67aa3c-a3c6-4407-a183-d8fe59cd1630] -description = "Grade returns an empty list if there are no students in that grade" +description = "Grade is empty if no students in the roster" + +[1e0cf06b-26e0-4526-af2d-a2e2df6a51d6] +description = "Grade is empty if no students in that grade" + +[2bfc697c-adf2-4b65-8d0f-c46e085f796e] +description = "Student not added to same grade more than once" + +[66c8e141-68ab-4a04-a15a-c28bc07fe6b9] +description = "Student not added to multiple grades" + +[c9c1fc2f-42e0-4d2c-b361-99271f03eda7] +description = "Student not added to other grade for multiple grades" + +[1bfbcef1-e4a3-49e8-8d22-f6f9f386187e] +description = "Students are sorted by name in a grade" diff --git a/exercises/practice/grade-school/grade_school_test.cpp b/exercises/practice/grade-school/grade_school_test.cpp index 54d0f46d3..365dc5618 100644 --- a/exercises/practice/grade-school/grade_school_test.cpp +++ b/exercises/practice/grade-school/grade_school_test.cpp @@ -8,93 +8,172 @@ using namespace std; -TEST_CASE("a_new_school_has_an_empty_roster") -{ - const grade_school::school school_{}; +TEST_CASE("Roster is empty when no student is added", "[a3f0fb58-f240-4723-8ddc-e644666b85cc]") { + const grade_school::school school_; REQUIRE(school_.roster().empty()); } #if defined(EXERCISM_RUN_ALL_TESTS) -TEST_CASE("adding_a_student_adds_them_to_the_roster_for_the_given_grade") -{ + +TEST_CASE("Add a student", "[9337267f-7793-4b90-9b4a-8e3978408824]") { grade_school::school school_; school_.add("Aimee", 2); + REQUIRE(school_.roster() == map>{{2, {"Aimee"}}}); +} - const auto actual = school_.roster(); +TEST_CASE("Student is added to the roster", "[6d0a30e4-1b4e-472e-8e20-c41702125667]") { + grade_school::school school_; + school_.add("Aimee", 2); + REQUIRE(school_.grade(2) == vector{"Aimee"}); +} - const map> expected{{2, {"Aimee"}}}; - REQUIRE(expected == actual); +TEST_CASE("Adding multiple students in the same grade in the roster", "[73c3ca75-0c16-40d7-82f5-ed8fe17a8e4a]") { + grade_school::school school_; + school_.add("Blair", 2); + school_.add("James", 2); + school_.add("Paul", 2); + REQUIRE(school_.grade(2) == vector{"Blair", "James", "Paul"}); } -TEST_CASE("adding_more_students_to_the_same_grade_adds_them_to_the_roster") -{ +TEST_CASE("Multiple students in the same grade are added to the roster", "[233be705-dd58-4968-889d-fb3c7954c9cc]") { grade_school::school school_; school_.add("Blair", 2); school_.add("James", 2); school_.add("Paul", 2); + REQUIRE(school_.roster() == map>{{2, {"Blair", "James", "Paul"}}}); +} - const auto actual = school_.roster(); +TEST_CASE("Cannot add student to same grade in the roster more than once", "[87c871c1-6bde-4413-9c44-73d59a259d83]") { + grade_school::school school_; + school_.add("Blair", 2); + school_.add("James", 2); + school_.add("James", 2); + school_.add("Paul", 2); + REQUIRE(school_.grade(2) == vector{"Blair", "James", "Paul"}); +} - const map> expected{{2, {"Blair", "James", "Paul"}}}; - REQUIRE(expected == actual); +TEST_CASE("Student not added to same grade in the roster more than once", "[d7982c4f-1602-49f6-a651-620f2614243a]") { + grade_school::school school_; + school_.add("Blair", 2); + school_.add("James", 2); + school_.add("James", 2); + school_.add("Paul", 2); + REQUIRE(school_.roster() == map>{{2, {"Blair", "James", "Paul"}}}); } -TEST_CASE("adding_students_to_different_grades_adds_them_to_the_roster") -{ +TEST_CASE("Adding students in multiple grades", "[e70d5d8f-43a9-41fd-94a4-1ea0fa338056]") { grade_school::school school_; school_.add("Chelsea", 3); school_.add("Logan", 7); + REQUIRE(school_.roster() == map>{{3, {"Chelsea"}}, {7, {"Logan"}}}); +} - const auto actual = school_.roster(); +TEST_CASE("Students in multiple grades are added to the roster", "[75a51579-d1d7-407c-a2f8-2166e984e8ab]") { + grade_school::school school_; + school_.add("Chelsea", 3); + school_.add("Logan", 7); + REQUIRE(school_.roster() == map>{{3, {"Chelsea"}}, {7, {"Logan"}}}); +} - const map> expected{{3, {"Chelsea"}}, {7, {"Logan"}}}; - REQUIRE(expected == actual); +TEST_CASE("Cannot add same student to multiple grades in the roster", "[7df542f1-57ce-433c-b249-ff77028ec479]") { + grade_school::school school_; + school_.add("Aimee", 2); + school_.add("Aimee", 3); + REQUIRE(school_.roster() == map>{{2, {"Aimee"}}}); } -TEST_CASE("grade_returns_the_students_in_that_grade_in_alphabetical_order") -{ +TEST_CASE("Student not added to multiple grades in the roster", "[c7ec1c5e-9ab7-4d3b-be5c-29f2f7a237c5]") { grade_school::school school_; - school_.add("Franklin", 5); - school_.add("Bradley", 5); - school_.add("Jeff", 1); + school_.add("Blair", 2); + school_.add("James", 2); + school_.add("James", 3); + school_.add("Paul", 3); + REQUIRE(school_.roster() == map>{{2, {"Blair", "James"}}, {3, {"Paul"}}}); +} - const auto actual = school_.grade(5); +TEST_CASE("Students are sorted by grades in the roster", "[d9af4f19-1ba1-48e7-94d0-dabda4e5aba6]") { + grade_school::school school_; + school_.add("Anna", 1); + school_.add("Jim", 3); + school_.add("Peter", 2); + REQUIRE(school_.roster() == map>{ + {1, {"Anna"}}, + {2, {"Peter"}}, + {3, {"Jim"}} + }); +} - const vector expected{"Bradley", "Franklin"}; - REQUIRE(expected == actual); +TEST_CASE("Students are sorted by name in the roster", "[d9fb5bea-f5aa-4524-9d61-c158d8906807]") { + grade_school::school school_; + school_.add("Zoe", 2); + school_.add("Peter", 2); + school_.add("Alex", 2); + REQUIRE(school_.roster() == map>{{2, {"Alex", "Peter", "Zoe"}}}); } -TEST_CASE("grade_returns_an_empty_array_if_there_are_no_students_in_that_grade") -{ - const grade_school::school school_{}; - const auto actual = school_.grade(1); +TEST_CASE("Students are sorted by grades and then by name in the roster", "[180a8ff9-5b94-43fc-9db1-d46b4a8c93b6]") { + grade_school::school school_; + school_.add("Jim", 3); + school_.add("Peter", 2); + school_.add("Anna", 1); + school_.add("Barb", 1); + school_.add("Zoe", 2); + school_.add("Alex", 2); + school_.add("Charlie", 1); + REQUIRE(school_.roster() == map>{ + {1, {"Anna", "Barb", "Charlie"}}, + {2, {"Alex", "Peter", "Zoe"}}, + {3, {"Jim"}} + }); +} - REQUIRE(actual.empty()); +TEST_CASE("Grade is empty if no students in the roster", "[5e67aa3c-a3c6-4407-a183-d8fe59cd1630]") { + const grade_school::school school_; + REQUIRE(school_.grade(1).empty()); } -TEST_CASE("the_student_names_in_each_grade_in_the_roster_are_sorted") -{ +TEST_CASE("Grade is empty if no students in that grade", "[1e0cf06b-26e0-4526-af2d-a2e2df6a51d6]") { grade_school::school school_; - school_.add("Jennifer", 4); - school_.add("Kareem", 6); - school_.add("Christopher", 4); - school_.add("Kyle", 3); + school_.add("Peter", 2); + school_.add("Zoe", 2); + school_.add("Alex", 2); + school_.add("Jim", 3); + REQUIRE(school_.grade(1).empty()); +} - const auto actual = school_.roster(); +TEST_CASE("Student not added to same grade more than once", "[2bfc697c-adf2-4b65-8d0f-c46e085f796e]") { + grade_school::school school_; + school_.add("Blair", 2); + school_.add("James", 2); + school_.add("James", 2); + school_.add("Paul", 2); + REQUIRE(school_.grade(2) == vector{"Blair", "James", "Paul"}); +} - const map> expected{ - {3, {"Kyle"}}, - {4, {"Christopher", "Jennifer"}}, - {6, {"Kareem"}} - }; - REQUIRE(expected == actual); +TEST_CASE("Student not added to multiple grades", "[66c8e141-68ab-4a04-a15a-c28bc07fe6b9]") { + grade_school::school school_; + school_.add("Blair", 2); + school_.add("James", 2); + school_.add("James", 3); + school_.add("Paul", 3); + REQUIRE(school_.grade(2) == vector{"Blair", "James"}); } -TEST_CASE("checking_a_grade_should_not_change_the_roster") -{ - const grade_school::school school_{}; - school_.grade(1); - REQUIRE(school_.roster().empty()); +TEST_CASE("Student not added to other grade for multiple grades", "[c9c1fc2f-42e0-4d2c-b361-99271f03eda7]") { + grade_school::school school_; + school_.add("Blair", 2); + school_.add("James", 2); + school_.add("James", 3); + school_.add("Paul", 3); + REQUIRE(school_.grade(3) == vector{"Paul"}); +} + +TEST_CASE("Students are sorted by name in a grade", "[1bfbcef1-e4a3-49e8-8d22-f6f9f386187e]") { + grade_school::school school_; + school_.add("Franklin", 5); + school_.add("Bradley", 5); + school_.add("Jeff", 1); + REQUIRE(school_.grade(5) == vector{"Bradley", "Franklin"}); } #endif From ee3f7415a413d285f72ea875700e53c9ecaca850 Mon Sep 17 00:00:00 2001 From: Elahi Concha Date: Wed, 27 Nov 2024 09:39:27 +0100 Subject: [PATCH 2/4] Run clang-format on test file --- .../grade-school/grade_school_test.cpp | 93 ++++++++++++------- 1 file changed, 58 insertions(+), 35 deletions(-) diff --git a/exercises/practice/grade-school/grade_school_test.cpp b/exercises/practice/grade-school/grade_school_test.cpp index 365dc5618..5625f3a3c 100644 --- a/exercises/practice/grade-school/grade_school_test.cpp +++ b/exercises/practice/grade-school/grade_school_test.cpp @@ -1,4 +1,5 @@ #include "grade_school.h" + #include #ifdef EXERCISM_TEST_SUITE #include @@ -8,7 +9,8 @@ using namespace std; -TEST_CASE("Roster is empty when no student is added", "[a3f0fb58-f240-4723-8ddc-e644666b85cc]") { +TEST_CASE("Roster is empty when no student is added", + "[a3f0fb58-f240-4723-8ddc-e644666b85cc]") { const grade_school::school school_; REQUIRE(school_.roster().empty()); } @@ -21,13 +23,15 @@ TEST_CASE("Add a student", "[9337267f-7793-4b90-9b4a-8e3978408824]") { REQUIRE(school_.roster() == map>{{2, {"Aimee"}}}); } -TEST_CASE("Student is added to the roster", "[6d0a30e4-1b4e-472e-8e20-c41702125667]") { +TEST_CASE("Student is added to the roster", + "[6d0a30e4-1b4e-472e-8e20-c41702125667]") { grade_school::school school_; school_.add("Aimee", 2); REQUIRE(school_.grade(2) == vector{"Aimee"}); } -TEST_CASE("Adding multiple students in the same grade in the roster", "[73c3ca75-0c16-40d7-82f5-ed8fe17a8e4a]") { +TEST_CASE("Adding multiple students in the same grade in the roster", + "[73c3ca75-0c16-40d7-82f5-ed8fe17a8e4a]") { grade_school::school school_; school_.add("Blair", 2); school_.add("James", 2); @@ -35,15 +39,18 @@ TEST_CASE("Adding multiple students in the same grade in the roster", "[73c3ca75 REQUIRE(school_.grade(2) == vector{"Blair", "James", "Paul"}); } -TEST_CASE("Multiple students in the same grade are added to the roster", "[233be705-dd58-4968-889d-fb3c7954c9cc]") { +TEST_CASE("Multiple students in the same grade are added to the roster", + "[233be705-dd58-4968-889d-fb3c7954c9cc]") { grade_school::school school_; school_.add("Blair", 2); school_.add("James", 2); school_.add("Paul", 2); - REQUIRE(school_.roster() == map>{{2, {"Blair", "James", "Paul"}}}); + REQUIRE(school_.roster() == + map>{{2, {"Blair", "James", "Paul"}}}); } -TEST_CASE("Cannot add student to same grade in the roster more than once", "[87c871c1-6bde-4413-9c44-73d59a259d83]") { +TEST_CASE("Cannot add student to same grade in the roster more than once", + "[87c871c1-6bde-4413-9c44-73d59a259d83]") { grade_school::school school_; school_.add("Blair", 2); school_.add("James", 2); @@ -52,66 +59,77 @@ TEST_CASE("Cannot add student to same grade in the roster more than once", "[87c REQUIRE(school_.grade(2) == vector{"Blair", "James", "Paul"}); } -TEST_CASE("Student not added to same grade in the roster more than once", "[d7982c4f-1602-49f6-a651-620f2614243a]") { +TEST_CASE("Student not added to same grade in the roster more than once", + "[d7982c4f-1602-49f6-a651-620f2614243a]") { grade_school::school school_; school_.add("Blair", 2); school_.add("James", 2); school_.add("James", 2); school_.add("Paul", 2); - REQUIRE(school_.roster() == map>{{2, {"Blair", "James", "Paul"}}}); + REQUIRE(school_.roster() == + map>{{2, {"Blair", "James", "Paul"}}}); } -TEST_CASE("Adding students in multiple grades", "[e70d5d8f-43a9-41fd-94a4-1ea0fa338056]") { +TEST_CASE("Adding students in multiple grades", + "[e70d5d8f-43a9-41fd-94a4-1ea0fa338056]") { grade_school::school school_; school_.add("Chelsea", 3); school_.add("Logan", 7); - REQUIRE(school_.roster() == map>{{3, {"Chelsea"}}, {7, {"Logan"}}}); + REQUIRE(school_.roster() == + map>{{3, {"Chelsea"}}, {7, {"Logan"}}}); } -TEST_CASE("Students in multiple grades are added to the roster", "[75a51579-d1d7-407c-a2f8-2166e984e8ab]") { +TEST_CASE("Students in multiple grades are added to the roster", + "[75a51579-d1d7-407c-a2f8-2166e984e8ab]") { grade_school::school school_; school_.add("Chelsea", 3); school_.add("Logan", 7); - REQUIRE(school_.roster() == map>{{3, {"Chelsea"}}, {7, {"Logan"}}}); + REQUIRE(school_.roster() == + map>{{3, {"Chelsea"}}, {7, {"Logan"}}}); } -TEST_CASE("Cannot add same student to multiple grades in the roster", "[7df542f1-57ce-433c-b249-ff77028ec479]") { +TEST_CASE("Cannot add same student to multiple grades in the roster", + "[7df542f1-57ce-433c-b249-ff77028ec479]") { grade_school::school school_; school_.add("Aimee", 2); school_.add("Aimee", 3); REQUIRE(school_.roster() == map>{{2, {"Aimee"}}}); } -TEST_CASE("Student not added to multiple grades in the roster", "[c7ec1c5e-9ab7-4d3b-be5c-29f2f7a237c5]") { +TEST_CASE("Student not added to multiple grades in the roster", + "[c7ec1c5e-9ab7-4d3b-be5c-29f2f7a237c5]") { grade_school::school school_; school_.add("Blair", 2); school_.add("James", 2); school_.add("James", 3); school_.add("Paul", 3); - REQUIRE(school_.roster() == map>{{2, {"Blair", "James"}}, {3, {"Paul"}}}); + REQUIRE(school_.roster() == + map>{{2, {"Blair", "James"}}, {3, {"Paul"}}}); } -TEST_CASE("Students are sorted by grades in the roster", "[d9af4f19-1ba1-48e7-94d0-dabda4e5aba6]") { +TEST_CASE("Students are sorted by grades in the roster", + "[d9af4f19-1ba1-48e7-94d0-dabda4e5aba6]") { grade_school::school school_; school_.add("Anna", 1); school_.add("Jim", 3); school_.add("Peter", 2); - REQUIRE(school_.roster() == map>{ - {1, {"Anna"}}, - {2, {"Peter"}}, - {3, {"Jim"}} - }); + REQUIRE(school_.roster() == map>{{1, {"Anna"}}, + {2, {"Peter"}}, + {3, {"Jim"}}}); } -TEST_CASE("Students are sorted by name in the roster", "[d9fb5bea-f5aa-4524-9d61-c158d8906807]") { +TEST_CASE("Students are sorted by name in the roster", + "[d9fb5bea-f5aa-4524-9d61-c158d8906807]") { grade_school::school school_; school_.add("Zoe", 2); school_.add("Peter", 2); school_.add("Alex", 2); - REQUIRE(school_.roster() == map>{{2, {"Alex", "Peter", "Zoe"}}}); + REQUIRE(school_.roster() == + map>{{2, {"Alex", "Peter", "Zoe"}}}); } -TEST_CASE("Students are sorted by grades and then by name in the roster", "[180a8ff9-5b94-43fc-9db1-d46b4a8c93b6]") { +TEST_CASE("Students are sorted by grades and then by name in the roster", + "[180a8ff9-5b94-43fc-9db1-d46b4a8c93b6]") { grade_school::school school_; school_.add("Jim", 3); school_.add("Peter", 2); @@ -120,19 +138,20 @@ TEST_CASE("Students are sorted by grades and then by name in the roster", "[180a school_.add("Zoe", 2); school_.add("Alex", 2); school_.add("Charlie", 1); - REQUIRE(school_.roster() == map>{ - {1, {"Anna", "Barb", "Charlie"}}, - {2, {"Alex", "Peter", "Zoe"}}, - {3, {"Jim"}} - }); + REQUIRE(school_.roster() == + map>{{1, {"Anna", "Barb", "Charlie"}}, + {2, {"Alex", "Peter", "Zoe"}}, + {3, {"Jim"}}}); } -TEST_CASE("Grade is empty if no students in the roster", "[5e67aa3c-a3c6-4407-a183-d8fe59cd1630]") { +TEST_CASE("Grade is empty if no students in the roster", + "[5e67aa3c-a3c6-4407-a183-d8fe59cd1630]") { const grade_school::school school_; REQUIRE(school_.grade(1).empty()); } -TEST_CASE("Grade is empty if no students in that grade", "[1e0cf06b-26e0-4526-af2d-a2e2df6a51d6]") { +TEST_CASE("Grade is empty if no students in that grade", + "[1e0cf06b-26e0-4526-af2d-a2e2df6a51d6]") { grade_school::school school_; school_.add("Peter", 2); school_.add("Zoe", 2); @@ -141,7 +160,8 @@ TEST_CASE("Grade is empty if no students in that grade", "[1e0cf06b-26e0-4526-af REQUIRE(school_.grade(1).empty()); } -TEST_CASE("Student not added to same grade more than once", "[2bfc697c-adf2-4b65-8d0f-c46e085f796e]") { +TEST_CASE("Student not added to same grade more than once", + "[2bfc697c-adf2-4b65-8d0f-c46e085f796e]") { grade_school::school school_; school_.add("Blair", 2); school_.add("James", 2); @@ -150,7 +170,8 @@ TEST_CASE("Student not added to same grade more than once", "[2bfc697c-adf2-4b65 REQUIRE(school_.grade(2) == vector{"Blair", "James", "Paul"}); } -TEST_CASE("Student not added to multiple grades", "[66c8e141-68ab-4a04-a15a-c28bc07fe6b9]") { +TEST_CASE("Student not added to multiple grades", + "[66c8e141-68ab-4a04-a15a-c28bc07fe6b9]") { grade_school::school school_; school_.add("Blair", 2); school_.add("James", 2); @@ -159,7 +180,8 @@ TEST_CASE("Student not added to multiple grades", "[66c8e141-68ab-4a04-a15a-c28b REQUIRE(school_.grade(2) == vector{"Blair", "James"}); } -TEST_CASE("Student not added to other grade for multiple grades", "[c9c1fc2f-42e0-4d2c-b361-99271f03eda7]") { +TEST_CASE("Student not added to other grade for multiple grades", + "[c9c1fc2f-42e0-4d2c-b361-99271f03eda7]") { grade_school::school school_; school_.add("Blair", 2); school_.add("James", 2); @@ -168,7 +190,8 @@ TEST_CASE("Student not added to other grade for multiple grades", "[c9c1fc2f-42e REQUIRE(school_.grade(3) == vector{"Paul"}); } -TEST_CASE("Students are sorted by name in a grade", "[1bfbcef1-e4a3-49e8-8d22-f6f9f386187e]") { +TEST_CASE("Students are sorted by name in a grade", + "[1bfbcef1-e4a3-49e8-8d22-f6f9f386187e]") { grade_school::school school_; school_.add("Franklin", 5); school_.add("Bradley", 5); From 0f67a9f7526b956484f8dce44c937614db815f9e Mon Sep 17 00:00:00 2001 From: Elahi Concha Date: Wed, 11 Dec 2024 16:28:42 +0100 Subject: [PATCH 3/4] Change example solution to accomodate new test cases --- .../practice/grade-school/.meta/example.cpp | 37 +++++++++++-------- .../practice/grade-school/.meta/example.h | 32 ++++++++-------- 2 files changed, 36 insertions(+), 33 deletions(-) diff --git a/exercises/practice/grade-school/.meta/example.cpp b/exercises/practice/grade-school/.meta/example.cpp index 72d7f7bb3..606afc8d8 100644 --- a/exercises/practice/grade-school/.meta/example.cpp +++ b/exercises/practice/grade-school/.meta/example.cpp @@ -1,22 +1,27 @@ #include "grade_school.h" -#include -using namespace std; +namespace grade_school { + void school::add(std::string const& name, int grade) { + // Check if the student already exists + if (students_.find(name) != students_.end()) + { + // Student already exists; do not add + return; + } + // Add the student to the set of students + students_.insert(name); + // Proceed to add the student to the given grade in sorted order + std::vector& grade_roster = roster_[grade]; + auto it = lower_bound(grade_roster.begin(), grade_roster.end(), name); + grade_roster.insert(it, name); + } -namespace grade_school -{ + std::vector school::grade(int grade) const + { + auto it = roster_.find(grade); + return (it != roster_.end()) ? it->second : std::vector{}; + } -void school::add(string const& name, int grade) -{ - vector& grade_roster = roster_[grade]; - auto it = lower_bound(grade_roster.begin(), grade_roster.end(), name); - grade_roster.insert(it, name); -} -vector school::grade(int grade) const -{ - auto it = roster_.find(grade); - return (it != roster_.end()) ? it->second : vector{}; -} +} // namespace grade_school -} diff --git a/exercises/practice/grade-school/.meta/example.h b/exercises/practice/grade-school/.meta/example.h index 9a6baf692..cc0f19cd7 100644 --- a/exercises/practice/grade-school/.meta/example.h +++ b/exercises/practice/grade-school/.meta/example.h @@ -1,28 +1,26 @@ -#if !defined(GRADE_SCHOOL_H) -#define GRADE_SCHOOL_H - -#include -#include +#include #include +#include +#include namespace grade_school { -class school -{ -public: - const std::map>& roster() const { - return roster_; - } + class school + { + public: + const std::map>& roster() const { + return roster_; + } - void add(std::string const& name, int grade); + void add(std::string const& name, int grade); - std::vector grade(int grade) const; + std::vector grade(int grade) const; -private: - std::map> roster_; -}; + private: + std::map> roster_; + std::set students_; // New member to track existing students + }; } -#endif From 93369fa04e5b643cf3199401af5e59d9fa1994fd Mon Sep 17 00:00:00 2001 From: Elahi Concha Date: Wed, 11 Dec 2024 16:33:32 +0100 Subject: [PATCH 4/4] Run clang-format to fix formatting --- .../practice/grade-school/.meta/example.cpp | 36 +++++++++---------- .../practice/grade-school/.meta/example.h | 33 ++++++++--------- 2 files changed, 31 insertions(+), 38 deletions(-) diff --git a/exercises/practice/grade-school/.meta/example.cpp b/exercises/practice/grade-school/.meta/example.cpp index 606afc8d8..560ee412f 100644 --- a/exercises/practice/grade-school/.meta/example.cpp +++ b/exercises/practice/grade-school/.meta/example.cpp @@ -1,27 +1,23 @@ #include "grade_school.h" namespace grade_school { - void school::add(std::string const& name, int grade) { - // Check if the student already exists - if (students_.find(name) != students_.end()) - { - // Student already exists; do not add - return; - } - // Add the student to the set of students - students_.insert(name); - // Proceed to add the student to the given grade in sorted order - std::vector& grade_roster = roster_[grade]; - auto it = lower_bound(grade_roster.begin(), grade_roster.end(), name); - grade_roster.insert(it, name); - } - - std::vector school::grade(int grade) const - { - auto it = roster_.find(grade); - return (it != roster_.end()) ? it->second : std::vector{}; +void school::add(std::string const& name, int grade) { + // Check if the student already exists + if (students_.find(name) != students_.end()) { + // Student already exists; do not add + return; } + // Add the student to the set of students + students_.insert(name); + // Proceed to add the student to the given grade in sorted order + std::vector& grade_roster = roster_[grade]; + auto it = lower_bound(grade_roster.begin(), grade_roster.end(), name); + grade_roster.insert(it, name); +} +std::vector school::grade(int grade) const { + auto it = roster_.find(grade); + return (it != roster_.end()) ? it->second : std::vector{}; +} } // namespace grade_school - diff --git a/exercises/practice/grade-school/.meta/example.h b/exercises/practice/grade-school/.meta/example.h index cc0f19cd7..1d0c3cb21 100644 --- a/exercises/practice/grade-school/.meta/example.h +++ b/exercises/practice/grade-school/.meta/example.h @@ -1,26 +1,23 @@ +#include #include -#include #include -#include - -namespace grade_school -{ +#include - class school - { - public: - const std::map>& roster() const { - return roster_; - } +namespace grade_school { - void add(std::string const& name, int grade); +class school { + public: + const std::map>& roster() const { + return roster_; + } - std::vector grade(int grade) const; + void add(std::string const& name, int grade); - private: - std::map> roster_; - std::set students_; // New member to track existing students - }; + std::vector grade(int grade) const; -} + private: + std::map> roster_; + std::set students_; // New member to track existing students +}; +} // namespace grade_school