feat: basic bump
This commit is contained in:
commit
c86df50063
12 changed files with 535 additions and 0 deletions
14
src/jalloc.c
Normal file
14
src/jalloc.c
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#define HEAP_SIZE (1024 * 1024)
|
||||
|
||||
static uint8_t heap[HEAP_SIZE];
|
||||
static size_t heap_offset = 0;
|
||||
|
||||
void* jalloc(size_t size) {
|
||||
void* ptr = &heap[heap_offset];
|
||||
heap_offset += size;
|
||||
return ptr;
|
||||
}
|
||||
3
src/jalloc.h
Normal file
3
src/jalloc.h
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
#include <stddef.h>
|
||||
|
||||
void* jalloc(size_t size);
|
||||
106
src/main.c
Normal file
106
src/main.c
Normal file
|
|
@ -0,0 +1,106 @@
|
|||
#include "jalloc.h"
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
void test_jalloc(void) {
|
||||
int passed = 0, failed = 0;
|
||||
|
||||
// --- Test 1: Basic write/read ---
|
||||
{
|
||||
size_t size = 64;
|
||||
uint8_t *buf = (uint8_t *)jalloc(size);
|
||||
if (!buf) {
|
||||
printf("[FAIL] Test 1: jalloc returned NULL for size %zu\n", size);
|
||||
failed++;
|
||||
} else {
|
||||
for (size_t i = 0; i < size; i++) buf[i] = (uint8_t)i;
|
||||
int ok = 1;
|
||||
for (size_t i = 0; i < size; i++) {
|
||||
if (buf[i] != (uint8_t)i) { ok = 0; break; }
|
||||
}
|
||||
printf("[%s] Test 1: sequential byte pattern (%zu bytes)\n",
|
||||
ok ? "PASS" : "FAIL", size);
|
||||
ok ? passed++ : failed++;
|
||||
}
|
||||
}
|
||||
|
||||
// --- Test 2: Multiple independent allocations don't overlap ---
|
||||
{
|
||||
size_t size = 128;
|
||||
uint8_t *a = (uint8_t *)jalloc(size);
|
||||
uint8_t *b = (uint8_t *)jalloc(size);
|
||||
if (!a || !b) {
|
||||
printf("[FAIL] Test 2: jalloc returned NULL\n");
|
||||
failed++;
|
||||
} else {
|
||||
memset(a, 0xAA, size);
|
||||
memset(b, 0xBB, size);
|
||||
int ok = 1;
|
||||
for (size_t i = 0; i < size; i++) {
|
||||
if (a[i] != 0xAA || b[i] != 0xBB) { ok = 0; break; }
|
||||
}
|
||||
printf("[%s] Test 2: two allocations don't overlap (%zu bytes each)\n",
|
||||
ok ? "PASS" : "FAIL", size);
|
||||
ok ? passed++ : failed++;
|
||||
}
|
||||
}
|
||||
|
||||
// --- Test 3: Small allocation (1 byte) ---
|
||||
{
|
||||
uint8_t *buf = (uint8_t *)jalloc(1);
|
||||
if (!buf) {
|
||||
printf("[FAIL] Test 3: jalloc returned NULL for 1 byte\n");
|
||||
failed++;
|
||||
} else {
|
||||
*buf = 0x42;
|
||||
int ok = (*buf == 0x42);
|
||||
printf("[%s] Test 3: single byte allocation\n", ok ? "PASS" : "FAIL");
|
||||
ok ? passed++ : failed++;
|
||||
}
|
||||
}
|
||||
|
||||
// --- Test 4: Large allocation ---
|
||||
{
|
||||
size_t size = 1024 * 1024; // 1 MiB
|
||||
uint8_t *buf = (uint8_t *)jalloc(size);
|
||||
if (!buf) {
|
||||
printf("[FAIL] Test 4: jalloc returned NULL for %zu bytes\n", size);
|
||||
failed++;
|
||||
} else {
|
||||
memset(buf, 0xCD, size);
|
||||
int ok = 1;
|
||||
for (size_t i = 0; i < size; i++) {
|
||||
if (buf[i] != 0xCD) { ok = 0; break; }
|
||||
}
|
||||
printf("[%s] Test 4: large allocation (%zu bytes)\n",
|
||||
ok ? "PASS" : "FAIL", size);
|
||||
ok ? passed++ : failed++;
|
||||
}
|
||||
}
|
||||
|
||||
// --- Test 5: String write/read ---
|
||||
{
|
||||
const char *msg = "Hello, jalloc!";
|
||||
size_t size = strlen(msg) + 1;
|
||||
char *buf = (char *)jalloc(size);
|
||||
if (!buf) {
|
||||
printf("[FAIL] Test 5: jalloc returned NULL\n");
|
||||
failed++;
|
||||
} else {
|
||||
strcpy(buf, msg);
|
||||
int ok = (strcmp(buf, msg) == 0);
|
||||
printf("[%s] Test 5: string integrity (\"%s\")\n",
|
||||
ok ? "PASS" : "FAIL", buf);
|
||||
ok ? passed++ : failed++;
|
||||
}
|
||||
}
|
||||
|
||||
printf("\n=== Results: %d passed, %d failed ===\n", passed, failed);
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
test_jalloc();
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue