Skip to content
GitLab
Explorer
Connexion
S'inscrire
Navigation principale
Rechercher ou aller à…
Projet
P
Projet3_first_pull_request
Gestion
Activité
Membres
Labels
Programmation
Tickets
Tableaux des tickets
Jalons
Wiki
Code
Requêtes de fusion
Dépôt
Branches
Validations
Étiquettes
Graphe du dépôt
Comparer les révisions
Extraits de code
Compilation
Pipelines
Jobs
Planifications de pipeline
Artéfacts
Déploiement
Releases
Registre de conteneur
Registre de modèles
Opération
Environnements
Surveillance
Incidents
Analyse
Données d'analyse des chaînes de valeur
Analyse des contributeurs
Données d'analyse CI/CD
Données d'analyse du dépôt
Expériences du modèle
Aide
Aide
Support
Documentation de GitLab
Comparer les forfaits GitLab
Forum de la communauté
Contribuer à GitLab
Donner votre avis
Conditions générales et politique de confidentialité
Raccourcis clavier
?
Extraits de code
Groupes
Projets
Afficher davantage de fils d'Ariane
Mélanie Colasse
Projet3_first_pull_request
Validations
8afe5ef0
Valider
8afe5ef0
rédigé
5 years ago
par
Mélanie Colasse
Parcourir les fichiers
Options
Téléchargements
Correctifs
Plain Diff
Upload New File
parent
fcb32b7b
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
Modifications
1
Masquer les modifications d'espaces
En ligne
Côte à côte
Affichage de
1 fichier modifié
projet k3/fact.c
+159
-0
159 ajouts, 0 suppression
projet k3/fact.c
avec
159 ajouts
et
0 suppression
projet k3/fact.c
0 → 100644
+
159
−
0
Voir le fichier @
8afe5ef0
#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
Ce diff est replié.
Cliquez pour l'agrandir.
Aperçu
0%
Chargement en cours
Veuillez réessayer
ou
joindre un nouveau fichier
.
Annuler
You are about to add
0
people
to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Enregistrer le commentaire
Annuler
Veuillez vous
inscrire
ou vous
se connecter
pour commenter