-
Notifications
You must be signed in to change notification settings - Fork 124
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
Add support for unit tests via $[test]
attribute
#928
Draft
Timmmm
wants to merge
2
commits into
rems-project:sail2
Choose a base branch
from
Timmmm:user/timh/unit_tests
base: sail2
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2225,13 +2225,47 @@ let compile_ast env effect_info output_chan c_includes ast = | |
let end_extern_cpp = separate hardline (List.map string [""; "#ifdef __cplusplus"; "}"; "#endif"]) in | ||
let hlhl = hardline ^^ hardline in | ||
|
||
(* TODO: Formatting here isn't quite right. What are the arguments to jump? *) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's explained a bit here https://github.com/fpottier/pprint/blob/767ef4ae0d5de95b21e0d650e8bb752830d75a47/src/PPrint.mli#L163 |
||
let unit_test_functions = string "unit (*const SAIL_TESTS[])(unit) = {" ^^ hardline ^^ jump 2 2 ( | ||
IdSet.fold (fun id acc -> codegen_function_id id ^^ string "," ^^ hardline ^^ acc) ctx.unit_test_ids empty | ||
^^ string "0" ^^ hardline | ||
) ^^ string "};" in | ||
|
||
let unit_test_names = string "const char* const SAIL_TEST_NAMES[] = {" ^^ hardline ^^ jump 2 2 ( | ||
IdSet.fold (fun id acc -> string ("\"" ^ String.escaped (string_of_id id) ^ "\"") ^^ string "," ^^ hardline ^^ acc) ctx.unit_test_ids empty | ||
^^ string "0" ^^ hardline | ||
) ^^ string "};" in | ||
|
||
(* A simple function to run the unit tests. It isn't called from anywhere | ||
by default and you don't need to use it - you can use SAIL_TESTS directly | ||
in your own custom test runner. *) | ||
let model_test = | ||
[ | ||
Printf.sprintf "%svoid model_test()" (static ()); | ||
"{"; | ||
" for (size_t i = 0; SAIL_TESTS[i] != 0 && SAIL_TEST_NAMES[i] != 0; ++i) {"; | ||
" model_init();"; | ||
" printf(\"Testing %s\\n\", SAIL_TEST_NAMES[i]);"; | ||
" SAIL_TESTS[i](UNIT);"; | ||
" printf(\"Pass\\n\");"; | ||
" model_fini();"; | ||
" }"; | ||
"}"; | ||
] | ||
|> List.map string |> separate hardline | ||
in | ||
|
||
Document.to_string | ||
(preamble ^^ hlhl ^^ docs ^^ hlhl | ||
^^ ( if not !opt_no_rts then | ||
model_init ^^ hlhl ^^ model_fini ^^ hlhl ^^ model_pre_exit ^^ hlhl ^^ model_default_main ^^ hlhl | ||
else empty | ||
) | ||
^^ model_main ^^ hardline ^^ end_extern_cpp ^^ hardline | ||
^^ model_main ^^ hlhl | ||
^^ unit_test_functions ^^ hlhl | ||
^^ unit_test_names ^^ hlhl | ||
^^ model_test ^^ hardline | ||
^^ end_extern_cpp ^^ hardline | ||
) | ||
|> output_string output_chan | ||
with Type_error.Type_error (l, err) -> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ok |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
default Order dec | ||
|
||
$[test] | ||
function test_0() -> unit = { | ||
print_endline("test_0"); | ||
} | ||
|
||
$[test] | ||
val test_1 : unit -> unit | ||
|
||
function test_1() = { | ||
print_endline("test_1"); | ||
} | ||
|
||
$[test] | ||
val test_2 : unit -> unit | ||
|
||
function test_2() -> unit = { | ||
print_endline("test_2"); | ||
} | ||
|
||
function main() -> unit = { | ||
print_endline("ok"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
test_bad must have type unit -> unit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
default Order dec | ||
|
||
$[test] | ||
function test_good() -> unit = { | ||
print_endline("test_good"); | ||
} | ||
|
||
$[test] | ||
function test_bad() -> int = 5 | ||
|
||
function main() -> unit = () |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
test_bad must have type unit -> unit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
default Order dec | ||
|
||
$[test] | ||
function test_bad(x : int) -> unit = () | ||
|
||
function main() -> unit = () |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
test_bad must have type unit -> unit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
default Order dec | ||
|
||
$[test] | ||
function test_bad(x : unit, y : unit) -> unit = () | ||
|
||
function main() -> unit = () |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
test_bad must have type unit -> unit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
default Order dec | ||
|
||
$[test] | ||
val test_bad : unit -> int | ||
|
||
function test_bad() = 5 | ||
|
||
function main() -> unit = () |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
test attribute must be on function declaration |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
default Order dec | ||
|
||
val test_bad : unit -> unit | ||
|
||
$[test] | ||
function test_bad() = () | ||
|
||
function main() -> unit = () |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think in general we put these annotations on the
val
type declarations. That's where$[property]
and$[counterexample]
are expected to be.If you have a function with an inline annotation:
it'll get expanded to
with the annotation on both.