diff --git a/src/ggT/ggT.c b/src/ggT/ggT.c new file mode 100644 index 0000000..ef9b8ef --- /dev/null +++ b/src/ggT/ggT.c @@ -0,0 +1,16 @@ +#include + + +int ggT(int a, int b){ + // handles the zero cases + if(a == 0) return b; + if(b == 0) return a; + + // Euclidean algorithm with modulo for getting the ggT + while (b != 0) { + int temp = b; + b = a % b; + a = temp; + } + return a; +} \ No newline at end of file diff --git a/src/ggT/ggT.h b/src/ggT/ggT.h new file mode 100644 index 0000000..4dc2850 --- /dev/null +++ b/src/ggT/ggT.h @@ -0,0 +1,6 @@ +#ifndef GGT_H +#define GGT_H + +int ggT(int a, int b); + +#endif diff --git a/test/ggT/test_ggT.c b/test/ggT/test_ggT.c new file mode 100644 index 0000000..6c14258 --- /dev/null +++ b/test/ggT/test_ggT.c @@ -0,0 +1,70 @@ +#include "unity.h" +#include "ggT.h" + +void setUp(){} +void tearDown(){} + +void test_ggTOf5And10(){ + //arrange + int a = 5; + int b = 10; + int expected = 5; + + //act + int result = ggT(a,b); + + //assert + TEST_ASSERT_EQUAL_INT(expected, result); +} + +void test_ggTOfFirstNumberZero(){ + //arrange + int a = 0; + int b = 5; + int expected = 5; + + //act + int result = ggT(a,b); + + //assert + TEST_ASSERT_EQUAL_INT(expected, result); +} + +void test_ggTOfSecondNumberZero(){ + //arrange + int a = 10; + int b = 0; + int expected = 10; + + //act + int result = ggT(a,b); + + //assert + TEST_ASSERT_EQUAL_INT(expected, result); +} + +void test_ggTOfBothNumbersZero(){ + //arrange + int a = 0; + int b = 0; + int expected = 0; + + //act + int result = ggT(a,b); + + //assert + TEST_ASSERT_EQUAL_INT(expected,result); +} + +void test_ggTOfCoprimes(){ + //arrange + int a = 13; + int b = 27; + int expected = 1; + + //act + int result = ggT(a,b); + + //assert + TEST_ASSERT_EQUAL_INT(expected,result); +} \ No newline at end of file