From d5e69f0ab0ea47a69542a0261ef29cd337a46897 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Loi=CC=88c=20Correnson?= <loic.correnson@cea.fr>
Date: Tue, 15 Jan 2019 15:15:48 +0100
Subject: [PATCH] [lib] bin & hex pretty printers for Integer

---
 tests/misc/oracle/pp_bin_hex.res.oracle | 86 +++++++++++++++++++++++++
 tests/misc/pp_bin_hex.i                 |  4 ++
 tests/misc/pp_bin_hex.ml                | 37 +++++++++++
 3 files changed, 127 insertions(+)
 create mode 100644 tests/misc/oracle/pp_bin_hex.res.oracle
 create mode 100644 tests/misc/pp_bin_hex.i
 create mode 100644 tests/misc/pp_bin_hex.ml

diff --git a/tests/misc/oracle/pp_bin_hex.res.oracle b/tests/misc/oracle/pp_bin_hex.res.oracle
new file mode 100644
index 00000000000..d01369b7720
--- /dev/null
+++ b/tests/misc/oracle/pp_bin_hex.res.oracle
@@ -0,0 +1,86 @@
+--------------------------------------------------
+Dec. 0
+Hex. 0x0000
+Bin. 0b00000000
+--------------------------------------------------
+Dec. 1
+Hex. 0x0001
+Bin. 0b00000001
+--------------------------------------------------
+Dec. -1
+Hex. 1xFFFF
+Bin. 1b11111111
+--------------------------------------------------
+Dec. 2
+Hex. 0x0002
+Bin. 0b00000010
+--------------------------------------------------
+Dec. -2
+Hex. 1xFFFE
+Bin. 1b11111110
+--------------------------------------------------
+Dec. 5
+Hex. 0x0005
+Bin. 0b00000101
+--------------------------------------------------
+Dec. -5
+Hex. 1xFFFB
+Bin. 1b11111011
+--------------------------------------------------
+Dec. 9
+Hex. 0x0009
+Bin. 0b00001001
+--------------------------------------------------
+Dec. -9
+Hex. 1xFFF7
+Bin. 1b11110111
+--------------------------------------------------
+Dec. 16
+Hex. 0x0010
+Bin. 0b00010000
+--------------------------------------------------
+Dec. -16
+Hex. 1xFFF0
+Bin. 1b11110000
+--------------------------------------------------
+Dec. 127
+Hex. 0x007F
+Bin. 0b01111111
+--------------------------------------------------
+Dec. -127
+Hex. 1xFF81
+Bin. 1b10000001
+--------------------------------------------------
+Dec. 128
+Hex. 0x0080
+Bin. 0b10000000
+--------------------------------------------------
+Dec. -128
+Hex. 1xFF80
+Bin. 1b10000000
+--------------------------------------------------
+Dec. 255
+Hex. 0x00FF
+Bin. 0b11111111
+--------------------------------------------------
+Dec. -255
+Hex. 1xFF01
+Bin. 1b00000001
+--------------------------------------------------
+Dec. 4279173135
+Hex. 0xFF0F000F
+Bin. 0b11111111000011110000000000001111
+--------------------------------------------------
+Dec. -4279173135
+Hex. 1x00F0FFF1
+Bin. 1b00000000111100001111111111110001
+--------------------------------------------------
+Dec. 386334727
+Hex. 0x17070007
+Bin. 0b00010111000001110000000000000111
+--------------------------------------------------
+Dec. -386334727
+Hex. 1xE8F8FFF9
+Bin. 1b11101000111110001111111111111001
+--------------------------------------------------
+[kernel] Parsing tests/misc/pp_bin_hex.i (no preprocessing)
diff --git a/tests/misc/pp_bin_hex.i b/tests/misc/pp_bin_hex.i
new file mode 100644
index 00000000000..c6563fc5bf0
--- /dev/null
+++ b/tests/misc/pp_bin_hex.i
@@ -0,0 +1,4 @@
+/* run.config
+   CMD: @frama-c@ -no-autoload-plugins
+   OPT: -load-script tests/misc/pp_bin_hex.ml
+*/
diff --git a/tests/misc/pp_bin_hex.ml b/tests/misc/pp_bin_hex.ml
new file mode 100644
index 00000000000..719fdbeb261
--- /dev/null
+++ b/tests/misc/pp_bin_hex.ml
@@ -0,0 +1,37 @@
+let pp_dec fmt z = Integer.pretty ~hexa:false fmt z
+let pp_hex fmt z = Integer.pp_hex ~nbits:16 fmt z
+let pp_bin fmt z = Integer.pp_bin ~nbits:8  fmt z
+
+let hrule () =
+  Format.printf "--------------------------------------------------@."
+
+let testcase z =
+  begin
+    hrule () ;
+    Format.printf "Dec. %a@." pp_dec z ;
+    Format.printf "Hex. %a@." pp_hex z ;
+    Format.printf "Bin. %a@." pp_bin z ;
+  end
+
+let () =
+  begin
+    List.iter
+      (fun z ->
+         testcase z ;
+         if not (Integer.equal z Integer.zero) then
+           testcase (Integer.neg z)
+      ) [
+        Integer.of_string "0" ;
+        Integer.of_string "1" ;
+        Integer.of_string "2" ;
+        Integer.of_string "5" ;
+        Integer.of_string "9" ;
+        Integer.of_string "16" ;
+        Integer.of_string "127" ;
+        Integer.of_string "128" ;
+        Integer.of_string "0xFF" ;
+        Integer.of_string "0xFF0F000F" ;
+        Integer.of_string "0x17070007" ;
+      ] ;
+    hrule () ;
+  end
-- 
GitLab