Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

94 lines
3.3KB

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