You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
3.3KB

  1. // modified from ArduinoTestSuite 0022 by William Greiman
  2. // Tests writing to and reading from a file, in particular the
  3. // the Stream implementation (e.g. read() and peek()).
  4. #include <SPI.h>
  5. #include <SdFat.h>
  6. #include <SdFatTestSuite.h>
  7. SdFat SD;
  8. #define FILE_WRITE O_RDWR | O_CREAT | O_AT_END
  9. #define ATS_PrintTestStatus(msg, b) testVerify_P(b, PSTR(msg))
  10. void setup() {
  11. boolean b;
  12. SdFile f;
  13. testBegin();
  14. ATS_PrintTestStatus("SD.begin()", b = SD.begin());
  15. if (!b) goto done;
  16. SD.remove("test.txt");
  17. f.open("test.txt", FILE_WRITE);
  18. ATS_PrintTestStatus("SD.open()", f.isOpen());
  19. if (!f.isOpen()) goto done;
  20. ATS_PrintTestStatus("initial position", f.curPosition() == 0);
  21. ATS_PrintTestStatus("initial size", f.fileSize() == 0);
  22. f.print("0123456789");
  23. ATS_PrintTestStatus("position after writing", f.curPosition() == 10);
  24. ATS_PrintTestStatus("size after writing", f.fileSize() == 10);
  25. f.seekSet(0);
  26. ATS_PrintTestStatus("size after seek", f.fileSize() == 10);
  27. ATS_PrintTestStatus("position after seek", f.curPosition() == 0);
  28. f.seekSet(7);
  29. ATS_PrintTestStatus("position after seek", f.curPosition() == 7);
  30. ATS_PrintTestStatus("reading after seek", f.read() == '7');
  31. ATS_PrintTestStatus("position after reading after seeking", f.curPosition() == 8);
  32. ATS_PrintTestStatus("reading after reading after seeking", f.read() == '8');
  33. f.seekSet(3);
  34. ATS_PrintTestStatus("position after seeking", f.curPosition() == 3);
  35. ATS_PrintTestStatus("peeking after seeking", f.peek() == '3');
  36. ATS_PrintTestStatus("position after peeking after seeking", f.curPosition() == 3);
  37. ATS_PrintTestStatus("peeking after peeking after seeking", f.peek() == '3');
  38. ATS_PrintTestStatus("position after peeking after seeking", f.curPosition() == 3);
  39. ATS_PrintTestStatus("peeking after peeking after seeking", f.read() == '3');
  40. ATS_PrintTestStatus("position after peeking after seeking", f.curPosition() == 4);
  41. f.seekSet(1);
  42. ATS_PrintTestStatus("position after seeking", f.curPosition() == 1);
  43. ATS_PrintTestStatus("peeking after seeking", f.peek() == '1');
  44. f.seekSet(4);
  45. ATS_PrintTestStatus("position after seeking", f.curPosition() == 4);
  46. ATS_PrintTestStatus("peeking after seeking", f.peek() == '4');
  47. f.seekSet(7);
  48. ATS_PrintTestStatus("position()", f.curPosition() == 7);
  49. ATS_PrintTestStatus("read()", f.read() == '7');
  50. f.seekSet(0);
  51. f.peek();
  52. f.print("AB");
  53. ATS_PrintTestStatus("position()", f.curPosition() == 2);
  54. ATS_PrintTestStatus("size()", f.fileSize() == 10);
  55. ATS_PrintTestStatus("read()", f.read() == '2');
  56. f.seekSet(0);
  57. ATS_PrintTestStatus("read()", f.read() == 'A');
  58. ATS_PrintTestStatus("read()", f.read() == 'B');
  59. ATS_PrintTestStatus("read()", f.read() == '2');
  60. f.close();
  61. f.open("test.txt", O_READ);
  62. ATS_PrintTestStatus("SD.open()", f.isOpen());
  63. if (!f.isOpen()) goto done;
  64. ATS_PrintTestStatus("position()", f.curPosition() == 0);
  65. ATS_PrintTestStatus("size()", f.fileSize() == 10);
  66. ATS_PrintTestStatus("peek()", f.peek() == 'A');
  67. ATS_PrintTestStatus("read()", f.read() == 'A');
  68. f.seekSet(4);
  69. ATS_PrintTestStatus("position()", f.curPosition() == 4);
  70. ATS_PrintTestStatus("size()", f.fileSize() == 10);
  71. ATS_PrintTestStatus("peek()", f.peek() == '4');
  72. ATS_PrintTestStatus("read()", f.read() == '4');
  73. f.close();
  74. done:
  75. testEnd();
  76. }
  77. void loop() {}