Skip to content

test: add test case for linked-list version of is-palindrome #3033

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

Closed
wants to merge 2 commits into from
Closed
Changes from all 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
98 changes: 98 additions & 0 deletions data_structures/linked_list/tests/test_is_palindrome.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
from dataclasses import dataclass
from typing import Any

import pytest

from data_structures.linked_list.is_palindrome import (
is_palindrome,
is_palindrome_dict,
is_palindrome_stack,
)


@dataclass
class Node:
val: Any
next: "Node" = None


@pytest.fixture
def single_char() -> Node:
return Node("a")


@pytest.fixture
def kayake() -> Node:
e = Node("e")
k = Node("k", e)
a = Node("a", k)
y = Node("y", a)
a = Node("a", y)
k = Node("k", a)
return k


@pytest.fixture
def racecar() -> Node:
r = Node("r")
a = Node("a", r)
c = Node("c", a)
e = Node("e", c)
c = Node("c", e)
a = Node("a", c)
r = Node("r", a)
return r


@pytest.fixture
def stats() -> Node:
s = Node("s")
t = Node("t", s)
a = Node("a", t)
t = Node("t", a)
s = Node("s", t)
return s


@pytest.fixture
def computer() -> Node:
r = Node("r")
e = Node("e", r)
t = Node("t", e)
u = Node("u", t)
p = Node("p", u)
m = Node("m", p)
o = Node("o", m)
c = Node("c", o)
return c


def test_is_palindrome_correct_result(racecar, stats, computer, kayake, single_char):
assert is_palindrome(None)
assert is_palindrome(single_char)
assert is_palindrome(racecar)
assert is_palindrome(stats)
assert not is_palindrome(kayake)
assert not is_palindrome(computer)


def test_is_palindrome_dict_correct_result(
racecar, stats, computer, kayake, single_char
):
assert is_palindrome_dict(None)
assert is_palindrome_dict(single_char)
assert is_palindrome_dict(racecar)
assert is_palindrome_dict(stats)
assert not is_palindrome_dict(kayake)
assert not is_palindrome_dict(computer)


def test_is_palindrome_stack_correct_result(
racecar, stats, computer, kayake, single_char
):
assert is_palindrome_stack(None)
assert is_palindrome_stack(single_char)
assert is_palindrome_stack(racecar)
assert is_palindrome_stack(stats)
assert not is_palindrome_stack(kayake)
assert not is_palindrome_stack(computer)