MemFilesystem_test

Dependencies:   SDFileSystem mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "MemFileSystem.h"
00003 #include "SDFileSystem.h"
00004 
00005 SDFileSystem sd(P8_5, P8_6, P8_3, P8_4, "sd");
00006 Timer timer;
00007 
00008 const char *mem_file_path = "/ram/out.txt";
00009 const char *sd_file_path = "/sd/out.txt";
00010 const char *dump_path = "/sd/fatdump";
00011 const int DATA_SIZE = 100 * 1024;
00012 uint8_t data_written[DATA_SIZE] = { 0 };
00013 
00014 void writeFile(const char *path)
00015 {
00016     FILE *f = fopen(path, "w"); 
00017     for (int i = 0; i < DATA_SIZE; i++) {
00018         data_written[i] = rand() % 0XFF;
00019         fprintf(f, "%c", data_written[i]);
00020     }
00021     fclose(f);    
00022 }
00023 
00024 bool readFile(const char *path)
00025 {
00026     bool result = true;
00027 
00028     FILE *f = fopen(path, "r");
00029     for (int i = 0; i < DATA_SIZE; i++) {
00030         uint8_t data = fgetc(f);
00031         if (data != data_written[i]) {
00032             result = false;
00033             break;
00034         }
00035     }
00036     fclose(f);
00037 
00038     return result;
00039 }
00040 
00041 int main()
00042 {    
00043     MemFileSystem *ramfs1 = new MemFileSystem("ram");
00044     ramfs1->format();
00045 
00046     /**
00047      * MemFileSystem read/write test
00048      */
00049     {
00050         printf("Mem: Writing ... ");
00051         writeFile(mem_file_path);
00052         printf("[OK]\r\n");
00053  
00054         printf("Mem: Reading data ... ");
00055         printf("[%s]\r\n", readFile(mem_file_path) ? "OK" : "FAIL");
00056     }
00057 
00058     /**
00059      * MemFileSystem dump/load test
00060      */
00061     {
00062         printf("Dump from MemFs to SDFs ... ");
00063         FILE *f = fopen(dump_path, "w");
00064         ramfs1->dump(f);
00065         fclose(f);
00066         printf("[OK]\n");
00067     }
00068 
00069     delete ramfs1;
00070     MemFileSystem *ramfs2 = new MemFileSystem("ram");
00071 
00072     {
00073         printf("Load from SDFs to MemFs ... ");
00074         FILE *f = fopen(dump_path, "r");
00075         ramfs2->load(f);
00076         fclose(f);
00077         printf("[OK]\n");
00078     }
00079 
00080     printf("Mem: Reading data ... ");
00081     printf("[%s]\n", readFile(mem_file_path) ? "OK" : "FAIL");
00082     
00083     /*
00084      * FileSytem speed comparison
00085      */
00086     {
00087         int cnt = 10;
00088         int s, e;
00089 
00090         timer.start();
00091         
00092         s = timer.read_us();
00093         printf("Mem: Writing 10 times ...");
00094         for (int i = 0; i < cnt; i++) {
00095             writeFile(mem_file_path);
00096         }
00097         e = timer.read_us();
00098         printf("[OK] avg = %dusec\n", (e - s) / cnt );
00099 
00100         s = timer.read_us();
00101         printf("SD: Writing 10 times ...");
00102         for (int i = 0; i < cnt; i++) {
00103             writeFile(sd_file_path);
00104         }
00105         e = timer.read_us();
00106         printf("[OK] avg = %dusec\n", (e - s) / cnt );
00107         
00108         timer.stop();
00109     }
00110 }