From 678dbd0846af3bb7c1ede49ccb571499f1165edc Mon Sep 17 00:00:00 2001 From: Aleksei Chubukov Date: Thu, 23 Nov 2023 23:06:22 +0400 Subject: [PATCH] rework of tests in main --- Makefile | 4 ++-- c02/ex00.c | 2 +- c02/ex01.c | 41 ++++++++++++++++++++++++++++++++++ c02/common.c => common/tests.c | 11 +-------- common/tests.h | 14 ++++++++++++ 5 files changed, 59 insertions(+), 13 deletions(-) create mode 100644 c02/ex01.c rename c02/common.c => common/tests.c (53%) create mode 100644 common/tests.h diff --git a/Makefile b/Makefile index 0a757af..31db7a2 100644 --- a/Makefile +++ b/Makefile @@ -18,8 +18,8 @@ clean: .PHONY: all norme clean .SECONDEXPANSION: -build/c%: $(OFFSHORE_PROJECTS)/c$$*/*.c $$(OFFSHORE_MAINS)/c$$*.c +build/c%: $(OFFSHORE_PROJECTS)/c$$*/*.c $$(OFFSHORE_MAINS)/c$$*.c $$(wildcard $(OFFSHORE_MAINS)/common/*.c) mkdir -p $(@D) - $(CC) -o $@ $(CFLAGS_MANDATORY) $(CFLAGS) $^ + $(CC) -o $@ $(CFLAGS_MANDATORY) -Icommon $(CFLAGS) $^ norme: $$(wildcard $(OFFSHORE_PROJECTS)/c*) norminette -R CheckForbiddenSourceHeader $^ diff --git a/c02/ex00.c b/c02/ex00.c index 8290f98..9bf26b3 100644 --- a/c02/ex00.c +++ b/c02/ex00.c @@ -1,4 +1,4 @@ -#include "common.c" +#include "tests.h" char *ft_strcpy(char *dest, char *src); int main(void) { diff --git a/c02/ex01.c b/c02/ex01.c new file mode 100644 index 0000000..a58ecb1 --- /dev/null +++ b/c02/ex01.c @@ -0,0 +1,41 @@ +#include "tests.h" +char *ft_strncpy(char*, char*, size_t); +int main(void) +{ + char s1[100] = TEST_SOURCE; + char d1[100] = TEST_DESTINATION; + char*r1; + char s2[100] = TEST_SOURCE; + char d2[100] = TEST_DESTINATION; + char*r2; + char s3[100] = TEST_SOURCE; + char d3[100] = TEST_DESTINATION; + char*r3; + char s4[100] = TEST_SOURCE; + char d4[100] = TEST_DESTINATION; + char*r4; + + + printf("initial strings:\n"); + dump(s1, TEST_SOURCE_LENGHT); + dump(d1, TEST_DESTINATION_LENGHT); + r1 = strncpy(d1 ,s1, 4); + printf("strncpy(d, s, 4):\n"); + dump(s1, TEST_SOURCE_LENGHT); + dump(d1, TEST_DESTINATION_LENGHT); + r2 = ft_strncpy(d2, s2, 4); + printf("ft_strncpy(d, s, 4:\n"); + dump(s2, TEST_SOURCE_LENGHT); + dump(d2, TEST_DESTINATION_LENGHT); + printf("buffers comparison: s:%i d:%i\n", bufcmp(s1, s2, TEST_SOURCE_LENGHT), bufcmp(d1, d2, TEST_DESTINATION_LENGHT)); + r3 = strncpy(d3 ,s3, 20); + printf("strncpy(d, s, 20):\n"); + dump(s3, TEST_SOURCE_LENGHT); + dump(d3, TEST_DESTINATION_LENGHT); + r4 = ft_strncpy(d4, s4, 20); + printf("ft_strncpy(d, s, 20):\n"); + dump(s4, TEST_SOURCE_LENGHT); + dump(d4, TEST_DESTINATION_LENGHT); + printf("buffers comparison: s:%i d:%i\n", bufcmp(s3, s4, TEST_SOURCE_LENGHT), bufcmp(d3, d4, TEST_DESTINATION_LENGHT)); + printf("return values comparison: %li %li %li %li\n", d1 - r1, d2 -r2, d3 - r3, d4 - r4); +} diff --git a/c02/common.c b/common/tests.c similarity index 53% rename from c02/common.c rename to common/tests.c index c1c70e3..2539c4f 100644 --- a/c02/common.c +++ b/common/tests.c @@ -1,13 +1,4 @@ -#include -#include -#define TEST_SOURCE "HeLlo world!\0It's a\tBEAUTIFUL\tday!" -#define TEST_SOURCE_LENGHT 35 -#define TEST_DESTINATION "GooDBye\0and thanks \tfor\rall the fish!" -#define TEST_DESTINATION_LENGHT 38 -#define FG_WHT "\x1B[37m" -#define FG_MAG "\x1B[35m" -#define COLOR_RESET "\x1B[0m" - +#include "tests.h" void dump(char *data, int len) diff --git a/common/tests.h b/common/tests.h new file mode 100644 index 0000000..ad40d89 --- /dev/null +++ b/common/tests.h @@ -0,0 +1,14 @@ +#ifndef TESTS_H +#define TESTS_H +#include +#include +#define TEST_SOURCE "HeLlo world!\0It's a\tBEAUTIFUL\tday!" +#define TEST_SOURCE_LENGHT 35 +#define TEST_DESTINATION "GooDBye\0and thanks \tfor\rall the fish!" +#define TEST_DESTINATION_LENGHT 38 +#define FG_WHT "\x1B[37m" +#define FG_MAG "\x1B[35m" +#define COLOR_RESET "\x1B[0m" +#endif +int bufcmp(char *this, char *other, int len); +void dump(char *data, int len);