-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTestFileOperations.ino
335 lines (282 loc) · 10.1 KB
/
TestFileOperations.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#include <Arduino_UnifiedStorage.h>
#define HAS_SD
#define HAS_USB
#define HAS_QSPI
#if defined(HAS_USB)
USBStorage usb = USBStorage();
#endif
#if defined(HAS_SD)
SDStorage sd = SDStorage();
#endif
#if defined(HAS_QSPI)
InternalStorage internal = InternalStorage(2, "user", FS_FAT);
#endif
bool testFileCreationWithOpen(Folder root) {
UFile file = UFile();
String path = root.getPathString() + "/test_open.txt";
Serial.println(path);
if (file.open(path, FileMode::WRITE)) {
Serial.println("\n--- Test creating file using file.open ---");
Serial.println("Test creating file using file.open - Success");
file.close();
file.remove();
return true;
} else {
Serial.println("Test creating file using file.open - Failed. Error: " + String(getErrno()));
return false;
}
}
bool testFileCreationWithCreate(Folder root) {
Serial.println("\n--- Test creating file using root.createFile ---");
UFile file = root.createFile("test_create.txt", FileMode::WRITE);
if (file.exists()) {
Serial.println("Test creating file using root.createFile - Success");
file.close();
file.remove();
return true;
} else {
Serial.println("Test creating file using root.createFile - Failed. Error: " + String(getErrno()));
return false;
}
}
bool testWritingCharBuffer(Folder root) {
UFile file = root.createFile("test_write.txt", FileMode::WRITE);
if (file.exists()) {
Serial.println("\n--- Test writing char * buffer ---");
size_t bytesWritten = file.write(reinterpret_cast<const uint8_t*>("Hello, World!"), 13);
Serial.println("Bytes written to file: " + String(bytesWritten));
file.close();
file.remove();
return bytesWritten == 13;
} else {
Serial.println("Test writing char * buffer - Failed. Error: " + String(getErrno()));
return false;
}
}
bool testWritingWithString(Folder root) {
UFile file = root.createFile("test_write_string.txt", FileMode::WRITE);
if (file.exists()) {
Serial.println("\n--- Test writing String ---");
String data = " This is a test with String data.";
size_t bytesWritten = file.write(data);
Serial.println("Bytes written to file (with String): " + String(bytesWritten));
file.close();
file.remove();
return true;
} else {
Serial.println("Test writing with String data type - Failed. Error: " + String(getErrno()));
return false;
}
}
bool testAppending(Folder root) {
UFile file = root.createFile("test_append.txt", FileMode::WRITE);
if (file.exists()) {
Serial.println("\n--- Test appending ---");
String data = " Appending some more data.";
size_t bytesWritten = file.write(reinterpret_cast<const uint8_t*>(data.c_str()), data.length());
Serial.println("Bytes written to file (appending): " + String(bytesWritten));
file.close();
file.remove();
return true;
} else {
Serial.println("Test appending to the file - Failed. Error: " + String(getErrno()));
return false;
}
}
bool testReadingAll(Folder root) {
UFile file = root.createFile("test_read.txt", FileMode::WRITE);
if (file.exists()) {
file.write(reinterpret_cast<const uint8_t*>("Hello, World!"), 13);
file.close();
if (file.open(root.getPathString() + "/test_read.txt", FileMode::READ)) {
char buffer[file.available()];
size_t bytesRead = file.read(reinterpret_cast<uint8_t*>(buffer), sizeof(buffer));
buffer[bytesRead] = '\0'; // Null-terminate the string
Serial.println("\n--- Test reading everything from the file ---");
Serial.println("Read from file (test_read.txt): " + String(buffer));
file.close();
file.remove();
return true;
} else {
Serial.println("Test reading everything from the file - Failed. Error: " + String(getErrno()));
return false;
}
} else {
Serial.println("Test reading everything from the file - Failed to create file. Error: " + String(getErrno()));
return false;
}
return false;
}
bool testSeeking(Folder root) {
UFile file = root.createFile("test_seek.txt", FileMode::WRITE);
if (file.exists()) {
file.write(reinterpret_cast<const uint8_t*>("Hello, World!"), 13);
file.close();
if (file.open(root.getPathString() + "/test_seek.txt", FileMode::READ)) {
Serial.println("\n--- Test seeking file ---");
file.seek(7);
char buffer[20];
size_t bytesRead = file.read(reinterpret_cast<uint8_t*>(buffer), sizeof(buffer));
buffer[bytesRead] = '\0'; // Null-terminate the string
Serial.println("Read after seeking: " + String(buffer));
file.close();
file.remove();
return true;
} else {
Serial.println("Test seeking in the file - Failed. Error: " + String(getErrno()));
return false;
}
} else {
Serial.println("Test seeking in the file - Failed to create file. Error: " + String(getErrno()));
return false;
}
}
bool testAvailableData(Folder root) {
UFile file = root.createFile("test_available.txt", FileMode::WRITE);
if (file.exists()) {
file.write(reinterpret_cast<const uint8_t*>("Hello, World!"), 13);
file.close();
if (file.open(root.getPathString() + "/test_available.txt", FileMode::READ)) {
Serial.println("\n--- Test available data ---");
int availableBytes = file.available();
Serial.println("Available bytes in file (test_available.txt): " + String(availableBytes));
file.close();
file.remove();
return true;
} else {
Serial.println("Test checking available data in the file - Failed. Error: " + String(getErrno()));
return false;
}
} else {
Serial.println("Test checking available data in the file - Failed to create file. Error: " + String(getErrno()));
return false;
}
return false;
}
bool testCopyingFile(Folder root) {
// Test copying a file
UFile sourceFile = root.createFile("test_source_copy.txt", FileMode::WRITE);
if (sourceFile.exists()) {
sourceFile.write(reinterpret_cast<const uint8_t*>("Hello, World!"), 13);
sourceFile.close();
Folder destinationFolder = root.createSubfolder("test_folder_copy");
if (destinationFolder.exists()) {
Serial.println("\n--- Test copying a file ---");
Serial.println("Source file name: " + String(sourceFile.getPathString()));
if (sourceFile.copyTo(destinationFolder)) {
Serial.println("File copied successfully!");
sourceFile.close();
sourceFile.remove();
destinationFolder.remove();
return true;
} else {
Serial.println("File copying failed. Error: " + String(getErrno()));
return false;
}
} else {
Serial.println("Test copying a file - Failed. Error: " + String(getErrno()));
return false;
}
} else {
Serial.println("Test copying a file - Failed to create destination folder. Error: " + String(getErrno()));
return false;
}
}
bool testMovingFile(Folder root) {
UFile sourceFileMove = root.createFile("test_source_move.txt", FileMode::WRITE);
if (sourceFileMove.exists()) {
sourceFileMove.write(reinterpret_cast<const uint8_t*>("Hello, World!"), 13);
sourceFileMove.close();
Folder destinationFolder = root.createSubfolder("test_folder_move");
if (destinationFolder.exists()) {
UFile movedFile = sourceFileMove;
if (movedFile.exists()) {
Serial.println("\n--- Test moving a file ---");
Serial.println("Source file name: " + String(sourceFileMove.getPathString()));
Serial.println("Destination file name: " + String(destinationFolder.getPathString()) + "/test_source_move.txt");
if (sourceFileMove.moveTo(destinationFolder)) {
Serial.println("File moved successfully!");
sourceFileMove.close();
movedFile.close();
movedFile.remove();
destinationFolder.remove();
sourceFileMove.remove();
return true;
} else {
Serial.println("File moving failed. Error: " + String(getErrno()));
return false;
}
} else {
Serial.println("Test moving a file - Failed. Error: " + String(getErrno()));
return false;
}
} else {
Serial.println("Test moving a file - Failed to create destination folder. Error: " + String(getErrno()));
return false;
}
} else {
Serial.println("Test moving a file - Failed. Error: " + String(getErrno()));
return false;
}
}
void printFolderContents(Folder dir, int indentation = 0) {
std::vector<Folder> directories = dir.getFolders();
std::vector<UFile>files = dir.getFiles();
// Print directories
for (Folder subdir : directories) {
for (int i = 0; i < indentation; i++) {
Serial.print(" ");
}
Serial.print("[D] ");
Serial.println(subdir.getPath());
printFolderContents(subdir, indentation + 1);
}
// Print files
for (UFile file : files) {
for (int i = 0; i < indentation; i++) {
Serial.print(" ");
}
Serial.print("[F] ");
Serial.println(file.getPath());
}
}
void runTests(Arduino_UnifiedStorage * storage, String storageType) {
if (storage->begin()) {
Folder root = storage->getRootFolder();
Serial.println("\n=== Testing " + storageType + " ===");
Serial.println("========= File Tests =========");
testFileCreationWithOpen(root);
testFileCreationWithCreate(root);
testWritingCharBuffer(root);
testWritingWithString(root);
testAppending(root);
testReadingAll(root);
testSeeking(root);
testAvailableData(root);
testCopyingFile(root);
testMovingFile(root);
Serial.println("\n========= FS Contents after File Tests =========");
Serial.println("Should be empty if deletion was succesful");
printFolderContents(root);
Serial.println("=============================\n");
storage -> unmount();
}
}
void setup(){
Serial.begin(115200);
while(!Serial);
#if defined(HAS_SD)
sd.formatFAT();
runTests(&sd, "SD");
#endif
#if defined(HAS_USB)
usb.formatFAT();
runTests(&usb, "USB");
#endif
#if defined(HAS_QSPI)
internal.formatFAT();
runTests(&internal, "QSPI");
#endif
}
void loop(){
}