Commit f355a1a4 authored by Jules Sang's avatar Jules Sang
Browse files

first push

parents
*~
build/
cmake_minimum_required(VERSION 2.6)
if(COMMAND cmake_policy)
cmake_policy(SET CMP0003 NEW)
endif(COMMAND cmake_policy)
project(EnsimagRappelDeC)
enable_testing()
set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -Wall -Wextra -Wconversion -std=gnu11 -fsanitize-address-use-after-scope")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wall -Wextra -Wconversion -std=gnu++11 -fsanitize-address-use-after-scope")
#########
# Gestion des logins
#########
# Vous devez editer la ligne suivante pour y mettre votre login. C'est
# inutile pour le rappel de C, mais vous ferez la même chose pour
# l'examen
#########
set(LOGINS sangj)
if (LOGINS MATCHES "login1")
message(FATAL_ERROR "** ERREUR / ERROR **: Vous devez modifier CMakeLists.txt pour remplacer login1 par votre login / Substitute login1 with your own login in CMakeLists.txt")
endif()
##
# Si vous utilisez d'autres fichiers .c, en plus de ceux listés,
# il faut les ajouter ici
##
add_executable(listechainee src/listechainee.c)
add_library(ensigc SHARED src/elempool.c src/bitset1000.cpp)
add_executable(ensitestgc tests/gctests.c tests/test0.c tests/test1.c tests/test2.c tests/test3.c tests/test4.c)
target_link_libraries(ensitestgc ensigc)
add_executable(binaires src/binaires.c)
add_executable(hello src/hello.c)
add_executable(flottants src/flottants.c)
add_executable(fqsort src/fqsort.c)
target_link_libraries(fqsort m)
###
# Ensimag CentOS 6: utilisation du compilateur installé pour Ada,
# beaucoup plus récent.
##
if(EXISTS /opt/gnat/bin/g++)
set(CMAKE_CXX_COMPILER /opt/gnat/bin/g++)
endif(EXISTS /opt/gnat/bin/g++)
if(EXISTS /opt/gnat/bin/gcc)
set(CMAKE_C_COMPILER /opt/gnat/bin/gcc)
endif(EXISTS /opt/gnat/bin/gcc)
##
# Programme de test
##
add_test(ListeChainee ${CMAKE_BINARY_DIR}/listechainee)
add_test(valgrindListeChainee valgrind ${CMAKE_BINARY_DIR}/listechainee)
add_test(GC ${CMAKE_BINARY_DIR}/ensitestgc --all)
add_test(ValgrindGC valgrind ${CMAKE_BINARY_DIR}/ensitestgc --all)
add_test(Binaire ${CMAKE_BINARY_DIR}/../tests/binairestests.rb)
add_test(Hello ${CMAKE_BINARY_DIR}/../tests/hellotests.rb)
add_test(Flottants ${CMAKE_BINARY_DIR}/../tests/flottantstests.rb)
add_test(TriComplexe ${CMAKE_BINARY_DIR}/fqsort)
add_test(valgrindTriComplexe valgrind ${CMAKE_BINARY_DIR}/fqsort)
# version verbose de tous les tests
add_custom_target(check ctest -V)
This diff is collapsed.
# ensimag-rappeldec
Quelques exercices de rappel sur C
## Pour compiler et lancer les tests
cd build
cmake ..
make
make test
make check
#include <stdlib.h>
#include <stdio.h>
static unsigned long long X = 123456ULL;
unsigned char crand48(void) {
/* Insérez votre code ici. */
unsigned long long a = 0x5DEECE66D;
unsigned char c = 0xB;
unsigned long long m = 2;
m = m << 48;
X = (a * X + c)%m;
unsigned long long mask = 0xFF00000000;
unsigned char res = (unsigned char) ((mask & X) >> 32);
return res;
}
int main(void)
{
printf("%hhu\n", crand48());
printf("%hhu\n", crand48());
printf("%hhu\n", crand48());
return EXIT_SUCCESS;
}
/**
Copyright (C) 2015-2016 by Gregory Mounie
This file is part of RappelDeC
RappelDeC is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
RappelDeC is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <bitset>
#include "bitset1000.h"
#include <cassert>
using namespace std;
static bitset<1000> bitfield {};
void bt1k_reset() {
bitfield.reset();;
}
void bt1k_set(long unsigned int n, bool val) {
assert(n < 1000);
bitfield[n] = val;
}
bool bt1k_get(long unsigned int n) {
assert(n < 1000);
return bitfield[n];
}
/**
Copyright (C) 2015-2016 by Gregory Mounie
This file is part of RappelDeC
RappelDeC is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
RappelDeC is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef BITSET1000_H
#define BITSET1000_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdbool.h>
bool bt1k_get(long unsigned int n);
void bt1k_set(long unsigned int n, bool val);
void bt1k_reset();
#ifdef __cplusplus
}
#endif
#endif
/**
Copyright (C) 2015-2016 by Gregory Mounie
This file is part of RappelDeC
RappelDeC is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
RappelDeC is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <string.h>
#include "elempool.h"
#include "bitset1000.h"
static unsigned char *memoire_elem_pool = 0;
/**
Fonction allouant un element
*/
struct elem *alloc_elem(void) {
/* ajouter votre code ici / add your code here */
return 0;
}
/**
Fonction ramasse-miette sur les elements qui ne sont pas
atteignables depuis les têtes de listes
*/
void gc_elems(struct elem **heads, int nbheads) {
/* ajouter votre code ici / add your code here */
}
void init_elems() {
bt1k_reset();
if (memoire_elem_pool == NULL) {
memoire_elem_pool = malloc(1000 * sizeof(struct elem));
}
memset(memoire_elem_pool, 0, 1000 * sizeof(struct elem));
}
/**
Copyright (C) 2015-2016 by Gregory Mounie
This file is part of RappelDeC
RappelDeC is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
RappelDeC is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef ELEM_H
#define ELEM_H
struct elem {
int val;
struct elem *next;
};
extern struct elem *alloc_elem(void);
extern void gc_elems(struct elem **heads, int nbheads);
extern void init_elems(void);
#endif
#include <stdlib.h>
int main(void)
{
return EXIT_SUCCESS;
}
#include <stdlib.h>
#include <complex.h>
#include <assert.h>
/**
Ajouter la fonction de comparaison du tri ici
*/
const int TAILLE = 100;
int main(void)
{
/* initialisation du tableau */
double complex tableau[TAILLE];
srand48(123456);
for(int i = 0; i < TAILLE; i++) {
tableau[i] = drand48() + (drand48() * 1.0i);
}
/**
Mettre le code d'appel de qsort ici
*/
/**
Vérification du tri
*/
for(int i = 0; i < TAILLE - 1; i++) {
assert(carg(tableau[i]) <= carg(tableau[i+1]));
}
return EXIT_SUCCESS;
}
#include <stdlib.h>
int main(void)
{
return EXIT_SUCCESS;
}
/**
Copyright (C) 2016 by Gregory Mounie
This file is part of RappelDeC
RappelDeC is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
RappelDeC is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
struct elem {
long unsigned int val;
struct elem *next;
};
/* Affiche les éléments de la liste passée en paramètre sur la sortie
* standard. */
void affichage_liste(struct elem *liste) {
struct elem* current_elem = liste;
printf("[");
while (current_elem != NULL) {
printf("%ld, ", current_elem->val);
current_elem = current_elem->next;
}
printf("]\n");
}
/* Crée une liste simplement chainée à partir des nb_elems éléments du
* tableau valeurs. */
struct elem *creation_liste(long unsigned int *valeurs, size_t nb_elems) {
struct elem* tete = NULL;
if(nb_elems > 0){
tete = malloc(sizeof(struct elem));
struct elem *cellule_cour = tete;
struct elem *cellule_pred = NULL;
for (size_t i=0; i<nb_elems; i++) {
struct elem *cellule_suiv = malloc(sizeof(struct elem));
cellule_cour->val = valeurs[i];
cellule_cour->next = cellule_suiv;
cellule_pred = cellule_cour;
cellule_cour = cellule_suiv;
}
free(cellule_pred->next);
cellule_pred->next = NULL;
}
return tete;
}
/* Libère toute la mémoire associée à la liste passée en paramètre. */
void destruction_liste(struct elem *liste) {
while(liste != NULL) {
struct elem* next = liste->next;
free(liste);
liste = next;
}
}
/* Inverse la liste simplement chainée passée en paramètre. Le
* paramètre liste contient l'adresse du pointeur sur la tête de liste
* à inverser. */
void inversion_liste(struct elem **liste) {
struct elem* current_cell = *liste;
struct elem* new_liste = NULL;
struct elem* begin_liste = NULL;
while(current_cell != NULL) {
begin_liste = malloc(sizeof(struct elem));
begin_liste->val = current_cell->val;
begin_liste->next = new_liste;
new_liste = begin_liste;
current_cell = current_cell->next;
}
destruction_liste(*liste);
*liste = new_liste;
}
const long unsigned int TAILLE = 100;
int main(void)
{
/* Test d'affichage d'une liste créée à la main. */
struct elem e1, e2, e3;
e1.val = 0;
e2.val = 1;
e3.val = 2;
e1.next = &e2;
e2.next = &e3;
e3.next = NULL;
affichage_liste(&e1);
long unsigned int valeurs[TAILLE];
for (size_t i = 0; i < TAILLE; i++) {
valeurs[i] = i;
}
struct elem *tab_elem = creation_liste(valeurs, TAILLE);
assert(tab_elem != NULL);
/* Test que la liste dans l'ordre */
struct elem *tmp;
long unsigned int idx;
for (tmp = tab_elem, idx = 0;
tmp != NULL;
idx++, tmp = tmp->next) {
printf("%ld, %ld\n", tmp->val, idx);
assert(tmp->val == idx);
}
/* Test que la liste est complète (à la suite du test sur l'ordre) */
assert(idx == TAILLE);
printf("Liste créée:\n");
affichage_liste(tab_elem);
inversion_liste(&tab_elem);
assert(tab_elem != NULL);
printf("Liste inversée:\n");
affichage_liste(tab_elem);
/* Vérifie que la liste est inversée et complète */
for (tmp = tab_elem, idx = TAILLE - 1; tmp != NULL; --idx, tmp = tmp->next) {
assert(tmp->val == idx);
}
assert(idx == (long unsigned int)(0UL - 1));
/* Pour tester le bon fonctionnement de la fonction suivante, on
* lancera valgrind sur le programme pour vérifier qu'aucune fuite
* mémoire n'est présente. */
destruction_liste(tab_elem);
return EXIT_SUCCESS;
}
#!/usr/bin/ruby
# -*- coding: utf-8 -*-
# Copyright (C) 2016 Gregory Mounie
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
require "expect"
require "pty"
def test_binaires
@pty_read, @pty_write, @pty_pid = PTY.spawn("../build/binaires")
a = @pty_read.expect(/49/m, 1) # attendre 1 seconde max
if a.nil?
puts "Valeur 1 attendue: '49'"
end
a = @pty_read.expect(/10/m, 1) # attendre 1 seconde max
if a.nil?
puts "Valeur 2 attendue: '10'"
end
a = @pty_read.expect(/197/m, 1) # attendre 1 seconde max
if a.nil?
puts "Valeur 3 attendue: '197'"
end
end
test_binaires
#!/usr/bin/ruby
# -*- coding: utf-8 -*-
# Copyright (C) 2016 Gregory Mounie
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
require "expect"
require "pty"
def test_flottant
@pty_read, @pty_write, @pty_pid = PTY.spawn("../build/flottants")
a = @pty_read.expect(/0.000000e\+00/m, 1) # attendre 1 seconde pour le hello world utf8
if a.nil?
puts "Valeur 1 attendue (flottants normaux): '0.000000e+00'"
end
a = @pty_read.expect(/5.551115e-17/m, 1) # attendre 1 seconde pour le hello world utf8
if a.nil?
puts "Valeur 2 attendue (flottants grands): '5.551115e-17'"
end
a = @pty_read.expect(/0.000000e\+00/m, 1) # attendre 1 seconde pour le hello world utf8
if a.nil?
puts "Valeur 3 attendue (flottants très grands): '0.000000e+00'"
end
end
test_flottant
/**
Copyright (C) 2015-2016 by Gregory Mounie
This file is part of RappelDeC
RappelDeC is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
RappelDeC is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <assert.h>
#include <stdbool.h>
#include <getopt.h>
#include "utest.h"
static struct option long_options[] = {
{"all", no_argument, 0, 0 },
{"t0", no_argument, 0, 1 },
{"t1", no_argument, 0, 2 },
{"t2", no_argument, 0, 3 },
{"t3", no_argument, 0, 4 },
{"t4", no_argument, 0, 5 },
{0, 0, 0, 0 }
};
extern void test0(void);
extern void test1(void);
extern void test2(void);
extern void test3(void);
extern void test4(void);
static void (*tests[])(void) = {
test0,
test1,
test2,
test3,
test4,
};
void usage(char *aname) {
fprintf(stderr, "usage: %s {--all | --t{0-4}}\n", aname);
}
/* error count variable instanciation */
int u_errnb = 0;
int main(int argc, char **argv) {
bool noargs = true;
while(1) {
int option_index = 0;
int nopt = getopt_long(argc, argv, "",