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

Create practice folder #2166

Merged
merged 3 commits into from
Mar 12, 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
2 changes: 1 addition & 1 deletion dune
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@
%{target}
(run %{ood_gen} changelog_feed))))))

(data_only_dirs playground data)
(data_only_dirs playground data practice)
1 change: 1 addition & 0 deletions dune-project
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
(river
(>= 0.3))
syndic
ounit
(alcotest :with-test)
(mdx
(and
Expand Down
1 change: 1 addition & 0 deletions ocamlorg.opam
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ depends: [
"hilite" {>= "0.4.0"}
"river" {>= "0.3"}
"syndic"
"ounit"
"alcotest" {with-test}
"mdx" {with-test & >= "1.10.0"}
"olinkcheck"
Expand Down
4 changes: 4 additions & 0 deletions practice/001/answer/impl.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
let rec last = function
| [] -> None
| [ x ] -> Some x
| _ :: t -> last t
2 changes: 2 additions & 0 deletions practice/001/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/001/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/001/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/001/dune-project
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(lang dune 3.7)
16 changes: 16 additions & 0 deletions practice/001/ex.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

open OUnit2

module type Testable = sig
val last : 'a list -> 'a option
end

module Make(Tested: Testable) : sig val v : test end = struct
let v = "last" >::: [
"nil" >:: (fun _ -> assert_equal None (Tested.last []));
"cons" >:: (fun _ -> assert_equal (Some "d") (Tested.last ["a"; "b"; "c"; "d"]));
]
end

module Work : Testable = Work.Impl
module Answer : Testable = Answer.Impl
1 change: 1 addition & 0 deletions practice/001/work/impl.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let last _ = failwith "Not yet implemented"
2 changes: 2 additions & 0 deletions practice/001/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/001/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
4 changes: 4 additions & 0 deletions practice/002/answer/impl.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
let rec last_two = function
| [] | [_] -> None
| [x; y] -> Some (x,y)
| _ :: t -> last_two t
2 changes: 2 additions & 0 deletions practice/002/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/002/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/002/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/002/dune-project
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(lang dune 3.7)
16 changes: 16 additions & 0 deletions practice/002/ex.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

open OUnit2

module type Testable = sig
val last_two : 'a list -> ('a * 'a) option
end

module Make(Tested: Testable) : sig val v : test end = struct
let v = "last_two" >::: [
"nil" >:: (fun _ -> assert_equal None (Tested.last_two ["a"]));
"cons" >:: (fun _ -> assert_equal (Some ("c", "d")) (Tested.last_two ["a"; "b"; "c"; "d"]));
]
end

module Work : Testable = Work.Impl
module Answer : Testable = Answer.Impl
1 change: 1 addition & 0 deletions practice/002/work/impl.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let last_two _ = failwith "Not yet implemented"
2 changes: 2 additions & 0 deletions practice/002/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/002/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
54 changes: 54 additions & 0 deletions practice/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
## Instruction for Learners

`cd` into the folder corresponding to the exercise you are willing to practice.

Run `dune build --root .`, it should compile without an error.

Run `dune test --root . answer`, it should pass, showing that there is a
solution to this exercise.

Hack your solution in the file `work/impl.ml`. Check it compiles by running
`dune build --root . `, check if it behaves as expected by running `dune test
--root . work`.

You are allowed to look at file `ex.ml`, it contains the test cases. That may help.

Don't look at the file `answer/impl.ml` it contains the solution.

## Instruction for Maintnainers

One XXX folder per exercise. Each is a standalone Dune project.

Each folder has this structure
```
XXX
├── answer
│ ├── impl.ml
│ └── test
│ ├── dune
│ └── run.ml
├── dune
├── dune-project
├── ex.ml
└── work
├── impl.ml
└── test
├── dune
└── run.ml
```

File `ex.ml` contains:
- Module type `Testable` the function to be implemented in the exercise
- Functor `Make` containing the test cases

File `answer/impl.ml` contains the reference answer to the exercise

File `work/impl.ml` contains a dummy definition, meant to be replaced
by the learner's solultion.

Files `test` folders should (hopefully) always be the sames