Created
November 8, 2011 04:14
-
-
Save maxdeliso/1346980 to your computer and use it in GitHub Desktop.
a test to see whether the functionality in libc is available and whether it works
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 <stdlib.h> | |
#include <string.h> | |
#include <errno.h> | |
#include <setjmp.h> | |
#define BYTE (1) | |
#define KILOBYTE (1024*(BYTE)) | |
#define MEGABYTE (1024*(KILOBYTE)) | |
#define GIGABYTE (1024*(MEGABYTE)) | |
const static char staticVar; | |
char globalVar; | |
struct memSeg { | |
char label[8]; | |
unsigned long address; | |
}; | |
void listArgs( int argc, char** argv, char** env ); | |
void listSizes( ); | |
void printMemLayout(); | |
int compareMemSegs( const struct memSeg* A, const struct memSeg* B ); | |
unsigned long long tempFileTest(unsigned long long n); | |
unsigned int mallocTest(unsigned int n); | |
void setJmpTest(); | |
int main( int argc, char ** argv, char** envv ) { | |
printf("#####LIBC PROBE LOG#####\n"); | |
listArgs( argc, argv, envv ); | |
listSizes(); | |
printMemLayout(); | |
if( tempFileTest(BYTE) ) { | |
if( tempFileTest(64 * KILOBYTE) ) { | |
if( tempFileTest(32 * MEGABYTE ) ) { | |
printf("-----ALL TMPFILE TESTS PASSED\n"); | |
} | |
} | |
} | |
if( mallocTest(BYTE) ) { | |
if( mallocTest(64 * KILOBYTE) ) { | |
if( mallocTest(32 * MEGABYTE ) ) { | |
if( mallocTest( 128 * MEGABYTE ) ) { | |
printf("-----ALL MALLOC TESTS PASSED\n"); | |
} | |
} | |
} | |
} | |
setJmpTest(); | |
return 0; | |
} | |
void listArgs( int argc, char** argv, char** envv ) | |
{ | |
int i; | |
printf("\n+++PROGRAM ARGUMENTS:\n"); | |
for( i = 0; i < argc; i++ ) { | |
printf( "-----%4i: %s\n", i, argv[i] ); | |
} | |
printf("\n+++PROGRAM ENVIRONMENTS:\n"); | |
i = -1; | |
do { | |
i++; | |
if( envv[i] ) { | |
printf("-----%4i: %s\n", i, envv[i] ); | |
} | |
} while( envv[i] ); | |
} | |
void listSizes( ) | |
{ | |
printf("\n+++VARIABLE SIZES( bytes)\n"); | |
printf("-----%15s = %5i\n", "char", sizeof(char)); | |
printf("-----%15s = %5i\n", "int", sizeof(int)); | |
printf("-----%15s = %5i\n", "long", sizeof(long)); | |
printf("-----%15s = %5i\n", "long long", sizeof(long long)); | |
printf("-----%15s = %5i\n", "float", sizeof(float)); | |
printf("-----%15s = %5i\n", "double", sizeof(double)); | |
} | |
void printMemLayout() | |
{ | |
char stackVar; | |
char *heapVar = malloc(1); | |
struct memSeg memorySegments[5]; | |
int i; | |
strcpy(memorySegments[0].label, "code"); | |
memorySegments[0].address = (unsigned long) main; | |
strcpy(memorySegments[1].label, "stack"); | |
memorySegments[1].address = (unsigned long) &stackVar; | |
strcpy(memorySegments[2].label, "heap"); | |
memorySegments[2].address = (unsigned long) heapVar; | |
strcpy(memorySegments[3].label, "static"); | |
memorySegments[3].address = (unsigned long) &staticVar; | |
strcpy(memorySegments[4].label, "global"); | |
memorySegments[4].address = (unsigned long) &globalVar; | |
qsort( memorySegments, 5, sizeof( struct memSeg ), compareMemSegs ); | |
printf("\n+++MEMORY LAYOUT\n"); | |
for( i = 0; i < 5; i++ ) { | |
printf("-----%8s : 0x%08x \n", | |
memorySegments[i].label, memorySegments[i].address ); | |
} | |
free( heapVar ); | |
} | |
int compareMemSegs( const struct memSeg* A, const struct memSeg* B ) | |
{ | |
return A->address - B->address; | |
} | |
unsigned long long tempFileTest(unsigned long long n) | |
{ | |
FILE* tmpFile = tmpfile(); | |
int c, err; | |
unsigned long long i; | |
char errBuf[64]; | |
for(i=0, c='A', err=0; err == 0 && i < n; i++, c++ ) { | |
if( fputc( c, tmpFile ) == EOF ) { | |
err = errno; | |
sprintf(errBuf, "-----TMPFILE(%i): FAILED\n", n ); | |
perror(errBuf); | |
} | |
} | |
fclose( tmpFile ); | |
if( !err ) { | |
printf("\n+++TMPFILE(%i): PASSED\n", n); | |
return i; | |
} else { | |
return 0; | |
} | |
} | |
unsigned int mallocTest(unsigned int n) | |
{ | |
void* memBlock = malloc( n ); | |
if( memBlock ) { | |
free( memBlock ); | |
printf("\n+++MALLOCTEST(%i): PASSED\n", n); | |
return n; | |
} else { | |
printf("\n+++MALLOCTEST(%i): PASSED\n", n); | |
return 0; | |
} | |
} | |
void setJmpTest() | |
{ | |
jmp_buf env; | |
int val; | |
printf("\n+++SETJMPTEST\n"); | |
if( !( val = setjmp( env ) ) ) | |
{ | |
printf("-----val = %i\n", val ); | |
longjmp( env, 1 ); | |
} else { | |
printf("-----val = %i\n", val ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
jmd5473@pollock201-lab-mac245[test]$ uname -a
Darwin pollock201-lab-mac245.clc-labs.its.psu.edu 10.7.0 Darwin Kernel Version 10.7.0: Sat Jan 29 15:17:16 PST 2011; root:xnu-1504.9.37~1/RELEASE_I386 i386
jmd5473@pollock201-lab-mac245[test]$ ./test
LIBC PROBE LOG
+++PROGRAM ARGUMENTS:
----- 0: ./test
+++PROGRAM ENVIRONMENTS:
----- 0: MANPATH=/usr/local/man:
----- 1: TERM_PROGRAM=Apple_Terminal
----- 2: TERM=xterm-color
----- 3: SHELL=/bin/bash
----- 4: TMPDIR=/var/folders/zz/zzzivhrRnAmviuee++AaKk+17Zg/-Tmp-/
----- 5: Apple_PubSub_Socket_Render=/tmp/launch-3KLmpe/Render
----- 6: TERM_PROGRAM_VERSION=273.1
----- 7: USER=jmd5473
----- 8: COMMAND_MODE=unix2003
----- 9: VRAY_FOR_MAYA2012_PLUGINS_x64=/Applications/Autodesk/maya2012/Maya.app/Contents/vray/vrayplugins
----- 10: SSH_AUTH_SOCK=/tmp/launch-6dV3dj/Listeners
----- 11: __CF_USER_TEXT_ENCODING=0x3265B:0:0
----- 12: PATH=/Library/PSU/Bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/Library/PSU/Bin:/Library/PSUadmin:/usr/texbin:/usr/X11/bin
----- 13: PWD=/tmp/test
----- 14: EDITOR=vi
----- 15: SVN=https://subversion.tlt.psu.edu
----- 16: LANG=en_US.UTF-8
----- 17: PS1=\u@\h[\W]$
----- 18: VRAY_PATH=:/Applications/ChaosGroup/V-Ray/Maya2012-x64/bin/x64/vc6
----- 19: VRAY_TOOLS_MAYA2012_x64=/Applications/ChaosGroup/V-Ray/Maya2012-x64/bin
----- 20: SHLVL=1
----- 21: HOME=/Users/psuguest
----- 22: VRAY_FOR_MAYA2012_PLUGINS_PowerPC=/Applications/Autodesk/maya2012/Maya.app/Contents/vray/vrayplugins
----- 23: LOGNAME=jmd5473
----- 24: VRAY_FOR_MAYA2012_MAIN_x64=/Applications/Autodesk/maya2012/Maya.app/Contents/vray
----- 25: Codebase=https://subversion.tlt.psu.edu/Codebase
----- 26: DISPLAY=/tmp/launch-qQLplm/org.x:0
----- 27: VRAY_FOR_MAYA2012_MAIN_PowerPC=/Applications/Autodesk/maya2012/Maya.app/Contents/vray
----- 28: _=./test
----- 29: OLDPWD=/tmp
+++VARIABLE SIZES( bytes)
----- char = 1
----- int = 4
----- long = 8
----- long long = 8
----- float = 4
----- double = 8
+++MEMORY LAYOUT
----- code : 0x00001604
----- static : 0x000020e0
----- global : 0x000020e1
----- heap : 0x00100080
----- stack : 0x5fbff4af
+++TMPFILE(1): PASSED
+++TMPFILE(65536): PASSED
+++TMPFILE(33554432): PASSED
-----ALL TMPFILE TESTS PASSED
+++MALLOCTEST(1): PASSED
+++MALLOCTEST(65536): PASSED
+++MALLOCTEST(33554432): PASSED
+++MALLOCTEST(134217728): PASSED
-----ALL MALLOC TESTS PASSED
+++SETJMPTEST
-----val = 0
-----val = 1