Add a test to the Perfect Numbers exercise

The “Perfect Numbers” exercise in the C track lacks a test titled “Perfect square abundant number is classified correctly” in the canonical data of the exercise. Because the calculation of the aliquot sum requires a special treatment for perfect squares in a certain approach, it is important to have this test included.

Here is a sample modified test code.

#include "test-framework/unity.h"
#include "perfect_numbers.h"

void setUp(void)
{
}

void tearDown(void)
{
}

static void test_smallest_perfect_number_is_classified_correctly(void)
{
   TEST_ASSERT_EQUAL(PERFECT_NUMBER, classify_number(6));
}

static void test_medium_perfect_number_is_classified_correctly(void)
{
   TEST_IGNORE();   // delete this line to run test
   TEST_ASSERT_EQUAL(PERFECT_NUMBER, classify_number(28));
}

static void test_large_perfect_number_is_classified_correctly(void)
{
   TEST_IGNORE();
   TEST_ASSERT_EQUAL(PERFECT_NUMBER, classify_number(33550336));
}

static void test_smallest_abundant_number_is_classified_correctly(void)
{
   TEST_IGNORE();
   TEST_ASSERT_EQUAL(ABUNDANT_NUMBER, classify_number(12));
}

static void test_medium_abundant_number_is_classified_correctly(void)
{
   TEST_IGNORE();
   TEST_ASSERT_EQUAL(ABUNDANT_NUMBER, classify_number(30));
}

static void test_large_abundant_number_is_classified_correctly(void)
{
   TEST_IGNORE();
   TEST_ASSERT_EQUAL(ABUNDANT_NUMBER, classify_number(33550335));
}

static void test_smallest_prime_deficient_number_is_classified_correctly(void)
{
   TEST_IGNORE();
   TEST_ASSERT_EQUAL(DEFICIENT_NUMBER, classify_number(2));
}

static void
test_smallest_non_prime_deficient_number_is_classified_correctly(void)
{
   TEST_IGNORE();
   TEST_ASSERT_EQUAL(DEFICIENT_NUMBER, classify_number(4));
}

static void test_medium_deficient_number_is_classified_correctly(void)
{
   TEST_IGNORE();
   TEST_ASSERT_EQUAL(DEFICIENT_NUMBER, classify_number(32));
}

static void test_large_deficient_number_is_classified_correctly(void)
{
   TEST_IGNORE();
   TEST_ASSERT_EQUAL(DEFICIENT_NUMBER, classify_number(33550337));
}

static void test_edge_case_is_classified_correctly(void)
{
   TEST_IGNORE();
   TEST_ASSERT_EQUAL(DEFICIENT_NUMBER, classify_number(1));
}

static void test_abandant_perfect_sqaure_is_classified_correctly(void)
{
   TEST_IGNORE();
   TEST_ASSERT_EQUAL(ABUNDANT_NUMBER, classify_number(196));
}


static void test_zero_is_rejected(void)
{
   TEST_IGNORE();
   TEST_ASSERT_EQUAL(ERROR, classify_number(0));
}

static void test_negative_integer_is_rejected(void)
{
   TEST_IGNORE();
   TEST_ASSERT_EQUAL(ERROR, classify_number(-1));
}

int main(void)
{
   UNITY_BEGIN();

   RUN_TEST(test_smallest_perfect_number_is_classified_correctly);
   RUN_TEST(test_medium_perfect_number_is_classified_correctly);
   RUN_TEST(test_large_perfect_number_is_classified_correctly);
   RUN_TEST(test_smallest_abundant_number_is_classified_correctly);
   RUN_TEST(test_medium_abundant_number_is_classified_correctly);
   RUN_TEST(test_large_abundant_number_is_classified_correctly);
   RUN_TEST(test_smallest_prime_deficient_number_is_classified_correctly);
   RUN_TEST(test_smallest_non_prime_deficient_number_is_classified_correctly);
   RUN_TEST(test_medium_deficient_number_is_classified_correctly);
   RUN_TEST(test_large_deficient_number_is_classified_correctly);
   RUN_TEST(test_abandant_perfect_sqaure_is_classified_correctly);
   RUN_TEST(test_edge_case_is_classified_correctly);
   RUN_TEST(test_zero_is_rejected);
   RUN_TEST(test_negative_integer_is_rejected);

   return UNITY_END();
}

Any opinion?

I think the standard opinion is that practice exercises should generally follow the problem spec and be updated accordingly, unless there is a compelling reason not to do so.

The last C exercise sync, from a quick git commit history, looks be be over a year old. There is an open issue about tests being out of sync. I suspect the C maintainers would be happy for PRs to sync things up here.

@IsaacG Thanks. I have just submitted the PR, which simply adds the missing test. It should synchronize the exercise with the canonical data.

1 Like