Upgrade gcc4mbed project used by Smoothie.
[clinton/Smoothieware.git] / gcc4mbed / samples / LocalFileSystem / main.cpp
CommitLineData
8fcce42e 1/* Copyright 2012 Adam Green (http://mbed.org/users/AdamGreen/)\r
4cff3ded
AW
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/* Basic unit tests for LocalFileSystem functionality. */\r
8fcce42e 16#include <mbed.h>\r
4cff3ded
AW
17\r
18LocalFileSystem local("local"); // Create the local filesystem under the name "local"\r
19\r
20int main() \r
21{\r
22 int Result = -1;\r
6c79da43 23 long Offset = -1;\r
4cff3ded
AW
24 char Buffer[32];\r
25 \r
26 printf("\r\n\r\nGCC4MBED Test Suite\r\n");\r
27 printf("LocalFileSystem Unit Tests\r\n");\r
28 \r
29 printf("Test 1: fopen() for write\r\n");\r
30 FILE *fp = fopen("/local/out.txt", "w"); // Open "out.txt" on the local file system for writing\r
31 if (NULL == fp)\r
32 {\r
33 error("%s(%d) fopen() failed\r\n", __FILE__, __LINE__);\r
34 }\r
35\r
36 printf("Test 2: fprintf()\r\n");\r
37 Result = fprintf(fp, "Hello World!");\r
38 if (Result < 0)\r
39 {\r
40 error("%s(%d) fprintf() failed\r\n", __FILE__, __LINE__);\r
41 }\r
42\r
43 printf("Test 3: fclose() on written file\r\n");\r
44 Result = fclose(fp);\r
45 if (0 != Result)\r
46 {\r
47 error("%s(%d) fclose() failed\r\n", __FILE__, __LINE__);\r
48 }\r
49 \r
50\r
51\r
52 printf("Test 4: fopen() for read\r\n");\r
53 fp = fopen("/local/out.txt", "r");\r
54 if (NULL == fp)\r
55 {\r
56 error("%s(%d) fopen() failed\r\n", __FILE__, __LINE__);\r
57 }\r
58\r
59 printf("Test 5: fscanf()\r\n");\r
60 Result = fscanf(fp, "%31s", Buffer);\r
61 if (EOF == Result)\r
62 {\r
63 error("%s(%d) fscanf() failed\r\n", __FILE__, __LINE__);\r
64 }\r
65 printf("Contents of /local/out.txt: %s\r\n", Buffer);\r
6c79da43
AG
66 if (0 != strcmp(Buffer, "Hello"))\r
67 {\r
68 error("%s(%d) fscanf read out wrong string\r\n", __FILE__, __LINE__);\r
69 }\r
4cff3ded 70\r
6c79da43
AG
71 printf("Test 6: Determine size of file through fseek and ftell calls\r\n");\r
72 Result = fseek(fp, 0, SEEK_END);\r
73 if (0 != Result)\r
74 {\r
75 error("%s(%d) fseek(..,0, SEEK_END) failed\r\n", __FILE__, __LINE__);\r
76 }\r
77 Offset = ftell(fp);\r
78 if (12 != Offset)\r
79 {\r
80 error("%s(%d) ftell didn't return the expected value of 12\r\n", __FILE__, __LINE__);\r
81 }\r
82 \r
83 printf("Test 7: fclose() on read file\r\n");\r
4cff3ded
AW
84 Result = fclose(fp);\r
85 if (0 != Result)\r
86 {\r
87 error("%s(%d) fclose() failed\r\n", __FILE__, __LINE__);\r
88 }\r
89 \r
90\r
91\r
6c79da43 92 printf("Test 8: remove()\r\n");\r
4cff3ded
AW
93 Result = remove("/local/out.txt"); // Removes the file "out.txt" from the local file system\r
94 if (0 != Result)\r
95 {\r
96 error("%s(%d) remove() failed\r\n", __FILE__, __LINE__);\r
97 }\r
98 \r
99\r
100\r
6c79da43 101 if (MRI_ENABLE)\r
4cff3ded 102 {\r
6c79da43 103 printf("Skipping dir tests when MRI is enabled as it doesn't support directory tests.\n");\r
4cff3ded 104 }\r
6c79da43
AG
105 else\r
106 {\r
107 printf("Test 9: opendir()\r\n");\r
108 DIR *d = opendir("/local"); // Opens the root directory of the local file system\r
109 if (NULL == d)\r
110 {\r
111 error("%s(%d) opendir() failed\r\n", __FILE__, __LINE__);\r
112 }\r
113 struct dirent *p;\r
114\r
115 printf("Test 10: readir() for all entries\r\n");\r
116 while((p = readdir(d)) != NULL) \r
117 { // Print the names of the files in the local file system\r
118 printf("%s\r\n", p->d_name); // to stdout.\r
119 }\r
120\r
121 printf("Test 11: closedir\r\n");\r
122 closedir(d);\r
4cff3ded 123 }\r
4cff3ded
AW
124 \r
125 printf("\r\nTest completed\r\n");\r
126}\r