summaryrefslogtreecommitdiff
path: root/tool/mbed/mbed-sdk/libraries/tests/utest/semihost_fs/semihost_fs.cpp
diff options
context:
space:
mode:
authorJun Wako <wakojun@gmail.com>2015-04-24 16:26:14 +0900
committerJun Wako <wakojun@gmail.com>2015-04-24 16:26:14 +0900
commit1fe4406f374291ab2e86e95a97341fd9c475fcb8 (patch)
tree1be0e16b4b07b5a31ea97ec50a9eb13a288c3d27 /tool/mbed/mbed-sdk/libraries/tests/utest/semihost_fs/semihost_fs.cpp
parenta20ef7052c6e937d2f7672dd59456e55a5c08296 (diff)
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
b9e0ea0 Merge commit '7fa9d8bdea3773d1195b04d98fcf27cf48ddd81d' as 'tool/mbed/mbed-sdk' 7fa9d8b Squashed 'tool/mbed/mbed-sdk/' content from commit 7c21ce5 git-subtree-dir: tmk_core git-subtree-split: b9e0ea08cb940de20b3610ecdda18e9d8cd7c552
Diffstat (limited to 'tool/mbed/mbed-sdk/libraries/tests/utest/semihost_fs/semihost_fs.cpp')
-rw-r--r--tool/mbed/mbed-sdk/libraries/tests/utest/semihost_fs/semihost_fs.cpp63
1 files changed, 63 insertions, 0 deletions
diff --git a/tool/mbed/mbed-sdk/libraries/tests/utest/semihost_fs/semihost_fs.cpp b/tool/mbed/mbed-sdk/libraries/tests/utest/semihost_fs/semihost_fs.cpp
new file mode 100644
index 0000000000..adb49025fb
--- /dev/null
+++ b/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 <stdio.h>
+
+#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);
+}