From 7d05d97c8e51e77f117fc14f060bb0c54b86a135 Mon Sep 17 00:00:00 2001 From: pacien Date: Fri, 27 Apr 2018 12:07:02 +0200 Subject: Sort regs before printing --- reg.ml | 12 ++++-------- reg.mli | 4 +++- urm_test.ml | 7 +++---- 3 files changed, 10 insertions(+), 13 deletions(-) diff --git a/reg.ml b/reg.ml index d065838..56c4ae6 100644 --- a/reg.ml +++ b/reg.ml @@ -10,11 +10,7 @@ let reg_val (Reg(_, value)) = value let reg_compar l r = (reg_val l) - (reg_val r) let reg_string (Reg (index, value)) = "(" ^ (string_of_int index) ^ "," ^ (string_of_int value) ^ ")" -let regs_get reglist index = - List.find (fun (Reg(idx, _)) -> idx = index) reglist |> reg_val - -let regs_set reglist index value = - Reg(index, value) :: List.filter (fun (Reg(idx, _)) -> idx != index) reglist - -let regs_string reglist = List.map (reg_string) reglist |> String.concat "," - +let regs_get reglist index = List.find (fun (Reg(idx, _)) -> idx = index) reglist |> reg_val +let regs_set reglist index value = Reg(index, value) :: List.filter (fun (Reg(idx, _)) -> idx != index) reglist +let regs_sort = List.sort (fun (Reg(l, _)) (Reg(r, _)) -> compare l r) +let regs_string reglist = regs_sort reglist |> List.map (reg_string) |> String.concat "," diff --git a/reg.mli b/reg.mli index f5e41a0..6e81259 100644 --- a/reg.mli +++ b/reg.mli @@ -22,6 +22,8 @@ val regs_get : reg list -> regidx -> regval * or creates it to the value specified if it does not exist *) val regs_set : reg list -> regidx -> regval -> reg list +(* Sorts a list of registers in ascending index order *) +val regs_sort : reg list -> reg list + (* Returns the string representation of a register list. *) val regs_string : reg list -> string - diff --git a/urm_test.ml b/urm_test.ml index bc3a0b8..f223bf6 100644 --- a/urm_test.ml +++ b/urm_test.ml @@ -6,10 +6,9 @@ open Common open Instptr open Urm +open Reg open Kaputt.Abbreviations -let sort_regs = List.sort (fun (Reg(l, _)) (Reg(r, _)) -> compare l r) - let () = Test.add_simple_test ~title:"example_urm_add_program" @@ -54,7 +53,7 @@ let () = in let output_regs = urm_run output_prgm in Assert.is_true (output_prgm = expected_urm); - Assert.is_true ((sort_regs output_regs) = expected_output)) + Assert.is_true ((regs_sort output_regs) = expected_output)) let () = Test.add_simple_test @@ -146,7 +145,7 @@ let () = in let output_regs = urm_run output_prgm in Assert.is_true (output_prgm = expected_urm); - Assert.is_true ((sort_regs output_regs) = expected_output)) + Assert.is_true ((regs_sort output_regs) = expected_output)) let () = if Array.mem "run-tests" Sys.argv then Test.launch_tests () -- cgit v1.2.3