Created
February 20, 2015 15:04
-
-
Save mrpossoms/9923ad1883618fba67e8 to your computer and use it in GitHub Desktop.
Test for memory allocators that recursively builds lorem ipsum strings, that effectively double in length for each level of depth.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <unistd.h> | |
#include <string.h> | |
#include <assert.h> | |
#include <stdlib.h> | |
void* myAlloc(void* optionalParam, size_t bytes) | |
{ | |
// as an example, we will use malloc() | |
return malloc(bytes); | |
} | |
void myFree(void* optionalParam, void* mem) | |
{ | |
// as an example we will use free() | |
free(mem); | |
} | |
const char* ipsum[] = { | |
"Lorem", | |
"ipsum", | |
"dolor", | |
"sit", | |
"amet", | |
"consectetur", | |
"adipiscing", | |
"elit", | |
"sed", | |
"do", | |
"eiusmod", | |
"tempor", | |
"incididunt", | |
"ut", | |
"labore", | |
"et", | |
"dolore", | |
"magna", | |
"aliqua", | |
"Ut", | |
"enim", | |
"ad", | |
"minim", | |
"veniam", | |
"quis", | |
"nostrud", | |
"exercitation", | |
"ullamco", | |
"laboris", | |
"nisi", | |
"ut", | |
"aliquip", | |
"ex", | |
"ea", | |
"commodo", | |
"consequat", | |
"Duis", | |
"aute", | |
"irure", | |
"dolor", | |
"in", | |
"reprehenderit", | |
"in", | |
"voluptate", | |
"velit", | |
"esse", | |
"cillum", | |
"dolore", | |
"eu", | |
"fugiat", | |
"nulla", | |
"pariatur", | |
"Excepteur", | |
"sint", | |
"occaecat", | |
"cupidatat", | |
"non", | |
"proident", | |
"sunt", | |
"in", | |
"culpa", | |
"qui", | |
"officia", | |
"deserunt", | |
"mollit", | |
"anim", | |
"id", | |
"est", | |
"laborum" | |
}; | |
const char* word(){ | |
return ipsum[rand() % (sizeof(ipsum) / sizeof(char*))]; | |
} | |
char* ipsumString(void* optionalParam, int depth) | |
{ | |
if(!depth){ | |
// return a random word that occurs in a lorem ipsum | |
return (char*)word(); | |
} | |
else{ | |
// recursively build the right and left sides | |
char* left = ipsumString(optionalParam, depth - 1); | |
char* right = ipsumString(optionalParam, depth - 1); | |
// allocate space to concatenate the strings | |
char* cat = myAlloc(optionalParam, strlen(left) + strlen(right) + 2); | |
sprintf(cat, "%s %s", left, right); | |
// we dont want to try to free the strings from the ipsum[] array | |
if(depth > 1){ | |
myFree(optionalParam, left); | |
myFree(optionalParam, right); | |
} | |
return cat; | |
} | |
} | |
int main(void) | |
{ | |
// run 1000 times, recursively building a random lorem ipsum string | |
// and allocating larger and larger blocks of memory to perform | |
// concatenations | |
for(int i = 1000; i--;){ | |
char* lorem = ipsumString(NULL, 8); | |
printf("%s\n\n", lorem); | |
// clean up | |
myFree(NULL, lorem); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment