From 0eb1825af2114c60792dc5cbd43ca1259ae95a24 Mon Sep 17 00:00:00 2001 From: RaymondDashWu <33266041+RaymondDashWu@users.noreply.github.com> Date: Fri, 27 Oct 2023 13:13:32 -0700 Subject: [PATCH] Tests for odd_even_transposition_parallel (#10926) * [ADD] tests for odd_even_transposition_parallel * adding another test because build failed 6 hrs * comment out all tests to see if it fails * list(range(10)[::-1]) test uncommented * [a, x, c] test uncommented * [1.9, 42.0, 2.8] test uncommented * [False, True, False] test uncommented * [1, 32.0, 9] test uncommented * [1, 32.0, 9] test uncommented * [-442, -98, -554, 266, -491, 985, -53, -529, 82, -429] test uncommented * test non global lock * [DEL] Testing multiple data types. Couldn't get doctest to work * [ADD] Comment on why non global process lock --- sorts/odd_even_transposition_parallel.py | 25 ++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/sorts/odd_even_transposition_parallel.py b/sorts/odd_even_transposition_parallel.py index 9e0d228bd..b8ab46df1 100644 --- a/sorts/odd_even_transposition_parallel.py +++ b/sorts/odd_even_transposition_parallel.py @@ -13,7 +13,8 @@ synchronization could be used. from multiprocessing import Lock, Pipe, Process # lock used to ensure that two processes do not access a pipe at the same time -process_lock = Lock() +# NOTE This breaks testing on build runner. May work better locally +# process_lock = Lock() """ The function run by the processes that sorts the list @@ -28,7 +29,7 @@ resultPipe = the pipe used to send results back to main def oe_process(position, value, l_send, r_send, lr_cv, rr_cv, result_pipe): - global process_lock + process_lock = Lock() # we perform n swaps since after n swaps we know we are sorted # we *could* stop early if we are sorted already, but it takes as long to @@ -72,6 +73,26 @@ arr = the list to be sorted def odd_even_transposition(arr): + """ + >>> odd_even_transposition(list(range(10)[::-1])) == sorted(list(range(10)[::-1])) + True + >>> odd_even_transposition(["a", "x", "c"]) == sorted(["x", "a", "c"]) + True + >>> odd_even_transposition([1.9, 42.0, 2.8]) == sorted([1.9, 42.0, 2.8]) + True + >>> odd_even_transposition([False, True, False]) == sorted([False, False, True]) + True + >>> odd_even_transposition([1, 32.0, 9]) == sorted([False, False, True]) + False + >>> odd_even_transposition([1, 32.0, 9]) == sorted([1.0, 32, 9.0]) + True + >>> unsorted_list = [-442, -98, -554, 266, -491, 985, -53, -529, 82, -429] + >>> odd_even_transposition(unsorted_list) == sorted(unsorted_list) + True + >>> unsorted_list = [-442, -98, -554, 266, -491, 985, -53, -529, 82, -429] + >>> odd_even_transposition(unsorted_list) == sorted(unsorted_list + [1]) + False + """ process_array_ = [] result_pipe = [] # initialize the list of pipes where the values will be retrieved