Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbed-os by
semihost_fs.cpp
00001 #include "TestHarness.h" 00002 #include "mbed.h" 00003 #include "semihost_api.h" 00004 #include <stdio.h> 00005 00006 #define FILENAME "/local/out.txt" 00007 #define TEST_STRING "Hello World!" 00008 00009 TEST_GROUP(FirstTestGroup) 00010 { 00011 00012 FILE *test_open(const char *mode) { 00013 FILE *f = fopen(FILENAME, mode); 00014 return f; 00015 } 00016 00017 bool test_write(FILE *f, char *str, int str_len) { 00018 int n = fprintf(f, str); 00019 return (n == str_len) ? true : false; 00020 } 00021 00022 bool test_read(FILE *f, char *str, int str_len) { 00023 int n = fread(str, sizeof(unsigned char), str_len, f); 00024 return (n == str_len) ? true : false; 00025 } 00026 00027 bool test_close(FILE *f) { 00028 int rc = fclose(f); 00029 return rc ? true : false; 00030 } 00031 00032 }; 00033 00034 TEST(FirstTestGroup, FirstTest) 00035 { 00036 CHECK_TEXT(semihost_connected(), "Semihost not connected") 00037 00038 LocalFileSystem local("local"); 00039 00040 char *str = TEST_STRING; 00041 char *buffer = (char *)malloc(sizeof(unsigned char) * strlen(TEST_STRING)); 00042 int str_len = strlen(TEST_STRING); 00043 00044 CHECK_TEXT(buffer != NULL, "Buffer allocation failed"); 00045 CHECK_TEXT(str_len > 0, "Test string is empty (len <= 0)"); 00046 00047 { 00048 // Perform write / read tests 00049 FILE *f = NULL; 00050 // Write 00051 f = test_open("w"); 00052 CHECK_TEXT(f != NULL, "Error opening file for writing") 00053 CHECK_TEXT(test_write(f, str, str_len), "Error writing file"); 00054 CHECK_TEXT(test_close(f) != EOF, "Error closing file after write"); 00055 00056 // Read 00057 f = test_open("r"); 00058 CHECK_TEXT(f != NULL, "Error opening file for reading") 00059 CHECK_TEXT(test_read(f, buffer, str_len), "Error reading file"); 00060 CHECK_TEXT(test_close(f) != EOF, "Error closing file after read"); 00061 } 00062 CHECK(strncmp(buffer, str, str_len) == 0); 00063 }
Generated on Tue Jul 12 2022 13:16:05 by
