Skip to content

Instantly share code, notes, and snippets.

@vinicius507
Created December 21, 2022 23:45
Show Gist options
  • Save vinicius507/7417cf4586ceaca211271262f00371bd to your computer and use it in GitHub Desktop.
Save vinicius507/7417cf4586ceaca211271262f00371bd to your computer and use it in GitHub Desktop.
Testing LIBFT functions using Python CFFI
##!/usr/bin/env python3
import pathlib
import tempfile
import unittest
import cffi
# You must compile a libft shared library
LIBFT_PATH = pathlib.Path("libft.so").absolute()
class TestGetNextLine(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.ffi = cffi.FFI()
# Load Lbift shared library
cls.libft = cls.ffi.dlopen(LIBFT_PATH.as_posix(), cls.ffi.RTLD_NOW)
# Define symbols we are going to use in our tests!
cls.ffi.cdef("char *get_next_line(int fd);")
@classmethod
def tearDownClass(cls):
cls.ffi.dlclose(cls.libft)
def test_invalid_fd_returns_null(self):
line = self.libft.get_next_line(-1)
assert line == self.ffi.NULL
def test_unused_fd_returns_null(self):
line = self.libft.get_next_line(63)
assert line == self.ffi.NULL
def test_valid_empty_file_returns_null(self):
with tempfile.TemporaryFile(mode="r+") as temp_file:
fd = temp_file.fileno()
line = self.libft.get_next_line(fd)
assert line == self.ffi.NULL
def test_valid_file_with_nl_returns_nl(self):
expected_line = b"\n"
with tempfile.TemporaryFile(mode="r+") as temp_file:
fd = temp_file.fileno()
temp_file.write("\n")
temp_file.seek(0)
line = self.libft.get_next_line(fd)
assert self.ffi.string(line) == expected_line
def test_valid_file_with_text_no_nl_returns_text(self):
expected_line = b"Hello, World!"
with tempfile.TemporaryFile(mode="r+") as temp_file:
fd = temp_file.fileno()
temp_file.write("Hello, World!")
temp_file.seek(0)
line = self.libft.get_next_line(fd)
assert self.ffi.string(line) == expected_line
def test_valid_file_with_text_with_nl_returns_line(self):
expected_line = b"Hello, World!\n"
with tempfile.TemporaryFile(mode="r+") as temp_file:
fd = temp_file.fileno()
temp_file.write("Hello, World!\n")
temp_file.seek(0)
line = self.libft.get_next_line(fd)
assert self.ffi.string(line) == expected_line
def test_valid_file_with_multiline_text_no_last_nl_returns_line(self):
expected_lines = [
b"Hello, World!\n",
b"This is a test!",
]
with tempfile.TemporaryFile(mode="r+") as temp_file:
fd = temp_file.fileno()
temp_file.write("Hello, World!\nThis is a test!")
temp_file.seek(0)
first_line = self.libft.get_next_line(fd)
second_line = self.libft.get_next_line(fd)
eof = self.libft.get_next_line(fd)
assert self.ffi.string(first_line) == expected_lines[0]
assert self.ffi.string(second_line) == expected_lines[1]
assert eof == self.ffi.NULL
def test_valid_file_with_multiline_text_with_last_nl_returns_line(self):
expected_lines = [
b"Hello, World!\n",
b"This is a test!\n",
]
with tempfile.TemporaryFile(mode="r+") as temp_file:
fd = temp_file.fileno()
temp_file.write("Hello, World!\nThis is a test!\n")
temp_file.seek(0)
first_line = self.libft.get_next_line(fd)
second_line = self.libft.get_next_line(fd)
eof = self.libft.get_next_line(fd)
assert self.ffi.string(first_line) == expected_lines[0]
assert self.ffi.string(second_line) == expected_lines[1]
assert eof == self.ffi.NULL, self.ffi.string(eof)
def test_big_file_just_for_the_lulz(self):
lines = list()
expected_lines = [b"NewLine\n" for _ in range(42)]
expected_lines.append(b"NoNewLine")
with tempfile.TemporaryFile(mode="r+") as temp_file:
fd = temp_file.fileno()
temp_file.write("NewLine\n" * 42 + "NoNewLine")
temp_file.seek(0)
while True:
line = self.libft.get_next_line(fd)
if line != self.ffi.NULL:
lines.append(line)
else:
break
for line, expected_line in zip(lines, expected_lines):
assert self.ffi.string(line) == expected_line
if __name__ == "__main__":
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment