From 281cbf5ea8e74d4e6a7698c50b9924c533bdf170 Mon Sep 17 00:00:00 2001 From: Normale <52136684+Normale@users.noreply.github.com> Date: Sun, 8 Jan 2023 18:57:59 +0100 Subject: [PATCH] important --- code/C-100000.csv | 2 +- code/conftest.py | 12 +----------- code/project.py | 5 ++--- code/test_main.py | 11 +++++++++-- 4 files changed, 13 insertions(+), 17 deletions(-) diff --git a/code/C-100000.csv b/code/C-100000.csv index a984acf0..c687f2a2 100644 --- a/code/C-100000.csv +++ b/code/C-100000.csv @@ -99997,4 +99997,4 @@ 110484 = 2 * 2 * 3 * 3 * 3 * 3 * 11 * 31 110485 = 5 * 19 * 1163 110486 = 2 * 55243 -110487 = 3 * 13 * 2833 +110487 = 3 * 13 * 2833 \ No newline at end of file diff --git a/code/conftest.py b/code/conftest.py index fa6f7d59..b7c34a90 100644 --- a/code/conftest.py +++ b/code/conftest.py @@ -24,15 +24,6 @@ def pytest_configure(config): pytest.composites = int(config.getoption("--composites")) -@pytest.fixture -def primes_obtained(): - return set(sieve_of_eratosthenes(pytest.primes + 1)) - - -@pytest.fixture -def composites_obtained(): - return set(sieve_of_eratosthenes(pytest.composites + 1)) - def pytest_generate_tests(metafunc): df_prime = pd.read_csv("P-100000.csv", header=None) df_prime = df_prime[df_prime.iloc[:, 1] < pytest.primes] @@ -45,7 +36,6 @@ def pytest_generate_tests(metafunc): metafunc.parametrize('prime', primes) metafunc.parametrize('primes_obtained', [sieve_of_eratosthenes(max(primes) + 1),]) - if metafunc.function.__name__ == 'test_negatives': + if metafunc.function.__name__ == 'test_composites': metafunc.parametrize('composite', composites) - metafunc.parametrize('primes_obtained', [sieve_of_eratosthenes(max(composites) + 1),]) diff --git a/code/project.py b/code/project.py index 35d422d0..9acb0831 100644 --- a/code/project.py +++ b/code/project.py @@ -10,6 +10,8 @@ def sieve_of_eratosthenes(num: int) -> list[int]: Returns: list[int]: List of prime numbers """ + if num <= 0: + raise ValueError("Number should not be negative.") # boolean list to store if a number is prime or not prime = [True] * (num+1) p = 2 # starting prime number @@ -41,8 +43,5 @@ def print_sieve(num: int) -> None: # Driver code if __name__ == '__main__': num = int(input("Enter a number: ")) - while num < 2: - print("Limit smaller than 2, pick another number") - num = int(input("Enter a number: ")) print(f"Following are the prime numbers smaller than or equal to {num}") print_sieve(num) \ No newline at end of file diff --git a/code/test_main.py b/code/test_main.py index 71c9abfa..bcc7488c 100644 --- a/code/test_main.py +++ b/code/test_main.py @@ -1,8 +1,15 @@ import pandas as pd +import pytest +from .project import sieve_of_eratosthenes def test_positives(prime, primes_obtained): assert prime in primes_obtained -def test_negatives(composite, primes_obtained): - assert composite not in primes_obtained \ No newline at end of file +def test_composites(composite, primes_obtained): + assert composite not in primes_obtained + + +def test_negatives(): + with pytest.raises(ValueError): + sieve_of_eratosthenes(-1) \ No newline at end of file