Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CS50P Problem Set 5 - test_fuel - check50 may incorrectly fail solution if solution tests for positive result in convert() #264

Open
kguryanov opened this issue Jul 24, 2024 · 0 comments

Comments

@kguryanov
Copy link

kguryanov commented Jul 24, 2024

Preconditions

  1. Have a test_fuel.py, which passes the check50 validation:
    import pytest
    from fuel import convert, gauge
    
    
    @pytest.mark.parametrize(
        "input, expected",
        [
            ("3/4", 75),
        ],
    )
    def test_convert(input, expected):
        assert convert(input) == expected
    
    
    @pytest.mark.parametrize(
        "input",
        [
            "10/3",
        ],
    )
    def test_convert_value_error(input):
        with pytest.raises(ValueError):
            assert convert(input)
    
    
    @pytest.mark.parametrize(
        "input",
        [
            "0/0"
        ],
    )
    def test_convert_zero_division(input):
        with pytest.raises(ZeroDivisionError):
            convert(input)
    
    
    @pytest.mark.parametrize(
     "input,expected",
        [
            (75, "75%"), (33, "33%"), (67, "67%"), (0, "E"), (1, "E"), (100, "F"), (99, "F"), (101, "F"), (-1, "E"),
        ],
    )
    def test_gauge(input, expected):
        assert gauge(input) == expected

Steps to reproduce:

  1. Add following test to your test_fuel.py:
    @pytest.mark.parametrize(
        "input",
        [
            "10/3",
            "-1/100"
        ],
    )
    def test_convert_value_error(input):
        with pytest.raises(ValueError):
            assert convert(input)
  2. Run check50 cs50/problems/2022/python/tests/fuel

Expected result

  1. The check50 validation should pass

Actual result

  1. The problem description for PSET 5 Refueling has the following description:
    image

  2. The check50 validation fails at step 2 :( correct fuel.py passes all test_fuel checks If user attempts to add a test for the requirement the nearest int between 0 and 100, inclusive. :

    def convert(input: str) -> int:
        # Will raise ValueError if int() conversion fails
        # Will raise ValueError if too many values to unpack
        dividend, divisor = (int(value) for value in input.split("/"))
    
        if dividend > divisor:
            raise ValueError("Dividend can not be larger than divisor.")
    
        result = round((dividend / divisor) * 100)
    
        if result not in range(0, 101):
            raise ValueError("Result must be an int between 0 and 100, inclusive.")
    
        return result

    When the test is enabled:
    image

    When skipping the test:
    image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant