Merge pull request #128 from logxen/edge
[clinton/Smoothieware.git] / gcc4mbed / samples / FileTest / main.cpp
CommitLineData
8fcce42e
AG
1/* Copyright 2012 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 static const char Filename[] = "/local/foo.bar";\r
23 unsigned char TestBuffer[256];\r
24 unsigned char ReadBuffer[256];\r
25 size_t i;\r
26 \r
27 // Fill in test buffer with every byte value possible.\r
28 for (i = 0 ; i < sizeof(TestBuffer) ; i++)\r
29 {\r
30 TestBuffer[i] = i;\r
31 }\r
32 memset(ReadBuffer, 0, sizeof(ReadBuffer));\r
33 \r
34 // Create a file in the LocalFileSystem with this data.\r
35 FILE* fp = fopen(Filename, "w");\r
36 if (!fp)\r
37 {\r
38 error("Failed to open %s for writing\n", Filename);\r
39 }\r
40 \r
41 int BytesWritten = fwrite(TestBuffer, 1, sizeof(TestBuffer), fp);\r
42 if (BytesWritten != sizeof(TestBuffer))\r
43 {\r
44 error("Failed to write all of the bytes to test file.\n");\r
45 }\r
46 \r
47 fclose(fp);\r
48 fp = NULL;\r
49 \r
50 // Now reopen the file and read in the data and make sure it matches.\r
51 fp = fopen(Filename, "r");\r
52 if (!fp)\r
53 {\r
54 error("Failed to open %s for writing\n", Filename);\r
55 }\r
56 \r
57 int BytesRead = fread(ReadBuffer, 1, sizeof(ReadBuffer), fp);\r
58 if (BytesRead != sizeof(ReadBuffer))\r
59 {\r
60 error("Failed to read all of the bytes from test file.\n");\r
61 }\r
62 if (0 != memcmp(TestBuffer, ReadBuffer, sizeof(TestBuffer)))\r
63 {\r
64 error("File data did match expected value.\n");\r
65 }\r
66 \r
67 fclose(fp);\r
68 fp = NULL;\r
69\r
70 // Can delete the file now that we are done with it.\r
71 remove(Filename);\r
72\r
73 printf("\r\nTest completed\r\n");\r
74}\r