From 6f132c0342ab7aa438ed88d7c5f987cb52d8ca05 Mon Sep 17 00:00:00 2001 From: Scott Gasch Date: Thu, 9 Sep 2021 16:42:13 -0700 Subject: Update tests / test harness. --- string_utils.py | 20 ++++---- tests/parallelize_itest.py | 85 +++++++++++++++++++++++++++++++++ tests/parallelize_test.py | 85 --------------------------------- tests/run_all_tests.sh | 13 ----- tests/run_tests.sh | 116 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 212 insertions(+), 107 deletions(-) create mode 100755 tests/parallelize_itest.py delete mode 100755 tests/parallelize_test.py delete mode 100755 tests/run_all_tests.sh create mode 100755 tests/run_tests.sh diff --git a/string_utils.py b/string_utils.py index 5eb03d2..0829846 100644 --- a/string_utils.py +++ b/string_utils.py @@ -28,7 +28,7 @@ URLS_RAW_STRING = ( r"([a-z-]+://)" # scheme r"([a-z_\d-]+:[a-z_\d-]+@)?" # user:password r"(www\.)?" # www. - r"((? bool: True >>> is_none_or_empty(None) True - >>> is_none_or_empty(" ") + >>> is_none_or_empty(" \t ") True >>> is_none_or_empty('Test') False @@ -175,18 +175,22 @@ def is_string(obj: Any) -> bool: def is_empty_string(in_str: Any) -> bool: + return is_empty(in_str) + + +def is_empty(in_str: Any) -> bool: """ Checks if input is a string and empty or only whitespace. - >>> is_empty_string('') + >>> is_empty('') True - >>> is_empty_string(' \t\t ') + >>> is_empty(' \t\t ') True - >>> is_empty_string('test') + >>> is_empty('test') False - >>> is_empty_string(100.88) + >>> is_empty(100.88) False - >>> is_empty_string([1, 2, 3]) + >>> is_empty([1, 2, 3]) False """ return is_string(in_str) and in_str.strip() == "" @@ -733,8 +737,6 @@ def is_ip(in_str: Any) -> bool: """ Checks if a string is a valid ip (either v4 or v6). - *Examples:* - >>> is_ip('255.200.100.75') True >>> is_ip('2001:db8:85a3:0000:0000:8a2e:370:7334') diff --git a/tests/parallelize_itest.py b/tests/parallelize_itest.py new file mode 100755 index 0000000..9d98710 --- /dev/null +++ b/tests/parallelize_itest.py @@ -0,0 +1,85 @@ +#!/usr/bin/env python3 + +import random +import sys + +import bootstrap +import parallelize as p +import decorator_utils +import executors +import math_utils +import smart_future + + +@p.parallelize(method=p.Method.THREAD) +def compute_factorial_thread(n): + total = 1 + for x in range(2, n): + total *= x + return total + + +@p.parallelize(method=p.Method.PROCESS) +def compute_factorial_process(n): + total = 1 + for x in range(2, n): + total *= x + return total + + +@p.parallelize(method=p.Method.REMOTE) +def list_primes(n): + """Calculates sum of all primes below given integer n""" + ret = [] + for x in range(2, n): + ret.append(math_utils.is_prime(x)) + return ret + + +@decorator_utils.timed +def test_thread_parallelization() -> None: + results = [] + for _ in range(50): + results.append(compute_factorial_thread(_)) + smart_future.wait_all(results) + for future in results: + print(f'Thread: {future}') + texecutor = executors.DefaultExecutors().thread_pool() + texecutor.shutdown() + + +@decorator_utils.timed +def test_process_parallelization() -> None: + results = [] + for _ in range(50): + results.append(compute_factorial_process(_)) + for future in smart_future.wait_any(results): + print(f'Process: {future}') + pexecutor = executors.DefaultExecutors().process_pool() + pexecutor.shutdown() + + +@decorator_utils.timed +def test_remote_parallelization() -> None: + results = {} + for _ in range(50): + n = random.randint(0, 100000) + results[n] = list_primes(n) + tot = 0 + for _ in results[n]: + tot += _ + print(tot) + rexecutor = executors.DefaultExecutors().remote_pool() + rexecutor.shutdown() + + +@bootstrap.initialize +def main() -> None: + test_thread_parallelization() + test_process_parallelization() + test_remote_parallelization() + sys.exit(0) + + +if __name__ == '__main__': + main() diff --git a/tests/parallelize_test.py b/tests/parallelize_test.py deleted file mode 100755 index 9d98710..0000000 --- a/tests/parallelize_test.py +++ /dev/null @@ -1,85 +0,0 @@ -#!/usr/bin/env python3 - -import random -import sys - -import bootstrap -import parallelize as p -import decorator_utils -import executors -import math_utils -import smart_future - - -@p.parallelize(method=p.Method.THREAD) -def compute_factorial_thread(n): - total = 1 - for x in range(2, n): - total *= x - return total - - -@p.parallelize(method=p.Method.PROCESS) -def compute_factorial_process(n): - total = 1 - for x in range(2, n): - total *= x - return total - - -@p.parallelize(method=p.Method.REMOTE) -def list_primes(n): - """Calculates sum of all primes below given integer n""" - ret = [] - for x in range(2, n): - ret.append(math_utils.is_prime(x)) - return ret - - -@decorator_utils.timed -def test_thread_parallelization() -> None: - results = [] - for _ in range(50): - results.append(compute_factorial_thread(_)) - smart_future.wait_all(results) - for future in results: - print(f'Thread: {future}') - texecutor = executors.DefaultExecutors().thread_pool() - texecutor.shutdown() - - -@decorator_utils.timed -def test_process_parallelization() -> None: - results = [] - for _ in range(50): - results.append(compute_factorial_process(_)) - for future in smart_future.wait_any(results): - print(f'Process: {future}') - pexecutor = executors.DefaultExecutors().process_pool() - pexecutor.shutdown() - - -@decorator_utils.timed -def test_remote_parallelization() -> None: - results = {} - for _ in range(50): - n = random.randint(0, 100000) - results[n] = list_primes(n) - tot = 0 - for _ in results[n]: - tot += _ - print(tot) - rexecutor = executors.DefaultExecutors().remote_pool() - rexecutor.shutdown() - - -@bootstrap.initialize -def main() -> None: - test_thread_parallelization() - test_process_parallelization() - test_remote_parallelization() - sys.exit(0) - - -if __name__ == '__main__': - main() diff --git a/tests/run_all_tests.sh b/tests/run_all_tests.sh deleted file mode 100755 index 25365bb..0000000 --- a/tests/run_all_tests.sh +++ /dev/null @@ -1,13 +0,0 @@ -#!/bin/bash - -for doctest in $(grep -l doctest ../*.py); do - echo "------------------------- ${doctest} -------------------------" - python3 ${doctest} -done - -for test in $(ls *_test.py); do - if [ "${test}" != "parallelize_test.py" ]; then - echo "------------------------- ${test} -------------------------" - ${test} - fi -done diff --git a/tests/run_tests.sh b/tests/run_tests.sh new file mode 100755 index 0000000..6e0c30c --- /dev/null +++ b/tests/run_tests.sh @@ -0,0 +1,116 @@ +#!/bin/bash + +source /home/scott/bin/color_vars.sh + +ROOT=/home/scott/lib/python_modules +DOCTEST=0 +UNITTEST=0 +INTEGRATION=0 +FAILURES=0 + +dup() { + if [ $# -ne 2 ]; then + echo "Usage: dup " + return + fi + local times=$(seq 1 $2) + for x in ${times}; do + echo -n "$1" + done +} + +make_header() { + if [ $# -ne 2 ]; then + echo "Usage: make_header " + return + fi + local title="$1" + local title_len=${#title} + title_len=$((title_len + 4)) + local width=70 + local left=4 + local right=$(($width-($title_len+$left))) + local color="$2" + dup '-' $left + echo -ne "[ ${color}${title}${NC} ]" + dup '-' $right + echo +} + + +while [[ $# -gt 0 ]]; do + key="$1" + case $key in + -a|--all) + DOCTEST=1 + UNITTEST=1 + INTEGRATION=1 + ;; + -d|--doctests) + DOCTEST=1 + ;; + -u|--unittests) + UNITTEST=1 + ;; + -i|--integration) + INTEGRATION=1 + ;; + *) # unknown option + echo "Usage: $0 [-a]|[-i][-u][-d]" + echo + echo "Runs tests under ${ROOT}. Options control which test types:" + echo + echo " -a | --all . . . . . . . . . . . . Run all types of tests" + echo " -d | --doctests . . . . . . . . . Run doctests" + echo " -u | --unittests . . . . . . . . . Run unittests" + echo " -i | --integration . . . . . . . . Run integration tests" + echo + echo "Argument $key was not recognized." + exit 1 + ;; + esac + shift +done + +if [ ${DOCTEST} -eq 1 ]; then + for doctest in $(grep -lR doctest ${ROOT}/*.py); do + BASE=$(basename ${doctest}) + BASE="${BASE} (doctest)" + make_header "${BASE}" "${CYAN}" + OUT=$( python3 ${doctest} 2>&1 ) + if [ "$OUT" == "" ]; then + echo "OK" + else + echo -e "${OUT}" + FAILURES=$((FAILURES+1)) + fi + done +fi + +if [ ${UNITTEST} -eq 1 ]; then + for test in $(find ${ROOT} -name "*_test.py" -print); do + BASE=$(basename ${test}) + BASE="${BASE} (unittest)" + make_header "${BASE}" "${GREEN}" + ${test} + if [ $? -ne 0 ]; then + FAILURES=$((FAILURES+1)) + fi + done +fi + +if [ ${INTEGRATION} -eq 1 ]; then + for test in $(find ${ROOT} -name "*_itest.py" -print); do + BASE=$(basename ${test}) + BASE="${BASE} (integration test)" + make_header "${BASE}" "${ORANGE}" + ${test} + if [ $? -ne 0 ]; then + FAILURES=$((FAILURES+1)) + fi + done +fi + +if [ ${FAILURES} -ne 0 ]; then + echo -e "${RED}There were ${FAILURES} failure(s).${NC}" +fi -- cgit v1.3