X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=tmk_core%2Ftool%2Fmbed%2Fmbed-sdk%2Flibraries%2Ftests%2Futest%2Fsemihost_fs%2Fsemihost_fs.cpp;fp=tmk_core%2Ftool%2Fmbed%2Fmbed-sdk%2Flibraries%2Ftests%2Futest%2Fsemihost_fs%2Fsemihost_fs.cpp;h=adb49025fbd3d7bb2c5271785af1c0959609e03b;hb=a3d96d3aa96318d339a67de1085e0ae495d57c84;hp=0000000000000000000000000000000000000000;hpb=1d5bac21dc6f1425b8ef4bbe7935330c37c3a93e;p=qmk_firmware.git diff --git a/tmk_core/tool/mbed/mbed-sdk/libraries/tests/utest/semihost_fs/semihost_fs.cpp b/tmk_core/tool/mbed/mbed-sdk/libraries/tests/utest/semihost_fs/semihost_fs.cpp new file mode 100644 index 000000000..adb49025f --- /dev/null +++ b/tmk_core/tool/mbed/mbed-sdk/libraries/tests/utest/semihost_fs/semihost_fs.cpp @@ -0,0 +1,63 @@ +#include "TestHarness.h" +#include "mbed.h" +#include "semihost_api.h" +#include + +#define FILENAME "/local/out.txt" +#define TEST_STRING "Hello World!" + +TEST_GROUP(FirstTestGroup) +{ + + FILE *test_open(const char *mode) { + FILE *f = fopen(FILENAME, mode); + return f; + } + + bool test_write(FILE *f, char *str, int str_len) { + int n = fprintf(f, str); + return (n == str_len) ? true : false; + } + + bool test_read(FILE *f, char *str, int str_len) { + int n = fread(str, sizeof(unsigned char), str_len, f); + return (n == str_len) ? true : false; + } + + bool test_close(FILE *f) { + int rc = fclose(f); + return rc ? true : false; + } + +}; + +TEST(FirstTestGroup, FirstTest) +{ + CHECK_TEXT(semihost_connected(), "Semihost not connected") + + LocalFileSystem local("local"); + + char *str = TEST_STRING; + char *buffer = (char *)malloc(sizeof(unsigned char) * strlen(TEST_STRING)); + int str_len = strlen(TEST_STRING); + + CHECK_TEXT(buffer != NULL, "Buffer allocation failed"); + CHECK_TEXT(str_len > 0, "Test string is empty (len <= 0)"); + + { + // Perform write / read tests + FILE *f = NULL; + // Write + f = test_open("w"); + CHECK_TEXT(f != NULL, "Error opening file for writing") + CHECK_TEXT(test_write(f, str, str_len), "Error writing file"); + CHECK_TEXT(test_close(f) != EOF, "Error closing file after write"); + + // Read + f = test_open("r"); + CHECK_TEXT(f != NULL, "Error opening file for reading") + CHECK_TEXT(test_read(f, buffer, str_len), "Error reading file"); + CHECK_TEXT(test_close(f) != EOF, "Error closing file after read"); + } + CHECK(strncmp(buffer, str, str_len) == 0); +}