Commit 8a55b41d authored by Gerald Oster's avatar Gerald Oster
Browse files

[feature] unit tests

parent 0bc49050
add_executable (test_astrological
test_astrological.c
../src/astrological.c
)
add_executable (test_stringtools
test_stringtools.c
../src/stringtools.c
)
add_executable (test_occurrences
test_occurrences.c
../src/occurrences.c
)
# Quelques informations utiles
Voici quelques informations utiles sur les tests qui vous sont fournis.
Tous les scripts de tests sont à exécuter depuis le répertoire racine de votre projet. Ils suppose que votre fichier compilé se trouve également à la racine (et non pas dans le repertoire de `src/`)
## Hello World
Le test fourni s'assure que l'affichage réalisé par la commande `helloworld` est bien celui attendu.
Pour réaliser le test, il suffit d'exécuter le script `test_helloworld.sh`
```bash
$ test/test_helloworld.sh
```
## Maximum de 3 valeurs
Trois tests sont fournis pour les trois exécutables attendus dans l'exercice.
```bash
$ test/test_max3.sh
```
La commande précédente ne teste rien mis à part l'existance de votre exécutable `max3`.
```bash
$ test/test_max3_stdin.sh
```
La commande précédente teste votre programme (`max3_stdin`) en lui fournissant les données sur l'entrée standard.
```bash
$ test/test_max3_args.sh
```
La commande précédente teste votre programme (`max3_args`) en lui fournissant les données comme arguments de votre commande.
## Signe astrologique
```bash
$ test/test_zodiac.sh
```
La commande précédente teste votre programme en lui fournissant les données comme arguments de votre commande (nommée `zodiac`).
## Jour de naissance
Deux tests sont fournis pour les deux exécutables attendus dans l'exercice.
Les valeurs seront lues comme arguments de votre commande (nommée `dayofbirth`).
```bash
$ test/test_dayofbirth.sh
```
Les valeurs seront lues depuis l'entrée standard (commande nommée `dayofbirth_stdin`).
```bash
$ test/test_dayofbirth_stdin.sh
```
## Programmation modulaire et compilation séparée
Une batterie de tests unitaires vous est fournie pour tester uniquement vos fonctions (`computer_zodiac()`, `validate_date()`, `zeller()`, `day_of_week()`). Afin d'exécuter ces tests unitaires vous devez compiler le programme de test correspondant de la manière suivante :
```bash
$ gcc -Wall src/astrological.c test/test_astrological.c -o test_astrological
$ ./test_astrological
```
Le script `test/test_astrological_app.sh` permet de tester vos programmes finaux (`astrological_main` et `astrological_main_stdin`).
## Transformations de chaînes de caractères
Une batterie de tests unitaires vous est fournie pour tester uniquement vos fonctions (`min2maj()`, `maj2min()`). Afin d'exécuter ces tests unitaires vous devez compiler le programme de test correspondant de la manière suivante :
```bash
$ gcc -Wall src/stringtools.c test/test_stringtools.c -o test_stringtools
$ ./test_stringtools
```
Le script `test/test_stringtools_app.sh` permet de tester votre programme final (`stringtools_main`).
## Occurrences d'un caractère dans un mot
Une batterie de tests unitaires vous est fournie pour tester uniquement vos fonctions (`first_occurrence()`, `last_occurrence()`). Afin d'exécuter ces tests unitaires vous devez compiler le programme de test correspondant de la manière suivante :
```bash
$ gcc -Wall src/occurrences.c test/test_occurrences.c -o test_occurrences
$ ./test_occurrences
```
Le script `test/test_occurrences_app.sh` permet de tester vos programmes finaux (`occurrences_main` et `occurrences_main_loop`).
#!/bin/bash
for file in $*
do
if [ ! -f "${file}" ]
then
echo "⚠️ File ${file} does not exist."
exit 1
fi
done
This diff is collapsed.
#define SNOW_ENABLED
#include "snow.h"
#include "../src/astrological.h"
#define assert_false(expr) assert(! expr)
describe(test_compute_zodiac) {
it("Conformance tests") {
asserteq_str(compute_zodiac(10,1), "Capricorne");
asserteq_str(compute_zodiac(10,2), "Verseau");
asserteq_str(compute_zodiac(10,3), "Poisson");
asserteq_str(compute_zodiac(10,4), "Bélier");
asserteq_str(compute_zodiac(10,5), "Taureau");
asserteq_str(compute_zodiac(10,6), "Gémeaux");
asserteq_str(compute_zodiac(10,7), "Cancer");
asserteq_str(compute_zodiac(10,8), "Lion");
asserteq_str(compute_zodiac(10,9), "Vierge");
asserteq_str(compute_zodiac(10,10), "Balance");
asserteq_str(compute_zodiac(10,11), "Scorpion");
asserteq_str(compute_zodiac(10,12), "Sagitaire");
}
it("Specific tests") {
asserteq_str(compute_zodiac(19,3), "Poisson");
asserteq_str(compute_zodiac(12,5), "Taureau");
asserteq_str(compute_zodiac(29,2), "Poisson");
asserteq_str(compute_zodiac(1,1), "Capricorne");
asserteq_str(compute_zodiac(20,8), "Lion");
asserteq_str(compute_zodiac(31,12), "Capricorne");
asserteq_str(compute_zodiac(21,7), "Cancer");
}
}
describe(test_validate_date) {
it("Conformance tests") {
assert(validate_date(1,1,2000));
assert(validate_date(31,1,2000));
assert_false(validate_date(32,1,2000));
assert(validate_date(28,2,2000));
assert(validate_date(29,2,2000));
assert_false(validate_date(30,2,2000));
assert(validate_date(31,3,2000));
assert(validate_date(30,4,2000));
assert_false(validate_date(31,4,2000));
assert(validate_date(31,5,2000));
assert(validate_date(30,6,2000));
assert_false(validate_date(31,6,2000));
assert(validate_date(31,7,2000));
assert(validate_date(31,8,2000));
assert(validate_date(30,9,2000));
assert_false(validate_date(31,9,2000));
assert(validate_date(31,10,2000));
assert(validate_date(30,11,2000));
assert_false(validate_date(31,11,2000));
assert(validate_date(31,12,2000));
assert_false(validate_date(4,13,2000));
assert_false(validate_date(-1,2,2000));
assert_false(validate_date(1,-2,2000));
assert_false(validate_date(1,1,-500));
assert_false(validate_date(1,1,0));
}
}
describe(test_zeller) {
it("Conformance tests") {
asserteq(zeller(19,3,1978), 0);
asserteq(zeller(20,3,1978), 1);
asserteq(zeller(21,3,1978), 2);
asserteq(zeller(22,3,1978), 3);
asserteq(zeller(23,3,1978), 4);
asserteq(zeller(24,3,1978), 5);
asserteq(zeller(25,3,1978), 6);
asserteq(zeller(26,3,1978), 0);
}
}
describe(test_day_of_week) {
it("Conformance tests") {
asserteq_str(day_of_week(19,3,1978), "Dimanche");
asserteq_str(day_of_week(20,3,1978), "Lundi");
asserteq_str(day_of_week(21,3,1978), "Mardi");
asserteq_str(day_of_week(22,3,1978), "Mercredi");
asserteq_str(day_of_week(23,3,1978), "Jeudi");
asserteq_str(day_of_week(24,3,1978), "Vendredi");
asserteq_str(day_of_week(25,3,1978), "Samedi");
asserteq_str(day_of_week(26,3,1978), "Dimanche");
}
it("Specific tests") {
asserteq_str(day_of_week(12,5,1956), "Samedi");
asserteq_str(day_of_week(29,2,2020), "Samedi");
asserteq_str(day_of_week(1,1,2018), "Lundi");
asserteq_str(day_of_week(20,8,1890), "Mercredi");
}
}
snow_main();
#!/bin/bash
REQUIRED_FILES="./astrological_main ./astrological_main_stdin"
for REQUIRE_FILE in ${REQUIRED_FILES}
do
if [ ! -f ${REQUIRE_FILE} ]
then
echo "⚠️ Executable ${REQUIRED_FILE} does not exist."
exit 1
fi
done
function test_astrological_main () {
if [ ! "$(./astrological_main "$1" "$2")" = "$1 est né(e) un $3, est du signe du $4" ];
then
echo "🔴 Test of astrological_main failed with arguments '$1 $2'"
exit 1
else
echo "✅ Test succeeded (astrological_main '$1 $2')."
fi
}
function test_astrological_stdin () {
content="$1\n$2"
if [ ! "$(echo -e $content | ./astrological_main_stdin)" = "$1 est né(e) un $3, est du signe du $4" ];
then
echo "🔴 Test of astrological_main_stdin failed with arguments '\"$1\" $2'"
exit 1
else
echo "✅ Test succeeded (astrological_main_stdin \"$1\" $2)."
fi
}
test_astrological_main "Gérald O." 19/03/1978 Dimanche Poisson
test_astrological_main "Homer Simpson" 12/05/1956 Samedi Taureau
test_astrological_main "John Snow" 29/2/2020 Samedi Poisson
test_astrological_main "Jane Doe" 1/1/2018 Lundi Capricorne
test_astrological_main "H.P. Lovecraft" 20/8/1890 Mercredi Lion
test_astrological_stdin "Gérald O." 19/03/1978 Dimanche Poisson
test_astrological_stdin "Homer Simpson" 12/05/1956 Samedi Taureau
test_astrological_stdin "John Snow" 29/2/2020 Samedi Poisson
test_astrological_stdin "Jane Doe" 1/1/2018 Lundi Capricorne
test_astrological_stdin "H.P. Lovecraft" 20/8/1890 Mercredi Lion
#!/bin/bash
REQUIRED_FILE="./dayofbirth"
if [ ! -f ${REQUIRED_FILE} ]
then
echo "⚠️ Executable ${REQUIRED_FILE} not found."
exit 1
fi
function test_dayofbirth () {
if [ ! "$(./dayofbirth "$1" "$2")" = "$1 est né(e) un $3" ];
then
echo "🔴 Test of dayofbirth failed with arguments '\"$1\" $2'"
exit 1
else
echo "✅ Test succeeded (dayofbirth \"$1\" $2)."
fi
}
function test_dayofbirth_should_fail () {
./dayofbirth "$1" $2> /dev/null
if [ $? -eq 0 ];
then
echo "🔴 Test of dayofbirth failed with arguments '$1 $2'"
exit 1
else
echo "✅ Test succeeded (dayofbirth \"$1\" $2)."
fi
}
test_dayofbirth "Gérald O." 19/03/1978 Dimanche
test_dayofbirth "Homer Simpson" 12/05/1956 Samedi
test_dayofbirth "John Snow" 29/2/2020 Samedi
test_dayofbirth "Jane Doe" 1/1/2018 Lundi
test_dayofbirth "H.P. Lovecraft" 20/8/1890 Mercredi
test_dayofbirth_should_fail "Riri" 32/1/1900
test_dayofbirth_should_fail "Fifi" 30/2/2018
test_dayofbirth_should_fail "Loulou" 4/13/1976
test_dayofbirth_should_fail "Thanatos" -1/2/1980
#!/bin/bash
REQUIRED_FILE="./dayofbirth_stdin"
if [ ! -f ${REQUIRED_FILE} ]
then
echo "⚠️ Executable ${REQUIRED_FILE} not found."
exit 1
fi
function test_dayofbirth_stdin () {
content="$1\n$2"
if [ ! "$(echo -e $content | ./dayofbirth_stdin)" = "$1 est né(e) un $3" ];
then
echo "🔴 Test of dayofbirth_stdin failed with arguments '\"$1\" $2'"
exit 1
else
echo "✅ Test succeeded (dayofbirth_stdin \"$1\" $2)."
fi
}
function test_dayofbirth_stdin_should_fail () {
content="$1\n$2"
echo -e $content | ./dayofbirth_stdin > /dev/null
if [ $? -eq 0 ];
then
echo "🔴 Test of dayofbirth_stdin failed with arguments '$1 $2'"
exit 1
else
echo "✅ Test succeeded (dayofbirth_stdin \"$1\" $2)."
fi
}
test_dayofbirth_stdin "Gérald O." 19/03/1978 Dimanche
test_dayofbirth_stdin "Homer Simpson" 12/05/1956 Samedi
test_dayofbirth_stdin "John Snow" 29/2/2020 Samedi
test_dayofbirth_stdin "Jane Doe" 1/1/2018 Lundi
test_dayofbirth_stdin "H.P. Lovecraft" 20/8/1890 Mercredi
test_dayofbirth_stdin_should_fail "Riri" 32/1/1900
test_dayofbirth_stdin_should_fail "Fifi" 30/2/2018
test_dayofbirth_stdin_should_fail "Loulou" 4/13/1976
test_dayofbirth_stdin_should_fail "Thanatos" -1/2/1980
#!/bin/bash
REQUIRED_FILE="./helloworld"
if [ ! -f "${REQUIRED_FILE}" ]
then
echo "⚠️ Executable ${REQUIRED_FILE} not found."
exit 1
fi
echo 'Hello, World!' > /tmp/expected_content
./helloworld > /tmp/output_content
diff --brief /tmp/expected_content /tmp/output_content
if [ $? -ne 0 ]
then
echo "🔴 Test failed. Expected value:"
echo "---"
cat /tmp/expected_content
echo "---"
rm -f /tmp/expected_content /tmp/output_content
exit 1
else
echo "✅ Test succeeded."
rm -f /tmp/expected_content /tmp/output_content
exit 0
fi
#!/bin/bash
REQUIRED_FILE="./max3"
if [ ! -f ${REQUIRED_FILE} ]
then
echo "⚠️ Executable ${REQUIRED_FILE} not found."
exit 1
fi
function test_max3 () {
echo "✅ Test succeeded (max3 -- we do not test the output)."
}
test_max3
#!/bin/bash
REQUIRED_FILE="./max3_args"
if [ ! -f ${REQUIRED_FILE} ]
then
echo "⚠️ Executable ${REQUIRED_FILE} not found."
exit 1
fi
function test_max3_args () {
if [ ! $(./max3_args $1 $2 $3) = "$4" ];
then
echo "🔴 Test of max3_args failed with arguments '$1 $2 $3'"
exit 1
else
echo "✅ Test succeeded (max3_args $1 $2 $3)."
fi
}
test_max3_args 0 1 3 "3"
test_max3_args 0 4 1 "4"
test_max3_args 5 0 1 "5"
test_max3_args -7 0 7 "7"
test_max3_args -3 -5 -7 "-3"
#!/bin/bash
REQUIRED_FILE="./max3_stdin"
if [ ! -f ${REQUIRED_FILE} ]
then
echo "⚠️ Executable ${REQUIRED_FILE} not found."
exit 1
fi
function test_max3_stdin () {
content="$1\n$2\n$3"
if [ ! "$(echo -e $content | ./max3_stdin)" = "$4" ];
then
echo "🔴 Test of max3_stdin failed with arguments '\"$1 $2 $3\"'"
exit 1
else
echo "✅ Test succeeded (max3_stdin '\"$1 $2 $3\"')."
fi
}
test_max3_stdin 0 1 3 "3"
test_max3_stdin 0 4 1 "4"
test_max3_stdin 5 0 1 "5"
test_max3_stdin -7 0 7 "7"
test_max3_stdin -3 -5 -7 "-3"
#define SNOW_ENABLED
#include "snow.h"
#include "../src/occurrences.h"
describe(first_occurence) {
it("Conformance tests") {
assert(first_occurrence('a', "abc") == 0);
assert(first_occurrence('b', "abc") == 1);
assert(first_occurrence('c', "abc") == 2);
assert(first_occurrence('a', "a") == 0);
assert(first_occurrence('a', "aa") == 0);
}
it("Conformance tests /2") {
assert(first_occurrence('x', "") == -1);
assert(first_occurrence('x', "abc") == -1);
}
}
describe(last_occurrence) {
it("Conformance tests") {
assert(last_occurrence('a', "abc") == 0);
assert(last_occurrence('b', "abc") == 1);
assert(last_occurrence('c', "abc") == 2);
assert(last_occurrence('a', "a") == 0);
assert(last_occurrence('a', "aa") == 1);
assert(last_occurrence('a', "aba") == 2);
}
it("Conformance tests /2") {
assert(last_occurrence('x', "") == -1);
assert(last_occurrence('x', "abc") == -1);
}
}
snow_main();
#!/bin/bash
REQUIRED_FILES="./occurrences_main ./occurrences_main_loop"
for REQUIRE_FILE in ${REQUIRED_FILES}
do
if [ ! -f ${REQUIRE_FILE} ]
then
echo "⚠️ Executable ${REQUIRED_FILE} does not exist."
exit 1
fi
done
function test_occurrences_main () {
content="$1\n$2"
if [ ! "$(echo -e $content | ./occurrences_main)" = "$3 $4" ];
then
echo "🔴 Test of occurrences_main failed with arguments '$1 $2'"
exit 1
else
echo "✅ Test succeeded (occurrences_main '$1 $2')."
fi
}
test_occurrences_main a abcdefga 0 7
test_occurrences_main b abc 1 1
test_occurrences_main a aa 0 1
test_occurrences_main a a 0 0
test_occurrences_main x abc -1 -1
#define SNOW_ENABLED
#include "snow.h"
#include "../src/stringtools.h"
describe(min2maj) {
it("Conformance tests") {
asserteq(min2maj('a'), 'A');
asserteq(min2maj('b'), 'B');
asserteq(min2maj('c'), 'C');
asserteq(min2maj('d'), 'D');
asserteq(min2maj('e'), 'E');
asserteq(min2maj('f'), 'F');
asserteq(min2maj('g'), 'G');
asserteq(min2maj('h'), 'H');
asserteq(min2maj('i'), 'I');
asserteq(min2maj('j'), 'J');
asserteq(min2maj('k'), 'K');
asserteq(min2maj('l'), 'L');
asserteq(min2maj('m'), 'M');
asserteq(min2maj('n'), 'N');
asserteq(min2maj('o'), 'O');
asserteq(min2maj('p'), 'P');
asserteq(min2maj('q'), 'Q');
asserteq(min2maj('r'), 'R');
asserteq(min2maj('s'), 'S');
asserteq(min2maj('t'), 'T');
asserteq(min2maj('u'), 'U');
asserteq(min2maj('v'), 'V');
asserteq(min2maj('w'), 'W');
asserteq(min2maj('x'), 'X');
asserteq(min2maj('y'), 'Y');
asserteq(min2maj('z'), 'Z');
}
it("Identity tests") {
asserteq(min2maj('A'), 'A');
asserteq(min2maj('B'), 'B');
asserteq(min2maj('T'), 'T');
asserteq(min2maj('Y'), 'Y');
asserteq(min2maj('Z'), 'Z');
}
it("Not a letter tests") {
asserteq(min2maj('0'), '0');
asserteq(min2maj('1'), '1');
asserteq(min2maj('9'), '9');
asserteq(min2maj(';'), ';');
asserteq(min2maj('-'), '-');
asserteq(min2maj('['), '[');
asserteq(min2maj('$'), '$');
asserteq(min2maj('&'), '&');
asserteq(min2maj(' '), ' ');
}
}
describe(maj2min) {
it("Conformance tests") {
asserteq(maj2min('A'), 'a');
asserteq(maj2min('B'), 'b');
asserteq(maj2min('C'), 'c');
asserteq(maj2min('D'), 'd');
asserteq(maj2min('E'), 'e');
asserteq(maj2min('F'), 'f');
asserteq(maj2min('G'), 'g');
asserteq(maj2min('H'), 'h');
asserteq(maj2min('I'), 'i');
asserteq(maj2min('J'), 'j');
asserteq(maj2min('K'), 'k');
asserteq(maj2min('L'), 'l');
asserteq(maj2min('M'), 'm');
asserteq(maj2min('N'), 'n');
asserteq(maj2min('O'), 'o');
asserteq(maj2min('P'), 'p');
asserteq(maj2min('Q'), 'q');
asserteq(maj2min('R'), 'r');
asserteq(maj2min('S'), 's');
asserteq(maj2min('T'), 't');
asserteq(maj2min('U'), 'u');
asserteq(maj2min('V'), 'v');
asserteq(maj2min('W'), 'w');
asserteq(maj2min('X'), 'x');
asserteq(maj2min('Y'), 'y');
asserteq(maj2min('Z'), 'z');
}
it("Identity tests") {
asserteq(maj2min('a'), 'a');
asserteq(maj2min('b'), 'b');
asserteq(maj2min('t'), 't');
asserteq(maj2min('y'), 'y');
asserteq(maj2min('z'), 'z');
}
it("Not a letter tests") {
asserteq(maj2min('0'), '0');
asserteq(maj2min('1'), '1');
asserteq(maj2min('9'), '9');
asserteq(maj2min(';'), ';');
asserteq(maj2min('-'), '-');
asserteq(maj2min('['), '[');
asserteq(maj2min('$'), '$');
asserteq(maj2min('&'), '&');
asserteq(maj2min(' '), ' ');
}
}
snow_main();
#!/bin/bash
REQUIRED_FILES="./stringtools_main"
for REQUIRE_FILE in ${REQUIRED_FILES}
do
if [ ! -f ${REQUIRE_FILE} ]
then
echo "⚠️ Executable ${REQUIRED_FILE} does not exist."
exit 1
fi
done
function test_stringtools_main () {
if [ ! $(./stringtools_main $1) = "$2" ];
then
echo "🔴 Test of stringtools_main failed with arguments '$1'"
exit 1
else
echo "✅ Test succeeded (stringtools_main '$1')."
fi
}
test_stringtools_main abc "ABC"
test_stringtools_main ABC "abc"
test_stringtools_main HelLo "hELlO"
test_stringtools_main "" ""
test_stringtools_main "ABC xyz" "abc XYZ"
test_stringtools_main ";()[]-_" ";()[]-_"
#!/bin/bash
REQUIRED_FILE="./zodiac"
if [ ! -f ${REQUIRED_FILE} ]
then
echo "⚠️ Executable ${REQUIRED_FILE} not found."
exit 1
fi
function test_zodiac () {
if [ ! $(./zodiac $1) = "$2" ];
then
echo "🔴 Test of zodiac failed with arguments '$1'"
exit 1
else
echo "✅ Test succeeded (zodiac $1)."
fi
}
function test_zodiac_should_fail () {
./zodiac $1 > /dev/null
if [ $? -eq 0 ];
then
echo "🔴 Test of zodiac failed with arguments '$1'"
exit 1
else
echo "✅ Test succeeded (zodiac $1)."
fi
}
test_zodiac 1/1/2012 "Capricorne"
test_zodiac 19/3/1978 "Poisson"
test_zodiac 21/7/1976 "Cancer"
test_zodiac 31/12/1999 "Capricorne"
test_zodiac_should_fail 32/1/2000
test_zodiac_should_fail 30/2/2001
test_zodiac_should_fail 4/13/2002
test_zodiac_should_fail -1/2/2003
test_zodiac_should_fail 1/2/-500
test_zodiac_should_fail 1/3/0
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment