Skip to content

Commit

Permalink
added a test commit with a broken unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
FlareCoding committed Sep 10, 2024
1 parent 780181b commit 3fba98a
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 6 deletions.
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ execute-unit-tests:
@echo ""
@echo "[LOG] Parsing unit test results"

# Grouping commands in a single shell to preserve RESULT and handle cleanup
@bash -c '\
bash parse_unit_test_results.sh; \
RESULT=$$?; \
Expand Down
56 changes: 54 additions & 2 deletions kernel/src/entry/tests/memory_allocation.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,36 @@ DECLARE_UNIT_TEST("Heap Allocate Test", kheapAllocateUnitTest) {
void* ptr = kmalloc(ALLOC_SIZE);
ASSERT_TRUE_CRITICAL(ptr, "Allocated memory pointer was null");

// Free the memory on success
kfree(ptr);

return UNIT_TEST_SUCCESS;
}

DECLARE_UNIT_TEST("Heap Allocate Aligned Test", kheapAllocateAlignedUnitTest) {
void* ptr = kmalloc(ALLOC_SIZE);
ASSERT_TRUE_CRITICAL(ptr, "Allocated memory pointer was null");
// Define sizes and alignments to test
size_t sizes[] = { 1024, 2048, 4096 };
size_t alignments[] = { 16, 32, 64, 128, 256 };

// Loop through each size and alignment combination
for (size_t i = 0; i < sizeof(sizes) / sizeof(sizes[0]); i++) {
for (size_t j = 0; j < sizeof(alignments) / sizeof(alignments[0]); j++) {
// Allocate memory with the given size and alignment
void* ptr = kmallocAligned(sizes[i], alignments[j]);

// Assert that the pointer is not null (allocation was successful)
ASSERT_TRUE_CRITICAL(ptr, "Allocated memory pointer was null");

// Check if the pointer is correctly aligned by verifying it's divisible by the alignment
ASSERT_TRUE_CRITICAL(((uint64_t)ptr % alignments[j]) == 0,
"Memory was not correctly aligned to the requested boundary");

kuPrint(UNIT_TEST "Allocated %llu bytes with alignment %llu: Success\n", (uint64_t)sizes[i], (uint64_t)alignments[j]);

// Optionally: Free the allocated memory (if applicable)
kfree(ptr);
}
}

return UNIT_TEST_SUCCESS;
}
Expand All @@ -29,5 +53,33 @@ DECLARE_UNIT_TEST("Heap Allocate and Free Test", kheapAllocateAndFreeUnitTest) {
ASSERT_TRUE_CRITICAL(ptr2, "Allocated memory pointer was null");
ASSERT_TRUE(ptr == ptr2, "Previously allocated and freed memory didn't get reused on a new allocation");

// Free the memory on success
kfree(ptr2);

return UNIT_TEST_SUCCESS;
}

DECLARE_UNIT_TEST("Heap Allocate - Heavy (x1 mil)", kheapHeavyAllocateTest) {
const size_t allocSize = 1000;
const size_t oneMillion = 1000000;
void** savedPointers = (void**)kmalloc(sizeof(void*) * allocSize);
ASSERT_TRUE_CRITICAL(savedPointers, "Failed to allocate a buffer for memory pointers");

// Perform a heavy-weight tight loop allocation test
for (size_t i = 0; i < oneMillion; i++) {
void* ptr = kmalloc(allocSize);
ASSERT_TRUE_CRITICAL(ptr, "Allocated memory pointer was null");

savedPointers[i] = ptr;
}

// Free all the allocated memory in this test
for (size_t i = 0; i < oneMillion; i++) {
kfree(savedPointers[i]);
}

// Free the pointer buffer
kfree(savedPointers);

return UNIT_TEST_SUCCESS;
}
6 changes: 3 additions & 3 deletions parse_unit_test_results.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ while IFS= read -r line; do
test_name=$(echo "$line" | sed -n 's/.*Test "\(.*\)" passed.*/\1/p')
passed_tests+=("$test_name")

# Look for failed tests
elif [[ "$line" == *"Test"* && "$line" == *"failed"* ]]; then
# Look for failed or critical failure tests
elif [[ "$line" == *"Test"* && ( "$line" == *"failed"* || "$line" == *"critical failure"* ) ]]; then
# Extract test name inside the quotes
test_name=$(echo "$line" | sed -n 's/.*Test "\(.*\)" failed.*/\1/p')
test_name=$(echo "$line" | sed -n 's/.*Test "\(.*\)".*/\1/p')
failed_tests+=("$test_name")
fi
done < "$log_file"
Expand Down

0 comments on commit 3fba98a

Please sign in to comment.