tests.unit.test_numbertheory Module

This module contains all unit tests for the lib.numbertheory package.

tests.unit.test_numbertheory – Unit Tests

tests.unit.test_numbertheory.test_divisor_count_bad_n_type_float()

Test that lib.numbertheory.divisor_count() raises a TypeError for a non-int value for n (float)

tests.unit.test_numbertheory.test_divisor_count_bad_n_type_str()

Test that lib.numbertheory.divisor_count() raises a TypeError for a non-int value for n (str)

tests.unit.test_numbertheory.test_divisor_count_bad_n_zero()

Test that lib.numbertheory.divisor_count() raises a ValueError for a non-positive value for n

tests.unit.test_numbertheory.test_divisor_count_correctness(value, expected_answer)

Test the correctness of lib.numbertheory.divisor_count() using known answer tests

Parameters:
  • value (int) – the input value
  • expected_answer (bool) – the expected answer
Raises:
tests.unit.test_numbertheory.test_divisor_sum_aliquot_bad_n_type_float()

Test that lib.numbertheory.divisor_sum_aliquot() raises a TypeError for a non-int value for n (float)

tests.unit.test_numbertheory.test_divisor_sum_aliquot_bad_n_type_str()

Test that lib.numbertheory.divisor_sum_aliquot() raises a TypeError for a non-int value for n (str)

tests.unit.test_numbertheory.test_divisor_sum_aliquot_bad_n_zero()

Test that lib.numbertheory.divisor_sum_aliquot() raises a ValueError for a non-positive value for n

tests.unit.test_numbertheory.test_divisor_sum_aliquot_correctness(value, expected_answer)

Test the correctness of lib.numbertheory.divisor_sum_aliquot() using known answer tests

Parameters:
  • value (int) – the input value
  • expected_answer (bool) – the expected answer
Raises:
tests.unit.test_numbertheory.test_divisor_sum_bad_n_type_float()

Test that lib.numbertheory.divisor_sum() raises a TypeError for a non-int value for n (float)

tests.unit.test_numbertheory.test_divisor_sum_bad_n_type_str()

Test that lib.numbertheory.divisor_sum() raises a TypeError for a non-int value for n (str)

tests.unit.test_numbertheory.test_divisor_sum_bad_n_zero()

Test that lib.numbertheory.divisor_sum() raises a ValueError for a non-positive value for n

tests.unit.test_numbertheory.test_divisor_sum_correctness(value, expected_answer)

Test the correctness of lib.numbertheory.divisor_sum() using known answer tests

Parameters:
  • value (int) – the input value
  • expected_answer (bool) – the expected answer
Raises:
tests.unit.test_numbertheory.test_divisors_sieve_bad_aggregate_invalid()

Test that lib.numbertheory.divisors_sieve() raises a ValueError for invalid value of aggregate

tests.unit.test_numbertheory.test_divisors_sieve_bad_aggregate_type_float()

Test that lib.numbertheory.divisors_sieve() raises a TypeError for a non-str value for aggregate (float)

tests.unit.test_numbertheory.test_divisors_sieve_bad_upper_bound_negative()

Test that lib.numbertheory.divisors_sieve() raises a ValueError for \(\mbox{upper_bound} \lt 0\)

tests.unit.test_numbertheory.test_divisors_sieve_bad_upper_bound_type_float()

Test that lib.numbertheory.divisors_sieve() raises a TypeError for a non-int value for upper_bound (float)

tests.unit.test_numbertheory.test_divisors_sieve_bad_upper_bound_type_str()

Test that lib.numbertheory.divisors_sieve() raises a TypeError for a non-int value for upper_bound (str)

tests.unit.test_numbertheory.test_divisors_sieve_correctness(upper_bound, proper, aggregate, expected_answer)

Test the correctness of lib.numbertheory.divisors_sieve() using known answer tests

Parameters:
  • upper_bound (int) – the upper bound of the sieve
  • proper (bool) – whether to consider just proper divisors or not
  • aggregate (Optional[str]) – the aggregation function to apply
  • expected_answer (Union[set, int]) – the expected answer
Raises:
tests.unit.test_numbertheory.test_divisors_sieve_correctness_not_proper()

Test the correctness of the divisors prime sieve lib.numbertheory.divisors_sieve()

The divisors yielded by this sieve are tested to ensure that they do divide the given integer \(n\), up to some moderate bound on \(n\). Additionally, it is checked that \(1\) is included as a divisor of along with \(n\) itself.

Raises:
tests.unit.test_numbertheory.test_divisors_sieve_correctness_proper()

Test the correctness of the divisors prime sieve lib.numbertheory.divisors_sieve()

The divisors yielded by this sieve are tested to ensure that they do divide the given integer \(n\), up to some moderate bound on \(n\). Additionally, it is checked that \(1\) is included as a proper divisor of \(n\) and that \(n\) is not.

Raises:
tests.unit.test_numbertheory.test_factor_bad_n_type_float()

Test that lib.numbertheory.factor() raises a TypeError for a non-int value for n (float)

tests.unit.test_numbertheory.test_factor_bad_n_type_str()

Test that lib.numbertheory.factor() raises a TypeError for a non-int value for n (str)

tests.unit.test_numbertheory.test_factor_correctness(value, expected_answer)

Test the correctness of lib.numbertheory.factor() with known-answer-tests

Raises:
tests.unit.test_numbertheory.test_factor_correctness_negative(value, expected_answer)

Test the correctness of lib.numbertheory.factor() with known-answer-tests

Each value is factored. This factorisation is combined with a factor of \(-1\) to give the expected factorisation of \(-value\). This negative value is then factored, and the two results are checked for equality.

Raises:
tests.unit.test_numbertheory.test_factor_correctness_random()

Test the correctness of lib.numbertheory.factor() with known-answer-tests

A random integer is built as a product of several small primes (selected with replacement); this random integer has a known factorisation. lib.numbertheory.factor() is used to factor the constructed number and it is compared to the known and expected factorisation.

Raises:
tests.unit.test_numbertheory.test_is_even_bad_n_type_float()

Test that lib.numbertheory.is_even() raises a TypeError for a non-int value for n (float)

tests.unit.test_numbertheory.test_is_even_bad_n_type_str()

Test that lib.numbertheory.is_even() raises a TypeError for a non-int value for n (str)

tests.unit.test_numbertheory.test_is_even_correctness(value, expected_answer)

Test the correctness of lib.numbertheory.is_even() using known answer tests

Parameters:
  • value (int) – the input value
  • expected_answer (bool) – the expected answer
Raises:
tests.unit.test_numbertheory.test_is_odd_bad_n_type_float()

Test that lib.numbertheory.is_odd() raises a TypeError for a non-int value for n (float)

tests.unit.test_numbertheory.test_is_odd_bad_n_type_str()

Test that lib.numbertheory.is_odd() raises a TypeError for a non-int value for n (str)

tests.unit.test_numbertheory.test_is_odd_correctness(value, expected_answer)

Test the correctness of lib.numbertheory.is_odd() using known answer tests

Parameters:
  • value (int) – the input value
  • expected_answer (bool) – the expected answer
Raises:
tests.unit.test_numbertheory.test_is_probably_prime_bad_k_type_float()

Test that lib.numbertheory.is_probably_prime() raises a TypeError for a non-int value for k (float)

tests.unit.test_numbertheory.test_is_probably_prime_bad_k_type_str()

Test that lib.numbertheory.is_probably_prime() raises a TypeError for a non-int value for k (str)

tests.unit.test_numbertheory.test_is_probably_prime_bad_n_type_float()

Test that lib.numbertheory.is_probably_prime() raises a TypeError for a non-int value for n (float)

tests.unit.test_numbertheory.test_is_probably_prime_bad_n_type_str()

Test that lib.numbertheory.is_probably_prime() raises a TypeError for a non-int value for n (str)

tests.unit.test_numbertheory.test_is_probably_prime_correctness()

Test the correctness of lib.numbertheory.is_probably_prime()

Raises:
tests.unit.test_numbertheory.test_is_square_bad_n_type_float()

Test that lib.numbertheory.is_square() raises a TypeError for a non-int value for n (float)

tests.unit.test_numbertheory.test_is_square_bad_n_type_str()

Test that lib.numbertheory.is_square() raises a TypeError for a non-int value for n (str)

tests.unit.test_numbertheory.test_is_square_correctness(value, expected_answer)

Test the correctness of lib.numbertheory.is_square() using known answer tests

Parameters:
  • value (int) – the input value
  • expected_answer (bool) – the expected answer
Raises:
tests.unit.test_numbertheory.test_miller_rabin_bad_k_type_float()

Test that lib.numbertheory.miller_rabin() raises a TypeError for a non-int value for k (float)

tests.unit.test_numbertheory.test_miller_rabin_bad_k_type_str()

Test that lib.numbertheory.miller_rabin() raises a TypeError for a non-int value for k (str)

tests.unit.test_numbertheory.test_miller_rabin_bad_k_zero()

Test that lib.numbertheory.miller_rabin() raises a ValueError for \(k=0\)

tests.unit.test_numbertheory.test_miller_rabin_bad_n_too_small()

Test that lib.numbertheory.miller_rabin() raises a ValueError for too small a value for n

tests.unit.test_numbertheory.test_miller_rabin_bad_n_type_float()

Test that lib.numbertheory.miller_rabin() raises a TypeError for a non-int value for n (float)

tests.unit.test_numbertheory.test_miller_rabin_bad_n_type_str()

Test that lib.numbertheory.miller_rabin() raises a TypeError for a non-int value for n (str)

tests.unit.test_numbertheory.test_perfect_power_exponent_bad_n_type_float()

Test that lib.numbertheory.perfect_power_exponent() raises a TypeError for a non-int value for n (float)

tests.unit.test_numbertheory.test_perfect_power_exponent_bad_n_type_str()

Test that lib.numbertheory.perfect_power_exponent() raises a TypeError for a non-int value for n (str)

tests.unit.test_numbertheory.test_perfect_power_exponent_bad_n_zero()

Test that lib.numbertheory.perfect_power_exponent() raises a ValueError for \(n=0\)

tests.unit.test_numbertheory.test_perfect_power_exponent_correctness_not_square_free(square_free)

Test the correctness of lib.numbertheory.perfect_power_exponent() for non square-free inputs

Raises:
  • AssertionError – if lib.numbertheory.perfect_power_exponent() returns the wrong type
  • AssertionError – if lib.numbertheory.perfect_power_exponent() returns the wrong value
tests.unit.test_numbertheory.test_perfect_power_exponent_correctness_square_free(square_free)

Test the correctness of lib.numbertheory.perfect_power_exponent() for square-free inputs

Raises:
  • AssertionError – if lib.numbertheory.perfect_power_exponent() returns the wrong type
  • AssertionError – if lib.numbertheory.perfect_power_exponent() returns the wrong value
tests.unit.test_numbertheory.test_prime_sieve_bad_upper_bound_negative()

Test that lib.numbertheory.prime_sieve() raises a ValueError for \(upper\_bound \lt 0\)

tests.unit.test_numbertheory.test_prime_sieve_bad_upper_bound_too_big()

Test that lib.numbertheory.prime_sieve() raises a ValueError for \(\mbox{upper_bound} \ge 10^{16}\)

tests.unit.test_numbertheory.test_prime_sieve_bad_upper_bound_type_float()

Test that lib.numbertheory.prime_sieve() raises a TypeError for a non-int value for upper_bound (float)

tests.unit.test_numbertheory.test_prime_sieve_bad_upper_bound_type_str()

Test that lib.numbertheory.prime_sieve() raises a TypeError for a non-int value for upper_bound (str)

tests.unit.test_numbertheory.test_prime_sieve_correctness(siever)
Test the correctness of all prime sieves in lib.numbertheory() including:
  • lib.numbertheory.primality.prime_sieve()
  • lib.numbertheory.primality.eratosthenes()
  • lib.numbertheory.primality.segmented_eratosthenes()

The integers yielded by these sieves are tested for primality, up to some moderate bound.

Raises:
  • AssertionError – if siever(upper_bound) yields the wrong type
  • AssertionError – if siever(upper_bound) yields the wrong value
tests.unit.test_numbertheory.test_prime_sieve_correctness_bound(upper_bound)
Test the correctness of all prime sieves in lib.numbertheory including:
  • lib.numbertheory.primality.prime_sieve()
  • lib.numbertheory.primality.eratosthenes()
  • lib.numbertheory.primality.segmented_eratosthenes()

The integers yielded by these sieves are tested for primality, up to some moderate bound. The parameter upper_bound is set to various values to ensure all sieves are utilised.

Raises: