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.

107 lines
3.0KB

  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. uint32_t fs;
  14. testBegin();
  15. ATS_PrintTestStatus("SD.begin()", b = SD.begin());
  16. if (!b) goto done;
  17. SD.remove("test.txt");
  18. f.open("test.txt", FILE_WRITE);
  19. ATS_PrintTestStatus("SD.open()", f.isOpen());
  20. if (!f.isOpen()) goto done;
  21. f.print("abc");
  22. f.print("de");
  23. f.close();
  24. f.open("test.txt", FILE_WRITE);
  25. ATS_PrintTestStatus("SD.open()", f.isOpen());
  26. if (!f.isOpen()) goto done;
  27. f.print("fgh");
  28. f.close();
  29. f.open("test.txt", O_READ);
  30. ATS_PrintTestStatus("SD.open()", f.isOpen());
  31. if (!f.isOpen()) goto done;
  32. fs =f.fileSize();
  33. ATS_PrintTestStatus("read()", f.read() == 'a');
  34. ATS_PrintTestStatus("peek()", f.peek() == 'b');
  35. ATS_PrintTestStatus("read()", f.read() == 'b');
  36. ATS_PrintTestStatus("read()", f.read() == 'c');
  37. ATS_PrintTestStatus("peek()", f.peek() == 'd');
  38. ATS_PrintTestStatus("peek()", f.peek() == 'd');
  39. ATS_PrintTestStatus("peek()", f.peek() == 'd');
  40. ATS_PrintTestStatus("peek()", f.peek() == 'd');
  41. ATS_PrintTestStatus("read()", f.read() == 'd');
  42. ATS_PrintTestStatus("available()", f.curPosition() != fs);
  43. ATS_PrintTestStatus("read()", f.read() == 'e');
  44. ATS_PrintTestStatus("available()", f.curPosition() != fs);
  45. ATS_PrintTestStatus("peek()", f.peek() == 'f');
  46. ATS_PrintTestStatus("read()", f.read() == 'f');
  47. ATS_PrintTestStatus("peek()", f.peek() == 'g');
  48. ATS_PrintTestStatus("available()", f.curPosition() != fs);
  49. ATS_PrintTestStatus("peek()", f.peek() == 'g');
  50. ATS_PrintTestStatus("read()", f.read() == 'g');
  51. ATS_PrintTestStatus("available()", f.curPosition() != fs);
  52. ATS_PrintTestStatus("available()", f.curPosition() != fs);
  53. ATS_PrintTestStatus("available()", f.curPosition() != fs);
  54. ATS_PrintTestStatus("peek()", f.peek() == 'h');
  55. ATS_PrintTestStatus("read()", f.read() == 'h');
  56. ATS_PrintTestStatus("available()", f.curPosition() == fs);
  57. ATS_PrintTestStatus("peek()", f.peek() == -1);
  58. ATS_PrintTestStatus("read()", f.read() == -1);
  59. ATS_PrintTestStatus("peek()", f.peek() == -1);
  60. ATS_PrintTestStatus("read()", f.read() == -1);
  61. f.close();
  62. SD.remove("test2.txt");
  63. f.open("test2.txt", FILE_WRITE);
  64. ATS_PrintTestStatus("SD.open()", f.isOpen());
  65. if (!f.isOpen()) goto done;
  66. f.print("ABC");
  67. f.close();
  68. f.open("test.txt", O_READ);
  69. ATS_PrintTestStatus("SD.open()", f.isOpen());
  70. if (!f.isOpen()) goto done;
  71. ATS_PrintTestStatus("peek()", f.peek() == 'a');
  72. f.close();
  73. f.open("test2.txt", O_READ);
  74. ATS_PrintTestStatus("SD.open()", f.isOpen());
  75. if (!f.isOpen()) goto done;
  76. ATS_PrintTestStatus("peek()", f.peek() == 'A');
  77. ATS_PrintTestStatus("read()", f.read() == 'A');
  78. f.close();
  79. done:
  80. testEnd();
  81. }
  82. void loop() {}