From 627d7b89b7593d7e356fdab4de8cb9bd78537ed4 Mon Sep 17 00:00:00 2001 From: hasslesstech Date: Wed, 11 Dec 2024 22:38:52 +0200 Subject: [PATCH] add testing suite for length difference function --- Makefile | 12 +++- src/tester.c | 148 +++++++++++++++++++++++++++++++++++++++++++++ test/testcases_len | 5 ++ 3 files changed, 163 insertions(+), 2 deletions(-) create mode 100644 src/tester.c create mode 100644 test/testcases_len diff --git a/Makefile b/Makefile index f56e586..97b715d 100644 --- a/Makefile +++ b/Makefile @@ -2,6 +2,14 @@ CFLAGS=-O3 -Iinc/ build: main +test: tester + +tester: out/tester.o out/swapper.o + gcc $(CFLAGS) out/tester.o out/swapper.o -o tester + +out/tester.o: out/ src/tester.c + gcc $(CFLAGS) -c src/tester.c -o out/tester.o + out/: mkdir out/ @@ -15,6 +23,6 @@ out/swapper.o: out/ src/swapper.c gcc $(CFLAGS) -c src/swapper.c -o out/swapper.o clean: - rm -r main out/ + rm -r main tester out/ -.PHONY: build clean +.PHONY: build test clean diff --git a/src/tester.c b/src/tester.c new file mode 100644 index 0000000..a8ecafd --- /dev/null +++ b/src/tester.c @@ -0,0 +1,148 @@ +#include +#include +#include +#include "swapper.h" + +enum read_state { + LEN = 0, + STR1 = 1, + STR2 = 2, + FINISH = 3, + IMPOSSIBLE_STATE = 4 +}; + +static int to_int(char *a) +{ + int value = 0; + + for (int i = 0; i < 4; i++) { + if (a[i] == '\0') + break; + + value *= 10; + value += (int) a[i] - (int) '0'; + } + + return value; +} + +static void inplace_memcpy(char *buf, int offset) +{ + for (int i = 0; i < 1024 - offset; i++) + buf[i] = buf[offset+i]; +} + +static int read_more(int f, char *buf, int bytes_read) +{ + // in-place memcpy + inplace_memcpy(buf, bytes_read); + + // reading more data + int new_bytes = read(f, &(buf[1024 - bytes_read]), bytes_read); + + // returning current amount of bytes in buffer + return 1024 - bytes_read + new_bytes; +} + +static int parse_case(char *cmd, int *target_length_diff, char *str_a, char *str_b) +{ + int bytes_read; + int write_offset = 0; + char len_buffer[4] = {0}; + enum read_state rs = LEN; + + for (bytes_read = 0; bytes_read < 1024; bytes_read++) { + switch (cmd[bytes_read]) { + case ' ': + switch (rs) { + case LEN: + *target_length_diff = to_int(len_buffer); + break; + case STR1: + str_a[write_offset] = '\0'; + break; + } + rs++; + write_offset = 0; + break; + + case '\n': + str_b[write_offset] = '\0'; + bytes_read++; + goto finish; + break; + + default: + switch(rs) { + case LEN: + len_buffer[write_offset] = cmd[bytes_read]; + break; + case STR1: + str_a[write_offset] = cmd[bytes_read]; + break; + case STR2: + str_b[write_offset] = cmd[bytes_read]; + break; + } + write_offset++; + break; + } + } + + finish: + return bytes_read; +} + +static int assert_length(int test_id, int target_length_diff, char *a, char *b) +{ + int len_diff = swapper_length_difference(a, b); + + if (len_diff == target_length_diff) { + printf("[TEST#%02d] OK\r", test_id); + return 0; + } else { + printf("\n[TEST#%02d] FAILED!\n", test_id); + printf("Case details:\n"); + printf("Str1: %s\n", a); + printf("Str2: %s\n", b); + printf("Mismatch: %d expected, %d retrieved\n", target_length_diff, len_diff); + return 1; + } +} + + +int main(void) +{ + int success_tests_counter = 0; + int failed_tests_counter = 0; + + int f = open("test/testcases_len", O_RDONLY); + + if (f < 0) + return 1; + + char cmd[1024]; + int remaining_buffer_length; + remaining_buffer_length = read(f, cmd, 1024); + + while (remaining_buffer_length) { + char str_a[1024], str_b[1024]; + int target_length_diff; + + int bytes_read = parse_case(cmd, &target_length_diff, str_a, str_b); + + if (assert_length(success_tests_counter + failed_tests_counter + 1, target_length_diff, str_a, str_b)) + failed_tests_counter++; + else + success_tests_counter++; + + if (remaining_buffer_length == 1024) + remaining_buffer_length = read_more(f, cmd, bytes_read); + else + inplace_memcpy(cmd, bytes_read); + remaining_buffer_length -= bytes_read; + } + + printf("\nSummary: %d passed, %d failed\n", success_tests_counter, failed_tests_counter); + close(f); +} diff --git a/test/testcases_len b/test/testcases_len new file mode 100644 index 0000000..f74edb7 --- /dev/null +++ b/test/testcases_len @@ -0,0 +1,5 @@ +2 wert tr +6 12345678 12 +0 1234 1234 +3 1 123 +6 12 88888888