Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Turned exercise 040 into practice #2283

Merged
merged 1 commit into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions practice/040/answer/impl.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
let is_prime n =
let n = abs n in
let rec is_not_divisor d =
d * d > n || (n mod d <> 0 && is_not_divisor (d + 1)) in
n <> 1 && is_not_divisor 2

let goldbach n =
let rec aux d =
if is_prime d && is_prime (n - d) then (d, n - d)
else aux (d + 1)
in aux 2
2 changes: 2 additions & 0 deletions practice/040/answer/test/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
(include_subdirs no)
(test (name run) (libraries ounit2 ex))
3 changes: 3 additions & 0 deletions practice/040/answer/test/run.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module Test = Ex.Make(Ex.Answer)

let () = OUnit2.run_test_tt_main Test.v
2 changes: 2 additions & 0 deletions practice/040/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
(include_subdirs qualified)
(library (name ex) (libraries ounit2))
1 change: 1 addition & 0 deletions practice/040/dune-project
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(lang dune 3.7)
21 changes: 21 additions & 0 deletions practice/040/ex.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
open OUnit2

module type Testable = sig
val goldbach : int -> int * int
end

module Make(Tested: Testable) : sig val v : test end = struct
let tests = "Goldbach's conjecture" >::: [
"goldbach of 4" >:: (fun _ ->
assert_equal (2, 2) (Tested.goldbach 4));
"goldbach of 28" >:: (fun _ ->
assert_equal (5, 23) (Tested.goldbach 28));
]

let v = "Tests for Goldbach's Conjecture" >::: [
tests
]
end

module Work : Testable = Work.Impl
module Answer : Testable = Answer.Impl
1 change: 1 addition & 0 deletions practice/040/work/impl.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let goldbach _ = failwith "goldbach not implemented"
2 changes: 2 additions & 0 deletions practice/040/work/test/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
(include_subdirs no)
(test (name run) (libraries ounit2 ex))
3 changes: 3 additions & 0 deletions practice/040/work/test/run.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module Test = Ex.Make(Ex.Work)

let () = OUnit2.run_test_tt_main Test.v