From 51b58b1ad46c5c78af487abb7df4d3edbf9a33bc Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Thu, 9 Mar 2017 12:03:14 -0600 Subject: [PATCH] Filesystem: Restored LocalFileSystem tests for mbed 2 --- drivers/FileSystemLike.h | 2 +- features/unsupported/tests/mbed/file/main.cpp | 78 +++++++++++++++++++ 2 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 features/unsupported/tests/mbed/file/main.cpp diff --git a/drivers/FileSystemLike.h b/drivers/FileSystemLike.h index c08b8c94eb..47ece539d9 100644 --- a/drivers/FileSystemLike.h +++ b/drivers/FileSystemLike.h @@ -114,7 +114,7 @@ public: * 0 on success or un-needed, * -1 on error */ - virtual int stat(const char *name, struct stat *st) = 0; + virtual int stat(const char *name, struct stat *st) { return -1; }; }; } // namespace mbed diff --git a/features/unsupported/tests/mbed/file/main.cpp b/features/unsupported/tests/mbed/file/main.cpp new file mode 100644 index 0000000000..8c85711f96 --- /dev/null +++ b/features/unsupported/tests/mbed/file/main.cpp @@ -0,0 +1,78 @@ +#include "test_env.h" +#include "mbed_semihost_api.h" + +Serial pc(USBTX, USBRX); + +#define FILENAME "/local/out.txt" +#define TEST_STRING "Hello World!" + +FILE *test_open(const char *mode) { + FILE *f = fopen(FILENAME, mode); + if (f == NULL) { + printf("Error opening file"NL); + notify_completion(false); + } + return f; +} + +void test_write(FILE *f, char *str, int str_len) { + int n = fprintf(f, str); + + if (n != str_len) { + printf("Error writing file"NL); + notify_completion(false); + } +} + +void test_read(FILE *f, char *str, int str_len) { + int n = fread(str, sizeof(unsigned char), str_len, f); + + if (n != str_len) { + printf("Error reading file"NL); + notify_completion(false); + } +} + +void test_close(FILE *f) { + int rc = fclose(f); + + if (rc != 0) { + printf("Error closing file"NL); + notify_completion(false); + } +} + +int main() { + MBED_HOSTTEST_TIMEOUT(20); + MBED_HOSTTEST_SELECT(default_auto); + MBED_HOSTTEST_DESCRIPTION(Semihost file system); + MBED_HOSTTEST_START("MBED_A2"); + + pc.printf("Test the Stream class\n"); + + printf("connected: %s\n", (semihost_connected()) ? ("Yes") : ("No")); + + char mac[16]; + mbed_mac_address(mac); + printf("mac address: %02x,%02x,%02x,%02x,%02x,%02x\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); + + LocalFileSystem local("local"); + + FILE *f; + char *str = TEST_STRING; + char *buffer = (char *)malloc(sizeof(unsigned char) * strlen(TEST_STRING)); + int str_len = strlen(TEST_STRING); + + // Write + f = test_open("w"); + test_write(f, str, str_len); + test_close(f); + + // Read + f = test_open("r"); + test_read(f, buffer, str_len); + test_close(f); + + // Check the two strings are equal + MBED_HOSTTEST_RESULT((strncmp(buffer, str, str_len) == 0)); +}