def main():
    for test in glob("tests/*[!*.a]"):
        print(f"Running {test}")
        with open(test, "r") as test_fh:
            test_input_buffer_size, test_input_n_requests = [
                int(val) for val in test_fh.readline().strip().split()
            ]
            test_input_requests = []
            for line in test_fh:
                arrived_at, time_to_process = [
                    int(val) for val in line.strip().split()
                ]
                test_input_requests.append(Request(arrived_at,
                                                   time_to_process))

        with open(f"{test}.a") as test_answer_fh:
            test_answer = " ".join(
                [str(val.strip()) for val in test_answer_fh.readlines()])

        try:
            buffer = Buffer(test_input_buffer_size)
            responses = process_requests(test_input_requests, buffer)
            test_output = " ".join([
                "-1" if response.was_dropped else str(response.started_at)
                for response in responses
            ])
            assert test_output == test_answer
        except AssertionError:
            print(
                f"AssertionError at {test}:\n    buffer size: {test_input_buffer_size}\n    n_requests:  {test_input_n_requests}\n    requests: {test_input_requests}\n    expected output: {test_answer}\n    actual output: {test_output}"
            )
            break
Esempio n. 2
0
 def test_six_requests_as_0_2_and_1_2_and_2_2_and_3_2_and_4_2_and_5_2(self):
     requests = [ Request(0, 2), Request(1, 2), Request(2, 2),
         Request(3, 2), Request(4, 2), Request(5, 2) ]
     responses = process_requests(requests, self.buffer)
     assert_responses(self,
         [ Response(False, 0), Response(False, 2), Response(False, 4),
             Response(False, 6), Response(False, 8), Response(True, -1) ],
         responses)
Esempio n. 3
0
def test_process_packages(request_response_generators):
    incoming_requests, expected_responses = request_response_generators
    buffer_size, n_requests = next(incoming_requests)

    requests = []
    for _ in range(n_requests):
        arrived_at, time_to_process = next(incoming_requests)
        requests.append(Request(arrived_at, time_to_process))

    buffer = Buffer(buffer_size)
    responses = process_requests(requests, buffer)

    assert responses == expected_responses
    def test_main(self):
        test_data = test_runner('./tests')
        for test in test_data:
            data, result, file = test

            buffer_size, n_requests = map(int, data[0].split())
            requests = []

            for i in range(n_requests):
                arrived_at, time_to_process = map(int, data[i + 1].split())
                requests.append(Request(arrived_at, time_to_process))

            buffer = Buffer(buffer_size)
            responses = list(
                map(lambda x: x.started_at, process_requests(requests,
                                                             buffer)))
            expected = list(map(int, result))
            print('file', file)
            self.assertCountEqual(responses, expected)
def check(stem):
    requests = []
    exp_responses = []
    buffer_size = 0
    with Path('./tests/{}.a'.format(stem)).open() as answer_file, \
            Path('./tests/{}'.format(stem)).open() as test_case_file:
        buffer_size, n_requests = map(int, test_case_file.readline().split())
        for _ in range(n_requests):
            arrived_at, time_to_process = map(
                int,
                test_case_file.readline().split())
            requests.append(Request(arrived_at, time_to_process))
        for _ in range(n_requests):
            exp_responses.append(int(answer_file.readline()))

    b = Buffer(buffer_size)
    responses = process_requests(requests, b)
    i_responses = list(
        map(lambda r: -1 if r.dropped else r.started_at, responses))
    if exp_responses == i_responses:
        print('.')
    else:
        print('F act={} exp={}'.format(i_responses, exp_responses))
Esempio n. 6
0
    def test(self):
        test_dir = 'tests'
        files = [f for f in listdir(test_dir) if isfile(join(test_dir, f))]
        for i in range(1, len(files) // 2 + 1):
            query_file_name = 'tests/' + str(i).zfill(2)
            answer_file_name = query_file_name + '.a'

            query_stream = None
            answer_stream = None

            try:
                query_stream = open(query_file_name, 'r')
                answer_stream = open(answer_file_name, 'r')
                answer_text = answer_stream.read().strip()

                size, count = map(int, query_stream.readline().strip().split())
                requests = read_requests(count, query_stream)

                print('Doing Test: ' + str(i))
                buffer = Buffer(size)
                responses = process_requests(requests, buffer)
                result_text = ''
                for i, response in enumerate(responses):
                    result_text += str(
                        (response.start_time if not response.dropped else -1))
                    if i != len(responses) - 1:
                        result_text += os.linesep

                self.assertEqual(answer_text, result_text)
            except AssertionError as e:
                print(query_file_name + ' fails: ' + str(e))
            finally:
                if query_stream is not None:
                    query_stream.close()
                if answer_stream is not None:
                    answer_stream.close()
Esempio n. 7
0
 def test_preceeding_lower_bound_of_process_time_among_requests(self):
     buffer = Buffer(1)
     requests = [ Request(0, 0), Request(0, -1), Request(0, 0) ]
     with self.assertRaisesRegex(AssertionError, ''):
         process_requests(requests, buffer)
Esempio n. 8
0
 def test_preceeding_lower_bound_of_arrival_time(self):
     buffer = Buffer(1)
     requests = [ Request(-1, 0) ]
     with self.assertRaisesRegex(AssertionError, ''):
         process_requests(requests, buffer)
Esempio n. 9
0
 def test_buffer_size_as_zero(self):
     buffer = Buffer(0)
     requests = []
     with self.assertRaisesRegex(AssertionError, ''):
         process_requests(requests, buffer)
Esempio n. 10
0
 def test_three_requests_as_0_1_and_3_1_and_10_1(self):
     requests = [ Request(0, 1), Request(3, 1), Request(10, 1) ]
     responses = process_requests(requests, self.buffer)
     assert_responses(self,
         [ Response(False, 0), Response(False, 3), Response(False, 10) ],
         responses)
Esempio n. 11
0
 def test_two_equal_requests_as_0_1(self):
     requests = [ Request(0, 1), Request(0, 1) ]
     responses = process_requests(requests, self.buffer)
     assert_responses(self,
         [ Response(False, 0), Response(True, -1) ],
         responses)
Esempio n. 12
0
 def test_three_requests_as_0_1_and_1_3_and_4_2(self):
     requests = [ Request(0, 1), Request(1, 3), Request(4, 2) ]
     responses = process_requests(requests, self.buffer)
     assert_responses(self,
         [ Response(False, 0), Response(False, 1), Response(False, 4) ],
         responses)
Esempio n. 13
0
 def test_two_requests_as_0_1_and_2_1(self):
     requests = [ Request(0, 1), Request(2, 1) ]
     responses = process_requests(requests, self.buffer)
     assert_responses(self,
         [ Response(False, 0), Response(False, 2) ],
         responses)
Esempio n. 14
0
q_files = sorted([f for f in all_files if not 'a' in f])
a_files = sorted([f for f in all_files if 'a' in f])

counter = 1

for q, a in zip(q_files[11:12], a_files[11:12]):
    with open(q, 'r') as f:
        buffer_size, n_requests = map(int, f.readline().split())

        requests = []
        for line in f:
            # f does not contain the first line because the previous f.readline().
            arrived_at, time_to_process = map(int, line.split())
            requests.append(Request(arrived_at, time_to_process))

    with open(a, 'r') as f:
        start_time_truth = []

        for line in f:
            start_time_truth.append(int(line))

    net_buffer = Buffer(buffer_size)

    my_result = process_requests(requests, net_buffer)
    my_result = [r.started_at for r in my_result]

    assert my_result == start_time_truth, 'fail case {}\n my result {}\n truth {}'.format(
        counter, my_result, start_time_truth)

    counter += 1
Esempio n. 15
0
 def test_misordered_arrival_times(self):
     buffer = Buffer(1)
     requests = [ Request(1, 0), Request(0, 0), Request(2, 0) ]
     with self.assertRaisesRegex(AssertionError, ''):
         process_requests(requests, buffer)
Esempio n. 16
0
 def test_one_request_as_1_1(self):
     requests = [ Request(1, 1) ]
     responses = process_requests(requests, self.buffer)
     assert_responses(self, [ Response(False, 1) ], responses)
Esempio n. 17
0
def assert_no_requests_and_buffer_size_as(unittest_self, size):
    buffer = Buffer(size)
    requests = []
    responses = process_requests(requests, buffer)
    unittest_self.assertEqual([], responses)
Esempio n. 18
0
 def test_three_requests_as_0_2_and_1_4_and_5_3(self):
     requests = [ Request(0, 2), Request(1, 4), Request(5, 3) ]
     responses = process_requests(requests, self.buffer)
     assert_responses(self,
         [ Response(False, 0), Response(False, 2), Response(False, 6) ],
         responses)
Esempio n. 19
0
        for i in range(n_requests):
            arrival_time, process_time = map(int, f.readline().strip().split())
            requests.append(
                process_packages.Request(arrival_time, process_time))
        tests.append((buffer_size, n_requests, requests))

    else:
        # Answer file
        responses = []
        for line in f:
            responses.append(int(line.strip()))
        answers.append(responses)

    f.close()

for i, test in enumerate(tests):

    buffer_size, n_requests, requests = test
    buffer = process_packages.Buffer(buffer_size)
    responses = process_packages.process_requests(requests, buffer)

    output_response = []
    for response in responses:
        output_response.append(
            response.started_at if not response.was_dropped else -1)

    if output_response != answers[i]:
        print("Test %d failed." % (i + 1))
    else:
        print("Test %d OK." % (i + 1))