forked from model-checking/kani
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A few output improvements (model-checking#976)
* Disable reach assertions in visualize mode * Make file location relative to current dir * Add unit tests
- Loading branch information
Showing
10 changed files
with
191 additions
and
3 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# SPDX-License-Identifier: Apache-2.0 OR MIT | ||
import unittest | ||
import os | ||
import tempfile | ||
from cbmc_json_parser import SourceLocation | ||
|
||
|
||
def source_json(filename=None, function=None, line=None, column=None): | ||
result = dict() | ||
if filename: | ||
result["file"] = filename | ||
if function: | ||
result["function"] = function | ||
if column: | ||
result["column"] = column | ||
if line: | ||
result["line"] = line | ||
return result | ||
|
||
|
||
class SourceLocationTest(unittest.TestCase): | ||
""" Unit tests for SourceLocation """ | ||
|
||
def test_source_location_valid_path(self): | ||
"""Path returned by filepath() works for valid paths | ||
Note: Check is loose because I couldn't find a reliable way to control a real path location. | ||
""" | ||
path = tempfile.gettempdir() | ||
json = source_json(path) | ||
src_loc = SourceLocation(json) | ||
possible_output = {path, os.path.relpath(path), os.path.relpath(path, os.path.expanduser("~"))} | ||
self.assertIn(src_loc.filepath(), possible_output) | ||
|
||
def test_source_location_invalid_path(self): | ||
"""Path returned by filepath() returns the input path if it doesn't exist""" | ||
path = "/rust/made/up/path/lib.rs" | ||
json = source_json(path) | ||
src_loc = SourceLocation(json) | ||
self.assertEqual(src_loc.filepath(), path) | ||
|
||
def test_source_location_relative_path(self): | ||
"""Path returned by filepath() is relative if the input is also relative""" | ||
relpath = "dummy/target.rs" | ||
json = source_json(relpath) | ||
src_loc = SourceLocation(json) | ||
self.assertEqual(src_loc.filepath(), relpath) | ||
|
||
def test_source_location_absolute_cwd_path(self): | ||
"""Path returned by filepath() is relative to current directory | ||
Note that the file may not exist that this should still hold. | ||
""" | ||
relpath = "dummy/target.rs" | ||
path = os.path.join(os.getcwd(), relpath) | ||
self.assertNotEqual(relpath, path) | ||
|
||
json = source_json(path) | ||
src_loc = SourceLocation(json) | ||
self.assertEqual(src_loc.filepath(), relpath) | ||
|
||
def test_source_location_absolute_user_path(self): | ||
"""Path returned by filepath() is relative to current directory | ||
Note that the file may not exist that this should still hold. | ||
""" | ||
relpath = "~/dummy/target.rs" | ||
path = os.path.expanduser(relpath) | ||
self.assertNotEqual(relpath, path) | ||
|
||
json = source_json(path) | ||
src_loc = SourceLocation(json) | ||
self.assertEqual(src_loc.filepath(), relpath) | ||
|
||
def test_source_location_relative_user_path(self): | ||
"""Path returned by filepath() is relative to current directory | ||
Note that the file may not exist that this should still hold. | ||
""" | ||
relpath = "~/dummy/target.rs" | ||
json = source_json(relpath) | ||
src_loc = SourceLocation(json) | ||
self.assertEqual(src_loc.filepath(), relpath) | ||
|
||
def test_source_location_with_no_path(self): | ||
"""Function filepath() is None if no file is given in the input""" | ||
json = source_json(function="main") | ||
src_loc = SourceLocation(json) | ||
self.assertIsNone(src_loc.filepath()) | ||
|
||
def test_source_location_output_format(self): | ||
"""Check that output includes all the information provided""" | ||
path = "/rust/made/up/path/lib.rs" | ||
function = "harness" | ||
line = 10 | ||
column = 8 | ||
json = source_json(path, function, line, column) | ||
src_loc = str(SourceLocation(json)) | ||
self.assertIn(path, src_loc) | ||
self.assertIn(f"{path}:{line}:{column}", src_loc) | ||
self.assertIn(function, src_loc) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module/mod.rs:10:5 in function module::not_empty | ||
main.rs:13:5 in function same_file | ||
/toolchains/ | ||
alloc/src/vec/mod.rs:2821:81 in function <std::vec::Vec<T, A> as std::ops::Drop>::drop | ||
|
||
VERIFICATION:- SUCCESSFUL | ||
|
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,25 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
// | ||
// kani-flags: --function harness | ||
// | ||
//! This test is to check how file names are displayed in the Kani output. | ||
mod module; | ||
|
||
use module::not_empty; | ||
|
||
fn same_file(cond: bool) { | ||
assert!(cond); | ||
} | ||
|
||
#[kani::proof] | ||
fn harness() { | ||
let x = true; | ||
same_file(x); | ||
|
||
let v = vec![1]; | ||
not_empty(&v); | ||
} | ||
|
||
fn main() {} |
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,2 @@ | ||
VERIFICATION:- SUCCESSFUL | ||
|
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,16 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
||
// kani-flags: --function empty_harness | ||
|
||
// This file is to be used as a module on a different test, but the compiletest will still run | ||
// kani on this file. Use an empty harness instead. | ||
|
||
pub fn not_empty(v: &[i32]) { | ||
assert!(!v.is_empty()); | ||
} | ||
|
||
#[kani::proof] | ||
fn empty_harness() { | ||
// No-op to overcome compile test. | ||
} |