Merge pull request #19 from triffid/extruder-pins
[clinton/Smoothieware.git] / gcc4mbed / samples / LocalFileSystem / main.cpp
CommitLineData
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/* Basic unit tests for LocalFileSystem functionality. */\r
16#include "mbed.h"\r
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
23 char Buffer[32];\r
24 \r
25 printf("\r\n\r\nGCC4MBED Test Suite\r\n");\r
26 printf("LocalFileSystem Unit Tests\r\n");\r
27 \r
28 printf("Test 1: fopen() for write\r\n");\r
29 FILE *fp = fopen("/local/out.txt", "w"); // Open "out.txt" on the local file system for writing\r
30 if (NULL == fp)\r
31 {\r
32 error("%s(%d) fopen() failed\r\n", __FILE__, __LINE__);\r
33 }\r
34\r
35 printf("Test 2: fprintf()\r\n");\r
36 Result = fprintf(fp, "Hello World!");\r
37 if (Result < 0)\r
38 {\r
39 error("%s(%d) fprintf() failed\r\n", __FILE__, __LINE__);\r
40 }\r
41\r
42 printf("Test 3: fclose() on written file\r\n");\r
43 Result = fclose(fp);\r
44 if (0 != Result)\r
45 {\r
46 error("%s(%d) fclose() failed\r\n", __FILE__, __LINE__);\r
47 }\r
48 \r
49\r
50\r
51 printf("Test 4: fopen() for read\r\n");\r
52 fp = fopen("/local/out.txt", "r");\r
53 if (NULL == fp)\r
54 {\r
55 error("%s(%d) fopen() failed\r\n", __FILE__, __LINE__);\r
56 }\r
57\r
58 printf("Test 5: fscanf()\r\n");\r
59 Result = fscanf(fp, "%31s", Buffer);\r
60 if (EOF == Result)\r
61 {\r
62 error("%s(%d) fscanf() failed\r\n", __FILE__, __LINE__);\r
63 }\r
64 printf("Contents of /local/out.txt: %s\r\n", Buffer);\r
65\r
66 printf("Test 6: fclose() on read file\r\n");\r
67 Result = fclose(fp);\r
68 if (0 != Result)\r
69 {\r
70 error("%s(%d) fclose() failed\r\n", __FILE__, __LINE__);\r
71 }\r
72 \r
73\r
74\r
75 printf("Test 7: remove()\r\n");\r
76 Result = remove("/local/out.txt"); // Removes the file "out.txt" from the local file system\r
77 if (0 != Result)\r
78 {\r
79 error("%s(%d) remove() failed\r\n", __FILE__, __LINE__);\r
80 }\r
81 \r
82\r
83\r
84 printf("Test 8: opendir()\r\n");\r
85 DIR *d = opendir("/local"); // Opens the root directory of the local file system\r
86 if (NULL == d)\r
87 {\r
88 error("%s(%d) opendir() failed\r\n", __FILE__, __LINE__);\r
89 }\r
90 struct dirent *p;\r
91\r
92 printf("Test 9: readir() for all entries\r\n");\r
93 while((p = readdir(d)) != NULL) \r
94 { // Print the names of the files in the local file system\r
95 printf("%s\r\n", p->d_name); // to stdout.\r
96 }\r
97\r
98 printf("Test 10: closedir\r\n");\r
99 closedir(d);\r
100 \r
101 printf("\r\nTest completed\r\n");\r
102}\r