Skip to content

Commit

Permalink
structure tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lucas-mior committed Apr 6, 2024
1 parent dbea18a commit 7447767
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 131 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ tags
*.o
*.tags.vim
.cache
*.exe
7 changes: 3 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -48,4 +47,4 @@ uninstall: all
rm -f $(DESTDIR)$(PREFIX)/man/man1/brn2.1

clean:
rm -rf ./brn2 *.o ./test
rm -rf ./brn2 *.o
12 changes: 12 additions & 0 deletions brn2.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
101 changes: 101 additions & 0 deletions hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 <stdlib.h>
#include <stdarg.h>
#include <setjmp.h>

#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;
}
127 changes: 0 additions & 127 deletions test.c

This file was deleted.

11 changes: 11 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 7447767

Please sign in to comment.