Commit | Line | Data |
---|---|---|
4cff3ded AW |
1 | /* Copyright 2011 Adam Green (http://mbed.org/users/AdamGreen/)\r |
2 | \r | |
3 | Licensed under the Apache License, Version 2.0 (the "License");\r | |
4 | you may not use this file except in compliance with the License.\r | |
5 | You may obtain a copy of the License at\r | |
6 | \r | |
7 | http://www.apache.org/licenses/LICENSE-2.0\r | |
8 | \r | |
9 | Unless required by applicable law or agreed to in writing, software\r | |
10 | distributed under the License is distributed on an "AS IS" BASIS,\r | |
11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r | |
12 | See the License for the specific language governing permissions and\r | |
13 | limitations under the License.\r | |
14 | */\r | |
15 | /* LocalFileSystem test modified to use SD cards instead. */\r | |
16 | #include "mbed.h"\r | |
068f6a12 | 17 | #include "SDFileSystem.h"\r |
4cff3ded AW |
18 | \r |
19 | \r | |
20 | SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board\r | |
4cff3ded AW |
21 | \r |
22 | \r | |
23 | int main() \r | |
24 | {\r | |
4cff3ded AW |
25 | int Result = -1;\r |
26 | char Buffer[32];\r | |
27 | \r | |
28 | printf("\r\n\r\nGCC4MBED Test Suite\r\n");\r | |
29 | printf("sdFileSystem Unit Tests\r\n");\r | |
30 | \r | |
31 | printf("Test 1: fopen() for write\r\n");\r | |
32 | FILE *fp = fopen("/sd/out.txt", "w"); // Open "out.txt" on the sd file system for writing\r | |
33 | if (NULL == fp)\r | |
34 | {\r | |
35 | error("%s(%d) fopen() failed\r\n", __FILE__, __LINE__);\r | |
36 | }\r | |
37 | \r | |
38 | printf("Test 2: fprintf()\r\n");\r | |
39 | Result = fprintf(fp, "Hello World!");\r | |
40 | if (Result < 0)\r | |
41 | {\r | |
42 | error("%s(%d) fprintf() failed\r\n", __FILE__, __LINE__);\r | |
43 | }\r | |
44 | \r | |
45 | printf("Test 3: fclose() on written file\r\n");\r | |
46 | Result = fclose(fp);\r | |
47 | if (0 != Result)\r | |
48 | {\r | |
49 | error("%s(%d) fclose() failed\r\n", __FILE__, __LINE__);\r | |
50 | }\r | |
51 | \r | |
52 | \r | |
53 | \r | |
54 | printf("Test 4: fopen() for read\r\n");\r | |
55 | fp = fopen("/sd/out.txt", "r");\r | |
56 | if (NULL == fp)\r | |
57 | {\r | |
58 | error("%s(%d) fopen() failed\r\n", __FILE__, __LINE__);\r | |
59 | }\r | |
60 | \r | |
61 | printf("Test 5: fscanf()\r\n");\r | |
62 | Result = fscanf(fp, "%31s", Buffer);\r | |
63 | if (EOF == Result)\r | |
64 | {\r | |
65 | error("%s(%d) fscanf() failed\r\n", __FILE__, __LINE__);\r | |
66 | }\r | |
67 | printf("Contents of /sd/out.txt: %s\r\n", Buffer);\r | |
68 | \r | |
69 | printf("Test 6: fclose() on read file\r\n");\r | |
70 | Result = fclose(fp);\r | |
71 | if (0 != Result)\r | |
72 | {\r | |
73 | error("%s(%d) fclose() failed\r\n", __FILE__, __LINE__);\r | |
74 | }\r | |
75 | \r | |
76 | \r | |
77 | \r | |
78 | printf("Test 7: remove()\r\n");\r | |
79 | Result = remove("/sd/out.txt"); // Removes the file "out.txt" from the sd file system\r | |
80 | if (0 != Result)\r | |
81 | {\r | |
82 | error("%s(%d) remove() failed\r\n", __FILE__, __LINE__);\r | |
83 | }\r | |
84 | \r | |
85 | \r | |
86 | printf("Test 8: opendir()\r\n");\r | |
87 | DIR *d = opendir("/sd"); // Opens the root directory of the sd file system\r | |
88 | printf("test\r\n"); \r | |
89 | if (NULL == d)\r | |
90 | {\r | |
91 | error("%s(%d) opendir() failed\r\n", __FILE__, __LINE__);\r | |
92 | }\r | |
93 | struct dirent *p;\r | |
94 | \r | |
95 | printf("Test 9: readir() for all entries\r\n");\r | |
96 | while((p = readdir(d)) != NULL) \r | |
97 | { // Print the names of the files in the sd file system\r | |
98 | printf("%s\r\n", p->d_name); // to stdout.\r | |
99 | }\r | |
100 | \r | |
101 | printf("Test 10: closedir\r\n");\r | |
102 | closedir(d);\r | |
103 | \r | |
4cff3ded AW |
104 | printf("\r\nTest completed\r\n");\r |
105 | } \r |