From 2706214e1f9ba8c62cea26055dcc9aa46fcd0c3d Mon Sep 17 00:00:00 2001 From: pacien Date: Tue, 1 May 2018 01:29:33 +0200 Subject: Add new tests --- src/eurm_test.ml | 94 +++++++++++--------------------- src/main.mli | 6 +++ src/makefile | 7 +-- src/urm_test.ml | 160 ++++++++++--------------------------------------------- 4 files changed, 69 insertions(+), 198 deletions(-) create mode 100644 src/main.mli diff --git a/src/eurm_test.ml b/src/eurm_test.ml index a3ef5a8..a86a1b2 100644 --- a/src/eurm_test.ml +++ b/src/eurm_test.ml @@ -4,73 +4,43 @@ *) open Common +open Main +open Parser open Urm open Eurm -open Kaputt.Abbreviations +open Reg +open Kaputt + +let run_eurm_prgm filename regs = + let prgm = read_prgm (fun f -> eurm_program_of_lex f |> urm_from_eurm) filename + in urm_mk prgm regs |> urm_run + +let () = + Test.add_simple_test + ~title:"example_eurm_add" + (fun () -> + let output = run_eurm_prgm "../examples/add.eurm" [Reg(1, 5); Reg(2, 3)] + in Assertion.equal (regs_get output 1) (5 + 3)) + +(*let () = + Test.add_simple_test + ~title:"example_eurm_factorial" + (fun () -> + let output = run_eurm_prgm "../examples/factorial.eurm" [Reg(1, 5)] + in Assertion.equal (regs_get output 1) 120) +*) +let () = + Test.add_simple_test + ~title:"example_eurm_sum_first_integers" + (fun () -> + let output = run_eurm_prgm "../examples/sum-first-integers.eurm" [Reg(1, 51)] + in Assertion.equal (regs_get output 1) (51*(51+1)/2)) -(* let () = Test.add_simple_test - ~title:"example_eurm_factorial_conversion" + ~title:"example_eurm_sum_first_odd_integers" (fun () -> - let input_eurm = [ - Comment "Compute r1! and place the result in r1"; - ZeroPredicate (1, "r1=0"); - Goto "r1>0"; - Comment "r1 holds 0"; - Label "r1=0"; - Inc 1; - Goto "done"; - Comment "r1 holds a positive integer"; - Label "r1>0"; - Copy (2, 1); - Zero 1; - Inc 1; - Zero 3; - Inc 3; - Comment "main loop"; - Label "loop"; - Mult (1, 3); - EqPredicate (2, 3, "done"); - Inc 3; - Goto "loop"; - Label "done"; - Quit] - and expected_urm = [ - URMZero 4; - URMJump (1, 4, 4); - URMZero 8; - URMJump (8, 8, 7); - URMSucc 1; - URMZero 9; - URMJump (9, 9, 29); - URMCopy (2, 1); - URMZero 1; - URMSucc 1; - URMZero 3; - URMSucc 3; - URMCopy (5, 1); - URMZero 1; - URMZero 6; - URMJump (3, 6, 25); - URMZero 7; - URMJump (5, 7, 22); - URMSucc 1; - URMSucc 7; - URMZero 10; - URMJump (10, 10, 17); - URMSucc 6; - URMZero 11; - URMJump (11, 11, 15); - URMJump (2, 3, 29); - URMSucc 3; - URMZero 12; - URMJump (12, 12, 12); - URMZero 13; - URMJump (13, 13, 38)] - in let output_urm = urm_from_eurm input_eurm - in - Assert.is_true (output_urm = expected_urm)) + let output = run_eurm_prgm "../examples/sum-first-odd-integers.eurm" [Reg(1, 51)] + in Assertion.equal (regs_get output 1) 676) let () = if Array.mem "run-tests" Sys.argv then Test.launch_tests () - *) diff --git a/src/main.mli b/src/main.mli new file mode 100644 index 0000000..73ba84e --- /dev/null +++ b/src/main.mli @@ -0,0 +1,6 @@ +(* + * UPEM / L3 / Functional programming / Project: URM + * Pacien TRAN-GIRARD, Adam NAILI + *) + +val read_prgm : (string list -> 'a list) -> string -> 'a list diff --git a/src/makefile b/src/makefile index 6aceab8..f061b74 100644 --- a/src/makefile +++ b/src/makefile @@ -6,9 +6,10 @@ SOURCES = \ parser.mli parser.ml \ instptr.mli instptr.ml \ reg.mli reg.ml \ - urm.mli urm.ml urm_test.ml \ - eurm.mli eurm.ml eurm_test.ml \ - main.ml + urm.mli urm.ml \ + eurm.mli eurm.ml \ + main.mli main.ml \ + urm_test.ml eurm_test.ml OCAMLMAKEFILE = /usr/share/ocamlmakefile/OCamlMakefile include $(OCAMLMAKEFILE) diff --git a/src/urm_test.ml b/src/urm_test.ml index bf620e0..c64da3b 100644 --- a/src/urm_test.ml +++ b/src/urm_test.ml @@ -4,148 +4,42 @@ *) open Common -open Instptr +open Main +open Parser open Urm open Reg -open Kaputt.Abbreviations +open Kaputt + +let run_urm_prgm filename regs = + let prgm = read_prgm (urm_program_of_lex) filename + in urm_mk prgm regs |> urm_run let () = Test.add_simple_test - ~title:"example_urm_add_program" + ~title:"example_urm_add" (fun () -> - let input_prgm = [ - URMZero 0; - URMZero 3; - URMJump (1, 3, 6); - URMSucc 0; - URMSucc 3; - URMJump (3, 3, 2); - URMZero 3; - URMJump (2, 3, 11); - URMSucc 0; - URMSucc 3; - URMJump (3, 3, 7)] - and input_regs = [ - Reg (1, 2); - Reg (2, 3)] - and expected_urm = { - instptr = InstPtr ([], [ - (0, URMZero 0); - (1, URMZero 3); - (2, URMJump (1, 3, 6)); - (3, URMSucc 0); - (4, URMSucc 3); - (5, URMJump (3, 3, 2)); - (6, URMZero 3); - (7, URMJump (2, 3, 11)); - (8, URMSucc 0); - (9, URMSucc 3); - (10, URMJump (3, 3, 7))]); - regs = [ - Reg (1, 2); - Reg (2, 3)]} - and expected_output = [ - Reg (0, 5); - Reg (1, 2); - Reg (2, 3); - Reg (3, 3)] - in let output_prgm = urm_mk input_prgm input_regs - in let output_regs = urm_run output_prgm - in - Assert.is_true (output_prgm = expected_urm); - Assert.is_true ((regs_sort output_regs) = expected_output)) + let output = run_urm_prgm "../examples/add.urm" [Reg(1, 5); Reg(2, 3)] + in Assertion.equal (regs_get output 1) (5 + 3)) let () = Test.add_simple_test - ~title:"example_urm_factorial_program" + ~title:"example_urm_factorial" (fun () -> - let input_prgm = [ - URMZero 4; - URMJump (1, 4, 4); - URMZero 8; - URMJump (8, 8, 7); - URMSucc 1; - URMZero 9; - URMJump (9, 9, 29); - URMCopy (2, 1); - URMZero 1; - URMSucc 1; - URMZero 3; - URMSucc 3; - URMCopy (5, 1); - URMZero 1; - URMZero 6; - URMJump (3, 6, 25); - URMZero 7; - URMJump (5, 7, 22); - URMSucc 1; - URMSucc 7; - URMZero 10; - URMJump (10, 10, 17); - URMSucc 6; - URMZero 11; - URMJump (11, 11, 15); - URMJump (2, 3, 29); - URMSucc 3; - URMZero 12; - URMJump (12, 12, 12); - URMZero 13; - URMJump (13, 13, 38)] - and input_regs = [ - Reg (1, 5)] - and expected_urm = { - instptr = InstPtr ([], [ - (0, URMZero 4); - (1, URMJump (1, 4, 4)); - (2, URMZero 8); - (3, URMJump (8, 8, 7)); - (4, URMSucc 1); - (5, URMZero 9); - (6, URMJump (9, 9, 29)); - (7, URMCopy (2, 1)); - (8, URMZero 1); - (9, URMSucc 1); - (10, URMZero 3); - (11, URMSucc 3); - (12, URMCopy (5, 1)); - (13, URMZero 1); - (14, URMZero 6); - (15, URMJump (3, 6, 25)); - (16, URMZero 7); - (17, URMJump (5, 7, 22)); - (18, URMSucc 1); - (19, URMSucc 7); - (20, URMZero 10); - (21, URMJump (10, 10, 17)); - (22, URMSucc 6); - (23, URMZero 11); - (24, URMJump (11, 11, 15)); - (25, URMJump (2, 3, 29)); - (26, URMSucc 3); - (27, URMZero 12); - (28, URMJump (12, 12, 12)); - (29, URMZero 13); - (30, URMJump (13, 13, 38))]); - regs = [ - Reg (1, 5)]} - and expected_output = [ - Reg (1, 120); - Reg (2, 5); - Reg (3, 5); - Reg (4, 0); - Reg (5, 24); - Reg (6, 5); - Reg (7, 24); - Reg (8, 0); - Reg (10, 0); - Reg (11, 0); - Reg (12, 0); - Reg (13, 0)] - in let output_prgm = urm_mk input_prgm input_regs - in let output_regs = urm_run output_prgm - in - Assert.is_true (output_prgm = expected_urm); - Assert.is_true ((regs_sort output_regs) = expected_output)) + let output = run_urm_prgm "../examples/factorial.urm" [Reg(1, 5)] + in Assertion.equal (regs_get output 1) 120) -let () = if Array.mem "run-tests" Sys.argv then Test.launch_tests () +let () = + Test.add_simple_test + ~title:"example_urm_sum_first_integers" + (fun () -> + let output = run_urm_prgm "../examples/sum-first-integers.urm" [Reg(1, 51)] + in Assertion.equal (regs_get output 1) (51*(51+1)/2)) +let () = + Test.add_simple_test + ~title:"example_urm_sum_first_odd_integers" + (fun () -> + let output = run_urm_prgm "../examples/sum-first-odd-integers.urm" [Reg(1, 51)] + in Assertion.equal (regs_get output 1) 676) + +let () = if Array.mem "run-tests" Sys.argv then Test.launch_tests () -- cgit v1.2.3