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

28 is leap year #47

Merged
merged 15 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions solutions/is_leap_year.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
A module for determining whether the given year is leap year or not

Module contents:
- is_leap: determines whether the given year is leap year or not.

Created on XX XX XX
@author: Khusro Sakhi
"""


def is_leap_year(year) -> bool:
Khusro-S marked this conversation as resolved.
Show resolved Hide resolved
"""Determines if the given year is a leap year.

Parameters:
year (int): The year to test.

Returns:
bool: True if the year is a leap year, False otherwise.

Raises:
AssertionError: If the input is a string.
AssertionError: If the input is a negative year.
AssertionError: If the input is None.
AssertionError: If the input is a float.

Khusro-S marked this conversation as resolved.
Show resolved Hide resolved
>>> is_leap(2000)
True

>>> is_leap(1990)
False

>>> is_leap(2100)
False

>>> is_leap(2004)
True
"""
assert isinstance(year, int), "entered year is not an integer"
assert year >= 0, "year is less than 0"
Khusro-S marked this conversation as resolved.
Show resolved Hide resolved

# A leap year is divisible by 4, but not by 100 unless also divisible by 400
return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
78 changes: 78 additions & 0 deletions solutions/tests/test_is_leap_year.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Test module for is_leap function.
Contains intentionally buggy tests for debugging practice.

Test categories:
- Standard cases: Common leap and non-leap years
- Edge cases: Smallest leap year, non-leap years close to leap years
- Defensive tests: Handling of invalid inputs (e.g., non-integer, negative values)

Created on XX XX XX
Khusro-S marked this conversation as resolved.
Show resolved Hide resolved

@author: Khusro Sakhi
"""

import unittest

from ..is_leap_year import is_leap_year


class TestLeapYear(unittest.TestCase):
"""Test suite for the is_leap function - contains buggy tests!"""

# Standard test cases
def test_leap_year_2000(self):
"""It should return True for the year 2000 (divisible by 400)"""
self.assertTrue(is_leap_year(2000))

def test_non_leap_year_1990(self):
"""It should return False for the year 1990 (not divisible by 4)"""
self.assertFalse(is_leap_year(1990))

def test_non_leap_year_2100(self):
"""It should return False for the year 2100 (divisible by 100 but not by 400)"""
self.assertFalse(is_leap_year(2100))

def test_leap_year_2004(self):
"""It should return True for the year 2004 (divisible by 4 but not by 100)"""
self.assertTrue(is_leap_year(2004))

# Edge cases
def test_leap_year_4(self):
"""It should return True for the year 4 (the smallest leap year)"""
self.assertTrue(is_leap_year(4))

def test_non_leap_year_1(self):
"""It should return False for the year 1 (not divisible by 4)"""
self.assertFalse(is_leap_year(1))

def test_non_leap_year_100(self):
"""It should return False for the year 100 (divisible by 100 but not by 400)"""
self.assertFalse(is_leap_year(100))

# Defensive tests
def test_string_input(self):
"""It should raise AssertionError for a string input"""
with self.assertRaises(AssertionError):
is_leap_year("2000")

def test_negative_year(self):
"""It should raise AssertionError for a negative year"""
with self.assertRaises(AssertionError):
is_leap_year(-4)

def test_none_input(self):
"""It should raise AssertionError for None input"""
with self.assertRaises(AssertionError):
is_leap_year(None)

def test_float_input(self):
"""It should raise AssertionError for a float input"""
with self.assertRaises(AssertionError):
is_leap_year(4.5)
Khusro-S marked this conversation as resolved.
Show resolved Hide resolved


if __name__ == "__main__":
unittest.main()
Loading