Solved exercise 2 - create a simple tree program clone
This commit is contained in:
27
exercises/tree/Makefile
Normal file
27
exercises/tree/Makefile
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
CC = gcc
|
||||||
|
CFLAGS = -Wall -Wextra -std=c11 -g
|
||||||
|
SRC = main.c tree.c
|
||||||
|
OBJ = $(SRC:.c=.o)
|
||||||
|
EXEC = tree
|
||||||
|
|
||||||
|
# Main target
|
||||||
|
all: $(EXEC)
|
||||||
|
|
||||||
|
# Build executable
|
||||||
|
$(EXEC): $(OBJ)
|
||||||
|
$(CC) $(CFLAGS) -o $@ $^
|
||||||
|
|
||||||
|
# Run test suite
|
||||||
|
test: all
|
||||||
|
@cd test && ./run_tests.sh
|
||||||
|
|
||||||
|
# Clean object files and executable
|
||||||
|
clean:
|
||||||
|
rm -f $(OBJ) $(EXEC)
|
||||||
|
|
||||||
|
# Full cleanup (also test artifacts)
|
||||||
|
distclean: clean
|
||||||
|
cd test && rm -f actual_output.txt valgrind.log expected_output.txt && rm -rf testdir
|
||||||
|
|
||||||
|
.PHONY: all test clean distclean
|
||||||
|
|
||||||
15
exercises/tree/main.c
Normal file
15
exercises/tree/main.c
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "tree.h"
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
|
const char *path = (argc > 1) ? argv[1] : ".";
|
||||||
|
|
||||||
|
if (print_tree(path, 0) == -1) {
|
||||||
|
perror("Error traversing directory");
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
68
exercises/tree/test/run_tests.sh
Executable file
68
exercises/tree/test/run_tests.sh
Executable file
@@ -0,0 +1,68 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
TREE_EXEC="../tree"
|
||||||
|
TEST_DIR="testdir"
|
||||||
|
REF_OUTPUT="expected_output.txt"
|
||||||
|
OUT_FILE="actual_output.txt"
|
||||||
|
VALGRIND_LOG="valgrind.log"
|
||||||
|
|
||||||
|
# Preparazione struttura di test
|
||||||
|
setup() {
|
||||||
|
rm -rf "$TEST_DIR"
|
||||||
|
mkdir -p "$TEST_DIR/sub1"
|
||||||
|
mkdir -p "$TEST_DIR/sub2"
|
||||||
|
touch "$TEST_DIR/file1.txt"
|
||||||
|
touch "$TEST_DIR/sub1/file2.txt"
|
||||||
|
touch "$TEST_DIR/sub2/file3.txt"
|
||||||
|
|
||||||
|
cat > "$REF_OUTPUT" <<EOF
|
||||||
|
|-- file1.txt
|
||||||
|
|-- sub1
|
||||||
|
|-- file2.txt
|
||||||
|
|-- sub2
|
||||||
|
|-- file3.txt
|
||||||
|
EOF
|
||||||
|
}
|
||||||
|
|
||||||
|
# Esecuzione tree e confronto output
|
||||||
|
run_test() {
|
||||||
|
echo "[*] Running functional test..."
|
||||||
|
"$TREE_EXEC" "$TEST_DIR" | grep -v "^\\." > "$OUT_FILE"
|
||||||
|
|
||||||
|
if diff -q "$OUT_FILE" "$REF_OUTPUT"; then
|
||||||
|
echo "[✓] Output matches expected output."
|
||||||
|
else
|
||||||
|
echo "[✗] Output differs!"
|
||||||
|
diff "$OUT_FILE" "$REF_OUTPUT"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# Valgrind check
|
||||||
|
run_valgrind() {
|
||||||
|
echo "[*] Running Valgrind..."
|
||||||
|
valgrind --leak-check=full --log-file="$VALGRIND_LOG" "$TREE_EXEC" "$TEST_DIR" > /dev/null
|
||||||
|
|
||||||
|
if grep -q "no leaks are possible" "$VALGRIND_LOG"; then
|
||||||
|
echo "[✓] Valgrind: No memory leaks."
|
||||||
|
else
|
||||||
|
echo "[✗] Valgrind detected memory issues:"
|
||||||
|
cat "$VALGRIND_LOG"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
cleanup() {
|
||||||
|
rm -rf "$TEST_DIR" "$OUT_FILE" "$VALGRIND_LOG" "$REF_OUTPUT"
|
||||||
|
}
|
||||||
|
|
||||||
|
main() {
|
||||||
|
setup
|
||||||
|
run_test || exit 1
|
||||||
|
run_valgrind || exit 1
|
||||||
|
cleanup
|
||||||
|
echo "[✓] All tests passed."
|
||||||
|
}
|
||||||
|
|
||||||
|
main
|
||||||
|
|
||||||
10
exercises/tree/test/test_dirs.sh
Executable file
10
exercises/tree/test/test_dirs.sh
Executable file
@@ -0,0 +1,10 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
# Crea una struttura di directory per test
|
||||||
|
|
||||||
|
mkdir -p testdir/{sub1,sub2}
|
||||||
|
touch testdir/file1.txt
|
||||||
|
touch testdir/sub1/file2.txt
|
||||||
|
touch testdir/sub2/file3.txt
|
||||||
|
|
||||||
|
echo "Directory structure created."
|
||||||
|
|
||||||
35
exercises/tree/tree.c
Normal file
35
exercises/tree/tree.c
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <dirent.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include "tree.h"
|
||||||
|
|
||||||
|
int print_tree(const char *path, int depth) {
|
||||||
|
DIR *dir = opendir(path);
|
||||||
|
if (!dir) return -1;
|
||||||
|
|
||||||
|
struct dirent *entry;
|
||||||
|
while ((entry = readdir(dir)) != NULL) {
|
||||||
|
// Skip . and ..
|
||||||
|
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
for (int i = 0; i < depth; i++) printf(" ");
|
||||||
|
printf("|-- %s\n", entry->d_name);
|
||||||
|
|
||||||
|
// Construct full path
|
||||||
|
char full_path[4096];
|
||||||
|
snprintf(full_path, sizeof(full_path), "%s/%s", path, entry->d_name);
|
||||||
|
|
||||||
|
struct stat st;
|
||||||
|
if (stat(full_path, &st) == 0 && S_ISDIR(st.st_mode)) {
|
||||||
|
print_tree(full_path, depth + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
closedir(dir);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
7
exercises/tree/tree.h
Normal file
7
exercises/tree/tree.h
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
#ifndef TREE_H
|
||||||
|
#define TREE_H
|
||||||
|
|
||||||
|
int print_tree(const char *path, int depth);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
Reference in New Issue
Block a user