您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

95 行
3.3KB

  1. #include <SPI.h>
  2. #include <SdFat.h>
  3. #include <SdFatTestSuite.h>
  4. SdFat sd;
  5. char *testName = "SDFAT.TST";
  6. //------------------------------------------------------------------------------
  7. void fstreamOpen() {
  8. ios::openmode nocreate[] = {ios::in, ios::in | ios::out};
  9. ios::openmode create[] =
  10. {ios::out, ios::out | ios::app, ios::app, ios::out | ios::trunc,
  11. ios::in | ios::out | ios::trunc, ios::in | ios::out | ios::app,
  12. ios::in | ios::app};
  13. ios::openmode illegal[] =
  14. {0, ios::trunc, ios::app | ios::trunc, ios::in | ios::app | ios::trunc,
  15. ios::in | ios::trunc, ios::out | ios::app | ios::trunc,
  16. ios::in | ios::out | ios::app | ios::trunc};
  17. sd.remove(testName);
  18. fstream file(testName);
  19. testVerifyMsg(!file.is_open()&& !sd.exists(testName), "fstream constructor");
  20. for (uint8_t i = 0 ; i < sizeof(nocreate)/sizeof(nocreate[1]); i++) {
  21. file.close();
  22. sd.remove(testName);
  23. file.open(testName, nocreate[i]);
  24. testVerifyMsg(!sd.exists(testName) && !file.is_open(), "fstream nocreate !exists");
  25. }
  26. for (uint8_t i = 0 ; i < sizeof(create)/sizeof(create[1]); i++) {
  27. file.close();
  28. sd.remove(testName);
  29. file.open(testName, create[i]);
  30. testVerifyMsg(sd.exists(testName) && file.is_open(), "fstream create openmode");
  31. }
  32. for (uint8_t i = 0 ; i < sizeof(illegal)/sizeof(illegal[1]); i++) {
  33. file.close();
  34. file.open(testName, illegal[i]);
  35. testVerifyMsg(sd.exists(testName) && !file.is_open(), "fstream illegal openmode");
  36. }
  37. for (uint8_t i = 0 ; i < sizeof(nocreate)/sizeof(nocreate[1]); i++) {
  38. file.close();
  39. file.open(testName, nocreate[i]);
  40. testVerifyMsg(sd.exists(testName) && file.is_open(), "fstream nocreate exists");
  41. }
  42. }
  43. //------------------------------------------------------------------------------
  44. void testPosition() {
  45. sd.remove(testName);
  46. ofstream ofs(testName);
  47. testVerifyBool(ofs.good() && ofs.tellp() == 0);
  48. ofs.seekp(0, ios::end);
  49. testVerifyBool(ofs.good() && ofs.tellp() == 0);
  50. ofs << "abcde";
  51. testVerifyBool(ofs.good() && ofs.tellp() == 5);
  52. ofs.seekp(4);
  53. testVerifyBool(ofs.good() && ofs.tellp() == 4);
  54. ofs.seekp(-1, ios::cur);
  55. testVerifyBool(ofs.good() && ofs.tellp() == 3);
  56. ofs.close();
  57. ifstream ifs(testName, ios::ate);
  58. testVerifyBool(ifs.good() && ifs.tellg() == 5);
  59. ifs.seekg(0);
  60. testVerifyBool(ifs.get() == 'a' && ifs.get() == 'b');
  61. testVerifyBool(ifs.tellg() == 2 && ifs.good());
  62. ifs.seekg(3, ios::cur);
  63. testVerifyBool(ifs.tellg() == 5 && ifs.good());
  64. ifs.seekg(4, ios::beg);
  65. testVerifyBool(ifs.good() && ifs.tellg() == 4);
  66. ifs.close();
  67. ofs.open(testName, ios::app);
  68. testVerifyBool(ofs.good() && ofs.tellp() == 0);
  69. ofs << 'f';
  70. testVerifyBool(ofs.good() && ofs.tellp() == 6);
  71. ofs.close();
  72. ofs.open(testName, ios::trunc);
  73. ofs.seekp(0, ios::end);
  74. testVerifyBool(ofs.good() && ofs.tellp() == 0);
  75. ofs << "ABCDEF";
  76. ofs.close();
  77. fstream fs(testName);
  78. testVerifyBool(fs.good() && fs.tellp() == 0 && fs.tellg() == 0);
  79. fs.seekg(2);
  80. testVerifyBool(fs.good() && fs.get() == 'C');
  81. }
  82. //------------------------------------------------------------------------------
  83. void setup() {
  84. testBegin();
  85. if (!sd.begin()) sd.initErrorHalt();
  86. fstreamOpen();
  87. testPosition();
  88. testEnd();
  89. }
  90. //------------------------------------------------------------------------------
  91. void loop() {}