From 51b7c94556738adfab18b3dcd73e6fc062ee678c Mon Sep 17 00:00:00 2001
From: Virgile Prevosto <virgile.prevosto@m4x.org>
Date: Tue, 16 Apr 2024 16:25:03 +0200
Subject: [PATCH] [tests] more stable oracle for ast-diff test

---
 tests/syntax/ast_diff_1.ml                  | 29 +++++++++-
 tests/syntax/oracle/ast_diff_1.0.res.oracle | 64 ++++++++++-----------
 tests/syntax/oracle/ast_diff_1.1.res.oracle | 58 +++++++++----------
 3 files changed, 88 insertions(+), 63 deletions(-)

diff --git a/tests/syntax/ast_diff_1.ml b/tests/syntax/ast_diff_1.ml
index 1a85f02e294..3bab5ff9510 100644
--- a/tests/syntax/ast_diff_1.ml
+++ b/tests/syntax/ast_diff_1.ml
@@ -1,3 +1,5 @@
+open Cil_types
+
 include Plugin.Register(
   struct
     let name = "AST diff test"
@@ -15,13 +17,36 @@ let show_fun kf c =
     Kernel_function.pretty kf
     Ast_diff.Kernel_function.pretty_data c
 
+let cmp_fun kf1 kf2 =
+  Datatype.String.compare
+    (Kernel_function.get_vi kf1).vname (Kernel_function.get_vi kf2).vname
+
+let cmp_var v1 v2 =
+  let res = Datatype.String.compare v1.vname v2.vname in
+  if res <> 0 then res
+  else if v1.vglob && v2.vglob then 0
+  else if v1.vglob then -1
+  else if v2.vglob then 1
+  else if v1.vstorage = Static && v2.vstorage = Static then 0
+  else if v1.vstorage = Static then -1
+  else if v2.vstorage = Static then 1
+  else begin
+    let prj = Ast_diff.Orig_project.get() in
+    let kf1 = Project.on prj Kernel_function.find_defining_kf v1 in
+    let kf2 = Project.on prj Kernel_function.find_defining_kf v2 in
+    if Option.is_none kf1 || Option.is_none kf2 then
+      fatal "Variable %a(%a) is not global but has no associated function"
+        Cil_datatype.Varinfo.pretty v1 Cil_datatype.Location.pretty v1.vdecl;
+    cmp_fun (Option.get kf1) (Option.get kf2)
+  end
+
 let show_correspondances () =
   if Kernel.AstDiff.get () then begin
     result "Showing correspondances between %s and %s"
       (Project.get_name (Ast_diff.Orig_project.get()))
       (Project.get_name (Project.current()));
-    Ast_diff.Varinfo.iter show_var;
-    Ast_diff.Kernel_function.iter show_fun;
+    Ast_diff.Varinfo.iter_sorted ~cmp:cmp_var show_var;
+    Ast_diff.Kernel_function.iter_sorted ~cmp:cmp_fun show_fun;
   end
 
 let () = Boot.Main.extend show_correspondances
diff --git a/tests/syntax/oracle/ast_diff_1.0.res.oracle b/tests/syntax/oracle/ast_diff_1.0.res.oracle
index ef54b4b6f7e..29789a1eea1 100644
--- a/tests/syntax/oracle/ast_diff_1.0.res.oracle
+++ b/tests/syntax/oracle/ast_diff_1.0.res.oracle
@@ -1,51 +1,51 @@
 [kernel] Parsing ast_diff_1.i (no preprocessing)
 [kernel] Parsing ast_diff_2.c (with preprocessing)
 [AST diff test] Showing correspondances between orig_default and default
-[AST diff test] Variable i:  => i
-[AST diff test] Variable local_var_use:  => local_var_use
-[AST diff test] Variable v:  => w
-[AST diff test] Variable a:  => q
-[AST diff test] Variable x:  => z
-[AST diff test] Variable y:  => t
-[AST diff test] Variable s:  => s
-[AST diff test] Variable use_s:  => use_s
-[AST diff test] Variable x:  => x
-[AST diff test] Variable with_goto_changed:  => with_goto_changed
+[AST diff test] Variable S:  => S
 [AST diff test] Variable X:  => X
 [AST diff test] Variable Y: N/A
+[AST diff test] Variable __retres:  => __retres
+[AST diff test] Variable a:  => q
 [AST diff test] Variable c:  => c
-[AST diff test] Variable f:  => f
-[AST diff test] Variable with_goto_unchanged:  => with_goto_unchanged
-[AST diff test] Variable x:  => x
 [AST diff test] Variable c:  => c
+[AST diff test] Variable decl:  => decl
+[AST diff test] Variable f:  => f
 [AST diff test] Variable g:  => g
-[AST diff test] Variable se:  => se
-[AST diff test] Variable S:  => S
-[AST diff test] Variable with_loop_unroll_same:  => with_loop_unroll_same
-[AST diff test] Variable i_0:  => i_0
 [AST diff test] Variable h:  => h
-[AST diff test] Variable with_loop_unroll_diff:  => with_loop_unroll_diff
-[AST diff test] Variable i_0:  => i_0
+[AST diff test] Variable has_static_local:  => has_static_local
 [AST diff test] Variable has_static_local_x:  => has_static_local_y
+[AST diff test] Variable i:  => i
+[AST diff test] Variable i_0:  => i_0
+[AST diff test] Variable i_0:  => i_0
+[AST diff test] Variable local_var_use:  => local_var_use
+[AST diff test] Variable ptr_func:  => ptr_func
+[AST diff test] Variable s:  => s
+[AST diff test] Variable se:  => se
 [AST diff test] Variable use_logic_builtin:  => use_logic_builtin
-[AST diff test] Variable __retres:  => __retres
+[AST diff test] Variable use_s:  => use_s
+[AST diff test] Variable used_in_decl:  => used_in_decl
+[AST diff test] Variable v:  => w
+[AST diff test] Variable with_goto_changed:  => with_goto_changed
+[AST diff test] Variable with_goto_unchanged:  => with_goto_unchanged
+[AST diff test] Variable with_loop_unroll_diff:  => with_loop_unroll_diff
+[AST diff test] Variable with_loop_unroll_same:  => with_loop_unroll_same
 [AST diff test] Variable x:  => x
+[AST diff test] Variable x:  => z
+[AST diff test] Variable x:  => x
+[AST diff test] Variable x:  => x
+[AST diff test] Variable y:  => t
 [AST diff test] Variable y:  => y
-[AST diff test] Variable has_static_local:  => has_static_local
-[AST diff test] Variable decl:  => decl
-[AST diff test] Variable used_in_decl:  => used_in_decl
-[AST diff test] Variable ptr_func:  => ptr_func
+[AST diff test] Function decl:  => decl
+[AST diff test] Function f:  => f
+[AST diff test] Function g: N/A
+[AST diff test] Function h: -> h (body changed)
+[AST diff test] Function has_static_local:  => has_static_local
 [AST diff test] Function i:  => i
 [AST diff test] Function local_var_use:  => local_var_use
+[AST diff test] Function se:  => se
+[AST diff test] Function use_logic_builtin:  => use_logic_builtin
 [AST diff test] Function use_s:  => use_s
 [AST diff test] Function with_goto_changed: -> with_goto_changed (body changed)
-[AST diff test] Function f:  => f
 [AST diff test] Function with_goto_unchanged:  => with_goto_unchanged
-[AST diff test] Function g: N/A
-[AST diff test] Function se:  => se
-[AST diff test] Function with_loop_unroll_same:  => with_loop_unroll_same
-[AST diff test] Function h: -> h (body changed)
 [AST diff test] Function with_loop_unroll_diff:  => with_loop_unroll_diff
-[AST diff test] Function use_logic_builtin:  => use_logic_builtin
-[AST diff test] Function has_static_local:  => has_static_local
-[AST diff test] Function decl:  => decl
+[AST diff test] Function with_loop_unroll_same:  => with_loop_unroll_same
diff --git a/tests/syntax/oracle/ast_diff_1.1.res.oracle b/tests/syntax/oracle/ast_diff_1.1.res.oracle
index 21e1ebbe13b..05b4855366c 100644
--- a/tests/syntax/oracle/ast_diff_1.1.res.oracle
+++ b/tests/syntax/oracle/ast_diff_1.1.res.oracle
@@ -2,48 +2,48 @@
 [kernel] Parsing ast_diff_2.c (with preprocessing)
 [kernel] Parsing ast_diff_1.i (no preprocessing)
 [AST diff test] Showing correspondances between orig_default and default
-[AST diff test] Variable v:  => w
-[AST diff test] Variable a:  => q
-[AST diff test] Variable x:  => z
-[AST diff test] Variable y:  => t
-[AST diff test] Variable s: N/A
-[AST diff test] Variable use_s: N/A
-[AST diff test] Variable with_goto_changed:  => with_goto_changed
 [AST diff test] Variable X:  => X
 [AST diff test] Variable Y: N/A
+[AST diff test] Variable a:  => q
 [AST diff test] Variable c:  => c
-[AST diff test] Variable f:  => f
-[AST diff test] Variable with_goto_unchanged:  => with_goto_unchanged
-[AST diff test] Variable x:  => x
 [AST diff test] Variable c:  => c
+[AST diff test] Variable decl:  => decl
+[AST diff test] Variable f:  => f
 [AST diff test] Variable g:  => g
-[AST diff test] Variable se:  => se
-[AST diff test] Variable with_loop_unroll_same:  => with_loop_unroll_same
-[AST diff test] Variable i_0:  => i_0
 [AST diff test] Variable h:  => h
-[AST diff test] Variable with_loop_unroll_diff:  => with_loop_unroll_diff
-[AST diff test] Variable i_0:  => i_0
+[AST diff test] Variable has_static_local:  => has_static_local
 [AST diff test] Variable has_static_local_x:  => has_static_local_y
+[AST diff test] Variable i:  => i
+[AST diff test] Variable i_0:  => i_0
+[AST diff test] Variable i_0:  => i_0
+[AST diff test] Variable local_var_use:  => local_var_use
+[AST diff test] Variable ptr_func:  => ptr_func
+[AST diff test] Variable s: N/A
+[AST diff test] Variable se:  => se
 [AST diff test] Variable use_logic_builtin:  => use_logic_builtin
+[AST diff test] Variable use_s: N/A
+[AST diff test] Variable used_in_decl:  => used_in_decl
+[AST diff test] Variable v:  => w
+[AST diff test] Variable with_goto_changed:  => with_goto_changed
+[AST diff test] Variable with_goto_unchanged:  => with_goto_unchanged
+[AST diff test] Variable with_loop_unroll_diff:  => with_loop_unroll_diff
+[AST diff test] Variable with_loop_unroll_same:  => with_loop_unroll_same
 [AST diff test] Variable x:  => x
+[AST diff test] Variable x:  => z
+[AST diff test] Variable x:  => x
+[AST diff test] Variable y:  => t
 [AST diff test] Variable y:  => y
-[AST diff test] Variable has_static_local:  => has_static_local
-[AST diff test] Variable decl:  => decl
-[AST diff test] Variable used_in_decl:  => used_in_decl
-[AST diff test] Variable ptr_func:  => ptr_func
-[AST diff test] Variable i:  => i
-[AST diff test] Variable local_var_use:  => local_var_use
-[AST diff test] Function use_s: N/A
-[AST diff test] Function with_goto_changed: -> with_goto_changed (body changed)
+[AST diff test] Function decl:  => decl
 [AST diff test] Function f:  => f
-[AST diff test] Function with_goto_unchanged:  => with_goto_unchanged
 [AST diff test] Function g: N/A
-[AST diff test] Function se: -> se (body changed)
-[AST diff test] Function with_loop_unroll_same:  => with_loop_unroll_same
 [AST diff test] Function h: -> h (body changed)
-[AST diff test] Function with_loop_unroll_diff:  => with_loop_unroll_diff
-[AST diff test] Function use_logic_builtin:  => use_logic_builtin
 [AST diff test] Function has_static_local:  => has_static_local
-[AST diff test] Function decl:  => decl
 [AST diff test] Function i:  => i
 [AST diff test] Function local_var_use:  => local_var_use
+[AST diff test] Function se: -> se (body changed)
+[AST diff test] Function use_logic_builtin:  => use_logic_builtin
+[AST diff test] Function use_s: N/A
+[AST diff test] Function with_goto_changed: -> with_goto_changed (body changed)
+[AST diff test] Function with_goto_unchanged:  => with_goto_unchanged
+[AST diff test] Function with_loop_unroll_diff:  => with_loop_unroll_diff
+[AST diff test] Function with_loop_unroll_same:  => with_loop_unroll_same
-- 
GitLab