diff --git a/projet k3/fact.c b/projet k3/fact.c new file mode 100644 index 0000000000000000000000000000000000000000..3a3f711047f9006b445522b8e5bd1a2c3192c82c --- /dev/null +++ b/projet k3/fact.c @@ -0,0 +1,159 @@ +#include "third.h" + +#include <CUnit/CUnit.h> +#include <CUnit/Basic.h> +#include <CUnit/TestRun.h> + +#include <stdlib.h> +#include <stdio.h> +#include <math.h> +#include <string.h> + +int is_prime(long value){ + long len = (value / 2) + 1; + for(long x = 2; x < len; x++){ + if(value % x == 0){ + return 0; + } + } return 1; +} + +long countLines(char *filename){ + int fd = open(filename, O_RDONLY); + if(fd == -1) exit(EXIT_FAILURE); + + struct stat finfo; + fstat(fd, &finfo); + char *map = mmap(NULL, finfo.st_size, PROT_READ, MAP_SHARED, fd, 0); + if(map == MAP_FAILED) exit(EXIT_FAILURE); + + size_t len = finfo.st_size / sizeof(char); + long counter = 0; + for(size_t i = 0; i < len; i++){ + if(map[i] == '\n'){ + counter++; + i++; + } + } + + if(munmap(map,sizeof(finfo.st_size)) == -1) exit(EXIT_FAILURE); + if(close(fd) == -1) exit(EXIT_FAILURE); + return counter; +} + +void test_assert_long(){ // test nbe lignes + + normalMode(4, "test_long.txt", "long.txt"); + CU_ASSERT_EQUAL(countLines("long.txt"), 199); +} + +void test_assert_neg(){ // test que wrong format + normalMode(4, "test_neg.txt", "neg.txt"); + CU_ASSERT_TRUE(countLines("neg.txt") < 5); + //CU_get_error_msg();//?? +} + +void test_assert_input(){ + normalMode(4, "test_input.txt", "normal.txt"); //teste les resultats d'une entree normale + + int fd = open("normal.txt", O_RDONLY); + if(fd == -1) exit(EXIT_FAILURE); + + struct stat finfo; + fstat(fd, &finfo); + char *map = mmap(NULL, finfo.st_size, PROT_READ, MAP_SHARED, fd, 0); + if(map == MAP_FAILED) exit(EXIT_FAILURE); + + size_t len = finfo.st_size / sizeof(char); + int got_first = 0; + long to_factor = 0, current_factor = 0; + for(size_t i = 0; i < len; i++){ + char c = map[i]; + if(c != '\n'){ + if(c == ' '){ + if(got_first){ + CU_ASSERT_TRUE(to_factor % current_factor == 0); + CU_ASSERT_TRUE(is_prime(current_factor)); + while(to_factor % current_factor == 0){ + to_factor /= current_factor; + } + current_factor = 0; + } + else{ + got_first = 1; + } + } + else{ + int tmp = c - '0'; + if(got_first){ + current_factor = (current_factor * 10) + tmp; + } + else{ + to_factor = (to_factor * 10) + tmp; + } + } + } + else{ + if(got_first){ + while(to_factor % current_factor == 0){ + to_factor /= current_factor; + } + CU_ASSERT_TRUE(to_factor == 1); + } + else{ + CU_ASSERT_TRUE(is_prime(to_factor)); + } + got_first = 0; + to_factor = 0; current_factor = 0; + } + } + + if(munmap(map,sizeof(finfo.st_size)) == -1) exit(EXIT_FAILURE); + if(close(fd) == -1) exit(EXIT_FAILURE); +} + +int setup(void) { return 0; } + +int teardown(void) { return 0; } + + +// gcc fact.c -o fact -lpthread -lm -lcunit +// ./fact -N 1 files/example_input.txt end.txt + + +int main(int argc,const char *argv[]){ + if(argv[1]==NULL){ + CU_pSuite pSuite = NULL; + + + + /* initialize the CUnit test registry */ + if (CUE_SUCCESS != CU_initialize_registry()) return CU_get_error(); + + /* add a suite to the registry */ + pSuite = CU_add_suite("Prime_suite", setup, teardown); + if (NULL == pSuite) { + CU_cleanup_registry(); + return CU_get_error(); + } + + /* add the tests to the suite */ + /* NOTE - ORDER IS IMPORTANT - MUST TEST fread() AFTER fprintf() */ + if ((NULL == CU_add_test(pSuite, "input", test_assert_input)) || + (NULL == CU_add_test(pSuite, "long", test_assert_long)) || + (NULL == CU_add_test(pSuite, "neg", test_assert_neg))) + { + CU_cleanup_registry(); + return CU_get_error(); + } + + /* Run all tests using the CUnit Basic interface */ + CU_basic_set_mode(CU_BRM_VERBOSE); + CU_basic_run_tests(); + CU_cleanup_registry(); + + return CU_get_error(); + } + else{normalMode(atoi(argv[2]), argv[3], argv[4]); } + return 0; +} \ No newline at end of file