From acb9752cc24c2a2d380897723641e44699d58cde Mon Sep 17 00:00:00 2001
From: Giovanna Stefanelli <giovanna.stefanelli@student.uclouvain.be>
Date: Thu, 23 Apr 2020 21:27:00 +0200
Subject: [PATCH] Add new file

---
 Fact_UnitTest.c | 151 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 151 insertions(+)
 create mode 100644 Fact_UnitTest.c

diff --git a/Fact_UnitTest.c b/Fact_UnitTest.c
new file mode 100644
index 0000000..60e262f
--- /dev/null
+++ b/Fact_UnitTest.c
@@ -0,0 +1,151 @@
+//
+// Created by Gio on 22/04/2020.
+//
+/*
+ * UNIT TEST WITH CUNIT
+ * Basic Functions:
+ * 1. is_div
+ * 2. is_prime
+ */
+#include <sys/types.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <math.h>
+#include <stdbool.h>
+
+//#ifndef _CUNIT_BASIC_H
+//#define _CUNIT_BASIC_H
+
+#include "CUnit/CUnit.h"
+#include "CUnit/Basic.h"
+
+
+/*
+ * FUNCTIONS UNDER UNIT TEST
+ */
+
+bool is_div(unsigned long numbr, unsigned long i){
+    if((numbr % i) == 0) {  // verify if i is the divider of numbr
+        return(true);       // i is a divider of numbr
+    } else {
+        return(false);      // i is not a divider of numbr
+    }
+}
+
+bool is_prime(unsigned long nbr) {
+    for (unsigned long j = 2; j <= sqrt(nbr); j++) {
+        if ((nbr % j) == 0) {       // verify if i is the divider of nbr
+            return (false);         // i is a divider of nbr
+        }
+    }
+    return (true); // i is a prime number
+}
+
+/*
+ * CUNIT TEST PROGRAMME
+ */
+#define IMPAIR 666343 // Prime dividers: 89 7487
+#define PAIR1 2548
+#define PAIR2 26
+#define ZERO 0
+#define NEGATIVE -56056
+#define PRIME1 529973
+#define PRIME2 89
+
+void test_is_div(void) {
+
+    CU_ASSERT_FALSE(is_div(IMPAIR, PAIR1) == false );   //False
+    CU_ASSERT_FALSE(is_div(ZERO, PAIR2) == false);      //False
+    CU_ASSERT_FALSE(is_div(PRIME1, IMPAIR) == false);   //False
+    CU_ASSERT_FALSE(is_div(NEGATIVE, PAIR1) == false);  //False**
+    CU_ASSERT_FALSE(is_div(PRIME1, PAIR2) == false);    //False
+    CU_ASSERT_TRUE(is_div(PAIR1, PAIR2) == true);       //True
+    CU_ASSERT_TRUE(is_div(IMPAIR, PRIME2) == true);     //True
+}
+
+void test_is_prime(void) {
+
+    CU_ASSERT_FALSE(is_prime(IMPAIR) == false);       //False
+    CU_ASSERT_FALSE(is_prime(ZERO) == false);         //False
+    CU_ASSERT_TRUE(is_prime(PRIME1) == true);               //True
+    CU_ASSERT_FALSE(is_prime(NEGATIVE) == false);     //False
+    CU_ASSERT_FALSE(is_prime(PAIR1) == false);              //False
+}
+
+/*
+ * MAIN CUNIT SUITE
+ */
+/* Pointer to the file used by the tests. */
+
+static FILE* temp_file = NULL;
+
+/*
+ * The suite initialization function.
+ * Opens a temporary file used by the tests.
+ */
+int init_suite(void) {
+    temp_file = fopen("test_file.txt", "w+");
+    if (temp_file == NULL) {
+        return -1;
+    } else {
+        return 0;
+    }
+}
+
+/*
+ * The suite cleanup function.
+ * Closes the temporary file used by the tests.
+ */
+int clean_suite(void) {
+    if (fclose(temp_file) != 0) {
+        return -1;
+    } else {
+        temp_file = NULL;
+        return 0;
+    }
+}
+
+int main() {
+    CU_pSuite pSuite = NULL;
+    //CU_pSuite pSuite2 = NULL;
+    /* Initialise CUnit test registry */
+    if (CU_initialize_registry() != CUE_SUCCESS) {
+        return (CU_get_error());
+    }
+
+/*
+ * Add suite1 to registry
+ */
+    pSuite = CU_add_suite("Basic_Test_Suite1", init_suite, clean_suite);
+    if (pSuite == NULL) {
+        CU_cleanup_registry();
+        return (CU_get_error());
+    }
+/*
+ * add test1 "is_div" to suite1
+ */
+    if ((CU_add_test(pSuite, "\n\n……… Testing is_div function……..\n\n", test_is_div)) == NULL)
+    {
+        CU_cleanup_registry();
+        return (CU_get_error());
+    }
+/*
+ * add test1 "is_prime" to suite1
+ */
+    if ((CU_add_test(pSuite, "\n\n……… Testing is_prime function……..\n\n", test_is_prime)) == NULL)
+    {
+        CU_cleanup_registry();
+        return (CU_get_error());
+    }
+/*
+ * Run all tests using the CUnit Basic interface
+ * and provide output to the screen
+ * Finally clean the registry
+ */
+    CU_basic_set_mode(CU_BRM_VERBOSE);
+    CU_basic_run_tests();
+    CU_cleanup_registry();
+
+    return (CU_get_error());
+}
\ No newline at end of file
-- 
GitLab