Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / mutation.ml @ df924fb3

History | View | Annotate | Download (20.1 KB)

1
open Corelang
2
open Log
3
open Format
4

    
5
let random_seed = ref 0
6
let threshold_delay = 95
7
let threshold_inc_int = 97
8
let threshold_dec_int = 97
9
let threshold_random_int = 96
10
let threshold_switch_int = 100 (* not implemented yet *)
11
let threshold_random_float = 100 (* not used yet *)
12
let threshold_negate_bool_var = 95
13
let threshold_arith_op = 95
14
let threshold_rel_op = 95
15
let threshold_bool_op = 95
16

    
17
let int_consts = ref []
18

    
19
let rename_app id = id ^ "_mutant"
20

    
21
(************************************************************************************)
22
(*                    Gathering constants in the code                               *)
23
(************************************************************************************)
24

    
25
module IntSet = Set.Make (struct type t = int let compare = compare end)
26
module OpCount = Mmap.Make (struct type t = string let compare = compare end)
27

    
28
type records = {
29
  consts: IntSet.t;
30
  nb_boolexpr: int;
31
  nb_pre: int;
32
  nb_op: int OpCount.t;
33
}
34

    
35
let arith_op = ["+" ; "-" ; "*" ; "/"] 
36
let bool_op = ["&&"; "||"; "xor";  "impl"] 
37
let rel_op = ["<" ; "<=" ; ">" ; ">=" ; "!=" ; "=" ] 
38
let ops = arith_op @ bool_op @ rel_op
39
let all_ops = "not" :: ops
40

    
41
let empty_records = 
42
  {consts=IntSet.empty; nb_boolexpr=0; nb_pre=0; nb_op=OpCount.empty}
43

    
44
let records = ref empty_records
45

    
46
let merge_records records_list = 
47
  let merge_record r1 r2 =
48
    {
49
      consts = IntSet.union r1.consts r2.consts;
50

    
51
      nb_boolexpr = r1.nb_boolexpr + r2.nb_boolexpr;
52
      nb_pre = r1.nb_pre + r2.nb_pre;
53

    
54
      nb_op = OpCount.merge (fun op r1opt r2opt ->
55
	match r1opt, r2opt with
56
	| None, _ -> r2opt
57
	| _, None -> r1opt
58
	| Some x, Some y -> Some (x+y)
59
      ) r1.nb_op r2.nb_op 
60
    }
61
  in
62
  List.fold_left merge_record empty_records records_list
63
  
64
let compute_records_const_value c =
65
  match c with
66
  | Const_int i -> {empty_records with consts = IntSet.singleton i}
67
  | _ -> empty_records
68

    
69
let rec compute_records_expr expr =
70
  let boolexpr = 
71
    if (Types.repr expr.expr_type).Types.tdesc = Types.Tbool then
72
      {empty_records with nb_boolexpr = 1}
73
    else
74
      empty_records
75
  in
76
  let subrec = 
77
    match expr.expr_desc with
78
    | Expr_const c -> compute_records_const_value c
79
    | Expr_tuple l -> merge_records (List.map compute_records_expr l)
80
    | Expr_ite (i,t,e) -> 
81
      merge_records (List.map compute_records_expr [i;t;e])
82
    | Expr_arrow (e1, e2) ->       
83
      merge_records (List.map compute_records_expr [e1;e2])
84
    | Expr_pre e -> 
85
      merge_records (
86
	({empty_records with nb_pre = 1})
87
	::[compute_records_expr e])
88
    | Expr_appl (op_id, args, r) -> 
89
      if List.mem op_id ops then
90
	merge_records (
91
	  ({empty_records with nb_op = OpCount.singleton op_id 1})
92
	  ::[compute_records_expr args])
93
      else
94
	compute_records_expr args
95
    | _ -> empty_records
96
  in
97
  merge_records [boolexpr;subrec]
98

    
99
let compute_records_eq eq = compute_records_expr eq.eq_rhs
100

    
101
let compute_records_node nd = 
102
  merge_records (List.map compute_records_eq nd.node_eqs)
103

    
104
let compute_records_top_decl td =
105
  match td.top_decl_desc with
106
  | Node nd -> compute_records_node nd
107
  | Consts constsl -> merge_records (List.map (fun c -> compute_records_const_value c.const_value) constsl)
108
  | _ -> empty_records
109

    
110
let compute_records prog = 
111
  merge_records (List.map compute_records_top_decl prog)
112

    
113
(*****************************************************************)
114
(*                  Random mutation                              *)
115
(*****************************************************************)
116

    
117
let check_mut e1 e2 =
118
  let rec eq e1 e2 =
119
    match e1.expr_desc, e2.expr_desc with
120
    | Expr_const c1, Expr_const c2 -> c1 = c2
121
    | Expr_ident id1, Expr_ident id2 -> id1 = id2
122
    | Expr_tuple el1, Expr_tuple el2 -> List.length el1 = List.length el2 && List.for_all2 eq el1 el2
123
    | Expr_ite (i1, t1, e1), Expr_ite (i2, t2, e2) -> eq i1 i2 && eq t1 t2 && eq e1 e2
124
    | Expr_arrow (x1, y1), Expr_arrow (x2, y2) -> eq x1 x2 && eq y1 y2
125
    | Expr_pre e1, Expr_pre e2 -> eq e1 e2
126
    | Expr_appl (id1, e1, _), Expr_appl (id2, e2, _) -> id1 = id2 && eq e1 e2
127
  | _ -> false
128
  in
129
  if not (eq e1 e2) then
130
    Some (e1, e2)
131
  else
132
    None
133

    
134
let mk_cst_expr c = mkexpr Location.dummy_loc (Expr_const c)
135

    
136
let rdm_mutate_int i = 
137
  if Random.int 100 > threshold_inc_int then
138
    i+1
139
  else if Random.int 100 > threshold_dec_int then
140
    i-1
141
  else if Random.int 100 > threshold_random_int then
142
    Random.int 10
143
  else if Random.int 100 > threshold_switch_int then
144
    let idx = Random.int (List.length !int_consts) in
145
    List.nth !int_consts idx
146
  else
147
    i
148
  
149
let rdm_mutate_float f =
150
  if Random.int 100 > threshold_random_float then
151
    Random.float 10.
152
  else 
153
    f
154

    
155
let rdm_mutate_op op = 
156
match op with
157
| "+" | "-" | "*" | "/" when Random.int 100 > threshold_arith_op ->
158
  let filtered = List.filter (fun x -> x <> op) ["+"; "-"; "*"; "/"] in
159
  List.nth filtered (Random.int 3)
160
| "&&" | "||" | "xor" | "impl" when Random.int 100 > threshold_bool_op ->
161
  let filtered = List.filter (fun x -> x <> op) ["&&"; "||"; "xor"; "impl"] in
162
  List.nth filtered (Random.int 3)
163
| "<" | "<=" | ">" | ">=" | "!=" | "=" when Random.int 100 > threshold_rel_op ->
164
  let filtered = List.filter (fun x -> x <> op) ["<"; "<="; ">"; ">="; "!="; "="] in
165
  List.nth filtered (Random.int 5)
166
| _ -> op
167

    
168

    
169
let rdm_mutate_var expr = 
170
  match (Types.repr expr.expr_type).Types.tdesc with 
171
  | Types.Tbool ->
172
    (* if Random.int 100 > threshold_negate_bool_var then *)
173
    let new_e = mkpredef_unary_call Location.dummy_loc "not" expr in
174
    Some (expr, new_e), new_e
175
    (* else  *)
176
    (*   expr *)
177
  | _ -> None, expr
178
    
179
let rdm_mutate_pre orig_expr = 
180
  let new_e = Expr_pre orig_expr in
181
  Some (orig_expr, {orig_expr with expr_desc = new_e}), new_e
182

    
183

    
184
let rdm_mutate_const_value c =
185
  match c with
186
  | Const_int i -> Const_int (rdm_mutate_int i)
187
  | Const_real s ->  Const_real s (* those are string, let's leave them *)
188
  | Const_float f -> Const_float (rdm_mutate_float f)
189
  | Const_array _
190
  | Const_tag _ -> c
191

    
192
let rdm_mutate_const c =
193
  let new_const = rdm_mutate_const_value c.const_value in
194
  let mut = check_mut (mk_cst_expr c.const_value) (mk_cst_expr new_const) in
195
  mut, { c with const_value = new_const }
196

    
197

    
198
let select_in_list list rdm_mutate_elem = 
199
  let selected = Random.int (List.length list) in
200
  let mutation_opt, new_list, _ = 
201
    List.fold_right
202
      (fun elem (mutation_opt, res, cpt) -> if cpt = selected then 
203
	  let mutation, new_elem = rdm_mutate_elem elem in
204
	  Some mutation, new_elem::res, cpt+1  else mutation_opt, elem::res, cpt+1)
205
      list 
206
      (None, [], 0)
207
  in
208
  match mutation_opt with
209
  | Some mut -> mut, new_list
210
  | _ -> assert false
211

    
212

    
213
let rec rdm_mutate_expr expr =
214
  let mk_e d = { expr with expr_desc = d } in
215
  match expr.expr_desc with
216
  | Expr_ident id -> rdm_mutate_var expr
217
  | Expr_const c -> 
218
    let new_const = rdm_mutate_const_value c in 
219
    let mut = check_mut (mk_cst_expr c) (mk_cst_expr new_const) in
220
    mut, mk_e (Expr_const new_const)
221
  | Expr_tuple l -> 
222
    let mut, l' = select_in_list l rdm_mutate_expr in
223
    mut, mk_e (Expr_tuple l')
224
  | Expr_ite (i,t,e) -> 
225
    let mut, [i'; t'; e'] = select_in_list [i; t; e] rdm_mutate_expr in
226
    mut, mk_e (Expr_ite (i', t', e'))
227
  | Expr_arrow (e1, e2) -> 
228
    let mut, [e1'; e2'] = select_in_list [e1; e2] rdm_mutate_expr in
229
    mut, mk_e (Expr_arrow (e1', e2'))
230
  | Expr_pre e -> 
231
    let select_pre = Random.bool () in
232
    if select_pre then
233
      let mut, new_expr = rdm_mutate_pre expr in
234
      mut, mk_e new_expr
235
    else
236
      let mut, e' = rdm_mutate_expr e in
237
      mut, mk_e (Expr_pre e')
238
  | Expr_appl (op_id, args, r) -> 
239
    let select_op = Random.bool () in
240
    if select_op then
241
      let new_op_id = rdm_mutate_op op_id in
242
      let new_e = mk_e (Expr_appl (new_op_id, args, r)) in
243
      let mut = check_mut expr new_e in
244
      mut, new_e
245
    else
246
      let mut, new_args = rdm_mutate_expr args in
247
      mut, mk_e (Expr_appl (op_id, new_args, r))
248
	
249
  (* Other constructs are kept.
250
  | Expr_fby of expr * expr
251
  | Expr_array of expr list
252
  | Expr_access of expr * Dimension.dim_expr
253
  | Expr_power of expr * Dimension.dim_expr
254
  | Expr_when of expr * ident * label
255
  | Expr_merge of ident * (label * expr) list
256
  | Expr_uclock of expr * int
257
  | Expr_dclock of expr * int
258
  | Expr_phclock of expr * rat *)
259
  (* | _ -> expr.expr_desc *)
260
  
261

    
262
let rdm_mutate_eq eq =
263
  let mutation, new_rhs = rdm_mutate_expr eq.eq_rhs in
264
  mutation, { eq with eq_rhs = new_rhs }
265

    
266
let rdm_mutate_node nd = 
267
  let mutation, new_node_eqs =       
268
    select_in_list 
269
      nd.node_eqs 
270
      (fun eq -> let mut, new_eq = rdm_mutate_eq eq in
271
		 report ~level:1 
272
		   (fun fmt -> fprintf fmt "mutation: %a becomes %a@." 
273
		     Printers.pp_node_eq eq
274
		     Printers.pp_node_eq new_eq);
275
		 mut, new_eq )
276
  in
277
  mutation, { nd with node_eqs = new_node_eqs }
278

    
279
let rdm_mutate_top_decl td =
280
  match td.top_decl_desc with
281
  | Node nd -> 
282
    let mutation, new_node = rdm_mutate_node nd in 
283
    mutation, { td with top_decl_desc = Node new_node}
284
  | Consts constsl -> 
285
    let mut, new_constsl = select_in_list constsl rdm_mutate_const in
286
    mut, { td with top_decl_desc = Consts new_constsl }
287
  | _ -> None, td
288
    
289
(* Create a single mutant with the provided random seed *)
290
let rdm_mutate_prog prog = 
291
  select_in_list prog rdm_mutate_top_decl
292

    
293
let rdm_mutate nb prog = 
294
  let rec iterate nb res =
295
    incr random_seed;
296
    if nb <= 0 then
297
      res
298
    else (
299
      Random.init !random_seed;
300
      let mutation, new_mutant = rdm_mutate_prog prog in
301
      match mutation with
302
	None -> iterate nb res 
303
      | Some mutation -> ( 
304
	if List.mem_assoc mutation res then (
305
	  iterate nb res
306
	)
307
	else (
308
	  report ~level:1 (fun fmt -> fprintf fmt "%i mutants remaining@." nb); 
309
	  iterate (nb-1) ((mutation, new_mutant)::res)
310
	)
311
      )
312
    )
313
  in
314
  iterate nb []
315

    
316

    
317
(*****************************************************************)
318
(*                  Random mutation                              *)
319
(*****************************************************************)
320

    
321
type mutant_t = Boolexpr of int | Pre of int | Op of string * int * string | IncrIntCst of int | DecrIntCst of int | SwitchIntCst of int * int 
322

    
323
let target : mutant_t option ref = ref None
324

    
325
let print_directive fmt d =
326
  match d with
327
  | Pre n -> Format.fprintf fmt "pre %i" n
328
  | Boolexpr n -> Format.fprintf fmt "boolexpr %i" n
329
  | Op (o, i, d) -> Format.fprintf fmt "%s %i -> %s" o i d
330
  | IncrIntCst n ->  Format.fprintf fmt "incr int cst %i" n
331
  | DecrIntCst n ->  Format.fprintf fmt "decr int cst %i" n
332
  | SwitchIntCst (n, m) ->  Format.fprintf fmt "switch int cst %i -> %i" n m
333

    
334
let fold_mutate_int i = 
335
  if Random.int 100 > threshold_inc_int then
336
    i+1
337
  else if Random.int 100 > threshold_dec_int then
338
    i-1
339
  else if Random.int 100 > threshold_random_int then
340
    Random.int 10
341
  else if Random.int 100 > threshold_switch_int then
342
    try
343
	let idx = Random.int (List.length !int_consts) in
344
        List.nth !int_consts idx
345
    with _ -> i
346
  else
347
    i
348
  
349
let fold_mutate_float f =
350
  if Random.int 100 > threshold_random_float then
351
    Random.float 10.
352
  else 
353
    f
354

    
355
let fold_mutate_op op = 
356
(* match op with *)
357
(* | "+" | "-" | "*" | "/" when Random.int 100 > threshold_arith_op -> *)
358
(*   let filtered = List.filter (fun x -> x <> op) ["+"; "-"; "*"; "/"] in *)
359
(*   List.nth filtered (Random.int 3) *)
360
(* | "&&" | "||" | "xor" | "impl" when Random.int 100 > threshold_bool_op -> *)
361
(*   let filtered = List.filter (fun x -> x <> op) ["&&"; "||"; "xor"; "impl"] in *)
362
(*   List.nth filtered (Random.int 3) *)
363
(* | "<" | "<=" | ">" | ">=" | "!=" | "=" when Random.int 100 > threshold_rel_op -> *)
364
(*   let filtered = List.filter (fun x -> x <> op) ["<"; "<="; ">"; ">="; "!="; "="] in *)
365
(*   List.nth filtered (Random.int 5) *)
366
(* | _ -> op *)
367
  match !target with
368
  | Some (Op(op_orig, 0, op_new)) when op_orig = op -> (
369
    target := None;
370
    op_new
371
  )
372
  | Some (Op(op_orig, n, op_new)) when op_orig = op -> (
373
    target := Some (Op(op_orig, n-1, op_new));
374
    op
375
  )
376
  | _ -> if List.mem op Basic_library.internal_funs then op else rename_app op
377

    
378

    
379
let fold_mutate_var expr = 
380
  (* match (Types.repr expr.expr_type).Types.tdesc with  *)
381
  (* | Types.Tbool -> *)
382
  (*     (\* if Random.int 100 > threshold_negate_bool_var then *\) *)
383
  (*     mkpredef_unary_call Location.dummy_loc "not" expr *)
384
  (*   (\* else  *\) *)
385
  (*   (\*   expr *\) *)
386
  (* | _ -> 
387
 *)expr
388

    
389
let fold_mutate_boolexpr expr =
390
  match !target with
391
  | Some (Boolexpr 0) -> (
392
    target := None;
393
    mkpredef_unary_call Location.dummy_loc "not" expr
394
  )
395
  | Some (Boolexpr n) ->
396
      (target := Some (Boolexpr (n-1)); expr)
397
  | _ -> expr
398
    
399
let fold_mutate_pre orig_expr e = 
400
  match !target with
401
    Some (Pre 0) -> (
402
      target := None;
403
      Expr_pre ({orig_expr with expr_desc = Expr_pre e}) 
404
    )
405
  | Some (Pre n) -> (
406
    target := Some (Pre (n-1));
407
    Expr_pre e
408
  )
409
  | _ -> Expr_pre e
410
    
411
let fold_mutate_const_value c = 
412
match c with
413
| Const_int i -> (
414
  match !target with
415
  | Some (IncrIntCst 0) -> (target := None; Const_int (i+1))
416
  | Some (DecrIntCst 0) -> (target := None; Const_int (i-1))
417
  | Some (SwitchIntCst (0, id)) -> (target := None; Const_int (List.nth (IntSet.elements (IntSet.remove i !records.consts)) id)) 
418
  | Some (IncrIntCst n) -> (target := Some (IncrIntCst (n-1)); c)
419
  | Some (DecrIntCst n) -> (target := Some (DecrIntCst (n-1)); c)
420
  | Some (SwitchIntCst (n, id)) -> (target := Some (SwitchIntCst (n-1, id)); c)
421
  | _ -> c)
422
| _ -> c
423

    
424
(*
425
  match c with
426
  | Const_int i -> Const_int (fold_mutate_int i)
427
  | Const_real s -> Const_real s (* those are string, let's leave them *)
428
  | Const_float f -> Const_float (fold_mutate_float f)
429
  | Const_array _
430
  | Const_tag _ -> c
431
TODO
432

    
433
				  *)
434
let fold_mutate_const c =
435
  { c with const_value = fold_mutate_const_value c.const_value }
436

    
437
let rec fold_mutate_expr expr =
438
  let new_expr = 
439
    match expr.expr_desc with
440
    | Expr_ident id -> fold_mutate_var expr
441
    | _ -> (
442
      let new_desc = match expr.expr_desc with
443
	| Expr_const c -> Expr_const (fold_mutate_const_value c)
444
	| Expr_tuple l -> Expr_tuple (List.fold_right (fun e res -> (fold_mutate_expr e)::res) l [])
445
	| Expr_ite (i,t,e) -> Expr_ite (fold_mutate_expr i, fold_mutate_expr t, fold_mutate_expr e)
446
	| Expr_arrow (e1, e2) -> Expr_arrow (fold_mutate_expr e1, fold_mutate_expr e2)
447
	| Expr_pre e -> fold_mutate_pre expr (fold_mutate_expr e)
448
	| Expr_appl (op_id, args, r) -> Expr_appl (fold_mutate_op op_id, fold_mutate_expr args, r)
449
  (* Other constructs are kept.
450
  | Expr_fby of expr * expr
451
  | Expr_array of expr list
452
  | Expr_access of expr * Dimension.dim_expr
453
  | Expr_power of expr * Dimension.dim_expr
454
  | Expr_when of expr * ident * label
455
  | Expr_merge of ident * (label * expr) list
456
  | Expr_uclock of expr * int
457
  | Expr_dclock of expr * int
458
  | Expr_phclock of expr * rat *)
459
  | _ -> expr.expr_desc
460
    
461
      in
462
      { expr with expr_desc = new_desc }
463
    )
464
  in
465
  if (Types.repr expr.expr_type).Types.tdesc = Types.Tbool then
466
    fold_mutate_boolexpr new_expr  
467
  else
468
    new_expr
469

    
470
let fold_mutate_eq eq =
471
  { eq with eq_rhs = fold_mutate_expr eq.eq_rhs }
472

    
473
let fold_mutate_node nd = 
474
  { nd with 
475
    node_eqs = 
476
      List.fold_right (fun e res -> (fold_mutate_eq e)::res) nd.node_eqs [];
477
    node_id = rename_app nd.node_id
478
  }
479

    
480
let fold_mutate_top_decl td =
481
  match td.top_decl_desc with
482
  | Node nd -> { td with top_decl_desc = Node (fold_mutate_node nd)}
483
  | Consts constsl -> { td with top_decl_desc = Consts (List.fold_right (fun e res -> (fold_mutate_const e)::res) constsl [])}
484
  | _ -> td
485
    
486
(* Create a single mutant with the provided random seed *)
487
let fold_mutate_prog prog = 
488
  List.fold_right (fun e res -> (fold_mutate_top_decl e)::res) prog []
489

    
490
let create_mutant prog directive =  
491
  target := Some directive; 
492
  let prog' = fold_mutate_prog prog in
493
  target := None;
494
  prog'
495
  
496

    
497
let op_mutation op = 
498
  let res =
499
    let rem_op l = List.filter (fun e -> e <> op) l in
500
  if List.mem op arith_op then rem_op arith_op else 
501
    if List.mem op bool_op then rem_op bool_op else 
502
      if List.mem op rel_op then rem_op rel_op else 
503
	(Format.eprintf "Failing with op %s@." op;
504
	  assert false
505
	)
506
  in
507
  (* Format.eprintf "Mutation op %s to [%a]@." op (Utils.fprintf_list ~sep:"," Format.pp_print_string) res; *)
508
  res
509

    
510
let rec remains select list =
511
  match list with 
512
    [] -> []
513
  | hd::tl -> if select hd then tl else remains select tl
514
      
515
let next_change m =
516
  let res = 
517
  let rec first_op () = 
518
    try
519
      let min_binding = OpCount.min_binding !records.nb_op in
520
      Op (fst min_binding, 0, List.hd (op_mutation (fst min_binding)))
521
    with Not_found -> first_boolexpr () 
522
  and first_boolexpr () =
523
    if !records.nb_boolexpr > 0 then 
524
      Boolexpr 0 
525
    else first_pre ()
526
  and first_pre () = 
527
    if !records.nb_pre > 0 then 
528
      Pre 0 
529
    else
530
      first_op ()
531
  and first_intcst () =
532
    if IntSet.cardinal !records.consts > 0 then
533
      IncrIntCst 0
534
    else
535
      first_boolexpr ()
536
  in
537
  match m with
538
  | Boolexpr n -> 
539
    if n+1 >= !records.nb_boolexpr then 
540
      first_pre ()
541
    else
542
      Boolexpr (n+1)
543
  | Pre n -> 
544
    if n+1 >= !records.nb_pre then 
545
      first_op ()
546
    else Pre (n+1)
547
  | Op (orig, id, mut_op) -> (
548
    match remains (fun x -> x = mut_op) (op_mutation orig) with
549
    | next_op::_ -> Op (orig, id, next_op)
550
    | [] -> if id+1 >= OpCount.find orig !records.nb_op then (
551
      match remains (fun (k1, _) -> k1 = orig) (OpCount.bindings !records.nb_op) with
552
      | [] -> first_intcst ()
553
      | hd::_ -> Op (fst hd, 0, List.hd (op_mutation (fst hd)))
554
    ) else
555
	Op(orig, id+1, List.hd (op_mutation orig))
556
  )
557
  | IncrIntCst n ->
558
    if n+1 >= IntSet.cardinal !records.consts then
559
      DecrIntCst 0
560
    else IncrIntCst (n+1)
561
  | DecrIntCst n ->
562
    if n+1 >= IntSet.cardinal !records.consts then
563
      SwitchIntCst (0, 0)
564
    else DecrIntCst (n+1)
565
  | SwitchIntCst (n, m) ->
566
    if m+1 > -1 + IntSet.cardinal !records.consts then
567
      SwitchIntCst (n, m+1)
568
    else if n+1 >= IntSet.cardinal !records.consts then
569
      SwitchIntCst (n+1, 0)
570
    else first_boolexpr ()
571

    
572
  in
573
  (* Format.eprintf "from: %a to: %a@." print_directive m print_directive res; *)
574
  res
575

    
576
let fold_mutate nb prog = 
577
  incr random_seed;
578
  Random.init !random_seed;
579
  let find_next_new mutants mutant =
580
    let rec find_next_new init current =
581
      if init = current then raise Not_found else
582
	if List.mem current mutants then
583
	  find_next_new init (next_change current)
584
	else
585
	  current
586
    in
587
    find_next_new mutant (next_change mutant) 
588
  in
589
  (* Creating list of nb elements of mutants *)
590
  let rec create_mutants_directives rnb mutants = 
591
    if rnb <= 0 then mutants 
592
    else 
593
      let random_mutation = 
594
	match Random.int 6 with
595
	| 5 -> IncrIntCst (try Random.int (IntSet.cardinal !records.consts) with _ -> 0)
596
	| 4 -> DecrIntCst (try Random.int (IntSet.cardinal !records.consts) with _ -> 0)
597
	| 3 -> SwitchIntCst ((try Random.int (IntSet.cardinal !records.consts) with _ -> 0), (try Random.int (-1 + IntSet.cardinal !records.consts) with _ -> 0))
598
	| 2 -> Pre (try Random.int !records.nb_pre with _ -> 0)
599
	| 1 -> Boolexpr (try Random.int !records.nb_boolexpr with _ -> 0)
600
	| 0 -> let bindings = OpCount.bindings !records.nb_op in
601
	       let op, nb_op = List.nth bindings (try Random.int (List.length bindings) with _ -> 0) in
602
	       let new_op = List.nth (op_mutation op) (try Random.int (List.length (op_mutation op)) with _ -> 0) in
603
	       Op (op, (try Random.int nb_op with _ -> 0), new_op)
604
	| _ -> assert false
605
      in
606
      if List.mem random_mutation mutants then
607
	try
608
	  let new_mutant = (find_next_new mutants random_mutation) in
609
	  report ~level:2 (fun fmt -> fprintf fmt " %i mutants generated out of %i expected@." (nb-rnb) nb);
610
	 create_mutants_directives (rnb-1) (new_mutant::mutants) 
611
	with Not_found -> (
612
	  report ~level:1 (fun fmt -> fprintf fmt "Only %i mutants generated out of %i expected@." (nb-rnb) nb); 
613
	  mutants
614
	)
615
      else
616
	create_mutants_directives (rnb-1) (random_mutation::mutants)
617
  in
618
  let mutants_directives = create_mutants_directives nb [] in
619
  List.map (fun d -> d, create_mutant prog d) mutants_directives 
620
  
621

    
622
let mutate nb prog =
623
  records := compute_records prog;
624
  (* Format.printf "Records: %i pre, %i boolexpr" (\* , %a ops *\) *)
625
  (*   !records.nb_pre *)
626
(*     !records.nb_boolexpr *)
627
(*     (\* !records.op *\) *)
628
(* ;  *)   
629
  fold_mutate nb prog, print_directive
630

    
631

    
632

    
633

    
634
(* Local Variables: *)
635
(* compile-command:"make -C .." *)
636
(* End: *)
637

    
638