Mandatory tested and finish
This commit is contained in:
55
.gitignore
vendored
Normal file
55
.gitignore
vendored
Normal file
@@ -0,0 +1,55 @@
|
||||
# Prerequisites
|
||||
*.d
|
||||
|
||||
# Object files
|
||||
*.o
|
||||
*.ko
|
||||
*.obj
|
||||
*.elf
|
||||
|
||||
# Linker output
|
||||
*.ilk
|
||||
*.map
|
||||
*.exp
|
||||
|
||||
# Precompiled Headers
|
||||
*.gch
|
||||
*.pch
|
||||
|
||||
# Libraries
|
||||
*.lib
|
||||
*.a
|
||||
*.la
|
||||
*.lo
|
||||
|
||||
# Shared objects (inc. Windows DLLs)
|
||||
*.dll
|
||||
*.so
|
||||
*.so.*
|
||||
*.dylib
|
||||
|
||||
# Executables
|
||||
*.exe
|
||||
*.out
|
||||
*.app
|
||||
*.i*86
|
||||
*.x86_64
|
||||
*.hex
|
||||
|
||||
# Debug files
|
||||
*.dSYM/
|
||||
*.su
|
||||
*.idb
|
||||
*.pdb
|
||||
|
||||
# Kernel Module Compile Results
|
||||
*.mod*
|
||||
*.cmd
|
||||
.tmp_versions/
|
||||
modules.order
|
||||
Module.symvers
|
||||
Mkfile.old
|
||||
dkms.conf
|
||||
|
||||
# debug information files
|
||||
*.dwo
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/11/17 10:52:23 by dgaillet #+# #+# */
|
||||
/* Updated: 2025/11/17 12:34:18 by dgaillet ### ########lyon.fr */
|
||||
/* Updated: 2025/11/17 12:47:41 by dgaillet ### ########lyon.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -17,11 +17,11 @@ int ft_printf(const char *first_arg, ...);
|
||||
|
||||
int ft_putnbr_base(unsigned long long nbr, char *base, int base_size);
|
||||
|
||||
int print_char(char c);
|
||||
int print_char(char c);
|
||||
int print_str(char *str);
|
||||
int print_pointer(unsigned long long p);
|
||||
int print_number(int nbr);
|
||||
int print_unsigned(unsigned int nbr);
|
||||
int print_hex(unsigned long long p, int upper);
|
||||
int print_hex(unsigned long long p, int upper);
|
||||
|
||||
#endif
|
||||
|
||||
BIN
libftprintf.a
BIN
libftprintf.a
Binary file not shown.
@@ -6,32 +6,32 @@
|
||||
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/11/17 10:54:03 by dgaillet #+# #+# */
|
||||
/* Updated: 2025/11/17 12:35:20 by dgaillet ### ########lyon.fr */
|
||||
/* Updated: 2025/11/17 12:47:03 by dgaillet ### ########lyon.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <unistd.h>
|
||||
#include "../include/ft_printf.h"
|
||||
#include "ft_printf.h"
|
||||
|
||||
static int ft_print_arg(char c, va_list args)
|
||||
{
|
||||
if (c == 'c')
|
||||
return (print_char(va_arg(args, int)));
|
||||
else if (c == 's')
|
||||
return (print_str(va_arg(args, char *)));
|
||||
return (print_str(va_arg(args, char *)));
|
||||
else if (c == 'p')
|
||||
return (print_pointer(va_arg(args, unsigned long long)));
|
||||
return (print_pointer(va_arg(args, unsigned long long)));
|
||||
else if (c == 'd' || c == 'i')
|
||||
return (print_number(va_arg(args, int)));
|
||||
return (print_number(va_arg(args, int)));
|
||||
else if (c == 'u')
|
||||
return (print_unsigned(va_arg(args, unsigned int)));
|
||||
return (print_unsigned(va_arg(args, unsigned int)));
|
||||
else if (c == 'x')
|
||||
return (print_hex(va_arg(args, unsigned int), 0));
|
||||
return (print_hex(va_arg(args, unsigned int), 0));
|
||||
else if (c == 'X')
|
||||
return (print_hex(va_arg(args, unsigned int), 1));
|
||||
return (print_hex(va_arg(args, unsigned int), 1));
|
||||
else if (c == '%')
|
||||
return (print_char('%'));
|
||||
return (print_char('%'));
|
||||
return (0);
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ int ft_printf(const char *first_arg, ...)
|
||||
{
|
||||
if (first_arg[i] == '%')
|
||||
{
|
||||
i++;
|
||||
i++;
|
||||
nb_print += ft_print_arg(first_arg[i], args);
|
||||
}
|
||||
else
|
||||
|
||||
@@ -1,2 +0,0 @@
|
||||
src/ft_printf.o: src/ft_printf.c src/../include/ft_printf.h
|
||||
src/../include/ft_printf.h:
|
||||
BIN
src/ft_printf.o
BIN
src/ft_printf.o
Binary file not shown.
@@ -6,11 +6,11 @@
|
||||
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/11/17 12:16:44 by dgaillet #+# #+# */
|
||||
/* Updated: 2025/11/17 12:17:14 by dgaillet ### ########lyon.fr */
|
||||
/* Updated: 2025/11/17 12:43:17 by dgaillet ### ########lyon.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../libft/libft.h"
|
||||
#include "libft.h"
|
||||
|
||||
int ft_putnbr_base(unsigned long long nbr, char *base, int base_size)
|
||||
{
|
||||
|
||||
@@ -1,2 +0,0 @@
|
||||
src/ft_putnbr_base.o: src/ft_putnbr_base.c src/../libft/libft.h
|
||||
src/../libft/libft.h:
|
||||
Binary file not shown.
@@ -6,13 +6,13 @@
|
||||
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/11/17 11:56:36 by dgaillet #+# #+# */
|
||||
/* Updated: 2025/11/17 12:01:47 by dgaillet ### ########lyon.fr */
|
||||
/* Updated: 2025/11/17 12:45:57 by dgaillet ### ########lyon.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
int print_char(char c)
|
||||
int print_char(char c)
|
||||
{
|
||||
return (write(1, &c, 1));
|
||||
}
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
src/print_char.o: src/print_char.c
|
||||
BIN
src/print_char.o
BIN
src/print_char.o
Binary file not shown.
@@ -6,11 +6,11 @@
|
||||
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/11/17 12:06:35 by dgaillet #+# #+# */
|
||||
/* Updated: 2025/11/17 12:38:35 by dgaillet ### ########lyon.fr */
|
||||
/* Updated: 2025/11/17 12:46:47 by dgaillet ### ########lyon.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../include/ft_printf.h"
|
||||
#include "ft_printf.h"
|
||||
#include <unistd.h>
|
||||
|
||||
int print_hex(unsigned long long nbr, int upper)
|
||||
@@ -21,5 +21,4 @@ int print_hex(unsigned long long nbr, int upper)
|
||||
return (ft_putnbr_base(nbr, "0123456789ABCDEF", 16));
|
||||
else
|
||||
return (ft_putnbr_base(nbr, "0123456789abcdef", 16));
|
||||
|
||||
}
|
||||
|
||||
@@ -1,2 +0,0 @@
|
||||
src/print_hex.o: src/print_hex.c src/../include/ft_printf.h
|
||||
src/../include/ft_printf.h:
|
||||
BIN
src/print_hex.o
BIN
src/print_hex.o
Binary file not shown.
@@ -6,21 +6,24 @@
|
||||
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/11/17 12:26:23 by dgaillet #+# #+# */
|
||||
/* Updated: 2025/11/17 12:31:22 by dgaillet ### ########lyon.fr */
|
||||
/* Updated: 2025/11/17 12:56:40 by dgaillet ### ########lyon.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../include/ft_printf.h"
|
||||
#include "ft_printf.h"
|
||||
#include <unistd.h>
|
||||
|
||||
int print_number(int nbr)
|
||||
{
|
||||
unsigned int p_nbr;
|
||||
|
||||
if (nbr == 0)
|
||||
return (write(1, "0", 1));
|
||||
if (nbr < 0)
|
||||
{
|
||||
write(1, "-", 1);
|
||||
return (1 + ft_putnbr_base(nbr * -1, "0123456789", 10));
|
||||
p_nbr = nbr * -1;
|
||||
return (1 + ft_putnbr_base(p_nbr, "0123456789", 10));
|
||||
}
|
||||
return (ft_putnbr_base(nbr, "0123456789", 10));
|
||||
}
|
||||
|
||||
@@ -1,2 +0,0 @@
|
||||
src/print_number.o: src/print_number.c src/../include/ft_printf.h
|
||||
src/../include/ft_printf.h:
|
||||
Binary file not shown.
@@ -6,15 +6,20 @@
|
||||
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/11/17 12:19:16 by dgaillet #+# #+# */
|
||||
/* Updated: 2025/11/17 12:25:20 by dgaillet ### ########lyon.fr */
|
||||
/* Updated: 2025/11/17 12:53:27 by dgaillet ### ########lyon.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../libft/libft.h"
|
||||
#include "../include/ft_printf.h"
|
||||
#include "libft.h"
|
||||
#include "ft_printf.h"
|
||||
|
||||
int print_pointer(unsigned long long p)
|
||||
{
|
||||
if (!p)
|
||||
{
|
||||
ft_putstr_fd("(nil)", 1);
|
||||
return (5);
|
||||
}
|
||||
ft_putstr_fd("0x", 1);
|
||||
return (2 + ft_putnbr_base(p, "0123456789abcdef", 16));
|
||||
}
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
src/print_pointer.o: src/print_pointer.c src/../libft/libft.h \
|
||||
src/../include/ft_printf.h
|
||||
src/../libft/libft.h:
|
||||
src/../include/ft_printf.h:
|
||||
Binary file not shown.
@@ -6,14 +6,19 @@
|
||||
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/11/17 12:02:03 by dgaillet #+# #+# */
|
||||
/* Updated: 2025/11/17 12:03:27 by dgaillet ### ########lyon.fr */
|
||||
/* Updated: 2025/11/17 12:51:56 by dgaillet ### ########lyon.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../libft/libft.h"
|
||||
#include "libft.h"
|
||||
|
||||
int print_str(char *str)
|
||||
{
|
||||
if (!str)
|
||||
{
|
||||
ft_putstr_fd("(null)", 1);
|
||||
return (6);
|
||||
}
|
||||
ft_putstr_fd(str, 1);
|
||||
return (ft_strlen(str));
|
||||
}
|
||||
|
||||
@@ -1,2 +0,0 @@
|
||||
src/print_str.o: src/print_str.c src/../libft/libft.h
|
||||
src/../libft/libft.h:
|
||||
BIN
src/print_str.o
BIN
src/print_str.o
Binary file not shown.
@@ -6,11 +6,11 @@
|
||||
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/11/17 12:26:23 by dgaillet #+# #+# */
|
||||
/* Updated: 2025/11/17 12:34:00 by dgaillet ### ########lyon.fr */
|
||||
/* Updated: 2025/11/17 12:44:47 by dgaillet ### ########lyon.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../include/ft_printf.h"
|
||||
#include "ft_printf.h"
|
||||
#include <unistd.h>
|
||||
|
||||
int print_unsigned(unsigned int nbr)
|
||||
|
||||
@@ -1,2 +0,0 @@
|
||||
src/print_unsigned.o: src/print_unsigned.c src/../include/ft_printf.h
|
||||
src/../include/ft_printf.h:
|
||||
Binary file not shown.
114
test.c
Normal file
114
test.c
Normal file
@@ -0,0 +1,114 @@
|
||||
#include <stdio.h>
|
||||
#include <limits.h>
|
||||
#include "include/ft_printf.h"
|
||||
|
||||
void test(const char *name, int (*test_func)(void))
|
||||
{
|
||||
printf("\n===== %s =====\n", name);
|
||||
test_func();
|
||||
}
|
||||
|
||||
int test_char(void)
|
||||
{
|
||||
int r1, r2;
|
||||
|
||||
r1 = printf("printf: [%c]\n", 'A');
|
||||
r2 = ft_printf("ft_printf: [%c]\n", 'A');
|
||||
printf("Return printf: %d | Return ft_printf: %d\n", r1, r2);
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
int test_string(void)
|
||||
{
|
||||
int r1, r2;
|
||||
|
||||
r1 = printf("printf: [%s]\n", "Hello");
|
||||
r2 = ft_printf("ft_printf: [%s]\n", "Hello");
|
||||
printf("Return printf: %d | Return ft_printf: %d\n", r1, r2);
|
||||
|
||||
r1 = printf("printf: [%s]\n", NULL);
|
||||
r2 = ft_printf("ft_printf: [%s]\n", NULL);
|
||||
printf("Return printf: %d | Return ft_printf: %d\n", r1, r2);
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
int test_pointer(void)
|
||||
{
|
||||
int x = 42;
|
||||
int r1, r2;
|
||||
|
||||
r1 = printf("printf: [%p]\n", &x);
|
||||
r2 = ft_printf("ft_printf: [%p]\n", &x);
|
||||
printf("Return printf: %d | Return ft_printf: %d\n", r1, r2);
|
||||
|
||||
r1 = printf("printf: [%p]\n", NULL);
|
||||
r2 = ft_printf("ft_printf: [%p]\n", NULL);
|
||||
printf("Return printf: %d | Return ft_printf: %d\n", r1, r2);
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
int test_decimal(void)
|
||||
{
|
||||
int r1, r2;
|
||||
|
||||
r1 = printf("printf: [%d] [%i]\n", 42, -42);
|
||||
r2 = ft_printf("ft_printf: [%d] [%i]\n", 42, -42);
|
||||
printf("Return printf: %d | Return ft_printf: %d\n", r1, r2);
|
||||
|
||||
r1 = printf("printf: [%d]\n", INT_MIN);
|
||||
r2 = ft_printf("ft_printf: [%d]\n", INT_MIN);
|
||||
printf("Return printf: %d | Return ft_printf: %d\n", r1, r2);
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
int test_unsigned(void)
|
||||
{
|
||||
int r1, r2;
|
||||
|
||||
r1 = printf("printf: [%u]\n", 4294967295u);
|
||||
r2 = ft_printf("ft_printf: [%u]\n", 4294967295u);
|
||||
printf("Return printf: %d | Return ft_printf: %d\n", r1, r2);
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
int test_hex(void)
|
||||
{
|
||||
int r1, r2;
|
||||
|
||||
r1 = printf("printf: [%x] [%X]\n", 3735928559u, 3735928559u);
|
||||
r2 = ft_printf("ft_printf: [%x] [%X]\n", 3735928559u, 3735928559u);
|
||||
printf("Return printf: %d | Return ft_printf: %d\n", r1, r2);
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
int test_percent(void)
|
||||
{
|
||||
int r1, r2;
|
||||
|
||||
r1 = printf("printf: [%%]\n");
|
||||
r2 = ft_printf("ft_printf: [%%]\n");
|
||||
printf("Return printf: %d | Return ft_printf: %d\n", r1, r2);
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
test("CHAR", test_char);
|
||||
test("STRING", test_string);
|
||||
test("POINTER", test_pointer);
|
||||
test("DECIMAL", test_decimal);
|
||||
test("UNSIGNED", test_unsigned);
|
||||
test("HEX", test_hex);
|
||||
test("PERCENT", test_percent);
|
||||
|
||||
printf("\n===== ALL TESTS DONE =====\n");
|
||||
return (0);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user