Skip to content

Commit

Permalink
autotest: added primitive check #9
Browse files Browse the repository at this point in the history
  • Loading branch information
Astana-Mirza committed Mar 14, 2023
1 parent 75df801 commit d5cbe53
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
31 changes: 31 additions & 0 deletions dataset_gen/src/grader/primitive_check.py
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()))
84 changes: 84 additions & 0 deletions dataset_gen/src/grader/test/primitive_check_test.py
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()

0 comments on commit d5cbe53

Please sign in to comment.