yggdrasil/test.c

79 lines
1.8 KiB
C

#include <assert.h>
#include <stdio.h>
#include <math.h>
#include <setjmp.h>
#include <stdlib.h>
#include <string.h>
static void test1(void) {
#define N 128
void *array[N] = {NULL};
for (size_t i = 0; i < N; ++i) {
array[i] = malloc(i * 2 + 1);
memset(array[i], 0xA3, i * 2 + 1);
}
for (size_t i = 0; i < N; i += 2) {
free(array[i]);
array[i] = NULL;
}
for (size_t i = 1; i < N; i += 2) {
for (size_t j = 0; j < i * 2 + 1; ++j) {
assert(((const unsigned char *) array[i])[j] == 0xA3);
}
}
for (size_t i = 0; i < N; i += 2) {
array[i] = malloc(i * 3 + 3);
memset(array[i], 0x7D, i * 3 + 3);
}
for (size_t i = 1; i < N; i += 2) {
for (size_t j = 0; j < i * 2 + 1; ++j) {
assert(((const unsigned char *) array[i])[j] == 0xA3);
}
}
for (size_t i = 0; i < N; i += 2) {
for (size_t j = 0; j < i * 3 + 3; ++j) {
assert(((const unsigned char *) array[i])[j] == 0x7D);
}
}
for (size_t i = 0; i < N; ++i) {
free(array[i]);
}
#undef N
}
static void test2(void) {
#define N 64
void *ptr0 = NULL;
void *ptr1 = NULL;
for (size_t i = 0; i < N; ++i) {
printf("Round #%zu\n", i);
// 2, 4, 6, ...
ptr0 = realloc(ptr0, (i + 1) * 2);
for (size_t j = 0; j < i * 2; ++j) {
assert(((const unsigned char *) ptr0)[j] == 0x12);
}
memset(ptr0, 0x12, (i + 1) * 2);
// 3, 6, 9, ...
ptr1 = realloc(ptr1, (i + 1) * 3);
for (size_t j = 0; j < i * 3; ++j) {
assert(((const unsigned char *) ptr1)[j] == 0x34);
}
memset(ptr1, 0x34, (i + 1) * 3);
}
#undef N
}
int main(int argc, const char **argv) {
test1();
test2();
return 0;
}