Source: CodeAbbey problem 1: Sum “A+B”

1
2
3
4
5
6
7
8
#include <stdio.h>
#include <string.h>
#include "../lib/unity/src/unity.h" // throwtheswitch.org


int sum(const int a, const int b) {
    return a + b;
}

Tests

1
2
3
4
5
6
7
8
9
10
11
void test_sum(void) {
    TEST_ASSERT_EQUAL_INT(1, sum(0, 1));
    TEST_ASSERT_EQUAL_INT(0, sum(-1, 1));
    TEST_ASSERT_EQUAL_INT(3, sum(1, 2));
}

int run_tests(void) {
    UNITY_BEGIN();
    RUN_TEST(test_sum);
    return UNITY_END();
}

Interaction

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main(int argc, char *argv[]) {
    run_tests();
    
    if (argc > 1 && !strcmp(argv[1], "-i")) {
        int a ,b;
        printf("Enter two integer values separated by a space. "
               "For example: 3 1\n"
               "> ");

        while (1) {
            scanf("%d %d", &a, &b);
            printf("%d\n> ", sum(a, b));
        }
    }

}
1
2
3
4
5
6
7
8
9
Enter two integer values separated by a space. For example: 3 1
> -1 0
-1
> -1 1
0
> 4 5
9
> -999 1000
1