]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/tests/mbed/file/main.cpp
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / tests / mbed / file / main.cpp
1 #include "test_env.h"
2 #include "semihost_api.h"
3
4 Serial pc(USBTX, USBRX);
5
6 #define FILENAME      "/local/out.txt"
7 #define TEST_STRING   "Hello World!"
8
9 FILE *test_open(const char *mode) {
10     FILE *f = fopen(FILENAME, mode);
11     if (f == NULL) {
12         printf("Error opening file"NL);
13         notify_completion(false);
14     }
15     return f;
16 }
17
18 void test_write(FILE *f, char *str, int str_len) {
19     int n = fprintf(f, str);
20
21     if (n != str_len) {
22         printf("Error writing file"NL);
23         notify_completion(false);
24     }
25 }
26
27 void test_read(FILE *f, char *str, int str_len) {
28     int n = fread(str, sizeof(unsigned char), str_len, f);
29
30     if (n != str_len) {
31         printf("Error reading file"NL);
32         notify_completion(false);
33     }
34 }
35
36 void test_close(FILE *f) {
37     int rc = fclose(f);
38
39     if (rc != 0) {
40         printf("Error closing file"NL);
41         notify_completion(false);
42     }
43 }
44
45 int main() {
46     MBED_HOSTTEST_TIMEOUT(20);
47     MBED_HOSTTEST_SELECT(default_auto);
48     MBED_HOSTTEST_DESCRIPTION(Semihost file system);
49     MBED_HOSTTEST_START("MBED_A2");
50
51     pc.printf("Test the Stream class\n");
52
53     printf("connected: %s\n", (semihost_connected()) ? ("Yes") : ("No"));
54
55     char mac[16];
56     mbed_mac_address(mac);
57     printf("mac address: %02x,%02x,%02x,%02x,%02x,%02x\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
58
59     LocalFileSystem local("local");
60
61     FILE *f;
62     char *str = TEST_STRING;
63     char *buffer = (char *)malloc(sizeof(unsigned char) * strlen(TEST_STRING));
64     int str_len = strlen(TEST_STRING);
65
66     // Write
67     f = test_open("w");
68     test_write(f, str, str_len);
69     test_close(f);
70
71     // Read
72     f = test_open("r");
73     test_read(f, buffer, str_len);
74     test_close(f);
75
76     // Check the two strings are equal
77     MBED_HOSTTEST_RESULT((strncmp(buffer, str, str_len) == 0));
78 }