From 862ee6ca32881940cda85da9caea90ea0205ac92 Mon Sep 17 00:00:00 2001 From: sofiiako <19rubisco93@gmail.com> Date: Wed, 17 Nov 2021 15:33:47 +0300 Subject: [PATCH 1/3] Updated tests: placed expected and actual values according to the docs of unittesting and numpy.testing; placed assertions into separate tests where needed. --- .../Broadcasting/tests/test_task.py | 8 +++-- .../tests/test_task.py | 8 +++-- .../Create an Empty Array/tests/test_task.py | 19 ++++++++--- .../Array Basics/Linspace/tests/test_task.py | 17 +++++++--- .../Random Sampling/tests/test_task.py | 12 ++++--- .../Random Shuffle/tests/test_task.py | 6 ++-- .../tests/test_task.py | 2 +- NumPy/Array Basics/Reshape/tests/test_task.py | 6 ++-- .../Boolean Indexing/tests/test_task.py | 10 ++++-- .../Indexing Basics/tests/test_task.py | 6 ++-- .../Integer Array Indexing/tests/test_task.py | 33 ++++++++++++++----- .../Basic Math Functions/tests/test_task.py | 11 ++++--- .../Linear Algebra/tests/test_task.py | 8 +++-- .../tests/test_task.py | 11 +++++-- .../Translate/tests/test_task.py | 12 +++++-- .../Compare with a scalar/tests/test_task.py | 5 ++- .../tests/test_task.py | 2 +- .../Find Unique Values/tests/test_task.py | 20 ++++++++--- .../Find maximum/tests/test_task.py | 2 +- .../Compare Search/Search/tests/test_task.py | 4 ++- .../bincount/tests/test_task.py | 12 ++++--- .../Concatenate and Stack/tests/test_task.py | 6 +++- .../Partial Sort/tests/test_task.py | 10 ++++-- .../Sort/tests/test_task.py | 14 +++++--- .../Transpose 1D/tests/test_task.py | 12 ++++--- .../Transpose/tests/test_task.py | 10 ++++-- requirements.txt | 2 ++ 27 files changed, 191 insertions(+), 77 deletions(-) create mode 100644 requirements.txt diff --git a/NumPy/Array Basics/Broadcasting/tests/test_task.py b/NumPy/Array Basics/Broadcasting/tests/test_task.py index b74e033..b83ae4c 100644 --- a/NumPy/Array Basics/Broadcasting/tests/test_task.py +++ b/NumPy/Array Basics/Broadcasting/tests/test_task.py @@ -4,8 +4,10 @@ class TestCase(unittest.TestCase): - def test_shape(self): - self.assertEqual(x.shape, (10, 1), msg="Wrong shape of x.") - self.assertEqual(w.shape, (3, 4), msg="Wrong shape of w.") + def test_shape_x(self): + self.assertEqual((10, 1), x.shape, msg="Wrong shape of x.") + + def test_shape_w(self): + self.assertEqual((3, 4), w.shape, msg="Wrong shape of w.") diff --git a/NumPy/Array Basics/Create an Array from List/tests/test_task.py b/NumPy/Array Basics/Create an Array from List/tests/test_task.py index 5a481e4..c1354e8 100644 --- a/NumPy/Array Basics/Create an Array from List/tests/test_task.py +++ b/NumPy/Array Basics/Create an Array from List/tests/test_task.py @@ -4,6 +4,8 @@ class TestCase(unittest.TestCase): - def test_add(self): - self.assertEqual(create_array(1, 2).shape, (1, 2), msg="Something is wrong.") - self.assertEqual(create_array(3, 5).shape, (3, 5), msg="Something is wrong.") + def test_one(self): + self.assertEqual((1, 2), create_array(1, 2).shape, msg="Something is wrong.") + + def test_two(self): + self.assertEqual((3, 5), create_array(3, 5).shape, msg="Something is wrong.") diff --git a/NumPy/Array Basics/Create an Empty Array/tests/test_task.py b/NumPy/Array Basics/Create an Empty Array/tests/test_task.py index 8230fc3..25df566 100644 --- a/NumPy/Array Basics/Create an Empty Array/tests/test_task.py +++ b/NumPy/Array Basics/Create an Empty Array/tests/test_task.py @@ -5,10 +5,19 @@ class TestCase(unittest.TestCase): - def test_create_arrays(self): + def test_dtype_ones(self): a, b = create_arrays(2, 3) - self.assertEqual(a.dtype, 'int64', msg="Wrong dtype in the .ones array.") - self.assertEqual(a.shape, (3, 2), msg="Wrong shape of the .ones array.") - self.assertEqual(b.dtype, 'bool', msg="Wrong dtype in the .full array.") - self.assertEqual(b.shape, (3, 2), msg="Wrong shape of the .full array.") + self.assertEqual('int64', a.dtype, msg="Wrong dtype in the .ones array.") + + def test_shape_ones(self): + a, b = create_arrays(2, 3) + self.assertEqual((3, 2), a.shape, msg="Wrong shape of the .ones array.") + + def test_dtype_full(self): + a, b = create_arrays(2, 3) + self.assertEqual('bool', b.dtype, msg="Wrong dtype in the .full array.") + + def test_shape_full(self): + a, b = create_arrays(2, 3) + self.assertEqual((3, 2), b.shape, msg="Wrong shape of the .full array.") diff --git a/NumPy/Array Basics/Linspace/tests/test_task.py b/NumPy/Array Basics/Linspace/tests/test_task.py index 783224d..01fc958 100644 --- a/NumPy/Array Basics/Linspace/tests/test_task.py +++ b/NumPy/Array Basics/Linspace/tests/test_task.py @@ -4,9 +4,18 @@ class TestCase(unittest.TestCase): + def test_linspace(self): + a, b = sine_array(0, 2 * pi, 100) + self.assertEqual(6.28318531, round(a[-1], 8), msg="Ooops!") + def test_sine(self): a, b = sine_array(0, 2 * pi, 100) - self.assertEqual(round(a[-1], 8), 6.28318531, msg="Ooops!") - self.assertAlmostEqual(b[-1], 0, msg="Ooops!") - self.assertEqual(a.shape, (100,), msg="Wrong num in array a.") - self.assertEqual(b.shape, (100,), msg="Wrong num in array b.") + self.assertAlmostEqual(0, b[-1], msg="Ooops!") + + def test_linspace_shape(self): + a, b = sine_array(0, 2 * pi, 100) + self.assertEqual((100,), a.shape, msg="Wrong shape of array a.") + + def test_sine_shape(self): + a, b = sine_array(0, 2 * pi, 100) + self.assertEqual((100,), b.shape, msg="Wrong shape of array b.") diff --git a/NumPy/Array Basics/Random Sampling/tests/test_task.py b/NumPy/Array Basics/Random Sampling/tests/test_task.py index 1b8ffd5..7851a8c 100644 --- a/NumPy/Array Basics/Random Sampling/tests/test_task.py +++ b/NumPy/Array Basics/Random Sampling/tests/test_task.py @@ -5,8 +5,12 @@ class TestCase(unittest.TestCase): - def test_normal(self): - self.assertEqual(s.shape, (1000,), msg="Draw 1000 samples.") - self.assertEqual(round(abs(mu - np.mean(s)), 1), 0.0, msg="Mean should be close to 0.0.") - self.assertEqual(round(abs(sigma - np.std(s, ddof=1)), 1), 0.0, msg="Variance should be close to 0.0.") + def test_draw(self): + self.assertEqual((1000,), s.shape, msg="Draw 1000 samples.") + + def test_mean(self): + self.assertEqual(0.0, round(abs(mu - np.mean(s)), 1), msg="Mean should be close to 0.0.") + + def test_variance(self): + self.assertEqual(0.0, round(abs(sigma - np.std(s, ddof=1)), 1), msg="Variance should be close to 0.0.") diff --git a/NumPy/Array Basics/Random Shuffle/tests/test_task.py b/NumPy/Array Basics/Random Shuffle/tests/test_task.py index 235a3f4..438e9f1 100644 --- a/NumPy/Array Basics/Random Shuffle/tests/test_task.py +++ b/NumPy/Array Basics/Random Shuffle/tests/test_task.py @@ -6,9 +6,9 @@ class TestCase(unittest.TestCase): def test_shape(self): - self.assertEqual(arr.shape, (5, 20), msg="Wrong shape of the array 'arr'.") - self.assertEqual(permuted_2d.shape, (5, 20), msg="Wrong shape of the array 'permuted_2d'.") - self.assertEqual(fully_random.shape, (5, 20), msg="Wrong shape of the array 'fully_random'.") + self.assertEqual((5, 20), arr.shape, msg="Wrong shape of the array 'arr'.") + self.assertEqual((5, 20), permuted_2d.shape, msg="Wrong shape of the array 'permuted_2d'.") + self.assertEqual((5, 20), fully_random.shape, msg="Wrong shape of the array 'fully_random'.") def test_arr(self): for i in arr: diff --git a/NumPy/Array Basics/Reading and Writing Files/tests/test_task.py b/NumPy/Array Basics/Reading and Writing Files/tests/test_task.py index c73a060..ccd61ac 100644 --- a/NumPy/Array Basics/Reading and Writing Files/tests/test_task.py +++ b/NumPy/Array Basics/Reading and Writing Files/tests/test_task.py @@ -7,4 +7,4 @@ # todo: replace this with an actual test class TestCase(unittest.TestCase): def test_add(self): - self.assertEqual(arr.shape, (100, 4), msg="Wrong array shape.") + self.assertEqual((100, 4), arr.shape, msg="Wrong array shape.") diff --git a/NumPy/Array Basics/Reshape/tests/test_task.py b/NumPy/Array Basics/Reshape/tests/test_task.py index 16ee0c6..fad34df 100644 --- a/NumPy/Array Basics/Reshape/tests/test_task.py +++ b/NumPy/Array Basics/Reshape/tests/test_task.py @@ -3,8 +3,10 @@ from task import a, b + class TestCase(unittest.TestCase): - def test_arrays(self): + def test_a(self): np.testing.assert_array_equal(a, np.arange(12, 30, 3), err_msg='Array a is not what we expected.') - np.testing.assert_array_equal(b, a.reshape(2, 3), err_msg='Array b is not what we expected.') + def test_b(self): + np.testing.assert_array_equal(b, a.reshape(2, 3), err_msg='Array b is not what we expected.') diff --git a/NumPy/Array Indexing and Slicing/Boolean Indexing/tests/test_task.py b/NumPy/Array Indexing and Slicing/Boolean Indexing/tests/test_task.py index c2f69d1..9ead359 100644 --- a/NumPy/Array Indexing and Slicing/Boolean Indexing/tests/test_task.py +++ b/NumPy/Array Indexing and Slicing/Boolean Indexing/tests/test_task.py @@ -5,11 +5,17 @@ class TestCase(unittest.TestCase): - def test_add(self): + def test_a(self): test_a = np.arange(20).reshape(2, 2, 5) - test_b = np.array([[True, False], [False, True]]) np.testing.assert_array_equal(a, test_a, err_msg='Array `a` seems off.') + + def test_c(self): + test_a = np.arange(20).reshape(2, 2, 5) + test_b = np.array([[True, False], [False, True]]) np.testing.assert_array_equal(c, test_a[test_b], err_msg='Array `c` seems off.') + + def test_mask(self): + test_b = np.array([[True, False], [False, True]]) np.testing.assert_array_equal(mask, test_b, err_msg='Something wrong with the mask array.') diff --git a/NumPy/Array Indexing and Slicing/Indexing Basics/tests/test_task.py b/NumPy/Array Indexing and Slicing/Indexing Basics/tests/test_task.py index 9a3a151..5a25508 100644 --- a/NumPy/Array Indexing and Slicing/Indexing Basics/tests/test_task.py +++ b/NumPy/Array Indexing and Slicing/Indexing Basics/tests/test_task.py @@ -6,9 +6,11 @@ class TestCase(unittest.TestCase): def test_arrays_shape(self): - self.assertEqual(b.shape, (5, 2), msg="Wrong shape of array b.") + self.assertEqual((5, 2), b.shape, msg="Wrong shape of array b.") def test_array_content(self): - self.assertEqual(a, 19, msg="a has to be equal to 19.") + self.assertEqual(19, a, msg="a has to be equal to 19.") + + def test_array_b(self): np.testing.assert_array_equal(b, x[::2, ::2], err_msg='Something wrong in array b.') diff --git a/NumPy/Array Indexing and Slicing/Integer Array Indexing/tests/test_task.py b/NumPy/Array Indexing and Slicing/Integer Array Indexing/tests/test_task.py index dcf6ddb..db6c34c 100644 --- a/NumPy/Array Indexing and Slicing/Integer Array Indexing/tests/test_task.py +++ b/NumPy/Array Indexing and Slicing/Integer Array Indexing/tests/test_task.py @@ -3,17 +3,34 @@ from task import x, y, a, b, c, d, e + class TestCase(unittest.TestCase): - def test_shape(self): - self.assertEqual(a.shape, (4,), msg='Wrong shape of array a.') - self.assertEqual(b.shape, (3, 3), msg='Wrong shape of array b.') - self.assertEqual(c.shape, (3, 7), msg='Wrong shape of array c.') - self.assertEqual(d.shape, (3,), msg='Wrong shape of array d.') - self.assertEqual(e.shape, (3,), msg='Wrong shape of array e.') - - def test_arrays(self): + def test_shape_a(self): + self.assertEqual((4,), a.shape, msg='Wrong shape of array a.') + + def test_shape_b(self): + self.assertEqual((3, 3), b.shape, msg='Wrong shape of array b.') + + def test_shape_c(self): + self.assertEqual((3, 7), c.shape, msg='Wrong shape of array c.') + + def test_shape_d(self): + self.assertEqual((3,), d.shape, msg='Wrong shape of array d.') + + def test_shape_e(self): + self.assertEqual((3,), e.shape, msg='Wrong shape of array e.') + + def test_array_a(self): np.testing.assert_array_equal(a, x[np.array([7, 13, 28, 33])], err_msg='Array a is not what we expected.') + + def test_array_b(self): np.testing.assert_array_equal(b, x[np.array([[0, 1, 2], [10, 11, 12], [28, 29, 30]])], err_msg='Array b is not what we expected.') + + def test_array_c(self): np.testing.assert_array_equal(c, y[np.array([0, 2, 4])], err_msg='Array c is not what we expected.') + + def test_array_d(self): np.testing.assert_array_equal(d, y[np.array([0, 2, 4]), np.array([0, 1, 2])], err_msg='Array d is not what we expected.') + + def test_array_e(self): np.testing.assert_array_equal(e, y[np.array([1, 2, 4]), 6], err_msg='Array e is not what we expected.') diff --git a/NumPy/Array Math/Basic Math Functions/tests/test_task.py b/NumPy/Array Math/Basic Math Functions/tests/test_task.py index c5878c7..f163a7f 100644 --- a/NumPy/Array Math/Basic Math Functions/tests/test_task.py +++ b/NumPy/Array Math/Basic Math Functions/tests/test_task.py @@ -15,8 +15,11 @@ class TestCase(unittest.TestCase): - def test_entropy(self): - self.assertEqual(calculate_entropy(task_data), -np.sum(task_data * np.log2(task_data)), msg='Wrong answer for task ' - 'dataset.') - self.assertEqual(calculate_entropy(b), -np.sum(b * np.log2(b)), msg='Wrong answer for test dataset.') + def test_entropy_task_data(self): + self.assertEqual(-np.sum(task_data * np.log2(task_data)), calculate_entropy(task_data), + msg='Wrong answer for task dataset.') + + def test_entropy_test(self): + self.assertEqual(-np.sum(b * np.log2(b)), calculate_entropy(b), + msg='Wrong answer for test dataset.') diff --git a/NumPy/Array Math/Linear Algebra/tests/test_task.py b/NumPy/Array Math/Linear Algebra/tests/test_task.py index 8587477..c52f132 100644 --- a/NumPy/Array Math/Linear Algebra/tests/test_task.py +++ b/NumPy/Array Math/Linear Algebra/tests/test_task.py @@ -7,10 +7,12 @@ class TestCase(unittest.TestCase): - def test_input(self): - np.testing.assert_array_equal(test_values, values, err_msg='Input values were not extracted properly from the ' + def test_values(self): + np.testing.assert_array_equal(values, test_values, err_msg='Input values were not extracted properly from the ' 'csv.') - np.testing.assert_array_equal(test_labels, labels, err_msg='Labels were not extracted properly from the ' + + def test_labels(self): + np.testing.assert_array_equal(labels, test_labels, err_msg='Labels were not extracted properly from the ' 'csv.') def test_predict(self): diff --git a/NumPy/Arrays of String and Unicode Values/Basic String Operations in NumPy/tests/test_task.py b/NumPy/Arrays of String and Unicode Values/Basic String Operations in NumPy/tests/test_task.py index 5e9e856..eeab808 100644 --- a/NumPy/Arrays of String and Unicode Values/Basic String Operations in NumPy/tests/test_task.py +++ b/NumPy/Arrays of String and Unicode Values/Basic String Operations in NumPy/tests/test_task.py @@ -24,11 +24,18 @@ def test_upper(self): uppercase_text = read_data('text.txt') np.testing.assert_array_equal(uppercase_text, test_uppercase_text, err_msg='The first function does not work ' 'as expected.') - self.assertEqual(uppercase_text.shape, test_uppercase_text.shape, msg="Expected array shape is (8,).") + + def test_upper_shape(self): + uppercase_text = read_data('text.txt') + self.assertEqual(test_uppercase_text.shape, uppercase_text.shape, msg="Expected array shape is (8,).") def test_lengths(self): uppercase_text = read_data('text.txt') line_lengths = get_line_lengths(uppercase_text) np.testing.assert_array_equal(line_lengths, test_line_lengths, err_msg='The second function does not work ' 'as expected.') - self.assertEqual(line_lengths.shape, test_line_lengths.shape, msg="Expected array shape is (8,).") + + def test_lengths_shape(self): + uppercase_text = read_data('text.txt') + line_lengths = get_line_lengths(uppercase_text) + self.assertEqual(test_line_lengths.shape, line_lengths.shape, msg="Expected array shape is (8,).") diff --git a/NumPy/Arrays of String and Unicode Values/Translate/tests/test_task.py b/NumPy/Arrays of String and Unicode Values/Translate/tests/test_task.py index 9c3c08a..79dc788 100644 --- a/NumPy/Arrays of String and Unicode Values/Translate/tests/test_task.py +++ b/NumPy/Arrays of String and Unicode Values/Translate/tests/test_task.py @@ -14,11 +14,17 @@ def test_array(self): err_msg="Your function does something else. Please check out the expected result in the" "task description.") - def test_chars(self): + def test_punctuation(self): txt = remove_extra_stuff(test_text) - check_digits = [char not in element for char in string.digits for element in txt] - check_whitespace = [char not in element for char in string.whitespace for element in txt] check_punctuation = [char not in element for char in string.punctuation for element in txt] self.assertTrue(all(check_punctuation), msg='Your result still contains punctuation.') + + def test_digits(self): + txt = remove_extra_stuff(test_text) + check_digits = [char not in element for char in string.digits for element in txt] self.assertTrue(all(check_digits), msg='Your result still contains digits.') + + def test_whitespaces(self): + txt = remove_extra_stuff(test_text) + check_whitespace = [char not in element for char in string.whitespace for element in txt] self.assertTrue(all(check_whitespace), msg='Your result still contains whitespaces.') diff --git a/NumPy/Compare Search/Compare with a scalar/tests/test_task.py b/NumPy/Compare Search/Compare with a scalar/tests/test_task.py index 3767c2b..b7c65ff 100644 --- a/NumPy/Compare Search/Compare with a scalar/tests/test_task.py +++ b/NumPy/Compare Search/Compare with a scalar/tests/test_task.py @@ -6,7 +6,10 @@ # todo: replace this with an actual test class TestCase(unittest.TestCase): - def test_filter(self): + def test_filter_shape(self): filter_ = arr % 4 == 0 np.testing.assert_array_equal(filtered_arr.shape, arr[filter_].shape, err_msg='filtered_array shape does not match the expected shape.') + + def test_filter(self): + filter_ = arr % 4 == 0 np.testing.assert_array_equal(filtered_arr, arr[filter_], err_msg='filtered_array does not match the expected result.') diff --git a/NumPy/Compare Search/Element-wise Comparison/tests/test_task.py b/NumPy/Compare Search/Element-wise Comparison/tests/test_task.py index 7f219c5..d5c119f 100644 --- a/NumPy/Compare Search/Element-wise Comparison/tests/test_task.py +++ b/NumPy/Compare Search/Element-wise Comparison/tests/test_task.py @@ -7,7 +7,7 @@ class TestCase(unittest.TestCase): def test_array_b(self): test_b = np.arange(0, 25, 6) - self.assertEqual(b.shape, test_b.shape, msg='Shape of array b has to be (5,).') + self.assertEqual(test_b.shape, b.shape, msg='Shape of array b has to be (5,).') def test_array_c(self): test_b = np.arange(0, 25, 6) diff --git a/NumPy/Compare Search/Find Unique Values/tests/test_task.py b/NumPy/Compare Search/Find Unique Values/tests/test_task.py index 4d3b439..ad0e6b7 100644 --- a/NumPy/Compare Search/Find Unique Values/tests/test_task.py +++ b/NumPy/Compare Search/Find Unique Values/tests/test_task.py @@ -12,21 +12,31 @@ class TestCase(unittest.TestCase): - def test_data(self): + def test_import(self): np.testing.assert_array_equal(csv, test_csv, err_msg='Dataset is imported improperly.') + + def test_measurements(self): np.testing.assert_array_equal(data, test_data, err_msg='Array of measurements is off.') + + def test_labels(self): np.testing.assert_array_equal(labels, test_labels, err_msg='Labels array is off.') - def test_unique(self): + def test_set_of_classes(self): np.testing.assert_array_equal(classes, test_classes, err_msg='The set of classes is wrong.') + + def test_unique_measurements(self): np.testing.assert_array_equal(unique_measurements, test_unique_measurements, err_msg='The set of unique measurements is wrong.') + + def test_occurrences(self): np.testing.assert_array_equal(unique_data_counts, test_unique_data_counts, err_msg='The set containing the number of occurrences of the unique values is wrong.') - def test_most_frequent(self): - self.assertEqual(most_frequent_index, test_most_frequent_index, + def test_most_frequent_index(self): + self.assertEqual(test_most_frequent_index, most_frequent_index, msg="The index of the most frequent value is incorrect.") - self.assertEqual(most_frequent_measurement, test_most_frequent_measurement, + + def test_most_frequent_value(self): + self.assertEqual(test_most_frequent_measurement, most_frequent_measurement, msg="The most frequent value is identified incorrectly.") diff --git a/NumPy/Compare Search/Find maximum/tests/test_task.py b/NumPy/Compare Search/Find maximum/tests/test_task.py index fb9bf45..7d0cfac 100644 --- a/NumPy/Compare Search/Find maximum/tests/test_task.py +++ b/NumPy/Compare Search/Find maximum/tests/test_task.py @@ -15,4 +15,4 @@ def test_array(self): np.testing.assert_array_equal(result, test_result, err_msg='Your result does not match the expected.') def test_array_shape(self): - self.assertEqual(result.shape, test_result.shape, msg='Shape of the array result should be (100, 1).') + self.assertEqual(test_result.shape, result.shape, msg='Shape of the array result should be (100, 1).') diff --git a/NumPy/Compare Search/Search/tests/test_task.py b/NumPy/Compare Search/Search/tests/test_task.py index 6566df9..600a4e6 100644 --- a/NumPy/Compare Search/Search/tests/test_task.py +++ b/NumPy/Compare Search/Search/tests/test_task.py @@ -11,7 +11,9 @@ def test_week(self): np.testing.assert_array_equal(result, np.where(temperatures > 15, high_test, low_test), err_msg='Your `result` array ' 'does not contain the values we expected.') - self.assertEqual(result.shape, days.shape, msg='Shape of the array `result` should match the shape of `days`.') + + def test_shape(self): + self.assertEqual(days.shape, result.shape, msg='Shape of the array `result` should match the shape of `days`.') def test_names(self): np.testing.assert_array_equal(warm_days, days[temperatures > 15], diff --git a/NumPy/Compare Search/bincount/tests/test_task.py b/NumPy/Compare Search/bincount/tests/test_task.py index ae1f88d..87e4e1b 100644 --- a/NumPy/Compare Search/bincount/tests/test_task.py +++ b/NumPy/Compare Search/bincount/tests/test_task.py @@ -4,7 +4,11 @@ class TestCase(unittest.TestCase): - def test_most_frequent_class(self): - self.assertEqual(find_most_frequent_class('data.csv'), 1, msg="Incorrect class found for task data.") - self.assertEqual(find_most_frequent_class('test_data1.csv'), 4, msg="Incorrect class found for test data.") - self.assertEqual(find_most_frequent_class('test_data2.csv'), 37, msg="Incorrect class found for test data.") \ No newline at end of file + def test_most_frequent_class1(self): + self.assertEqual(1, find_most_frequent_class('data.csv'), msg="Incorrect class found for task data.") + + def test_most_frequent_class2(self): + self.assertEqual(4, find_most_frequent_class('test_data1.csv'), msg="Incorrect class found for test data.") + + def test_most_frequent_class3(self): + self.assertEqual(37, find_most_frequent_class('test_data2.csv'), msg="Incorrect class found for test data.") \ No newline at end of file diff --git a/NumPy/Transposing Sorting Concatenating/Concatenate and Stack/tests/test_task.py b/NumPy/Transposing Sorting Concatenating/Concatenate and Stack/tests/test_task.py index cc31000..81f6eee 100644 --- a/NumPy/Transposing Sorting Concatenating/Concatenate and Stack/tests/test_task.py +++ b/NumPy/Transposing Sorting Concatenating/Concatenate and Stack/tests/test_task.py @@ -5,10 +5,14 @@ class TestCase(unittest.TestCase): - def test_arr(self): + def test_b(self): np.testing.assert_array_equal(b, np.arange(4).reshape(1, 4), err_msg='Array b should be of shape (1, 4) and have values from 0 to 3.') + + def test_c(self): np.testing.assert_array_equal(c, np.concatenate((a, np.arange(4).reshape(1, 4)), axis=0), err_msg='Array c is not what we expected.') + + def test_stacked(self): np.testing.assert_array_equal(stacked, np.vstack((np.arange(10), np.arange(20, 30), np.arange(40, 50))), err_msg='Array stacked is not what we expected.') diff --git a/NumPy/Transposing Sorting Concatenating/Partial Sort/tests/test_task.py b/NumPy/Transposing Sorting Concatenating/Partial Sort/tests/test_task.py index 5cd57ad..b61b8d6 100644 --- a/NumPy/Transposing Sorting Concatenating/Partial Sort/tests/test_task.py +++ b/NumPy/Transposing Sorting Concatenating/Partial Sort/tests/test_task.py @@ -4,9 +4,13 @@ class TestCase(unittest.TestCase): - def test_add(self): - self.assertEqual(indices.shape, distances.shape, msg="`indices` has to have the same shape as `distances`.") + def test_indices(self): + self.assertEqual(distances.shape, indices.shape, msg="`indices` has to have the same shape as `distances`.") + + def test_partition(self): self.assertTrue(all([partitioned_by_distance[i] < partitioned_by_distance[k] for i in range(k)]) and all([partitioned_by_distance[i] >= partitioned_by_distance[k] for i in range(k, arr.shape[0])]), msg='`partitioned_by_distance` does not seem to be partitioned.') - self.assertEqual(k_nearest.shape[0], 3, msg='k_nearest should contain 3 values closest to 0.') + + def test_k_nearest(self): + self.assertEqual(3, k_nearest.shape[0], msg='k_nearest should contain 3 values closest to 0.') diff --git a/NumPy/Transposing Sorting Concatenating/Sort/tests/test_task.py b/NumPy/Transposing Sorting Concatenating/Sort/tests/test_task.py index a510708..f49b120 100644 --- a/NumPy/Transposing Sorting Concatenating/Sort/tests/test_task.py +++ b/NumPy/Transposing Sorting Concatenating/Sort/tests/test_task.py @@ -4,17 +4,23 @@ class TestCase(unittest.TestCase): - def test_array_b(self): - self.assertEqual(b.shape, a.shape, msg="Array b needs to be of the same shape as a.") + def test_array_b_shape(self): + self.assertEqual(a.shape, b.shape, msg="Array b needs to be of the same shape as a.") + + def test_array_b_sorted(self): self.assertTrue(all([b[0, n] <= b[1, n] <= b[2, n] <= b[3, n] <= b[4, n] for n in range(b.shape[1])]), msg='Columns in b do not appear to be sorted.') def test_array_ind(self): - self.assertEqual(ind.shape, a.shape, msg="Array ind needs to be of the same shape as a.") + self.assertEqual(a.shape, ind.shape, msg="Array ind needs to be of the same shape as a.") def test_array_c(self): - self.assertEqual(c.shape, a.shape, msg="Array c needs to be of the same shape as a.") + self.assertEqual(a.shape, c.shape, msg="Array c needs to be of the same shape as a.") + + def test_array_c_sorted(self): self.assertTrue(all([c[0, n] <= c[1, n] <= c[2, n] <= c[3, n] <= c[4, n] for n in range(c.shape[1])]), msg='Columns in c do not appear to be sorted.') + + def test_array_c_sorted_rows(self): for i in c: self.assertTrue(i[0] == min(i) and i[-1] == max(i), msg="Rows in array c should be sorted.") diff --git a/NumPy/Transposing Sorting Concatenating/Transpose 1D/tests/test_task.py b/NumPy/Transposing Sorting Concatenating/Transpose 1D/tests/test_task.py index cee8fdc..866f13f 100644 --- a/NumPy/Transposing Sorting Concatenating/Transpose 1D/tests/test_task.py +++ b/NumPy/Transposing Sorting Concatenating/Transpose 1D/tests/test_task.py @@ -10,7 +10,11 @@ def transpose_test(start, stop, step=1): class TestCase(unittest.TestCase): - def test_transpose_shape(self): - np.testing.assert_array_equal(transpose_test(0, 1000, 10).shape, reshape_transpose(0, 1000, 10).shape, err_msg='Expected and actual shapes do not match.') - np.testing.assert_array_equal(transpose_test(100, 0, -10).shape, reshape_transpose(100, 0, -10).shape, err_msg='Expected and actual shapes do not match.') - np.testing.assert_array_equal(transpose_test(0, 100).shape, reshape_transpose(0, 100).shape, err_msg='Expected and actual shapes do not match.') + def test_transpose_shape1(self): + np.testing.assert_array_equal(reshape_transpose(0, 1000, 10).shape, transpose_test(0, 1000, 10).shape, err_msg='Expected and actual shapes do not match.') + + def test_transpose_shape2(self): + np.testing.assert_array_equal(reshape_transpose(100, 0, -10).shape, transpose_test(100, 0, -10).shape, err_msg='Expected and actual shapes do not match.') + + def test_transpose_shape3(self): + np.testing.assert_array_equal(reshape_transpose(0, 100).shape, transpose_test(0, 100).shape, err_msg='Expected and actual shapes do not match.') diff --git a/NumPy/Transposing Sorting Concatenating/Transpose/tests/test_task.py b/NumPy/Transposing Sorting Concatenating/Transpose/tests/test_task.py index 04d498e..3cc6b3f 100644 --- a/NumPy/Transposing Sorting Concatenating/Transpose/tests/test_task.py +++ b/NumPy/Transposing Sorting Concatenating/Transpose/tests/test_task.py @@ -5,13 +5,17 @@ class TestCase(unittest.TestCase): - def test_arrays(self): - b_test = np.array([[0, 3, 6], [1, 4, 7], [2, 5, 8]]).transpose() - c_test = np.arange(12).reshape(3, 2, 2).transpose() + def test_a(self): a_test = np.array([['clear', 'conscience', 'is'], ['usually', 'the', 'sign'], ['of', 'bad', 'memory']]) np.testing.assert_array_equal(a, a_test, err_msg='Array `a` is off.') + + def test_b(self): + b_test = np.array([[0, 3, 6], [1, 4, 7], [2, 5, 8]]).transpose() np.testing.assert_array_equal(b, b_test, err_msg='Array `b` is off.') + + def test_c(self): + c_test = np.arange(12).reshape(3, 2, 2).transpose() np.testing.assert_array_equal(c, c_test, err_msg='Array `c` is off.') diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..806f221 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +numpy +matplotlib \ No newline at end of file From 426ed6f2ffa7a0612a7d2bf17f220b3fd6738ade Mon Sep 17 00:00:00 2001 From: sofiiako <19rubisco93@gmail.com> Date: Wed, 17 Nov 2021 15:58:58 +0300 Subject: [PATCH 2/3] Corrected an error in the test for task Partial Sort --- .../Partial Sort/task-info.yaml | 6 +++--- .../Transposing Sorting Concatenating/Partial Sort/task.md | 2 +- .../Transposing Sorting Concatenating/Partial Sort/task.py | 4 +++- .../Partial Sort/tests/test_task.py | 5 +++-- 4 files changed, 10 insertions(+), 7 deletions(-) diff --git a/NumPy/Transposing Sorting Concatenating/Partial Sort/task-info.yaml b/NumPy/Transposing Sorting Concatenating/Partial Sort/task-info.yaml index 65e478d..ad5c654 100644 --- a/NumPy/Transposing Sorting Concatenating/Partial Sort/task-info.yaml +++ b/NumPy/Transposing Sorting Concatenating/Partial Sort/task-info.yaml @@ -3,13 +3,13 @@ files: - name: task.py visible: true placeholders: - - offset: 135 + - offset: 157 length: 29 placeholder_text: '# TODO' - - offset: 191 + - offset: 213 length: 12 placeholder_text: '# TODO' - - offset: 216 + - offset: 238 length: 27 placeholder_text: '# TODO' - name: tests/test_task.py diff --git a/NumPy/Transposing Sorting Concatenating/Partial Sort/task.md b/NumPy/Transposing Sorting Concatenating/Partial Sort/task.md index 58e9879..e165d89 100644 --- a/NumPy/Transposing Sorting Concatenating/Partial Sort/task.md +++ b/NumPy/Transposing Sorting Concatenating/Partial Sort/task.md @@ -53,7 +53,7 @@ Output: [0 1 1 2 3]] ``` ### Task -You have an array `arr` with 20 numbers and a target value `target`. You need to find `k = 3` data points +You have an array `arr` with 20 random numbers and a target value `target`. You need to find `k = 3` data points in the array `arr` that are the closest to `target` by value. The array `differences` contains the distances between `target` and each element of `arr`. 1. Get indices of `distances` partitioned by the k-th element. diff --git a/NumPy/Transposing Sorting Concatenating/Partial Sort/task.py b/NumPy/Transposing Sorting Concatenating/Partial Sort/task.py index 8bc52e0..170c676 100644 --- a/NumPy/Transposing Sorting Concatenating/Partial Sort/task.py +++ b/NumPy/Transposing Sorting Concatenating/Partial Sort/task.py @@ -2,7 +2,9 @@ rng = np.random.default_rng() k = 3 -arr = rng.normal(1, 1, 10) +mu = 1 +sigma = 1 +arr = rng.normal(mu, sigma, 10) target = 0 distances = abs(arr - target) diff --git a/NumPy/Transposing Sorting Concatenating/Partial Sort/tests/test_task.py b/NumPy/Transposing Sorting Concatenating/Partial Sort/tests/test_task.py index b61b8d6..c5c0794 100644 --- a/NumPy/Transposing Sorting Concatenating/Partial Sort/tests/test_task.py +++ b/NumPy/Transposing Sorting Concatenating/Partial Sort/tests/test_task.py @@ -8,8 +8,9 @@ def test_indices(self): self.assertEqual(distances.shape, indices.shape, msg="`indices` has to have the same shape as `distances`.") def test_partition(self): - self.assertTrue(all([partitioned_by_distance[i] < partitioned_by_distance[k] for i in range(k)]) and - all([partitioned_by_distance[i] >= partitioned_by_distance[k] for i in range(k, arr.shape[0])]), + partitioned_distances = abs(partitioned_by_distance - target) + self.assertTrue(all([partitioned_distances[i] < partitioned_distances[k] for i in range(k)]) and + all([partitioned_distances[i] >= partitioned_distances[k] for i in range(k, arr.shape[0])]), msg='`partitioned_by_distance` does not seem to be partitioned.') def test_k_nearest(self): From 61a777f0a03cd32cfe783bc972c196b24b2a9d24 Mon Sep 17 00:00:00 2001 From: sofiiako <19rubisco93@gmail.com> Date: Wed, 17 Nov 2021 20:41:57 +0300 Subject: [PATCH 3/3] replaced self.assertTrue(False) with self.fail() --- .../Array Basics/Create an Array from Range/tests/test_task.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NumPy/Array Basics/Create an Array from Range/tests/test_task.py b/NumPy/Array Basics/Create an Array from Range/tests/test_task.py index 32de869..da14180 100644 --- a/NumPy/Array Basics/Create an Array from Range/tests/test_task.py +++ b/NumPy/Array Basics/Create an Array from Range/tests/test_task.py @@ -19,4 +19,4 @@ def test_no_step(self): except TypeError: class TestCase2(unittest.TestCase): def test_bad(self): - self.assertTrue(False, msg='Your function definition possibly has an invalid number of arguments.') + self.fail('Your function definition possibly has an invalid number of arguments.')