From 74477676aa1145f3909ed364f0ed02f65cc59609 Mon Sep 17 00:00:00 2001 From: lucas-mior Date: Sat, 6 Apr 2024 16:49:42 -0300 Subject: [PATCH] structure tests --- .gitignore | 1 + Makefile | 7 ++- brn2.c | 12 +++++ hash.c | 101 ++++++++++++++++++++++++++++++++++++++++++ test.c | 127 ----------------------------------------------------- test.sh | 11 +++++ 6 files changed, 128 insertions(+), 131 deletions(-) delete mode 100644 test.c create mode 100755 test.sh diff --git a/.gitignore b/.gitignore index 4b13b53..b607d89 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ tags *.o *.tags.vim .cache +*.exe diff --git a/Makefile b/Makefile index e559224..2e3c46a 100644 --- a/Makefile +++ b/Makefile @@ -27,9 +27,8 @@ release: brn2 debug: CFLAGS += -g -fsanitize=undefined debug: clean brn2 -test: $(objs) test.c - $(CC) $(CFLAGS) -o $@ $(objs) test.c -lcmocka $(LDFLAGS) - ./test +test: util.c hash.c brn2.c + ./test.sh brn2: $(objs) main.c -ctags --kinds-C=+l *.h *.c @@ -48,4 +47,4 @@ uninstall: all rm -f $(DESTDIR)$(PREFIX)/man/man1/brn2.1 clean: - rm -rf ./brn2 *.o ./test + rm -rf ./brn2 *.o diff --git a/brn2.c b/brn2.c index 13d3840..69771b6 100644 --- a/brn2.c +++ b/brn2.c @@ -446,3 +446,15 @@ brn2_usage(FILE *stream) { " : Rename filenames listed in this argument.\n"); exit((int)(stream != stdout)); } + +#ifndef MAIN +#define MAIN 0 +#endif + +#if MAIN +// flags: hash.o util.o +int main(int argc, char **argv) { + brn2_usage(stdout); + exit(0); +} +#endif diff --git a/hash.c b/hash.c index e1a13ba..4339ba2 100644 --- a/hash.c +++ b/hash.c @@ -310,3 +310,104 @@ hash_map_expected_collisions(HashMap *map) { long double result = n - m * (1 - powl((m - 1)/m, n)); return (uint32) (roundl(result)); } + +#include +#include +#include + +#include "brn2.h" +#include "hash.h" +#include "util.h" + +static char * +random_string(void) { + struct timespec t; + clock_gettime(CLOCK_MONOTONIC_RAW, &t); + srand(t.tv_nsec); + + const char ALLOWED[] = "abcdefghijklmnopqrstuvwxyz1234567890"; + char *random_string = util_malloc(11); + + for (int i = 0; i < 10; i += 1) { + int c = rand() % (sizeof(ALLOWED) - 1); + random_string[i] = ALLOWED[c]; + } + + random_string[10] = '\0'; + + return random_string; +} + +#define NSTRINGS 4096 + +static void +hash_test(void **state) { + HashMap *map = hash_map_create(NSTRINGS); + assert_non_null(map); + assert_true(hash_map_capacity(map) >= NSTRINGS); + + assert_true(hash_map_insert(map, "a", 0)); + assert_false(hash_map_insert(map, "a", 1)); + assert_true(hash_map_insert(map, "b", 2)); + + for (int i = 0; i < NSTRINGS; i += 1) { + char *key = random_string(); + int value = rand(); + assert_true(hash_map_insert(map, key, value)); + } + + printf("\nOriginal hash map:\n"); + hash_map_print_summary(map); + + uint32 collisions_before = hash_map_collisions(map); + map = hash_map_balance(map); + printf("\nAfter balance:\n"); + hash_map_print_summary(map); + assert_true(collisions_before > hash_map_collisions(map)); + + assert_true(hash_map_length(map) == (2 + NSTRINGS)); + assert_true(*(uint32 *) hash_map_lookup(map, "a") == 0); + assert_null(hash_map_lookup(map, "c")); + + assert_false(hash_map_remove(map, "c")); + assert_true(hash_map_remove(map, "b")); + + assert_true(hash_map_length(map) == (1 + NSTRINGS)); + + assert_true(hash_map_remove(map, "a")); + + hash_map_free_keys(map); + hash_map_destroy(map); + (void) state; + return; +} + +static bool +contains_filename(FileList *list, FileName file) { + for (uint32 i = 0; i < list->length; i += 1) { + if (!strcmp(list->files[i].name, file.name)) + return true; + } + return false; +} + +static void +brn2_test(void **state) { + FileList *list1; + FileList *list2; + char *command = "ls -a > /tmp/brn2test"; + char *file = command + 8; + system(command); + list1 = brn2_list_from_dir("."); + list2 = brn2_list_from_lines(file, 0); + + assert_true(list1->length == list2->length); + + for (uint32 i = 0; i < list1->length; i += 1) + assert_true(contains_filename(list2, list1->files[i])); + + brn2_free_list(list1); + brn2_free_list(list2); + unlink(file); + (void) state; +} diff --git a/test.c b/test.c deleted file mode 100644 index 9e8a822..0000000 --- a/test.c +++ /dev/null @@ -1,127 +0,0 @@ -/* - * Copyright (C) 2023 Mior, Lucas; - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -#include -#include -#include -#include - -#include "brn2.h" -#include "hash.h" -#include "util.h" - -static char * -random_string(void) { - struct timespec t; - clock_gettime(CLOCK_MONOTONIC_RAW, &t); - srand(t.tv_nsec); - - const char ALLOWED[] = "abcdefghijklmnopqrstuvwxyz1234567890"; - char *random_string = util_malloc(11); - - for (int i = 0; i < 10; i += 1) { - int c = rand() % (sizeof(ALLOWED) - 1); - random_string[i] = ALLOWED[c]; - } - - random_string[10] = '\0'; - - return random_string; -} - -#define NSTRINGS 4096 - -static void -hash_test(void **state) { - HashMap *map = hash_map_create(NSTRINGS); - assert_non_null(map); - assert_true(hash_map_capacity(map) >= NSTRINGS); - - assert_true(hash_map_insert(map, "a", 0)); - assert_false(hash_map_insert(map, "a", 1)); - assert_true(hash_map_insert(map, "b", 2)); - - for (int i = 0; i < NSTRINGS; i += 1) { - char *key = random_string(); - int value = rand(); - assert_true(hash_map_insert(map, key, value)); - } - - printf("\nOriginal hash map:\n"); - hash_map_print_summary(map); - - uint32 collisions_before = hash_map_collisions(map); - map = hash_map_balance(map); - printf("\nAfter balance:\n"); - hash_map_print_summary(map); - assert_true(collisions_before > hash_map_collisions(map)); - - assert_true(hash_map_length(map) == (2 + NSTRINGS)); - assert_true(*(uint32 *) hash_map_lookup(map, "a") == 0); - assert_null(hash_map_lookup(map, "c")); - - assert_false(hash_map_remove(map, "c")); - assert_true(hash_map_remove(map, "b")); - - assert_true(hash_map_length(map) == (1 + NSTRINGS)); - - assert_true(hash_map_remove(map, "a")); - - hash_map_free_keys(map); - hash_map_destroy(map); - (void) state; - return; -} - -static bool -contains_filename(FileList *list, FileName file) { - for (uint32 i = 0; i < list->length; i += 1) { - if (!strcmp(list->files[i].name, file.name)) - return true; - } - return false; -} - -static void -brn2_test(void **state) { - FileList *list1; - FileList *list2; - char *command = "ls -a > /tmp/brn2test"; - char *file = command + 8; - system(command); - list1 = brn2_list_from_dir("."); - list2 = brn2_list_from_lines(file, 0); - - assert_true(list1->length == list2->length); - - for (uint32 i = 0; i < list1->length; i += 1) - assert_true(contains_filename(list2, list1->files[i])); - - brn2_free_list(list1); - brn2_free_list(list2); - unlink(file); - (void) state; -} - -int main(void) { - const struct CMUnitTest tests[] = { - cmocka_unit_test(hash_test), - cmocka_unit_test(brn2_test), - }; - - return cmocka_run_group_tests(tests, NULL, NULL); -} diff --git a/test.sh b/test.sh new file mode 100755 index 0000000..9fee08d --- /dev/null +++ b/test.sh @@ -0,0 +1,11 @@ +#!/bin/sh + +for src in *.c; do + [ "$src" = "main.c" ] && continue + [ "$src" != "brn2.c" ] && continue + echo "Testing $src...\n" + flags="$(awk '/flags:/ { $1=$2=""; print $0 }' "$src")" + gcc -D MAIN=1 $src -o $src.exe $flags \ + || printf "${RED}Failed to compile $src, is main() defined? ${RES}\n" + ./$src.exe +done