code completion

This commit is contained in:
Tubui 2025-12-16 00:19:36 +01:00
parent 291c8a93e7
commit f0085da7fe
4 changed files with 89 additions and 0 deletions

View File

@ -0,0 +1,53 @@
#include <stdio.h>
#include <stdlib.h>
#include "numbers.h"
int main() {
printf("===== TEST NUMBERS =====\n");
unsigned int len = 20;
unsigned int *arr = createNumbers(len);
if (arr == NULL) {
printf("FAIL: createNumbers returned NULL\n");
return 1;
}
// Check length: should contain len numbers
printf("PASS: createNumbers != NULL\n");
// Count duplicates — exactly one number must appear twice
int countDuplicate = 0;
for (unsigned int i = 0; i < len; i++) {
for (unsigned int j = i + 1; j < len; j++) {
if (arr[i] == arr[j]) {
countDuplicate++;
}
}
}
if (countDuplicate != 1) {
printf("FAIL: Array must contain exactly one duplicate, found %d\n", countDuplicate);
free(arr);
return 1;
}
printf("PASS: exactly one duplicate\n");
unsigned int found = getDuplicate(arr, len);
printf("Duplicate found by getDuplicate(): %u\n", found);
if (found == 0) {
printf("FAIL: getDuplicate returned 0\n");
free(arr);
return 1;
}
printf("PASS: getDuplicate\n");
free(arr);
printf("PASS: free array\n");
printf("ALL NUMBERS TESTS PASSED\n");
return 0;
}

BIN
test_numbers.exe Normal file

Binary file not shown.

View File

@ -0,0 +1,36 @@
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
int main() {
printf("===== TEST STACK =====\n");
StackNode *s = NULL;
// Test push
int a = 10, b = 20, c = 30;
s = push(s, &a);
s = push(s, &b);
s = push(s, &c);
if (*(int*)top(s) != 30) {
printf("FAIL: top() should return 30\n");
return 1;
}
printf("PASS: push + top\n");
// Test pop
s = pop(s);
if (*(int*)top(s) != 20) {
printf("FAIL: pop() should remove 30\n");
return 1;
}
printf("PASS: pop\n");
// Clear
clearStack(s);
printf("PASS: clearStack (no crash)\n");
printf("ALL STACK TESTS PASSED\n");
return 0;
}

BIN
test_stack.exe Normal file

Binary file not shown.