Projet Informatique Théorique 2016
Instructions et Documentation
test_translater_etat.c
1 /*
2  * Ce fichier fait partie d'un projet de programmation donné en Licence 3
3  * à l'Université de Bordeaux
4  *
5  * Copyright (C) 2015 Adrien Boussicault
6  *
7  * This Library is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This Library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this Library. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 
22 #include "automate.h"
23 #include "outils.h"
24 #include <limits.h>
25 
26 #include "ensemble.h"
27 
28 #include <stdarg.h>
29 
30 int sont_dans_l_ensemble( int n, Ensemble * ens, ... ){
31  va_list pile;
32  int res = 1;
33  int i;
34  va_start(pile,ens);
35  for (i=0;i<n;i++){
36  res &= est_dans_l_ensemble( ens, va_arg(pile,int) );
37  }
38  va_end(pile);
39  return res;
40 }
41 
42 int l_ensemble_est_egal( int n, const Ensemble* ens, ... ){
43  va_list pile;
44  int res;
45  int i;
46 
47  Ensemble * e = creer_ensemble( NULL, NULL, NULL );
48 
49  va_start(pile,ens);
50  for (i=0;i<n;i++){
51  ajouter_element( e, va_arg(pile,int) );
52  }
53 
54  res = comparer_ensemble( ens, e );
55  liberer_ensemble( e );
56 
57  va_end(pile);
58  return res == 0;
59 }
60 
61 void action_test_transitions_automate(
62  int origine, char lettre, int fin, void* data
63 ){
64  int * res = (int*) data;
65  (*res) += 1;
66 }
67 
68 int test_transitions_automate( int nb_transitions, const Automate* aut, ... ){
69  va_list pile;
70  int i;
71  int nb = 0;
72  pour_toute_transition( aut, action_test_transitions_automate, &nb );
73  if( nb_transitions != nb ){ return 0; }
74 
75  va_start(pile,aut);
76  for (i=0;i<nb_transitions;i++){
77  int origine = va_arg(pile,int);
78  char lettre = (char) va_arg(pile,int);
79  int fin = va_arg(pile,int);
80  if(
82  aut, origine, lettre, fin
83  )
84  ){ return 0; }
85  }
86 
87  va_end(pile);
88  return 1;
89 }
90 
91 
92 
93 
94 int test_get_min_max_etat(){
95  int result = 1;
96 
97 
98  {
99  Automate * automate = creer_automate();
100 
101  TEST(
102  1
103  && automate
104  && get_max_etat( automate ) == INT_MIN
105  && get_min_etat( automate ) == INT_MAX
106  , result
107  );
108  liberer_automate( automate );
109  }
110 
111 
112  {
113  Automate * automate = creer_automate();
114 
115  ajouter_transition( automate, 1, 'a', 2 );
116  ajouter_transition( automate, 1, 'b', 3 );
117  ajouter_transition( automate, 4, 'c', 3 );
118  ajouter_transition( automate, -5, 'd', 6 );
119  ajouter_transition( automate, 7, 'e', 8 );
120  ajouter_transition( automate, 4, 'f', 2 );
121  ajouter_etat_initial( automate, 1);
122  ajouter_etat_initial( automate, -5);
123  ajouter_etat_initial( automate, 9);
124  ajouter_etat_final( automate, 3);
125  ajouter_etat_final( automate, 8);
126  ajouter_etat_final( automate, 9);
127 
128  TEST(
129  1
130  && automate
131  && get_max_etat( automate ) == 9
132  && get_min_etat( automate ) == -5
133  , result
134  );
135  liberer_automate( automate );
136  }
137 
138  return result;
139 }
140 
141 
142 int test_translater_automate_entier(){
143  int result = 1;
144 
145  {
146  Automate * automate = creer_automate();
147  Automate * trans = translater_automate_entier( automate, 3 );
148 
149  TEST(
150  1
151  && trans
152  && taille_ensemble( get_etats( trans ) ) == 0
153  && taille_ensemble( get_initiaux( trans ) ) == 0
154  && taille_ensemble( get_finaux( trans ) ) == 0
155  , result
156  );
157  liberer_automate( trans );
158  liberer_automate( automate );
159  }
160 
161  {
162  Automate * automate = creer_automate();
163  ajouter_transition( automate, -3, 'a', 5 );
164  ajouter_etat_initial( automate, -3);
165  ajouter_etat_initial( automate, 0);
166  ajouter_etat_final( automate, 5);
167  ajouter_etat_final( automate, 7);
168 
169  Automate * trans = translater_automate_entier( automate, 2 );
170 
171  TEST(
172  1
173  && trans
174  && l_ensemble_est_egal( 4, get_etats( trans ), -1, 2, 7, 9)
175  && l_ensemble_est_egal( 2, get_initiaux( trans ), -1, 2)
176  && l_ensemble_est_egal( 2, get_finaux( trans ), 7, 9)
177  && test_transitions_automate(
178  1, trans,
179  -1, 'a', 7
180  )
181  , result
182  );
183  liberer_automate( trans );
184  liberer_automate( automate );
185  }
186 
187  return result;
188 }
189 
190 
191 int test_translater_automate(){
192  int result = 1;
193 
194  {
195  Automate * automate = creer_automate();
196  Automate * automate_eviter = creer_automate();
197  Automate * trans = translater_automate( automate, automate_eviter );
198 
199  TEST(
200  1
201  && trans
202  && taille_ensemble( get_etats( trans ) ) == 0
203  && taille_ensemble( get_initiaux( trans ) ) == 0
204  && taille_ensemble( get_finaux( trans ) ) == 0
205  , result
206  );
207  liberer_automate( trans );
208  liberer_automate( automate_eviter );
209  liberer_automate( automate );
210  }
211 
212  {
213  Automate * automate = creer_automate();
214  ajouter_transition( automate, -3, 'a', 5 );
215  ajouter_etat_initial( automate, -3);
216  ajouter_etat_initial( automate, 0);
217  ajouter_etat_final( automate, 5);
218  ajouter_etat_final( automate, 7);
219 
220  Automate * automate_eviter = creer_automate();
221  Automate * trans = translater_automate( automate, automate_eviter );
222 
223  TEST(
224  1
225  && trans
226  && l_ensemble_est_egal( 4, get_etats( trans ), -3, 0, 5, 7)
227  && l_ensemble_est_egal( 2, get_initiaux( trans ), -3, 0)
228  && l_ensemble_est_egal( 2, get_finaux( trans ), 5, 7)
229  && test_transitions_automate(
230  1, trans,
231  -3, 'a', 5
232  )
233  , result
234  );
235  liberer_automate( trans );
236  liberer_automate( automate_eviter );
237  liberer_automate( automate );
238  }
239 
240  {
241  Automate * automate = creer_automate();
242 
243  Automate * automate_eviter = creer_automate();
244  ajouter_transition( automate_eviter, -3, 'a', 5 );
245  ajouter_etat_initial( automate_eviter, -3);
246  ajouter_etat_initial( automate_eviter, 0);
247  ajouter_etat_final( automate_eviter, 5);
248  ajouter_etat_final( automate_eviter, 7);
249 
250  Automate * trans = translater_automate( automate, automate_eviter );
251 
252  TEST(
253  1
254  && trans
255  && taille_ensemble( get_etats( trans ) ) == 0
256  && taille_ensemble( get_initiaux( trans ) ) == 0
257  && taille_ensemble( get_finaux( trans ) ) == 0
258  , result
259  );
260  liberer_automate( trans );
261  liberer_automate( automate_eviter );
262  liberer_automate( automate );
263  }
264 
265  {
266  Automate * automate = creer_automate();
267  ajouter_transition( automate, -3, 'a', 5 );
268  ajouter_etat_initial( automate, -3);
269  ajouter_etat_initial( automate, 0);
270  ajouter_etat_final( automate, 5);
271  ajouter_etat_final( automate, 7);
272 
273  Automate * automate_eviter = creer_automate();
274  ajouter_transition( automate_eviter, -3, 'a', 5 );
275  ajouter_etat_initial( automate_eviter, -3);
276  ajouter_etat_initial( automate_eviter, 0);
277  ajouter_etat_final( automate_eviter, 5);
278  ajouter_etat_final( automate_eviter, 7);
279  Automate * trans = translater_automate( automate, automate_eviter );
280 
281  TEST(
282  1
283  && trans
284  && l_ensemble_est_egal( 4, get_etats( trans ), 8, 11, 16, 18)
285  && l_ensemble_est_egal( 2, get_initiaux( trans ), 8, 11)
286  && l_ensemble_est_egal( 2, get_finaux( trans ), 16, 18)
287  && test_transitions_automate(
288  1, trans,
289  8, 'a', 16
290  )
291  , result
292  );
293  liberer_automate( trans );
294  liberer_automate( automate_eviter );
295  liberer_automate( automate );
296  }
297 
298  return result;
299 }
300 
301 
302 int main(){
303 
304  if( ! test_get_min_max_etat() ){ return 1; }
305  if( ! test_translater_automate_entier() ){ return 1; }
306  if( ! test_translater_automate() ){ return 1; }
307 
308  return 0;
309 }
Le type d'un automate.
Definition: automate.h:39
void liberer_automate(Automate *automate)
Détruit un automate.
Definition: automate.c:168
int est_une_transition_de_l_automate(const Automate *automate, int origine, char lettre, int fin)
Renvoie 1 si ('origine', 'lettre', 'fin') est une transition de l'automate et 0 sinon.
Definition: automate.c:392
Automate * translater_automate_entier(const Automate *automate, int translation)
Crée une copie de l'automate passé en paramètre dont les numéros de tous les états ont été translatés...
Definition: automate.c:106
void ajouter_transition(Automate *automate, int origine, char lettre, int fin)
Ajoute une transition à l'automate passé en paramètre.
Definition: automate.c:206
Automate * creer_automate()
Crée un automate vide, sans états, sans lettres et sans transitions.
Definition: automate.c:91
int get_max_etat(const Automate *automate)
Definition: automate.c:38
const Ensemble * get_finaux(const Automate *automate)
Renvoie l'ensemble des états finaux d'un automate.
Definition: automate.c:190
void pour_toute_transition(const Automate *automate, void(*action)(int origine, char lettre, int fin, void *data), void *data)
La fonction passe en revue toutes les transitions de l'automate et appelle la fonction passée en para...
Definition: automate.c:294
const Ensemble * get_initiaux(const Automate *automate)
Renvoie l'ensemble des états intiaux d'un automate.
Definition: automate.c:186
const Ensemble * get_etats(const Automate *automate)
Renvoie l'ensemble des états d'un automate.
Definition: automate.c:182
void ajouter_etat_final(Automate *automate, int etat_final)
Ajoute un état final à un automate passé en paramètre.
Definition: automate.c:226
void ajouter_etat_initial(Automate *automate, int etat_initial)
Ajoute un état initial à un automate passé en paramètre.
Definition: automate.c:233
int get_min_etat(const Automate *automate)
Renvoie l'état ayant le numéro le plus petit de l'automate passé en paramètre.
Definition: automate.c:47
Automate * translater_automate(const Automate *automate, const Automate *automate_a_eviter)
Crée une copie de l'automate passé en paramètre. Les entiers des états du nouvel automate évitent ceu...
Definition: automate.c:375