1
1
mirror of https://github.com/anoma/juvix.git synced 2024-12-04 17:07:28 +03:00
juvix/tests/runtime/positive/test004.c
2022-11-03 09:38:09 +01:00

52 lines
1.3 KiB
C

/* Allocator for unstructured objects */
#include <juvix/mem/alloc.h>
static unsigned long seed = 123456789UL;
static uint myrandom() {
return ((seed = seed * 214013L + 2531011L) >> 16) & 0x7fff;
}
#define M 64
#define N \
(M * \
((PAGE_SIZE - align(sizeof(pool_t), sizeof(dword_t))) / sizeof(word_t)))
int main() {
uint s;
word_t *ptr = NULL;
alloc_init();
for (uint i = 0; i < PAGE_SIZE / sizeof(word_t); ++i) {
word_t *next = alloc(1);
*next = (word_t)ptr;
ptr = next;
}
ASSERT(juvix_max_allocated_pages_num == juvix_allocated_pages_num);
ASSERT(juvix_max_allocated_pages_num == 2);
s = 0;
while (ptr != NULL) {
ptr = (word_t *)*ptr;
++s;
}
ASSERT(s == PAGE_SIZE / sizeof(word_t));
for (uint i = 0; i < N; ++i) {
uint n = myrandom() % (MAX_FIELDS + 1) + 1;
word_t *next = alloc(n);
*next = (word_t)ptr;
ptr = next;
}
s = 0;
while (ptr != NULL) {
ptr = (word_t *)*ptr;
++s;
}
ASSERT(s == N);
ASSERT(juvix_max_allocated_pages_num == juvix_allocated_pages_num);
alloc_cleanup();
ASSERT(juvix_max_allocated_pages_num >= M + 2);
ASSERT(juvix_max_allocated_pages_num <= 2 + M * (MAX_FIELDS + 1));
ASSERT(juvix_allocated_pages_num == 0);
return s != N;
}