adpkg

A package containing Python mo...
Log | Files | Refs | README | LICENSE

test1.py (13123B)


      1 import adcustom as c
      2 
      3 print("--- Comprehensive Test Suite for custom module ---")
      4 
      5 # --- Helper function to check test results ---
      6 def check_test(test_name, result, expected, function_prints_error=False):
      7     """
      8     Checks the test result against the expected value.
      9     'function_prints_error' is set to True if the tested function is expected
     10     to print an error message to console instead of just returning None.
     11     """
     12     print(f"  Test Status: ", end="")
     13     if result == expected:
     14         print(f"PASS")
     15     else:
     16         print(f"FAIL - Expected {expected}, Got {result}")
     17     if function_prints_error:
     18         print("  (Note: The function may have printed an error message above)")
     19 
     20 # --- Test Cases for check_prime() ---
     21 print("\n--- Testing check_prime() function ---")
     22 result_prime_1 = c.check_prime(7)
     23 print(f"Test check_prime 1: 7")
     24 print(f"Expected: 1")
     25 print(f"Result: {result_prime_1}")
     26 check_test("check_prime 1", result_prime_1, 1)
     27 
     28 result_prime_2 = c.check_prime(10)
     29 print(f"\nTest check_prime 2: 10")
     30 print(f"Expected: 0")
     31 print(f"Result: {result_prime_2}")
     32 check_test("check_prime 2", result_prime_2, 0)
     33 
     34 result_prime_3 = c.check_prime(2)
     35 print(f"\nTest check_prime 3: 2 (edge case)")
     36 print(f"Expected: 1")
     37 print(f"Result: {result_prime_3}")
     38 check_test("check_prime 3", result_prime_3, 1)
     39 
     40 result_prime_4 = c.check_prime(1)
     41 print(f"\nTest check_prime 4: 1 (not prime)")
     42 print(f"Expected: 0")
     43 print(f"Result: {result_prime_4}")
     44 check_test("check_prime 4", result_prime_4, 0)
     45 
     46 result_prime_5 = c.check_prime(-5)
     47 print(f"\nTest check_prime 5: -5 (negative input)")
     48 print(f"Expected: None")
     49 print(f"Result: {result_prime_5}")
     50 check_test("check_prime 5", result_prime_5, None, function_prints_error=True)
     51 
     52 result_prime_6 = c.check_prime("abc")
     53 print(f"\nTest check_prime 6: 'abc' (invalid input type)")
     54 print(f"Expected: None")
     55 print(f"Result: {result_prime_6}")
     56 check_test("check_prime 6", result_prime_6, None, function_prints_error=True)
     57 
     58 # --- Test Cases for factorial() ---
     59 print("\n--- Testing factorial() function ---")
     60 result_fact_1 = c.factorial(5)
     61 print(f"Test factorial 1: 5")
     62 print(f"Expected: 120")
     63 print(f"Result: {result_fact_1}")
     64 check_test("factorial 1", result_fact_1, 120)
     65 
     66 result_fact_2 = c.factorial(0)
     67 print(f"\nTest factorial 2: 0 (edge case)")
     68 print(f"Expected: 1")
     69 print(f"Result: {result_fact_2}")
     70 check_test("factorial 2", result_fact_2, 1)
     71 
     72 result_fact_3 = c.factorial(-3)
     73 print(f"\nTest factorial 3: -3 (negative input)")
     74 print(f"Expected: None")
     75 print(f"Result: {result_fact_3}")
     76 check_test("factorial 3", result_fact_3, None, function_prints_error=True)
     77 
     78 # --- Test Cases for permutation() ---
     79 print("\n--- Testing permutation() function ---")
     80 result_perm_1 = c.permutation(5, 2)
     81 print(f"Test permutation 1: P(5, 2)")
     82 print(f"Expected: 20")
     83 print(f"Result: {result_perm_1}")
     84 check_test("permutation 1", result_perm_1, 20)
     85 
     86 result_perm_2 = c.permutation(3, 3)
     87 print(f"\nTest permutation 2: P(3, 3)")
     88 print(f"Expected: 6")
     89 print(f"Result: {result_perm_2}")
     90 check_test("permutation 2", result_perm_2, 6)
     91 
     92 result_perm_3 = c.permutation(5, 7)
     93 print(f"\nTest permutation 3: P(5, 7) (k > n)")
     94 print(f"Expected: None")
     95 print(f"Result: {result_perm_3}")
     96 check_test("permutation 3", result_perm_3, None, function_prints_error=True)
     97 
     98 # --- Test Cases for combination() ---
     99 print("\n--- Testing combination() function ---")
    100 result_comb_1 = c.combination(5, 2)
    101 print(f"Test combination 1: C(5, 2)")
    102 print(f"Expected: 10")
    103 print(f"Result: {result_comb_1}")
    104 check_test("combination 1", result_comb_1, 10)
    105 
    106 result_comb_2 = c.combination(3, 3)
    107 print(f"\nTest combination 2: C(3, 3)")
    108 print(f"Expected: 1")
    109 print(f"Result: {result_comb_2}")
    110 check_test("combination 2", result_comb_2, 1)
    111 
    112 result_comb_3 = c.combination(5, 7)
    113 print(f"\nTest combination 3: C(5, 7) (k > n)")
    114 print(f"Expected: 0") # Your function returns 0 for k > n
    115 print(f"Result: {result_comb_3}")
    116 check_test("combination 3", result_comb_3, 0)
    117 
    118 # --- Test Cases for string_reverse() ---
    119 print("\n--- Testing string_reverse() function ---")
    120 result_str_rev_1 = c.string_reverse("hello")
    121 print(f"Test string_reverse 1: 'hello'")
    122 print(f"Expected: 'olleh'")
    123 print(f"Result: {result_str_rev_1}")
    124 check_test("string_reverse 1", result_str_rev_1, "olleh")
    125 
    126 result_str_rev_2 = c.string_reverse("")
    127 print(f"\nTest string_reverse 2: '' (empty string)")
    128 print(f"Expected: None")
    129 print(f"Result: {result_str_rev_2}")
    130 check_test("string_reverse 2", result_str_rev_2, None, function_prints_error=True)
    131 
    132 # --- Test Cases for matrix_addition() ---
    133 print("\n--- Testing matrix_addition() function ---")
    134 mat_add_1 = [[1, 2], [3, 4]]
    135 mat_add_2 = [[5, 6], [7, 8]]
    136 expected_add_mat = "6 8 \n10 12 \n"
    137 result_mat_add_1 = c.matrix_addition(mat_add_1, mat_add_2)
    138 print(f"Test matrix_addition 1: Adding {mat_add_1} and {mat_add_2}")
    139 print(f"Expected:\n{expected_add_mat}")
    140 print(f"Result:\n{result_mat_add_1}")
    141 check_test("matrix_addition 1", result_mat_add_1, expected_add_mat)
    142 
    143 mat_add_incompatible_1 = [[1, 2]]
    144 mat_add_incompatible_2 = [[1, 2, 3]]
    145 result_mat_add_2 = c.matrix_addition(mat_add_incompatible_1, mat_add_incompatible_2)
    146 print(f"\nTest matrix_addition 2: Incompatible dimensions {mat_add_incompatible_1} and {mat_add_incompatible_2}")
    147 print(f"Expected: None")
    148 print(f"Result: {result_mat_add_2}")
    149 check_test("matrix_addition 2", result_mat_add_2, None, function_prints_error=True)
    150 
    151 # --- Test Cases for matrix_multiplication() ---
    152 print("\n--- Testing matrix_multiplication() function ---")
    153 mat_mult_1 = [[1, 2], [3, 4]]
    154 mat_mult_2 = [[5, 6], [7, 8]]
    155 expected_mult_mat = "19 22 \n43 50 \n"
    156 result_mat_mult_1 = c.matrix_multiplication(mat_mult_1, mat_mult_2)
    157 print(f"Test matrix_multiplication 1: Multiplying {mat_mult_1} and {mat_mult_2}")
    158 print(f"Expected:\n{expected_mult_mat}")
    159 print(f"Result:\n{result_mat_mult_1}")
    160 check_test("matrix_multiplication 1", result_mat_mult_1, expected_mult_mat)
    161 
    162 mat_mult_incompatible_1 = [[1, 2]]
    163 mat_mult_incompatible_2 = [[1], [2], [3]]
    164 result_mat_mult_2 = c.matrix_multiplication(mat_mult_incompatible_1, mat_mult_incompatible_2)
    165 print(f"\nTest matrix_multiplication 2: Incompatible dimensions {mat_mult_incompatible_1} and {mat_mult_incompatible_2}")
    166 print(f"Expected: None")
    167 print(f"Result: {result_mat_mult_2}")
    168 check_test("matrix_multiplication 2", result_mat_mult_2, None, function_prints_error=True)
    169 
    170 # --- Test Cases for matrix_transpose() ---
    171 print("\n--- Testing matrix_transpose() function ---")
    172 mat_trans_1 = [[1, 2, 3], [4, 5, 6]]
    173 expected_trans_mat = "1 4 \n2 5 \n3 6 \n"
    174 result_mat_trans_1 = c.matrix_transpose(mat_trans_1)
    175 print(f"Test matrix_transpose 1: Transposing {mat_trans_1}")
    176 print(f"Expected:\n{expected_trans_mat}")
    177 print(f"Result:\n{result_mat_trans_1}")
    178 check_test("matrix_transpose 1", result_mat_trans_1, expected_trans_mat)
    179 
    180 mat_trans_2 = [[10]]
    181 expected_trans_mat_2 = "10 \n"
    182 result_mat_trans_2 = c.matrix_transpose(mat_trans_2)
    183 print(f"\nTest matrix_transpose 2: Transposing {mat_trans_2}")
    184 print(f"Expected:\n{expected_trans_mat_2}")
    185 print(f"Result:\n{result_mat_trans_2}")
    186 check_test("matrix_transpose 2", result_mat_trans_2, expected_trans_mat_2)
    187 
    188 
    189 # --- Test Cases for determinant_value() ---
    190 print("\n--- Testing determinant_value() function ---")
    191 det_1 = [[5]]
    192 result_det_1 = c.determinant_value(det_1)
    193 print(f"Test determinant_value 1: 1x1 matrix {det_1}")
    194 print(f"Expected: 5")
    195 print(f"Result: {result_det_1}")
    196 check_test("determinant_value 1", result_det_1, 5)
    197 
    198 det_2 = [[1, 2], [3, 4]]
    199 result_det_2 = c.determinant_value(det_2)
    200 print(f"\nTest determinant_value 2: 2x2 matrix {det_2}")
    201 print(f"Expected: -2") # (1*4 - 2*3) = 4 - 6 = -2
    202 print(f"Result: {result_det_2}")
    203 check_test("determinant_value 2", result_det_2, -2)
    204 
    205 det_3 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
    206 result_det_3 = c.determinant_value(det_3)
    207 print(f"\nTest determinant_value 3: 3x3 matrix {det_3}")
    208 print(f"Expected: 0")
    209 print(f"Result: {result_det_3}")
    210 check_test("determinant_value 3", result_det_3, 0)
    211 
    212 det_non_square = [[1, 2], [3, 4], [5, 6]]
    213 result_det_4 = c.determinant_value(det_non_square)
    214 print(f"\nTest determinant_value 4: Non-square matrix {det_non_square}")
    215 print(f"Expected: None")
    216 print(f"Result: {result_det_4}")
    217 check_test("determinant_value 4", result_det_4, None, function_prints_error=True)
    218 
    219 # --- Test Cases for mean() ---
    220 print("\n--- Testing mean() function ---")
    221 test_mean_1 = [1, 2, 3, 4, 5]
    222 result_mean_1 = c.mean(test_mean_1)
    223 print(f"Test mean 1: {test_mean_1}")
    224 print(f"Expected: 3.0")
    225 print(f"Result: {result_mean_1}")
    226 check_test("mean 1", result_mean_1, 3.0)
    227 
    228 test_mean_2 = [10, 20, 30]
    229 result_mean_2 = c.mean(test_mean_2)
    230 print(f"\nTest mean 2: {test_mean_2}")
    231 print(f"Expected: 20.0")
    232 print(f"Result: {result_mean_2}")
    233 check_test("mean 2", result_mean_2, 20.0)
    234 
    235 test_mean_3 = []
    236 result_mean_3 = c.mean(test_mean_3)
    237 print(f"\nTest mean 3: Empty list {test_mean_3}")
    238 print(f"Expected: None")
    239 print(f"Result: {result_mean_3}")
    240 check_test("mean 3", result_mean_3, None, function_prints_error=True)
    241 
    242 # --- Test Cases for median() ---
    243 print("\n--- Testing median() function ---")
    244 test_median_odd = [5, 2, 8, 1, 9] # Sorted: [1, 2, 5, 8, 9] -> Median: 5
    245 result_median_odd = c.median(test_median_odd)
    246 print(f"Test median 1: Odd length list {test_median_odd}")
    247 print(f"Expected: 5.0")
    248 print(f"Result: {result_median_odd}")
    249 check_test("median 1", result_median_odd, 5.0)
    250 
    251 test_median_even = [5, 2, 8, 1, 9, 3] # Sorted: [1, 2, 3, 5, 8, 9] -> Median: (3+5)/2 = 4
    252 result_median_even = c.median(test_median_even)
    253 print(f"\nTest median 2: Even length list {test_median_even}")
    254 print(f"Expected: 4.0")
    255 print(f"Result: {result_median_even}")
    256 check_test("median 2", result_median_even, 4.0)
    257 
    258 test_median_empty = []
    259 result_median_empty = c.median(test_median_empty)
    260 print(f"\nTest median 3: Empty list {test_median_empty}")
    261 print(f"Expected: None")
    262 print(f"Result: {result_median_empty}")
    263 check_test("median 3", result_median_empty, None, function_prints_error=True)
    264 
    265 # --- Test Cases for mode() ---
    266 # Re-using the test cases that were previously confirmed to work with your mode implementation
    267 print("\n--- Testing mode() function ---")
    268 
    269 # Test Case 1: Standard case with a clear mode
    270 test_list_mode_1 = [1, 2, 3, 2, 1, 5, 1, 1, 2, 6, 5, 54, 52, 5]
    271 print(f"\nTest mode 1: Standard list: {test_list_mode_1}")
    272 result_mode_1 = c.mode(test_list_mode_1)
    273 print(f"Expected: ([1], 4)")
    274 print(f"Result: {result_mode_1}")
    275 check_test("mode 1", result_mode_1, ([1], 4))
    276 
    277 # Test Case 2: List with multiple modes
    278 test_list_mode_2 = [10, 20, 30, 20, 10, 40, 50]
    279 print(f"\nTest mode 2: List with multiple modes: {test_list_mode_2}")
    280 result_mode_2 = c.mode(test_list_mode_2)
    281 print(f"Expected: ([10, 20], 2)")
    282 print(f"Result: {result_mode_2}")
    283 check_test("mode 2", result_mode_2, ([10, 20], 2))
    284 
    285 # Test Case 3: List with no repeating elements
    286 test_list_mode_3 = [1, 2, 3, 4, 5]
    287 print(f"\nTest mode 3: List with no repeating elements: {test_list_mode_3}")
    288 result_mode_3 = c.mode(test_list_mode_3)
    289 print(f"Expected: ([1, 2, 3, 4, 5], 1)")
    290 print(f"Result: {result_mode_3}")
    291 check_test("mode 3", result_mode_3, ([1, 2, 3, 4, 5], 1))
    292 
    293 # Test Case 4: List with all same elements
    294 test_list_mode_4 = [7, 7, 7, 7, 7]
    295 print(f"\nTest mode 4: List with all same elements: {test_list_mode_4}")
    296 result_mode_4 = c.mode(test_list_mode_4)
    297 print(f"Expected: ([7], 5)")
    298 print(f"Result: {result_mode_4}")
    299 check_test("mode 4", result_mode_4, ([7], 5))
    300 
    301 # Test Case 5: Empty list
    302 test_list_mode_5 = []
    303 print(f"\nTest mode 5: Empty list: {test_list_mode_5}")
    304 result_mode_5 = c.mode(test_list_mode_5)
    305 print(f"Expected: None")
    306 print(f"Result: {result_mode_5}")
    307 check_test("mode 5", result_mode_5, None, function_prints_error=True)
    308 
    309 # Test Case 6: List with negative numbers and zeros
    310 test_list_mode_6 = [-1, 0, -5, 0, -1, 10]
    311 print(f"\nTest mode 6: List with negative numbers and zeros: {test_list_mode_6}")
    312 result_mode_6 = c.mode(test_list_mode_6)
    313 print(f"Expected: ([-1, 0], 2)")
    314 print(f"Result: {result_mode_6}")
    315 check_test("mode 6", result_mode_6, ([-1, 0], 2))
    316 
    317 # Test Case 7: List with strings (checking versatility for different data types)
    318 test_list_mode_7 = ["apple", "banana", "apple", "orange", "banana", "apple"]
    319 print(f"\nTest mode 7: List with strings: {test_list_mode_7}")
    320 result_mode_7 = c.mode(test_list_mode_7)
    321 print(f"Expected: (['apple'], 3)")
    322 print(f"Result: {result_mode_7}")
    323 check_test("mode 7", result_mode_7, (['apple'], 3))
    324 
    325 # Test Case 8: Invalid input (integer) for mode
    326 test_invalid_input_mode = 123
    327 print(f"\nTest mode 8: Invalid input (integer): {test_invalid_input_mode}")
    328 result_invalid_mode = c.mode(test_invalid_input_mode)
    329 print(f"Expected: None")
    330 print(f"Result: {result_invalid_mode}")
    331 check_test("mode 8", result_invalid_mode, None, function_prints_error=True)
    332 
    333 
    334 print("\n--- All tests completed. Review the 'Test Status' lines for results. ---")
© notamitgamer • Site Built: 2026-07-21 13:58:23 UTC • git-mirror commit: 1037f62 [view raw info]
Originally created with stagit • modified by notamitgamer
Forked from github.com/notamitgamer/git-mirror