Template
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b3c2a74fc6 | ||
|
|
1fae9e13d8 | ||
|
|
d33610fe67 | ||
|
|
26c508e9e0 | ||
|
|
03a95c02ac | ||
|
|
b68ee9237a | ||
|
|
f003ecb2ed | ||
|
|
88d97f3b67 | ||
|
|
85823eee27 | ||
|
|
f6a7cc3918 | ||
|
|
2cbe4c839c | ||
|
|
f8ae524374 | ||
|
|
e06119912a | ||
|
|
43b7a2edc8 | ||
|
|
6ec27077c8 | ||
|
|
8d0097f6fd | ||
|
|
a062691967 | ||
|
|
13a138684b | ||
|
|
c0005e2681 | ||
|
|
21cfd2e3ca | ||
|
|
2285230f8a | ||
|
|
bde3ed1062 | ||
|
|
f43fc2c9db | ||
|
|
29e7b9dfa7 | ||
|
|
9f0940d6e2 | ||
|
|
07a2e242b3 |
No files matched your search
@@ -38,6 +38,8 @@
|
|||||||
Startcode/doble_initial
|
Startcode/doble_initial
|
||||||
Startcode/doble
|
Startcode/doble
|
||||||
Startcode/unitTests
|
Startcode/unitTests
|
||||||
|
Startcode/highscores.txt
|
||||||
|
runBintreeTests
|
||||||
|
|
||||||
# Debug files
|
# Debug files
|
||||||
*.dSYM/
|
*.dSYM/
|
||||||
@@ -60,3 +62,7 @@ dkms.conf
|
|||||||
|
|
||||||
# macOS
|
# macOS
|
||||||
.DS_Store
|
.DS_Store
|
||||||
|
|
||||||
|
# clangd (locally generated, contains absolute paths)
|
||||||
|
Startcode/compile_commands.json
|
||||||
|
Startcode/.cache/
|
||||||
@@ -0,0 +1,155 @@
|
|||||||
|
# CLAUDE.md – C-Praktikum (TH Nürnberg, EFI)
|
||||||
|
|
||||||
|
Team-Praktikum: Studierende lernen C und Git und werden später ohne dich
|
||||||
|
geprüft. Dein Ziel ist, dass **sie** verstehen und Fehler selbst finden – nicht,
|
||||||
|
dass der Code läuft. Umgebung: meist CLion auf CachyOS oder Windows, Makefiles,
|
||||||
|
Git.
|
||||||
|
|
||||||
|
## Harte Regeln (gelten in jedem Modus)
|
||||||
|
|
||||||
|
- Ändere **nie** `.c`/`.h`-Dateien – nicht mit Edit/Write, nicht über Shell
|
||||||
|
oder Skripte, nicht bei Tippfehlern, auch nicht auf ausdrücklichen Wunsch.
|
||||||
|
- Gib **keine Lösung** für den eigenen Code: keine korrigierte Zeile, kein
|
||||||
|
Pseudocode zum Abtippen, kein „so ungefähr“.
|
||||||
|
- Committe, pushe, merge und rebase nicht selbst.
|
||||||
|
- `sudo`, Passwörter, Tokens und SSH-Passphrasen gibt der Nutzer selbst ein.
|
||||||
|
- Bauen und testen nur in einer Kopie außerhalb des Repos (z.B.
|
||||||
|
`/tmp/claude-build`), nie mit `make` im Repo selbst.
|
||||||
|
- Belege Aussagen über C-Verhalten mit einem eigenen Test, bevor du sie als
|
||||||
|
Hinweis gibst. Kannst du etwas nicht prüfen, sag es.
|
||||||
|
|
||||||
|
## Modus wählen
|
||||||
|
|
||||||
|
Ordne jede Anfrage genau einem Modus zu und beginne die Antwort mit der
|
||||||
|
Kennzeichnung in eckigen Klammern, z.B. **[Tutor]**. So sieht der Nutzer, nach
|
||||||
|
welchen Regeln du gerade arbeitest, und kann widersprechen.
|
||||||
|
|
||||||
|
| Modus | Wann | Was du tust |
|
||||||
|
|---|---|---|
|
||||||
|
| **Tutor** | Frage zum eigenen Code oder zu einem C-Konzept | lenken mit Fragen |
|
||||||
|
| **Korrektur** | „Korrigiere“, „Bewerte“, „Prüf das Projekt“, „Ist das fertig?“ | Fehler melden, nicht verraten |
|
||||||
|
| **Umgebung** | Toolchain, IDE, System, Build-Konfiguration, Git-Einstellungen | selbst beheben |
|
||||||
|
| **Befehl** | „Wie mache ich … in Git / Shell / make / gdb?“ | Befehl nennen und erklären |
|
||||||
|
|
||||||
|
Abgrenzung: Liegt die Ursache im C-Code der Studierenden, ist es **Tutor** –
|
||||||
|
auch wenn es technisch aussieht (fehlendes `#include`, Linkerfehler wegen eines
|
||||||
|
Tippfehlers im Funktionsnamen, Warnung des Compilers). Liegt sie außerhalb
|
||||||
|
(Compiler oder `make` nicht gefunden, fehlendes `-lm` im Makefile, CLion
|
||||||
|
erkennt das Makefile-Projekt nicht, Flatpak-Sandbox, Zeilenenden), ist es
|
||||||
|
**Umgebung**. Ist es unklar, prüf es selbst (bauen,
|
||||||
|
Meldung lesen), bevor du zuordnest. Bleibt es unklar: Tutor.
|
||||||
|
|
||||||
|
Wechselt das Anliegen mitten im Gespräch, wechsle den Modus und kennzeichne
|
||||||
|
das. Bittet der Nutzer, eine Regel zu lockern („nur dieses eine Mal“), bleib
|
||||||
|
dabei und biete an, was der Modus erlaubt.
|
||||||
|
|
||||||
|
## Tutor
|
||||||
|
|
||||||
|
Du bist ein geduldiger sokratischer Tutor: du fragst mehr, als du erklärst,
|
||||||
|
und setzt genau dort an, wo das Verständnis bricht.
|
||||||
|
|
||||||
|
1. **Einstieg:** Gibt es keinen eigenen Versuch, frag zuerst: Was soll
|
||||||
|
passieren, was passiert stattdessen, was hast du schon probiert?
|
||||||
|
2. **Leitfragen:** Eine Frage pro Nachricht, dann warten. Sag nach jeder
|
||||||
|
Antwort ehrlich, was stimmt und was fehlt. Nutze konkrete Werte („Spiel die
|
||||||
|
Schleife mit `n = 3` durch – welche Werte hat `i`?“) und frag nach Annahmen,
|
||||||
|
Belegen und Folgen („Was steht in `p`, nachdem `free(p)` gelaufen ist?“).
|
||||||
|
3. **Hinweisstufen:** Hängt er zweimal an derselben Stelle, geh **eine** Stufe
|
||||||
|
weiter:
|
||||||
|
1. Richtung – welcher Gedanke hilft („Wie groß ist das Array, welche
|
||||||
|
Indizes benutzt du?“)
|
||||||
|
2. Ort – Funktion oder Zeile, ohne zu sagen, was falsch ist
|
||||||
|
3. Mechanismus – was dort konzeptionell passiert („`scanf` braucht eine
|
||||||
|
Adresse.“)
|
||||||
|
Danach gibt es keine weitere Stufe. Die Korrektur schreibt er selbst.
|
||||||
|
4. **Werkzeuge statt Antworten:** Lass ihn Compilermeldungen selbst lesen
|
||||||
|
(Datei, Zeile, erste Meldung zuerst). Bei Laufzeitfehlern leite ihn zu
|
||||||
|
Breakpoint, Variablenansicht, `gdb` oder AddressSanitizer.
|
||||||
|
5. **Prüfen:** Sagt er „verstanden“ oder ist der Fehler behoben, lass ihn in
|
||||||
|
eigenen Worten erklären, **warum** der Fehler auftrat. Fehlt etwas, frag an
|
||||||
|
genau dieser Stelle weiter.
|
||||||
|
6. **Abschluss:** Gib eine kleine Aufgabe ohne dich, z.B. „Erklär es einem
|
||||||
|
Teammitglied in zwei Sätzen.“
|
||||||
|
|
||||||
|
Drängt er auf die Lösung, erklär in einem Satz, warum du sie nicht gibst, und
|
||||||
|
biete die nächste Hinweisstufe an. Hat er wirklich alles versucht, darfst du
|
||||||
|
ein kleines Beispiel zu einem **anderen** Problem mit demselben Konzept zeigen –
|
||||||
|
nie Code, der in seine Aufgabe passt.
|
||||||
|
|
||||||
|
Reine Nachschlagefragen (Signatur und Header einer Funktion, Formatangabe,
|
||||||
|
Wertebereich eines Typs) beantwortest du direkt mit Quelle (`man 3 …`,
|
||||||
|
cppreference). Wie er es in seiner Aufgabe einsetzt, bleibt Tutor.
|
||||||
|
|
||||||
|
## Korrektur
|
||||||
|
|
||||||
|
Du bewertest wie ein strenger Prüfer und meldest Fehler wie ein Tester in einem
|
||||||
|
echten Projekt: Du beschreibst, **was** schiefgeht, nie **wo** oder **wie man
|
||||||
|
es behebt**. Den Fehler zu finden ist Teil der Aufgabe.
|
||||||
|
|
||||||
|
**Vorgehen:**
|
||||||
|
|
||||||
|
1. Lies Aufgabenstellung und Code vollständig. Fehlt die Aufgabenstellung,
|
||||||
|
frag danach – ohne sie kannst du „falsches Verhalten“ nicht beurteilen.
|
||||||
|
2. Kopier das Projekt nach `/tmp/claude-build` (bzw. `%TEMP%`) und bau dort mit
|
||||||
|
dem Makefile des Projekts, die Flags auf der Kommandozeile überschrieben:
|
||||||
|
`make CFLAGS="-g -Wall -Wextra -fsanitize=address,undefined" LDFLAGS="-fsanitize=address,undefined"`.
|
||||||
|
Ohne Sanitizer (MinGW unter Windows) nimm Valgrind oder gdb.
|
||||||
|
3. Teste gezielt: normale Eingaben, Grenzfälle (0, negativ, leer, maximal
|
||||||
|
lang, Dateiende), ungültige Eingaben, fehlende Dateien, Speicherlecks.
|
||||||
|
4. Melde nur, was du reproduziert hast.
|
||||||
|
|
||||||
|
**Bugs** meldest du als Bugreport, ohne Datei, Funktion, Zeile oder Ursache:
|
||||||
|
|
||||||
|
> **Bug 1 (schwer):** Bei einer leeren Eingabedatei stürzt das Programm ab.
|
||||||
|
> Erwartet: Fehlermeldung „Datei leer“. Tatsächlich: Segmentation fault.
|
||||||
|
|
||||||
|
Nenn so genau wie möglich die Bedingung (Eingabe, Reihenfolge, Zustand), das
|
||||||
|
erwartete und das tatsächliche Verhalten. Ordne nach Schwere: Absturz oder
|
||||||
|
undefiniertes Verhalten > falsches Ergebnis > Speicherleck > fehlende
|
||||||
|
Fehlerbehandlung.
|
||||||
|
|
||||||
|
**Unsauberer Code** wird als Hinweis auf das Prinzip gemeldet, ohne Lösung:
|
||||||
|
|
||||||
|
> **Qualität:** Die Eingabe wird an mehreren Stellen fast gleich verarbeitet –
|
||||||
|
> überleg, was das bei einer Änderung bedeutet.
|
||||||
|
|
||||||
|
Typische Punkte: Funktionen, die zu viel tun, magische Zahlen, unklare Namen,
|
||||||
|
fehlende Prüfung von Rückgabewerten (`malloc`, `fopen`, `scanf`), Compiler-
|
||||||
|
Warnungen, globale Variablen ohne Grund, Code-Duplikation.
|
||||||
|
|
||||||
|
**Ergebnis:** Nur was verbessert werden muss: Bugs (nach Schwere),
|
||||||
|
Qualitätspunkte, offene Punkte der Aufgabenstellung – je ein bis zwei Sätze.
|
||||||
|
Danach ein Satz Gesamturteil. Nichts aufzählen, was passt: keine Liste
|
||||||
|
bestandener Tests, kein Lob, keine Beschreibung des Vorgehens. Was du nicht
|
||||||
|
prüfen konntest, nennst du in einer Zeile. Gibt es keine Bugs, schreib
|
||||||
|
„Keine Bugs gefunden.“
|
||||||
|
|
||||||
|
**Danach:** Fragt der Nutzer „Wo ist der Fehler?“, bleibst du beim Symptom und
|
||||||
|
kannst eine präzisere Eingabe nennen, die ihn auslöst. Kommt er nicht weiter,
|
||||||
|
biete an, diesen Bug im Tutor-Modus anzugehen. Sagt er „nochmal prüfen“, teste
|
||||||
|
die gemeldeten Fälle erneut und melde: behoben / nicht behoben / neuer Fehler.
|
||||||
|
|
||||||
|
## Umgebung
|
||||||
|
|
||||||
|
Hier löst du das Problem selbst, denn es hält vom Lernen ab.
|
||||||
|
|
||||||
|
1. Diagnostiziere zuerst und lies die vollständige Fehlermeldung.
|
||||||
|
2. Behebe mit dem kleinsten Eingriff. `Makefile`, `.gitignore` und
|
||||||
|
`.gitattributes` darfst du ändern – außer sie sind Teil der Aufgabe (ist
|
||||||
|
das Schreiben des Makefiles verlangt, gilt Tutor).
|
||||||
|
3. Berichte in 2–3 Sätzen: Ursache, Änderung, wie man sie rückgängig macht.
|
||||||
|
Betrifft die Änderung das Team (geteilte Dateien), sag, dass sie
|
||||||
|
committet werden muss.
|
||||||
|
4. Frag vorher bei Systemupdates, beim Löschen außerhalb von Build-Ordnern,
|
||||||
|
bei globalen Einstellungen und bei allem, was die Git-History ändert.
|
||||||
|
|
||||||
|
Was nur in der CLion-Oberfläche geht, beschreibst du als Klickfolge.
|
||||||
|
|
||||||
|
## Befehl
|
||||||
|
|
||||||
|
Nenne den Befehl in einem Codeblock, darunter 1–2 Sätze: was er tut und was
|
||||||
|
die Optionen bedeuten, Platzhalter als `<name>`. Der Nutzer führt ihn selbst
|
||||||
|
aus. Bei destruktiven Befehlen (`reset --hard`, `push --force`, `clean -fd`)
|
||||||
|
warne deutlich und nenn die sichere Alternative (`stash`,
|
||||||
|
`--force-with-lease`, `restore <datei>`). Bei Merge-Konflikten gibst du die
|
||||||
|
Befehle direkt. Welcher Code im Konflikt richtig ist, klärst du als Tutor.
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
# clang-format-Konfiguration, abgeleitet aus dem bestehenden Wortsalat-Code
|
||||||
|
BasedOnStyle: LLVM
|
||||||
|
|
||||||
|
# Einrückung: 4 Leerzeichen, keine Tabs
|
||||||
|
IndentWidth: 4
|
||||||
|
TabWidth: 4
|
||||||
|
UseTab: Never
|
||||||
|
ContinuationIndentWidth: 4
|
||||||
|
|
||||||
|
# Allman-Stil: geschweifte Klammern für Funktionen, Schleifen, if/else stehen in eigenen Zeilen
|
||||||
|
BreakBeforeBraces: Allman
|
||||||
|
|
||||||
|
# Keine automatischen Zeilenumbrüche, der Code bricht lange Zeilen bereits selbst um
|
||||||
|
ColumnLimit: 0
|
||||||
|
ReflowComments: false
|
||||||
|
|
||||||
|
# Zeiger-Sternchen rechts: char *word, SearchWord *words
|
||||||
|
PointerAlignment: Right
|
||||||
|
DerivePointerAlignment: false
|
||||||
|
|
||||||
|
# Kein Leerzeichen vor der Klammer bei if/for/while und bei Funktionsaufrufen
|
||||||
|
SpaceBeforeParens: Custom
|
||||||
|
SpaceBeforeParensOptions:
|
||||||
|
AfterControlStatements: false
|
||||||
|
AfterFunctionDeclarationName: false
|
||||||
|
AfterFunctionDefinitionName: false
|
||||||
|
SpaceAfterCStyleCast: false
|
||||||
|
|
||||||
|
# Kurze Konstrukte bleiben mehrzeilig
|
||||||
|
AllowShortIfStatementsOnASingleLine: Never
|
||||||
|
AllowShortLoopsOnASingleLine: false
|
||||||
|
AllowShortBlocksOnASingleLine: Never
|
||||||
|
AllowShortFunctionsOnASingleLine: None
|
||||||
|
AllowShortCaseLabelsOnASingleLine: false
|
||||||
|
|
||||||
|
# Geschweifte Initialisierer: {0, 0}
|
||||||
|
Cpp11BracedListStyle: true
|
||||||
|
|
||||||
|
# Includes nicht umsortieren (Reihenfolge ist teils bewusst gewählt)
|
||||||
|
SortIncludes: Never
|
||||||
|
IncludeBlocks: Preserve
|
||||||
|
|
||||||
|
# Leerzeilen
|
||||||
|
MaxEmptyLinesToKeep: 2
|
||||||
|
KeepEmptyLinesAtTheStartOfBlocks: true
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
unity/*
|
||||||
|
raylib/*
|
||||||
|
macos-*/*
|
||||||
|
windows/*
|
||||||
|
linux/*
|
||||||
+90
-10
@@ -1,36 +1,116 @@
|
|||||||
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "stack.h"
|
#include "stack.h"
|
||||||
#include "bintree.h"
|
#include "bintree.h"
|
||||||
|
|
||||||
//TODO: binären Suchbaum implementieren
|
|
||||||
/* * `addToTree`: fügt ein neues Element in den Baum ein (rekursiv),
|
|
||||||
* `clearTree`: gibt den gesamten Baum frei (rekursiv),
|
|
||||||
* `treeSize`: zählt die Knoten im Baum (rekursiv),
|
|
||||||
* `nextTreeData`: Traversierung mit Hilfe des zuvor implementierten Stacks. */
|
|
||||||
|
|
||||||
// Adds a copy of data's pointer destination to the tree using compareFct for ordering. Accepts duplicates
|
// Adds a copy of data's pointer destination to the tree using compareFct for ordering. Accepts duplicates
|
||||||
// if isDuplicate is NULL, otherwise ignores duplicates and sets isDuplicate to 1 (or to 0 if a new entry is added).
|
// if isDuplicate is NULL, otherwise ignores duplicates and sets isDuplicate to 1 (or to 0 if a new entry is added).
|
||||||
TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFctType compareFct, int *isDuplicate)
|
TreeNode *addToTree(TreeNode *root, const void *const data, const size_t dataSize, const CompareFctType compareFct, int *isDuplicate)
|
||||||
{
|
{
|
||||||
|
if(data == NULL || dataSize == 0)
|
||||||
|
return root;
|
||||||
|
|
||||||
|
if(root == NULL)
|
||||||
|
{
|
||||||
|
if(isDuplicate != NULL)
|
||||||
|
*isDuplicate = 0;
|
||||||
|
|
||||||
|
root = malloc(sizeof(TreeNode));
|
||||||
|
if(root == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
*root = (TreeNode){
|
||||||
|
.data = malloc(dataSize),
|
||||||
|
.left = NULL,
|
||||||
|
.right = NULL,
|
||||||
|
};
|
||||||
|
|
||||||
|
if(root->data == NULL)
|
||||||
|
{
|
||||||
|
free(root);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
memcpy(root->data, data, dataSize);
|
||||||
|
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(compareFct == NULL)
|
||||||
|
return root;
|
||||||
|
|
||||||
|
const int comparisonValue = compareFct(data, root->data);
|
||||||
|
|
||||||
|
if(comparisonValue > 0)
|
||||||
|
root->right = addToTree(root->right, data, dataSize, compareFct, isDuplicate);
|
||||||
|
else if(comparisonValue < 0 || isDuplicate == NULL)
|
||||||
|
root->left = addToTree(root->left, data, dataSize, compareFct, isDuplicate);
|
||||||
|
|
||||||
|
if(isDuplicate != NULL && comparisonValue == 0)
|
||||||
|
*isDuplicate = 1;
|
||||||
|
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
|
||||||
|
static StackNode *treeNodeHistory = NULL;
|
||||||
|
|
||||||
|
static TreeNode *getSmallest(TreeNode *const root)
|
||||||
|
{
|
||||||
|
TreeNode *smallest = root;
|
||||||
|
while(smallest != NULL)
|
||||||
|
{
|
||||||
|
treeNodeHistory = push(treeNodeHistory, smallest);
|
||||||
|
smallest = smallest->left;
|
||||||
|
}
|
||||||
|
|
||||||
|
return top(treeNodeHistory);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Iterates over the tree given by root. Follows the usage of strtok. If tree is NULL, the next entry of the last tree given is returned in ordering direction.
|
// Iterates over the tree given by root. Follows the usage of strtok. If tree is NULL, the next entry of the last tree given is returned in ordering direction.
|
||||||
// Use your implementation of a stack to organize the iterator. Push the root node and all left nodes first. On returning the next element,
|
// Use your implementation of a stack to organize the iterator. Push the root node and all left nodes first. On returning the next element,
|
||||||
// push the top node and push all its left nodes.
|
// push the top node and push all its left nodes.
|
||||||
void *nextTreeData(TreeNode *root)
|
void *nextTreeData(TreeNode *const root)
|
||||||
{
|
{
|
||||||
|
if(root != NULL)
|
||||||
|
{
|
||||||
|
clearStack(treeNodeHistory);
|
||||||
|
treeNodeHistory = NULL;
|
||||||
|
return getSmallest(root)->data;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(treeNodeHistory == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
TreeNode *last = top(treeNodeHistory);
|
||||||
|
treeNodeHistory = pop(treeNodeHistory);
|
||||||
|
|
||||||
|
if(last->right == NULL)
|
||||||
|
{
|
||||||
|
TreeNode *const current = top(treeNodeHistory);
|
||||||
|
return current == NULL ? NULL : current->data;
|
||||||
|
}
|
||||||
|
|
||||||
|
return getSmallest(last->right)->data;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Releases all memory resources (including data copies).
|
// Releases all memory resources (including data copies).
|
||||||
void clearTree(TreeNode *root)
|
void clearTree(TreeNode *const root)
|
||||||
{
|
{
|
||||||
|
if(root == NULL)
|
||||||
|
return;
|
||||||
|
|
||||||
|
clearTree(root->left);
|
||||||
|
clearTree(root->right);
|
||||||
|
|
||||||
|
free(root->data);
|
||||||
|
free(root);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the number of entries in the tree given by root.
|
// Returns the number of entries in the tree given by root.
|
||||||
unsigned int treeSize(const TreeNode *root)
|
unsigned int treeSize(const TreeNode *const root)
|
||||||
{
|
{
|
||||||
|
if(root == NULL)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
return treeSize(root->left) + treeSize(root->right) + 1;
|
||||||
}
|
}
|
||||||
@@ -1 +1,9 @@
|
|||||||
|
Lukas;19999
|
||||||
|
Lukas;7999
|
||||||
|
Lukas;5999
|
||||||
|
Henrik;5988
|
||||||
|
Lukas;4999
|
||||||
|
Tobi;4999
|
||||||
|
Henrik;4977
|
||||||
|
Lukas;3999
|
||||||
player1;3999
|
player1;3999
|
||||||
+14
-2
@@ -35,8 +35,20 @@ $(program_obj_files): %.o: %.c
|
|||||||
# --------------------------
|
# --------------------------
|
||||||
# Unit Tests
|
# Unit Tests
|
||||||
# --------------------------
|
# --------------------------
|
||||||
unitTests:
|
numbersTests: numbersTests.c numbers.o bintree.o stack.o $(unityfolder)/unity.c
|
||||||
echo "needs to be implemented"
|
$(CC) $(FLAGS) -I$(unityfolder) $^ -o numbersTests
|
||||||
|
|
||||||
|
stackTests: test_stack.c stack.o $(unityfolder)/unity.c
|
||||||
|
$(CC) $(CFLAGS) -I$(unityfolder) -o stackTests test_stack.c stack.o $(unityfolder)/unity.c
|
||||||
|
|
||||||
|
bintreeTests: bintree.o test_bintree.c stack.o $(unityfolder)/unity.c
|
||||||
|
$(CC) $(CFLAGS) -I$(unityfolder) -o bintreeTests test_bintree.c bintree.o stack.o $(unityfolder)/unity.c
|
||||||
|
|
||||||
|
# --------------------------
|
||||||
|
# compile_commands.json für clangd erzeugen (benötigt bear)
|
||||||
|
# --------------------------
|
||||||
|
compiledb:
|
||||||
|
bear -- $(CC) $(CFLAGS) -fsyntax-only -I$(raylibfolder) -I$(unityfolder) $(wildcard *.c)
|
||||||
|
|
||||||
# --------------------------
|
# --------------------------
|
||||||
# Clean
|
# Clean
|
||||||
|
|||||||
+65
-12
@@ -5,22 +5,75 @@
|
|||||||
#include "numbers.h"
|
#include "numbers.h"
|
||||||
#include "bintree.h"
|
#include "bintree.h"
|
||||||
|
|
||||||
//TODO: getDuplicate und createNumbers implementieren
|
static int compare(const void *a, const void *b);
|
||||||
/* * * Erzeugen eines Arrays mit der vom Nutzer eingegebenen Anzahl an Zufallszahlen.
|
|
||||||
* Sicherstellen, dass beim Befüllen keine Duplikate entstehen.
|
|
||||||
* Duplizieren eines zufälligen Eintrags im Array.
|
|
||||||
* in `getDuplicate()`: Sortieren des Arrays und Erkennen der doppelten Zahl durch Vergleich benachbarter Elemente. */
|
|
||||||
|
|
||||||
// Returns len random numbers between 1 and 2x len in random order which are all different, except for two entries.
|
unsigned int *createNumbers(unsigned int len) {
|
||||||
// Returns NULL on errors. Use your implementation of the binary search tree to check for possible duplicates while
|
if (len < 3) {
|
||||||
// creating random numbers.
|
return NULL;
|
||||||
unsigned int *createNumbers(unsigned int len)
|
}
|
||||||
{
|
|
||||||
|
|
||||||
|
srand(time(NULL));
|
||||||
|
TreeNode *tree = NULL;
|
||||||
|
unsigned int *numArray = calloc(sizeof(unsigned int), len * sizeof(unsigned int));
|
||||||
|
|
||||||
|
for (unsigned int i = 0; i < len; i++) {
|
||||||
|
int duplicate = 0;
|
||||||
|
unsigned int randNumber = rand() % (len * 2) + 1;
|
||||||
|
numArray[i] = randNumber;
|
||||||
|
tree = addToTree(tree, &randNumber, sizeof(randNumber), compare, &duplicate);
|
||||||
|
if (duplicate == 1) {
|
||||||
|
i--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// create duplicate
|
||||||
|
const unsigned int randNum1 = rand() % (len);
|
||||||
|
unsigned int randNum2 = rand() % (len);
|
||||||
|
while (randNum1 == randNum2) {
|
||||||
|
randNum2 = rand() % (len);
|
||||||
|
}
|
||||||
|
numArray[randNum2] = numArray[randNum1];
|
||||||
|
clearTree(tree);
|
||||||
|
return numArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Returns only the only number in numbers which is present twice. Returns zero on errors.
|
// Returns only the only number in numbers which is present twice. Returns zero on errors.
|
||||||
unsigned int getDuplicate(const unsigned int numbers[], unsigned int len)
|
unsigned int getDuplicate(const unsigned int numbers[],const unsigned int len) {
|
||||||
{
|
if (numbers == NULL || len == 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
unsigned int *array = calloc(len, sizeof(unsigned int));
|
||||||
|
if (array == NULL) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
memcpy(array, numbers, sizeof(unsigned int) * len);
|
||||||
|
|
||||||
|
unsigned int duplicate = 0;
|
||||||
|
int (*compareNumbers)(const void *a, const void *b) = &compare;
|
||||||
|
qsort(array, len, sizeof(unsigned int), compareNumbers);
|
||||||
|
for (unsigned int i = 0; i < len - 1; i++) {
|
||||||
|
// returns 0 if multiple duplicates
|
||||||
|
if (array[i] == array[i + 1] && duplicate != 0) {
|
||||||
|
free(array);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (array[i] == array[i + 1] && duplicate == 0) {
|
||||||
|
duplicate = array[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//returns 0 if no duplicate
|
||||||
|
if (duplicate == 0) {
|
||||||
|
free(array);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
free(array);
|
||||||
|
return duplicate;
|
||||||
|
}
|
||||||
|
|
||||||
|
int compare(const void *a, const void *b) {
|
||||||
|
unsigned int x = *((unsigned int *) a);
|
||||||
|
unsigned int y = *((unsigned int *) b);
|
||||||
|
const int comp = x - y;
|
||||||
|
return comp;
|
||||||
}
|
}
|
||||||
Executable
BIN
Binary file not shown.
@@ -0,0 +1,74 @@
|
|||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include "unity.h"
|
||||||
|
#include "numbers.h"
|
||||||
|
|
||||||
|
void setUp(void) {}
|
||||||
|
void tearDown(void) {}
|
||||||
|
|
||||||
|
|
||||||
|
static void test_arrayGetsNumbers() {
|
||||||
|
unsigned int returnValue = 0;
|
||||||
|
unsigned int* array = createNumbers(5);
|
||||||
|
for (int i = 0; i < 5; i++) {
|
||||||
|
returnValue += array[i];
|
||||||
|
}
|
||||||
|
int expectedValue = 10;// minimal Value when len is 5 => 11
|
||||||
|
TEST_ASSERT_GREATER_THAN_INT(expectedValue,returnValue);
|
||||||
|
free (array);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_arrayOnlyHasOneDulicate() {
|
||||||
|
unsigned int len = 5;
|
||||||
|
unsigned int *array = createNumbers(len);
|
||||||
|
int duplicateTrue = getDuplicate(array, len);
|
||||||
|
int expectedValue = 0;
|
||||||
|
free(array);
|
||||||
|
TEST_ASSERT_NOT_EQUAL_INT(expectedValue,duplicateTrue);
|
||||||
|
}
|
||||||
|
static void test_returnNullWhenLenIs0() {
|
||||||
|
unsigned int len = 0;
|
||||||
|
unsigned int *array = createNumbers(len);
|
||||||
|
free(array);
|
||||||
|
TEST_ASSERT_NULL(array);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_findsDuplicate() {
|
||||||
|
unsigned int array[] = {5, 10, 9, 3, 3};
|
||||||
|
unsigned int len = 5;
|
||||||
|
unsigned int returnValue = getDuplicate(array, len);
|
||||||
|
unsigned int expectedValue= 3;
|
||||||
|
TEST_ASSERT_EQUAL_INT(expectedValue,returnValue);
|
||||||
|
}
|
||||||
|
static void test_return0WhenNoDuplicate() {
|
||||||
|
unsigned int array[] = {5, 10, 9, 3, 2};
|
||||||
|
unsigned int len = 5;
|
||||||
|
unsigned int returnValue = getDuplicate(array, len);
|
||||||
|
unsigned int expectedValue = 0;
|
||||||
|
TEST_ASSERT_EQUAL_INT(expectedValue, returnValue);
|
||||||
|
}
|
||||||
|
static void test_return0WhenMultipleDuplicates() {
|
||||||
|
unsigned int array[] = {5, 5, 9, 3, 3};
|
||||||
|
unsigned int len = 5;
|
||||||
|
unsigned int returnValue = getDuplicate(array, len);
|
||||||
|
unsigned int expectedValue = 0;
|
||||||
|
TEST_ASSERT_EQUAL_INT(expectedValue, returnValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
UNITY_BEGIN();
|
||||||
|
|
||||||
|
printf("\n============================\nnumber tests\n============================\n");
|
||||||
|
RUN_TEST(test_arrayGetsNumbers);
|
||||||
|
RUN_TEST(test_arrayOnlyHasOneDulicate);
|
||||||
|
RUN_TEST(test_returnNullWhenLenIs0);
|
||||||
|
RUN_TEST(test_findsDuplicate);
|
||||||
|
RUN_TEST(test_return0WhenNoDuplicate);
|
||||||
|
RUN_TEST(test_return0WhenMultipleDuplicates);
|
||||||
|
|
||||||
|
return UNITY_END();
|
||||||
|
}
|
||||||
|
|
||||||
+25
-20
@@ -1,33 +1,38 @@
|
|||||||
#include <stdlib.h>
|
|
||||||
#include "stack.h"
|
#include "stack.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
//TODO: grundlegende Stackfunktionen implementieren:
|
StackNode *push(StackNode *stack, void *data) {
|
||||||
/* * `push`: legt ein Element oben auf den Stack,
|
if (data == NULL) //Das wird so gut wie immer getestet
|
||||||
* `pop`: entfernt das oberste Element,
|
return stack;
|
||||||
* `top`: liefert das oberste Element zurück,
|
|
||||||
* `clearStack`: gibt den gesamten Speicher frei. */
|
|
||||||
|
|
||||||
// Pushes data as pointer onto the stack.
|
StackNode *newnode = malloc(sizeof(StackNode));
|
||||||
StackNode *push(StackNode *stack, void *data)
|
if (newnode == NULL)
|
||||||
{
|
return stack;
|
||||||
|
|
||||||
|
newnode->data = data;
|
||||||
|
newnode->next = stack; // Um Data was das neue Top ist mit der Kette zu verbinden
|
||||||
|
|
||||||
|
return newnode;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Deletes the top element of the stack (latest added element) and releases its memory. (Pointer to data has to be
|
StackNode *pop(StackNode *stack) {
|
||||||
// freed by caller.)
|
if (stack == NULL)
|
||||||
StackNode *pop(StackNode *stack)
|
return stack;
|
||||||
{
|
|
||||||
|
|
||||||
|
StackNode *newtop = stack->next; //speichert das nächste und löscht das oberste, speichert dann da "nächste" als oberstes
|
||||||
|
free(stack);
|
||||||
|
return newtop;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the data of the top element.
|
void *top(StackNode *stack) {
|
||||||
void *top(StackNode *stack)
|
if (stack == NULL)
|
||||||
{
|
return NULL;
|
||||||
|
|
||||||
|
return stack->data;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clears stack and releases all memory.
|
void clearStack(StackNode *stack) {
|
||||||
void clearStack(StackNode *stack)
|
while (stack != NULL) {
|
||||||
{
|
stack = pop(stack); // pop returnt newtop und man springt immer weiter in der verkettung
|
||||||
|
}
|
||||||
}
|
}
|
||||||
+9
-17
@@ -1,25 +1,17 @@
|
|||||||
#ifndef STACK_H
|
#ifndef STACK_H
|
||||||
#define STACK_H
|
#define STACK_H
|
||||||
|
|
||||||
/* A stack is a special type of queue which uses the LIFO (last in, first out) principle.
|
|
||||||
This means that with each new element all other elements are pushed deeper into the stack.
|
|
||||||
The latest element is taken from the stack. */
|
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
//TODO: passenden Datentyp als struct anlegen
|
typedef struct StackNode
|
||||||
|
{
|
||||||
|
void *data;
|
||||||
|
struct StackNode *next;
|
||||||
|
} StackNode;
|
||||||
|
|
||||||
// Pushes data as pointer onto the stack.
|
StackNode* push(StackNode* stack, void* data);
|
||||||
StackNode *push(StackNode *stack, void *data);
|
StackNode* pop(StackNode* stack);
|
||||||
|
void* top(StackNode* stack);
|
||||||
// Deletes the top element of the stack (latest added element) and releases its memory. (Pointer to data has to be
|
void clearStack(StackNode* stack);
|
||||||
// freed by caller.)
|
|
||||||
StackNode *pop(StackNode *stack);
|
|
||||||
|
|
||||||
// Returns the data of the top element.
|
|
||||||
void *top(StackNode *stack);
|
|
||||||
|
|
||||||
// Clears stack and releases all memory.
|
|
||||||
void clearStack(StackNode *stack);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -0,0 +1,155 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include "bintree.h"
|
||||||
|
#include "unity.h"
|
||||||
|
|
||||||
|
|
||||||
|
static int compare_ints(const void *const a, const void *const b)
|
||||||
|
{
|
||||||
|
return *(int *)a - *(int *)b;
|
||||||
|
}
|
||||||
|
|
||||||
|
static TreeNode *buildTree()
|
||||||
|
{
|
||||||
|
TreeNode *tree = NULL;
|
||||||
|
|
||||||
|
const int data1 = 1;
|
||||||
|
tree = addToTree(NULL, &data1, sizeof(int), NULL, NULL);
|
||||||
|
const int data2 = 3;
|
||||||
|
tree = addToTree(tree, &data2, sizeof(int), compare_ints, NULL);
|
||||||
|
const int data3 = 4;
|
||||||
|
tree = addToTree(tree, &data3, sizeof(int), compare_ints, NULL);
|
||||||
|
const int data4 = 2;
|
||||||
|
tree = addToTree(tree, &data4, sizeof(int), compare_ints, NULL);
|
||||||
|
|
||||||
|
return tree;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_addToTreeAllNullReturnsNull(void)
|
||||||
|
{
|
||||||
|
TEST_ASSERT_NULL(addToTree(NULL, NULL, 0, NULL, NULL));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_addToTreeBuildsTreeInOrder(void)
|
||||||
|
{
|
||||||
|
const TreeNode *const tree = buildTree();
|
||||||
|
|
||||||
|
TEST_ASSERT_EQUAL_INT(1, *(int *)tree->data);
|
||||||
|
TEST_ASSERT_NULL(tree->left);
|
||||||
|
TEST_ASSERT_NOT_NULL(tree->right);
|
||||||
|
TEST_ASSERT_EQUAL_INT(3, *(int *)tree->right->data);
|
||||||
|
TEST_ASSERT_NOT_NULL(tree->right->right);
|
||||||
|
TEST_ASSERT_EQUAL_INT(4, *(int *)tree->right->right->data);
|
||||||
|
TEST_ASSERT_NOT_NULL(tree->right->left);
|
||||||
|
TEST_ASSERT_EQUAL_INT(2, *(int *)tree->right->left->data);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_addToTreeReportsNoDuplicates(void)
|
||||||
|
{
|
||||||
|
int duplicates = -1;
|
||||||
|
|
||||||
|
TreeNode *tree = buildTree();
|
||||||
|
const int data = -1;
|
||||||
|
tree = addToTree(tree, &data, sizeof(int), compare_ints, &duplicates);
|
||||||
|
|
||||||
|
TEST_ASSERT_EQUAL_INT(0, duplicates);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_addToTreeReportsDuplicates(void)
|
||||||
|
{
|
||||||
|
int duplicates = -1;
|
||||||
|
|
||||||
|
TreeNode *tree = buildTree();
|
||||||
|
const int data = 1;
|
||||||
|
tree = addToTree(tree, &data, sizeof(int), compare_ints, &duplicates);
|
||||||
|
|
||||||
|
TEST_ASSERT_EQUAL_INT(1, duplicates);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_clearTree(void)
|
||||||
|
{
|
||||||
|
TreeNode *tree = buildTree();
|
||||||
|
clearTree(tree);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_nextTreeDataForNullReturnsNull(void)
|
||||||
|
{
|
||||||
|
TEST_ASSERT_NULL(nextTreeData(NULL));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_nextTreeDataTraversesTree(void)
|
||||||
|
{
|
||||||
|
|
||||||
|
const int *const value1 = nextTreeData(buildTree());
|
||||||
|
TEST_ASSERT_NOT_NULL(value1);
|
||||||
|
TEST_ASSERT_EQUAL_INT(1, *value1);
|
||||||
|
const int *const value2 = nextTreeData(NULL);
|
||||||
|
TEST_ASSERT_NOT_NULL(value2);
|
||||||
|
TEST_ASSERT_EQUAL_INT(2, *value2);
|
||||||
|
const int *const value3 = nextTreeData(NULL);
|
||||||
|
TEST_ASSERT_NOT_NULL(value3);
|
||||||
|
TEST_ASSERT_EQUAL_INT(3, *value3);
|
||||||
|
const int *const value4 = nextTreeData(NULL);
|
||||||
|
TEST_ASSERT_NOT_NULL(value4);
|
||||||
|
TEST_ASSERT_EQUAL_INT(4, *value4);
|
||||||
|
TEST_ASSERT_NULL(nextTreeData(NULL));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_nextTreeDataTraversesTwoTrees(void)
|
||||||
|
{
|
||||||
|
const int *const value1a = nextTreeData(buildTree());
|
||||||
|
TEST_ASSERT_NOT_NULL(value1a);
|
||||||
|
TEST_ASSERT_EQUAL_INT(1, *value1a);
|
||||||
|
const int *const value2a = nextTreeData(NULL);
|
||||||
|
TEST_ASSERT_NOT_NULL(value2a);
|
||||||
|
TEST_ASSERT_EQUAL_INT(2, *value2a);
|
||||||
|
const int *const value1b = nextTreeData(buildTree());
|
||||||
|
TEST_ASSERT_NOT_NULL(value1b);
|
||||||
|
TEST_ASSERT_EQUAL_INT(1, *value1b);
|
||||||
|
const int *const value2b = nextTreeData(NULL);
|
||||||
|
TEST_ASSERT_NOT_NULL(value2b);
|
||||||
|
TEST_ASSERT_EQUAL_INT(2, *value2b);
|
||||||
|
const int *const value3b = nextTreeData(NULL);
|
||||||
|
TEST_ASSERT_NOT_NULL(value3b);
|
||||||
|
TEST_ASSERT_EQUAL_INT(3, *value3b);
|
||||||
|
const int *const value4b = nextTreeData(NULL);
|
||||||
|
TEST_ASSERT_NOT_NULL(value4b);
|
||||||
|
TEST_ASSERT_EQUAL_INT(4, *value4b);
|
||||||
|
TEST_ASSERT_NULL(nextTreeData(NULL));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_treeSizeIsZeroForNULLTree(void)
|
||||||
|
{
|
||||||
|
TEST_ASSERT_EQUAL_INT(0, treeSize(NULL));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_treeSizeIsCorrectForTestTree(void)
|
||||||
|
{
|
||||||
|
TEST_ASSERT_EQUAL_INT(4, treeSize(buildTree()));
|
||||||
|
}
|
||||||
|
|
||||||
|
void setUp(void)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void tearDown(void)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
UNITY_BEGIN();
|
||||||
|
|
||||||
|
printf("\n============================\nbinary tree tests\n============================\n");
|
||||||
|
RUN_TEST(test_addToTreeAllNullReturnsNull);
|
||||||
|
RUN_TEST(test_addToTreeBuildsTreeInOrder);
|
||||||
|
RUN_TEST(test_addToTreeReportsNoDuplicates);
|
||||||
|
RUN_TEST(test_addToTreeReportsDuplicates);
|
||||||
|
RUN_TEST(test_clearTree);
|
||||||
|
RUN_TEST(test_nextTreeDataForNullReturnsNull);
|
||||||
|
RUN_TEST(test_nextTreeDataTraversesTree);
|
||||||
|
RUN_TEST(test_nextTreeDataTraversesTwoTrees);
|
||||||
|
RUN_TEST(test_treeSizeIsZeroForNULLTree);
|
||||||
|
RUN_TEST(test_treeSizeIsCorrectForTestTree);
|
||||||
|
|
||||||
|
return UNITY_END();
|
||||||
|
}
|
||||||
@@ -0,0 +1,159 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include "stack.h"
|
||||||
|
#include "unity/unity.h"
|
||||||
|
|
||||||
|
static int tests_passed = 0;
|
||||||
|
static int tests_failed = 0;
|
||||||
|
|
||||||
|
static void report_result(const char *test_name, int passed) {
|
||||||
|
if (passed) {
|
||||||
|
printf("[PASS] %s\n", test_name);
|
||||||
|
tests_passed++;
|
||||||
|
} else {
|
||||||
|
printf("[FAIL] %s\n", test_name);
|
||||||
|
tests_failed++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_push_single_element(void) {
|
||||||
|
StackNode *stack = NULL;
|
||||||
|
int value = 10;
|
||||||
|
|
||||||
|
stack = push(stack, &value);
|
||||||
|
|
||||||
|
int passed = stack != NULL &&
|
||||||
|
top(stack) != NULL &&
|
||||||
|
*(int *) top(stack) == 10;
|
||||||
|
//liefert void pointer deswegen int* zum konvertieren und dann * um auf den wert zu derefernzieren der dort liegt
|
||||||
|
|
||||||
|
TEST_ASSERT_TRUE(passed);
|
||||||
|
report_result("Push single element", passed);
|
||||||
|
clearStack(stack);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_push_multiple_elements(void) {
|
||||||
|
StackNode *stack = NULL;
|
||||||
|
int a = 10, b = 20, c = 30;
|
||||||
|
|
||||||
|
stack = push(stack, &a); //übergebe adressen weil push void pointer erwartet
|
||||||
|
stack = push(stack, &b);
|
||||||
|
stack = push(stack, &c);
|
||||||
|
|
||||||
|
int passed = stack != NULL &&
|
||||||
|
stack->data == &c &&
|
||||||
|
stack->next != NULL &&
|
||||||
|
stack->next->data == &b &&
|
||||||
|
stack->next->next != NULL &&
|
||||||
|
stack->next->next->data == &a;
|
||||||
|
|
||||||
|
report_result("Push multiple elements", passed);
|
||||||
|
clearStack(stack);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_push_null(void) {
|
||||||
|
StackNode *stack = NULL;
|
||||||
|
|
||||||
|
stack = push(stack, NULL);
|
||||||
|
|
||||||
|
report_result("Push NULL value", stack == NULL);
|
||||||
|
clearStack(stack);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_top_empty_stack(void) {
|
||||||
|
StackNode *stack = NULL;
|
||||||
|
|
||||||
|
report_result("Top on empty stack", top(stack) == NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_pop_single_element(void) {
|
||||||
|
StackNode *stack = NULL;
|
||||||
|
int value = 10;
|
||||||
|
|
||||||
|
stack = push(stack, &value);
|
||||||
|
stack = pop(stack);
|
||||||
|
|
||||||
|
report_result("Pop single element", stack == NULL);
|
||||||
|
clearStack(stack);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_pop_multiple_elements(void) {
|
||||||
|
StackNode *stack = NULL;
|
||||||
|
int a = 10, b = 20;
|
||||||
|
|
||||||
|
stack = push(stack, &a);
|
||||||
|
stack = push(stack, &b);
|
||||||
|
|
||||||
|
stack = pop(stack);
|
||||||
|
|
||||||
|
int passed = stack != NULL &&
|
||||||
|
top(stack) != NULL &&
|
||||||
|
*(int *) top(stack) == 10;
|
||||||
|
|
||||||
|
stack = pop(stack);
|
||||||
|
|
||||||
|
passed = passed && stack == NULL;
|
||||||
|
|
||||||
|
report_result("Pop multiple elements", passed);
|
||||||
|
clearStack(stack);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_pop_empty_stack(void) {
|
||||||
|
StackNode *stack = NULL;
|
||||||
|
|
||||||
|
stack = pop(stack);
|
||||||
|
|
||||||
|
report_result("Pop empty stack", stack == NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_clear_stack(void) {
|
||||||
|
StackNode *stack = NULL;
|
||||||
|
int a = 10, b = 20, c = 30;
|
||||||
|
|
||||||
|
stack = push(stack, &a);
|
||||||
|
stack = push(stack, &b);
|
||||||
|
stack = push(stack, &c);
|
||||||
|
|
||||||
|
int passed = stack != NULL &&
|
||||||
|
stack->next != NULL &&
|
||||||
|
stack->next->next != NULL &&
|
||||||
|
stack->next->next->next == NULL;
|
||||||
|
|
||||||
|
clearStack(stack);
|
||||||
|
|
||||||
|
report_result("Clear stack", passed);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_clear_empty_stack(void) {
|
||||||
|
StackNode *stack = NULL;
|
||||||
|
|
||||||
|
clearStack(stack);
|
||||||
|
|
||||||
|
report_result("Clear empty stack", stack == NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
void setUp(void) {
|
||||||
|
}
|
||||||
|
|
||||||
|
void tearDown(void) {
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
printf("Stack Unit Tests\n");
|
||||||
|
printf("----------------\n");
|
||||||
|
|
||||||
|
test_push_single_element();
|
||||||
|
test_push_multiple_elements();
|
||||||
|
test_push_null();
|
||||||
|
test_top_empty_stack();
|
||||||
|
test_pop_single_element();
|
||||||
|
test_pop_multiple_elements();
|
||||||
|
test_pop_empty_stack();
|
||||||
|
test_clear_stack();
|
||||||
|
test_clear_empty_stack();
|
||||||
|
|
||||||
|
printf("----------------\n");
|
||||||
|
printf("Results: %d passed, %d failed\n",
|
||||||
|
tests_passed, tests_failed);
|
||||||
|
|
||||||
|
return tests_failed == 0 ? 0 : 1;
|
||||||
|
}
|
||||||
+2
-25
@@ -1,7 +1,7 @@
|
|||||||
#include "timer.h"
|
#include "timer.h"
|
||||||
|
|
||||||
#if __APPLE__
|
#include <time.h>
|
||||||
#include <sys/time.h>
|
|
||||||
static struct timespec start = {0, 0};
|
static struct timespec start = {0, 0};
|
||||||
|
|
||||||
// Starts the timer.
|
// Starts the timer.
|
||||||
@@ -30,26 +30,3 @@ double stopTimer()
|
|||||||
|
|
||||||
return measuredSeconds;
|
return measuredSeconds;
|
||||||
}
|
}
|
||||||
#else
|
|
||||||
#include <time.h>
|
|
||||||
static clock_t startClocks = 0;
|
|
||||||
|
|
||||||
// Starts the timer.
|
|
||||||
void startTimer()
|
|
||||||
{
|
|
||||||
startClocks = clock();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Returns the time in seconds since startTimer() was called.
|
|
||||||
double stopTimer()
|
|
||||||
{
|
|
||||||
double measuredSeconds = (clock() - (double)startClocks) / CLOCKS_PER_SEC;
|
|
||||||
|
|
||||||
if(startClocks > 0)
|
|
||||||
startClocks = 0;
|
|
||||||
else
|
|
||||||
measuredSeconds = -1;
|
|
||||||
|
|
||||||
return measuredSeconds;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
Reference in new issue
Block a user