Skip to content

Instantly share code, notes, and snippets.

@paleozogt
Created February 17, 2012 17:53
Show Gist options
  • Save paleozogt/1854610 to your computer and use it in GitHub Desktop.
Save paleozogt/1854610 to your computer and use it in GitHub Desktop.
emscripten fileio tests
$echo 'this is a test' > testfile.txt
$
$emcc test_fileio.c -o test_fileio.js
clang: warning: argument unused during compilation: '-nostdinc++'
$
$node test_fileio.js testfile.txt 'this is a test'
reading testfile.txt
could not open testfile.txt
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
exit(1) called, at Error
at __exit (/Users/asimmons/Development/test/emscripten/test_fileio.js:57207:50)
at _exit (/Users/asimmons/Development/test/emscripten/test_fileio.js:57209:7)
at _test_file_read (/Users/asimmons/Development/test/emscripten/test_fileio.js:6352:7)
at _test_fileio (/Users/asimmons/Development/test/emscripten/test_fileio.js:6280:3)
at _main (/Users/asimmons/Development/test/emscripten/test_fileio.js:6322:3)
at Object.callMain (/Users/asimmons/Development/test/emscripten/test_fileio.js:57971:10)
at run (/Users/asimmons/Development/test/emscripten/test_fileio.js:58165:18)
at Object.<anonymous> (/Users/asimmons/Development/test/emscripten/test_fileio.js:58180:13)
at Module._compile (module.js:441:26)
at Object..js (module.js:459:10)
$
$echo 'this is a test' > testfile.txt
$
$gcc test_fileio.c -o test_fileio
$
$./test_fileio testfile.txt 'this is a test'
reading testfile.txt
file contents: this is a test
chdir to test
reading ../testfile.txt
file contents: this is a test
chdir to ..
reading ./testfile.txt
file contents: this is a test
$
/**
* This tests a number of file io related features:
* - reading from files
* - getting a file's size
* - relative paths
* - changing the current working directory
* - creating/destroying files
* - creating/destroying directories
*
* It should pass using regular gcc on most POSIX systems (it probably doesn't work on Win32).
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/stat.h>
// tests
void test_chdir(const char dirname[]);
void test_mkdir(const char dirname[]);
void test_rmdir(const char dirname[]);
void test_file_read(const char filename[], const char teststr[]);
void test_file_delete(const char filename[]);
// utility function
size_t get_file_size(const char filename[]);
void test_fileio(const char filename[], const char teststr[]) {
char filename_temp[PATH_MAX]= "";
const char dirname[]= "test";
strcpy(filename_temp, filename);
test_file_read(filename_temp, teststr);
test_mkdir(dirname);
test_chdir(dirname);
strcpy(filename_temp, "../");
strcat(filename_temp, filename);
test_file_read(filename_temp, teststr);
test_chdir("..");
strcpy(filename_temp, "./");
strcat(filename_temp, filename);
test_file_read(filename_temp, teststr);
test_rmdir(dirname);
}
void test_chdir(const char dirname[]) {
printf("chdir to %s \n", dirname);
int ret= 0;
char cwd_orig[PATH_MAX]= "", cwd_new[PATH_MAX]= "";
getcwd(cwd_orig, sizeof(cwd_orig));
if (strlen(cwd_orig) == 0) {
printf("get cwd failed \n");
exit(1);
}
ret= chdir(dirname);
if (ret != 0) {
printf("unable to change dir to %s \n", dirname);
exit(1);
}
getcwd(cwd_new, sizeof(cwd_new));
if (strlen(cwd_new) == 0) {
printf("get cwd failed \n");
exit(1);
}
if (strncmp(cwd_new, cwd_orig, sizeof(cwd_new)) == 0) {
printf("we're probably not running in root \n");
exit(1);
}
}
void test_mkdir(const char dirname[]) {
int ret= mkdir(dirname, 0777);
if (ret != 0) {
printf("unable to make dir %s \n", dirname);
exit(1);
}
DIR *dir= opendir(dirname);
if (dir == NULL) {
printf("unable to open dir %s \n", dirname);
exit(1);
}
closedir(dir);
}
void test_rmdir(const char dirname[]) {
int ret= rmdir(dirname);
if (ret != 0) {
exit(1);
}
DIR *dir= opendir(dirname);
if (dir != NULL) {
printf("able to open %s even tho its deleted \n", dirname);
exit(1);
}
}
void test_file_read(const char filename[], const char teststr[]) {
printf("reading %s \n", filename);
FILE *file= fopen(filename, "r");
if (file == NULL) {
printf("could not open %s \n", filename);
exit(1);
}
// slurp the file
int filesize= get_file_size(filename);
char *filestr= (char*)malloc(filesize + sizeof(char));
memset(filestr, 0, filesize);
fread(filestr, 1, filesize, file);
printf("file contents: %s \n", filestr);
// make sure the file's contents match the test string
if (strncmp(filestr, teststr, strlen(teststr)) != 0) {
printf("expected \"%s\" but was \"%s\" \n", teststr, filestr);
exit(1);
}
free(filestr);
fclose(file);
}
size_t get_file_size(const char filename[]) {
struct stat stbuf;
if (stat(filename, &stbuf) == -1) {
fprintf(stdout, "can't stat %s \n", filename);
exit(1);
}
return stbuf.st_size;
}
int main(int argc, char *argv[]) {
test_fileio(argv[1], argv[2]);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment