Browse Source

Merge branch 'ggT' into 'master'

ggT

See merge request fdai8031/cstools101!1
remotes/origin/userinput
fdai7777 11 months ago
parent
commit
2d4f1600d7
  1. 16
      src/ggT/ggT.c
  2. 6
      src/ggT/ggT.h
  3. 70
      test/ggT/test_ggT.c

16
src/ggT/ggT.c

@ -0,0 +1,16 @@
#include <stdio.h>
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;
}

6
src/ggT/ggT.h

@ -0,0 +1,6 @@
#ifndef GGT_H
#define GGT_H
int ggT(int a, int b);
#endif

70
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);
}
Loading…
Cancel
Save