-
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.
- Loading branch information
1 parent
75df801
commit d5cbe53
Showing
2 changed files
with
115 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#! /usr/bin/python3 | ||
# -*- coding: utf-8 -*- | ||
## Primitive check of code validity | ||
|
||
## @brief Ensure that code line contains command with valid syntax | ||
# @param[in] line string with one command | ||
# @return True if line is valid, False otherwise | ||
def _primitive_check_line(line : str) -> bool: | ||
if not line: | ||
return False | ||
tokens = line.split() | ||
if tokens[0] != "move" and tokens[0] != "rotate" and tokens[0] != "translate": | ||
return False | ||
if tokens[0] == "move" and len(tokens) != 5: # move drone_name x y z | ||
return False | ||
if tokens[0] == "rotate" and len(tokens) != 5: # rotate drone_name yaw pitch roll | ||
return False | ||
if tokens[0] == "translate" and len(tokens) != 8: # translate drone_name x y z yaw pitch roll | ||
return False | ||
try: | ||
all(map(float, tokens[2:])) | ||
return True | ||
except ValueError: | ||
return False | ||
|
||
|
||
## @brief Ensure that code is non-empty and contains only commands with valid syntax | ||
# @param[in] code multiline string with user's code | ||
# @return True if code is valid, False otherwise | ||
def primitive_check(code : str) -> bool: | ||
return any(filter(lambda line : _primitive_check_line(line.strip()), code.splitlines())) |
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,84 @@ | ||
#! /usr/bin/python3 | ||
# -*- coding: utf-8 -*- | ||
|
||
## Unit tests for primitive_check | ||
|
||
import sys; sys.path.append("../") | ||
from primitive_check import primitive_check | ||
|
||
empty_code = [ | ||
"", | ||
"\t\t \t\r\n \r \n\r" | ||
] | ||
|
||
wrong_code = [ | ||
"asdasd 19201 sj", | ||
"line1\nline2\nline3", | ||
"move drone_name 42069", | ||
"rotate drone_name 1 2 3 4 5 6", | ||
"move bull sh 1 t" | ||
] | ||
|
||
|
||
correct_code = [ | ||
"move drone 0 0 0", | ||
"move drone 1.34 3.14 346\n move drone 0120 874 1231", | ||
" rotate \t\t drone 1 2 3.5", | ||
"translate drone 1 2 3.5 4 5.76 6 " | ||
] | ||
|
||
|
||
def test_empty_code(): | ||
fail = False | ||
if primitive_check(empty_code[0]): | ||
print("TEST 1 FAILED: empty code (empty_code[0])", file=sys.stderr) | ||
fail = True | ||
if primitive_check(empty_code[1]): | ||
print("TEST 1 FAILED: empty code with whitespaces (empty_code[1])", file=sys.stderr) | ||
fail = True | ||
if not fail: | ||
print("TEST 1 OK") | ||
|
||
|
||
def test_wrong_commands(): | ||
fail = False | ||
if primitive_check(wrong_code[0]): | ||
print("TEST 2 FAILED: wrong code line (wrong_code[0])", file=sys.stderr) | ||
fail = True | ||
if primitive_check(wrong_code[1]): | ||
print("TEST 2 FAILED: several wrong code lines (wrong_code[1])", file=sys.stderr) | ||
fail = True | ||
if primitive_check(wrong_code[2]): | ||
print("TEST 2 FAILED: not enough arguments (wrong_code[2])", file=sys.stderr) | ||
fail = True | ||
if primitive_check(wrong_code[3]): | ||
print("TEST 2 FAILED: too many arguments (wrong_code[3])", file=sys.stderr) | ||
fail = True | ||
if primitive_check(wrong_code[4]): | ||
print("TEST 2 FAILED: wrong arguments' format (wrong_code[4])", file=sys.stderr) | ||
fail = True | ||
if not fail: | ||
print("TEST 2 OK") | ||
|
||
|
||
def test_correct_commands(): | ||
fail = False | ||
if not primitive_check(correct_code[0]): | ||
print("TEST 3 FAILED: move to 0 0 0 (correct_code[0])", file=sys.stderr) | ||
fail = True | ||
if not primitive_check(correct_code[1]): | ||
print("TEST 3 FAILED: 2 moves (correct_code[1])", file=sys.stderr) | ||
fail = True | ||
if not primitive_check(correct_code[2]): | ||
print("TEST 3 FAILED: rotate (correct_code[2])", file=sys.stderr) | ||
fail = True | ||
if not primitive_check(correct_code[3]): | ||
print("TEST 3 FAILED: translate (correct_code[3])", file=sys.stderr) | ||
fail = True | ||
if not fail: | ||
print("TEST 3 OK") | ||
|
||
|
||
test_empty_code() | ||
test_wrong_commands() | ||
test_correct_commands() |