How our code is supposed to take exercism tests?

Hi,
doing “Armstrong Numbers” in C, I wrote a program on my pc which asks the user to input the number and check wether is armstrong or not. but how should I modify it to run the tests? Deleting printf() and scanf() isn’t important, since it isn’t the important part of the code, but however, in general and also for the following exercises, how exercism tests work?

#include <stdio.h>
#include <math.h>

int getdig(int n);
void check();
int getsum(int n, int ndig);

int main() {
    int num;
    printf("Type your number: ");
    scanf("%d", &num);
    

    int digits = getdig(num);
    printf("Number fdits: %d\n", digits);
    int somma = getsum(num, digits);
    printf("somma: %d\n", somma);

    check(num, somma);
    
    return 0;
}


// get number of digits
int getdig(int n) {
    int ndig = 0;
    while (n != 0) {   
        n /= 10;
        ndig++ ;
    }
    return ndig;
}
//raise and sum

int getsum(int n, int ndig) {
    int somma, cifra;
    while (n != 0) {
    cifra = n % 10;
    somma += pow(cifra, ndig);
    n /= 10;
    }
    return somma;
}

// check if equal
void check(int n, int somma) {
if (somma == n) {
    printf("%d is an Armstrong number", n);
}
else {
    printf("%d is not an Armstrong number", n);
}
}

Here at Exercism you write functions that get compiled with the tests into a binary, and the tests call your functions and check the results.
You don’t write a main() function or print something out.

For the exercise armstrong-numbers the initial setup looks like this:

armstrong_numbers.h:

#ifndef ARMSTRONG_NUMBERS_H
#define ARMSTRONG_NUMBERS_H

#include <stdbool.h>

bool is_armstrong_number(int candidate);

#endif

armstrong_numbers.c:

#include "armstrong_numbers.h"

The function is_armstrong_number() takes an int and returns a bool, either true if the parameter is an Armstrong number, or false otherwise.

As you can see, the function is already declared in the .h file.
You now have to define the function in the armstrong_numbers.c.

1 Like

Thank you very much.

As for the tests:

The C track here at Exercism uses the testing framework Unity.

This is a part of the tests:

// ...

static void test_zero_is_an_armstrong_number(void)
{
   TEST_ASSERT_TRUE(is_armstrong_number(0));
}

static void test_single_digit_numbers_are_armstrong_numbers(void)
{
   TEST_IGNORE();   // delete this line to run test
   TEST_ASSERT_TRUE(is_armstrong_number(5));
}

// ...

You can see the tests in the web interface in the tab “Tests”, or if you use the CLI in the file test_armstrong_numbers.c.
(If you use the CLI to download the exercise and solve it on your computer you might want to read the file HELP.md for instructions how to compile and execute the test suite, and how to enable the tests, one by one.)