43 static const char EXISTING_FILE[] =
"fat_test.txt";
44 static const char EXISTING_FILE_UPPER[] =
"FAT_TEST.TXT";
45 static const char NEW_FILE_NAME[] =
"new_test.txt";
49 void error_checker (
const ErrorCode err) {
58 pwOut <<
"No print string yet for FatFS's error #" << err - FatFS::BEG_ERROR <<
" (raw = " << err <<
")\n";
60 pwOut <<
"Unknown error: " << err <<
'\n';
65 driver->
flush(buffer);
71 void clear_buffer (
File *file) {
72 clear_buffer(file->m_driver, file->m_buf);
76 PropWare::ErrorCode err;
78 err = testable->
open();
80 MESSAGE(
"Setup failed!");
86 if (NULL != testable) {
88 clear_buffer(testable);
95 TEST(ConstructorDestructor) {
97 ASSERT_NEQ_MSG(0, strcmp(EXISTING_FILE, EXISTING_FILE_UPPER));
101 ASSERT_EQ_MSG(0, strcmp(EXISTING_FILE_UPPER, testable->
get_name()));
102 ASSERT_EQ_MSG((
unsigned int) &
pwOut, (
unsigned int) testable->m_logger);
103 ASSERT_EQ_MSG((
unsigned int) g_fs->get_driver(), (
unsigned int) testable->m_driver);
104 ASSERT_EQ_MSG((
unsigned int) &g_fs->m_buf, (
unsigned int) testable->m_buf);
105 ASSERT_EQ_MSG((
unsigned int) testable->m_fs, (
unsigned int) g_fs);
107 ASSERT_EQ_MSG(
false, testable->m_fileMetadataModified);
112 TEST(Exists_doesNotExist) {
114 ASSERT_FALSE(testable->
exists());
118 TEST(Exists_doesExist) {
121 PropWare::ErrorCode err;
122 const bool exists = testable->
exists(err);
128 TEST(OpenClose_ExistingFile) {
133 err = testable->
open();
135 ASSERT_EQ_MSG(0, err);
139 err = testable->
close();
141 ASSERT_EQ_MSG(0, err);
146 TEST(OpenCloseDelete_NonExistingFile) {
151 ASSERT_FALSE(testable->
exists());
153 err = testable->
open();
155 ASSERT_EQ_MSG(0, err);
159 err = testable->
close();
161 ASSERT_EQ_MSG(0, err);
163 clear_buffer(testable);
164 ASSERT_TRUE(testable->
exists());
168 ASSERT_EQ_MSG(0, err);
169 err = testable->
flush();
171 ASSERT_EQ_MSG(0, err);
173 clear_buffer(testable);
174 ASSERT_FALSE(testable->
exists());
179 TEST(SafePutChar_FileNotOpened) {
184 TEST(SafePutChar_singleChar) {
185 const char sampleChar =
'a';
187 PropWare::ErrorCode err;
193 ASSERT_EQ_MSG(0, err);
196 err = testable->
close();
198 ASSERT_EQ_MSG(0, err);
206 clear_buffer(driver, buffer);
210 ASSERT_EQ_MSG(0, reader.open());
211 ASSERT_EQ_MSG(1, reader.get_length());
212 ASSERT_EQ_MSG(sampleChar, reader.get_char());
218 ASSERT_EQ_MSG(0, err);
219 err = testable->
flush();
221 ASSERT_EQ_MSG(0, err);
223 clear_buffer(testable);
224 ASSERT_FALSE(testable->
exists());
229 TEST(SafePutChar_MultiLine) {
230 PropWare::ErrorCode err;
233 const char testString[] =
"Sample text line\n";
235 for (
unsigned int i = 0; i <
sizeof(testString); ++i) {
238 ASSERT_EQ_MSG(0, err);
241 ASSERT_EQ_MSG(
sizeof(testString), testable->
get_length());
242 err = testable->
close();
244 ASSERT_EQ_MSG(0, err);
252 clear_buffer(driver, buffer);
256 ASSERT_EQ_MSG(0, reader.open());
257 ASSERT_EQ_MSG(
sizeof(testString), reader.get_length());
258 for (
unsigned int i = 0; i <
sizeof(testString); ++i)
259 ASSERT_EQ_MSG(testString[i], reader.get_char());
265 ASSERT_EQ_MSG(0, err);
266 err = testable->
flush();
268 ASSERT_EQ_MSG(0, err);
270 clear_buffer(testable);
271 ASSERT_FALSE(testable->
exists());
277 PropWare::ErrorCode err;
280 uint8_t rawBuffer[SD::SECTOR_SIZE];
284 ASSERT_EQ_MSG(0, reader.open());
286 MESSAGE(
"Files opened...");
287 while (!reader.eof()) {
290 ASSERT_EQ_MSG(0, err);
292 MESSAGE(
"File copied...");
294 err = testable->
close();
296 ASSERT_EQ_MSG(0, err);
298 MESSAGE(
"Writer closed...")
304 ASSERT_EQ_MSG(0, g_fs->flush_fat());
306 clear_buffer(driver, buffer);
309 MESSAGE(
"Writer deleted...")
312 ASSERT_EQ_MSG(0, reader.seek(0,
File::BEG));
315 ASSERT_EQ_MSG(0, fileWriterChecker.open());
316 ASSERT_EQ_MSG(reader.get_length(), fileWriterChecker.get_length());
318 MESSAGE("Readers opened...")
320 while (!fileWriterChecker.eof()) {
322 ASSERT_EQ_MSG(0, fileWriterChecker.safe_get_char(c));
324 char expectedChar = reader.get_char();
325 if (expectedChar != c) {
326 FAIL(
"Failure on char %d", reader.tell() - 1);
330 MESSAGE(
"File content confirmed! Cleaning up...")
332 fileWriterChecker.close();
335 err = testable->remove();
337 ASSERT_EQ_MSG(0, err);
338 err = testable->flush();
340 ASSERT_EQ_MSG(0, err);
342 clear_buffer(testable);
343 ASSERT_FALSE(testable->exists());
349 PropWare::ErrorCode err;
351 START(FatFileReaderTest);
354 if ((err = g_fs->mount())) {
356 failures = (uint8_t) -1;
360 RUN_TEST(ConstructorDestructor);
361 RUN_TEST(Exists_doesNotExist);
362 RUN_TEST(Exists_doesExist);
363 RUN_TEST(OpenClose_ExistingFile);
364 RUN_TEST(OpenCloseDelete_NonExistingFile);
365 RUN_TEST(SafePutChar_singleChar);
366 RUN_TEST(SafePutChar_MultiLine);
virtual uint16_t get_sector_size() const =0
Return the size of a sector (also known as a "block") for the given storage device.
PropWare::ErrorCode safe_put_char(const char c)
Write a character to the file.
PropWare::ErrorCode open()
Open the file.
Concrete class for writing or modifying a FAT 16/32 file.
static void print_error_str(const Printer &printer, const ErrorCode err)
If an error occurs, this method can be used to determine what that error actually means...
Generic definitions and functions for the Parallax Propeller.
bool exists() const
Determine if a file exists (file does not have to be open)
int32_t get_length() const
Return the number of bytes (characters) in the file.
void print_error_str(const Printer *printer, const ErrorCode err) const
Print through UART an error string followed by entering an infinite loop.
const PropWare::Printer pwOut
Most common use of printing in PropWare applications (not thread safe; see PropWare::pwSyncOut for mu...
virtual PropWare::ErrorCode close()
Close a file - a required step in any workflow that includes opening a file.
Any device that uses blocks as hardware level abstraction.
const char * get_name() const
Determine the name of a file.
Basic file interface for opening and closing files.
A simple SD driver communicating over the SPI protocol.
PropWare::ErrorCode remove()
Mark a file as delete and free its clusters in the FAT. File content will not be cleared unless overw...
PropWare::ErrorCode flush()
Flush any modified data back to the SD card.
In-memory buffer containing a single block of data.
ErrorCode flush(Buffer *buffer) const
Flush the contents of a buffer and mark as unmodified.
Read a file on a FAT 16 or FAT 32 storage device.
static SPI * get_instance()
Best way to use SPI is through here, where you can get a shared instance of the SPI module (not threa...