Skip to content

Commit

Permalink
sum-of-multiples: align with specification per #527 (#577)
Browse files Browse the repository at this point in the history
* sum-of-multiples: align with specification per #527

* Update names to match spec
  • Loading branch information
wolf99 authored Jan 26, 2021
1 parent 953aa59 commit 70ec476
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 94 deletions.
32 changes: 16 additions & 16 deletions exercises/sum-of-multiples/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -1,49 +1,49 @@
[canonical-tests]

# no multiples within limit
#"54aaab5a-ce86-4edc-8b40-d3ab2400a279" = true
"54aaab5a-ce86-4edc-8b40-d3ab2400a279" = true

# one factor has multiples within limit
#"361e4e50-c89b-4f60-95ef-5bc5c595490a" = true
"361e4e50-c89b-4f60-95ef-5bc5c595490a" = true

# more than one multiple within limit
#"e644e070-040e-4ae0-9910-93c69fc3f7ce" = true
"e644e070-040e-4ae0-9910-93c69fc3f7ce" = true

# more than one factor with multiples within limit
#"607d6eb9-535c-41ce-91b5-3a61da3fa57f" = true
"607d6eb9-535c-41ce-91b5-3a61da3fa57f" = true

# each multiple is only counted once
#"f47e8209-c0c5-4786-b07b-dc273bf86b9b" = true
"f47e8209-c0c5-4786-b07b-dc273bf86b9b" = true

# a much larger limit
#"28c4b267-c980-4054-93e9-07723db615ac" = true
"28c4b267-c980-4054-93e9-07723db615ac" = true

# three factors
#"09c4494d-ff2d-4e0f-8421-f5532821ee12" = true
"09c4494d-ff2d-4e0f-8421-f5532821ee12" = true

# factors not relatively prime
#"2d0d5faa-f177-4ad6-bde9-ebb865083751" = true
"2d0d5faa-f177-4ad6-bde9-ebb865083751" = true

# some pairs of factors relatively prime and some not
#"ece8f2e8-96aa-4166-bbb7-6ce71261e354" = true
"ece8f2e8-96aa-4166-bbb7-6ce71261e354" = true

# one factor is a multiple of another
#"624fdade-6ffb-400e-8472-456a38c171c0" = true
"624fdade-6ffb-400e-8472-456a38c171c0" = true

# much larger factors
#"949ee7eb-db51-479c-b5cb-4a22b40ac057" = true
"949ee7eb-db51-479c-b5cb-4a22b40ac057" = true

# all numbers are multiples of 1
#"41093673-acbd-482c-ab80-d00a0cbedecd" = true
"41093673-acbd-482c-ab80-d00a0cbedecd" = true

# no factors means an empty sum
#"1730453b-baaa-438e-a9c2-d754497b2a76" = true
"1730453b-baaa-438e-a9c2-d754497b2a76" = true

# the only multiple of 0 is 0
#"214a01e9-f4bf-45bb-80f1-1dce9fbb0310" = true
"214a01e9-f4bf-45bb-80f1-1dce9fbb0310" = true

# the factor 0 does not affect the sum of multiples of other factors
#"c423ae21-a0cb-4ec7-aeb1-32971af5b510" = true
"c423ae21-a0cb-4ec7-aeb1-32971af5b510" = true

# solutions using include-exclude must extend to cardinality greater than 3
#"17053ba9-112f-4ac0-aadb-0519dd836342" = true
"17053ba9-112f-4ac0-aadb-0519dd836342" = true
13 changes: 6 additions & 7 deletions exercises/sum-of-multiples/src/example.c
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
#include "sum_of_multiples.h"
#include <stdbool.h>

unsigned int sum_of_multiples(const unsigned int *multiples,
const size_t number_of_multiples,
const unsigned int upper_bound)
unsigned int sum(const unsigned int *factors,
const size_t number_of_factors, const unsigned int limit)
{
unsigned int sum = 0;
if (NULL == multiples)
if (NULL == factors)
return sum;

for (unsigned int current = 1; current < upper_bound; current++) {
for (size_t i = 0; i < number_of_multiples; i++) {
if (0 != multiples[i] && (current % multiples[i]) == 0) {
for (unsigned int current = 1; current < limit; current++) {
for (size_t i = 0; i < number_of_factors; i++) {
if (0 != factors[i] && (current % factors[i]) == 0) {
sum += current;
break;
}
Expand Down
5 changes: 2 additions & 3 deletions exercises/sum-of-multiples/src/example.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@

#include <stddef.h>

unsigned int sum_of_multiples(const unsigned int *multiples,
const size_t number_of_multiples,
const unsigned int up_to);
unsigned int sum(const unsigned int *factors,
const size_t number_of_factors, const unsigned int limit);

#endif
5 changes: 2 additions & 3 deletions exercises/sum-of-multiples/src/sum_of_multiples.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@

#include <stddef.h>

unsigned int sum_of_multiples(const unsigned int *multiples,
const size_t number_of_multiples,
const unsigned int up_to);
unsigned int sum(const unsigned int *factors,
const size_t number_of_factors, const unsigned int limit);

#endif
130 changes: 65 additions & 65 deletions exercises/sum-of-multiples/test/test_sum_of_multiples.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,143 +11,143 @@ void tearDown(void)
{
}

static void test_sum_of_multiples_of_3_and_5_up_to_1(void)
static void test_no_multiples_within_limit(void)
{
const unsigned int multiples[] = { 3, 5 };
TEST_ASSERT_EQUAL(0,
sum_of_multiples(multiples, NUM_OF_ELEMENTS(multiples),
1));
TEST_ASSERT_EQUAL(0, sum(multiples, NUM_OF_ELEMENTS(multiples), 1));
}

static void test_sum_of_multiples_of_3_and_5_up_to_4(void)
static void test_one_factor_has_multiples_within_limit(void)
{
TEST_IGNORE(); // delete this line to run test
const unsigned int multiples[] = { 3, 5 };
TEST_ASSERT_EQUAL(3,
sum_of_multiples(multiples, NUM_OF_ELEMENTS(multiples),
4));
TEST_ASSERT_EQUAL(3, sum(multiples, NUM_OF_ELEMENTS(multiples), 4));
}

static void test_sum_of_multiples_of_3_and_5_up_to_10(void)
static void test_more_than_one_multiple_withiin_limit(void)
{
TEST_IGNORE();
const unsigned int multiples[] = { 3 };
TEST_ASSERT_EQUAL(9, sum(multiples, NUM_OF_ELEMENTS(multiples), 7));
}

static void test_more_than_one_factor_with_multiples_within_limit(void)
{
TEST_IGNORE();
const unsigned int multiples[] = { 3, 5 };
TEST_ASSERT_EQUAL(23,
sum_of_multiples(multiples, NUM_OF_ELEMENTS(multiples),
10));
TEST_ASSERT_EQUAL(23, sum(multiples, NUM_OF_ELEMENTS(multiples), 10));
}

static void test_sum_of_multiples_of_3_and_5_up_to_100(void)
static void test_each_multiple_is_only_counted_once(void)
{
TEST_IGNORE();
const unsigned int multiples[] = { 3, 5 };
TEST_ASSERT_EQUAL(2318,
sum_of_multiples(multiples, NUM_OF_ELEMENTS(multiples),
100));
TEST_ASSERT_EQUAL(2318, sum(multiples, NUM_OF_ELEMENTS(multiples), 100));
}

static void test_sum_of_multiples_of_3_and_5_up_to_1000(void)
static void test_a_much_larger_limit(void)
{
TEST_IGNORE();
const unsigned int multiples[] = { 3, 5 };
TEST_ASSERT_EQUAL(233168,
sum_of_multiples(multiples, NUM_OF_ELEMENTS(multiples),
1000));
TEST_ASSERT_EQUAL(233168, sum(multiples, NUM_OF_ELEMENTS(multiples), 1000));
}

static void test_sum_of_multiples_of_7_13_and_17_up_to_20(void)
static void test_three_factors(void)
{
TEST_IGNORE();
const unsigned int multiples[] = { 7, 13, 17 };
TEST_ASSERT_EQUAL(51,
sum_of_multiples(multiples, NUM_OF_ELEMENTS(multiples),
20));
TEST_ASSERT_EQUAL(51, sum(multiples, NUM_OF_ELEMENTS(multiples), 20));
}

static void test_sum_of_multiples_of_4_and_6_up_to_15(void)
static void test_factors_not_relatively_prime(void)
{
TEST_IGNORE();
const unsigned int multiples[] = { 4, 6 };
TEST_ASSERT_EQUAL(30,
sum_of_multiples(multiples, NUM_OF_ELEMENTS(multiples),
15));
TEST_ASSERT_EQUAL(30, sum(multiples, NUM_OF_ELEMENTS(multiples), 15));
}

static void test_sum_of_multiples_of_5_6_and_8_up_to_150(void)
static void test_some_pairs_of_factors_relatively_prime_and_some_not(void)
{
TEST_IGNORE();
const unsigned int multiples[] = { 5, 6, 8 };
TEST_ASSERT_EQUAL(4419,
sum_of_multiples(multiples, NUM_OF_ELEMENTS(multiples),
150));
TEST_ASSERT_EQUAL(4419, sum(multiples, NUM_OF_ELEMENTS(multiples), 150));
}

static void test_sum_of_multiples_of_5_and_25_up_to_51(void)
static void test_one_factor_is_a_multiple_of_another(void)
{
TEST_IGNORE();
const unsigned int multiples[] = { 5, 25 };
TEST_ASSERT_EQUAL(275,
sum_of_multiples(multiples, NUM_OF_ELEMENTS(multiples),
51));
TEST_ASSERT_EQUAL(275, sum(multiples, NUM_OF_ELEMENTS(multiples), 51));
}

static void test_sum_of_multiples_of_43_and_47_up_to_10000(void)
static void test_much_larger_factors(void)
{
TEST_IGNORE();
const unsigned int multiples[] = { 43, 47 };
TEST_ASSERT_EQUAL(2203160,
sum_of_multiples(multiples, NUM_OF_ELEMENTS(multiples),
10000));
sum(multiples, NUM_OF_ELEMENTS(multiples), 10000));
}

static void test_sum_of_multiples_of_1_up_to_100(void)
static void test_all_numbers_are_multiples_of_1(void)
{
TEST_IGNORE();
const unsigned int multiples[] = { 1 };
TEST_ASSERT_EQUAL(4950,
sum_of_multiples(multiples, NUM_OF_ELEMENTS(multiples),
100));
TEST_ASSERT_EQUAL(4950, sum(multiples, NUM_OF_ELEMENTS(multiples), 100));
}

static void test_sum_of_multiples_of_0_up_to_100(void)
static void test_no_factors_means_an_empty_sum(void)
{
TEST_IGNORE();
const unsigned int multiples[] = { 0 };
TEST_ASSERT_EQUAL(0,
sum_of_multiples(multiples, NUM_OF_ELEMENTS(multiples),
100));
TEST_ASSERT_EQUAL(0, sum(multiples, NUM_OF_ELEMENTS(multiples), 1000));
}

static void test_no_multiples_given(void)
static void test_the_only_multiple_of_zero_is_zero(void)
{
TEST_IGNORE();
TEST_ASSERT_EQUAL(0, sum_of_multiples(NULL, 0, 10000));
const unsigned int multiples[] = { 0 };
TEST_ASSERT_EQUAL(0, sum(multiples, NUM_OF_ELEMENTS(multiples), 1));
}

static void test_null_ptr_multiple(void)
static void
test_the_factor_0_does_not_affect_the_sum_of_multiples_of_other_factors(void)
{
TEST_IGNORE();
TEST_ASSERT_EQUAL(0, sum_of_multiples(NULL, 3, 10000));
const unsigned int multiples[] = { 3, 0 };
TEST_ASSERT_EQUAL(3, sum(multiples, NUM_OF_ELEMENTS(multiples), 4));
}

static void
test_solutions_using_include_exclude_must_extend_to_cardinality_greater_than_3
(void) {
TEST_IGNORE();
const unsigned int multiples[] = { 2, 3, 5, 7, 11 };
TEST_ASSERT_EQUAL(39614537,
sum(multiples, NUM_OF_ELEMENTS(multiples), 10000));
}

int main(void)
{
UnityBegin("test/test_sum_of_multiples.c");

RUN_TEST(test_sum_of_multiples_of_3_and_5_up_to_1);
RUN_TEST(test_sum_of_multiples_of_3_and_5_up_to_4);
RUN_TEST(test_sum_of_multiples_of_3_and_5_up_to_10);
RUN_TEST(test_sum_of_multiples_of_3_and_5_up_to_100);
RUN_TEST(test_sum_of_multiples_of_3_and_5_up_to_1000);
RUN_TEST(test_sum_of_multiples_of_7_13_and_17_up_to_20);
RUN_TEST(test_sum_of_multiples_of_4_and_6_up_to_15);
RUN_TEST(test_sum_of_multiples_of_5_6_and_8_up_to_150);
RUN_TEST(test_sum_of_multiples_of_5_and_25_up_to_51);
RUN_TEST(test_sum_of_multiples_of_43_and_47_up_to_10000);
RUN_TEST(test_sum_of_multiples_of_1_up_to_100);
RUN_TEST(test_sum_of_multiples_of_0_up_to_100);
RUN_TEST(test_no_multiples_given);
RUN_TEST(test_null_ptr_multiple);
RUN_TEST(test_no_multiples_within_limit);
RUN_TEST(test_one_factor_has_multiples_within_limit);
RUN_TEST(test_more_than_one_multiple_withiin_limit);
RUN_TEST(test_more_than_one_factor_with_multiples_within_limit);
RUN_TEST(test_each_multiple_is_only_counted_once);
RUN_TEST(test_a_much_larger_limit);
RUN_TEST(test_three_factors);
RUN_TEST(test_factors_not_relatively_prime);
RUN_TEST(test_some_pairs_of_factors_relatively_prime_and_some_not);
RUN_TEST(test_one_factor_is_a_multiple_of_another);
RUN_TEST(test_much_larger_factors);
RUN_TEST(test_all_numbers_are_multiples_of_1);
RUN_TEST(test_no_factors_means_an_empty_sum);
RUN_TEST(test_the_only_multiple_of_zero_is_zero);
RUN_TEST
(test_the_factor_0_does_not_affect_the_sum_of_multiples_of_other_factors);
RUN_TEST
(test_solutions_using_include_exclude_must_extend_to_cardinality_greater_than_3);

return UnityEnd();
}

0 comments on commit 70ec476

Please sign in to comment.