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.

260 lines
9.2KB

  1. #include <SdFat.h>
  2. #include <SdFatTestSuite.h>
  3. char buf[100];
  4. ibufstream ib;
  5. #define ibInit(s) ibInit_P(PSTR(s))
  6. //----------------------------------------------------------
  7. void ibInit_P(PGM_P p) {
  8. if (strlen_P(p) >= sizeof(buf)) {
  9. ib.init("");
  10. ib.setstate(ios::badbit);
  11. } else {
  12. ib.clear();
  13. strncpy_P(buf, p, sizeof(buf));
  14. ib.init(buf);
  15. }
  16. }
  17. //------------------------------------------------------------------------------
  18. void istreamBool() {
  19. bool b;
  20. ibInit(" 0 1 2");
  21. testVerifyBool((ib >> b) && !b);
  22. testVerifyBool((ib >> b) && b);
  23. testVerifyBool(!(ib >> b) && !ib.good());
  24. ibInit(" true false err");
  25. testVerifyBool((ib >> boolalpha >> b) && b && ib.good());
  26. testVerifyBool((ib >> b) && !b && ib.good());
  27. testVerifyBool(!(ib >> b) && ib.fail());
  28. ibInit("1");
  29. testVerifyBool((ib >> noboolalpha >> b) && b && ib.eof());
  30. }
  31. //------------------------------------------------------------------------------
  32. void istreamChar() {
  33. char c;
  34. signed char sc;
  35. unsigned char uc;
  36. ibInit("c s u g");
  37. testVerifyBool((ib >> c) && ib.good() && c == 'c');
  38. testVerifyBool((ib >> sc) && ib.good() && sc == 's');
  39. testVerifyBool((ib >> uc) && ib.good() && uc == 'u');
  40. testVerifyBool(ib.get() == ' ');
  41. testVerifyBool(ib.peek() == 'g' && ib.good());
  42. testVerifyBool(ib.get() == 'g' && ib.good());
  43. testVerifyBool(ib.get() == -1 && ib.eof());
  44. }
  45. //------------------------------------------------------------------------------
  46. void istreamDouble() {
  47. double f;
  48. ibInit("0 .1 1. 2 3.4 .1e5 1e5 -1E6 +2.3e-3 -123.4567");
  49. testVerifyBool((ib >> f) && f == 0 && ib.good());
  50. testVerifyBool((ib >> f) && f == 0.1 && ib.good());
  51. testVerifyBool((ib >> f) && f == 1.0 && ib.good());
  52. testVerifyBool((ib >> f) && f == 2.0 && ib.good());
  53. testVerifyBool((ib >> f) && f == 3.4 && ib.good());
  54. testVerifyBool((ib >> f) && f == 10000.0 && ib.good());
  55. testVerifyBool((ib >> f) && f == 1e5 && ib.good());
  56. testVerifyBool((ib >> f) && f == -1E6 && ib.good());
  57. testVerifyBool((ib >> f) && f == 2.3e-3 && ib.good());
  58. testVerifyBool((ib >> f) && fabs(f + 123.4567) < 1e-5 && ib.eof());
  59. if (fabs(f + 123.4567) >= 1e-5) Serial.println(f, 8);
  60. }
  61. //------------------------------------------------------------------------------
  62. void istreamFloat() {
  63. float f;
  64. ibInit("0 .1 1. 2 3.4 .1e5 1e5 -1E6 +2.3e-3 -123.4567");
  65. testVerifyBool((ib >> f) && f == 0 && ib.good());
  66. testVerifyBool((ib >> f) && f == 0.1f && ib.good());
  67. testVerifyBool((ib >> f) && f == 1.0 && ib.good());
  68. testVerifyBool((ib >> f) && f == 2.0 && ib.good());
  69. testVerifyBool((ib >> f) && f == 3.4f && ib.good());
  70. testVerifyBool((ib >> f) && f == 10000.0 && ib.good());
  71. testVerifyBool((ib >> f) && f == 1e5 && ib.good());
  72. testVerifyBool((ib >> f) && f == -1E6 && ib.good());
  73. testVerifyBool((ib >> f) && f == 2.3e-3f && ib.good());
  74. testVerifyBool((ib >> f) && fabs(f + 123.4567f) < 1e-5 && ib.eof());
  75. if (fabs(f + 123.4567) >= 1e-5) Serial.println(f, 8);
  76. }
  77. //------------------------------------------------------------------------------
  78. void istreamGet() {
  79. char s[4];
  80. ibInit("ab c");
  81. testVerifyBool(ib.get() == 'a' && ib.good() && ib.gcount() == 1);
  82. testVerifyBool(ib.get() == 'b' && ib.good() && ib.gcount() == 1);
  83. testVerifyBool(ib.get() == ' ' && ib.good() && ib.gcount() == 1);
  84. testVerifyBool(ib.get() == 'c' && ib.good() && ib.gcount() == 1);
  85. testVerifyBool(ib.get() == -1 && ib.eof() && ib.gcount() == 0);
  86. ibInit("ab\ncdef");
  87. ib.get(s, sizeof(s));
  88. testVerifyBool(ib.good() && ib.gcount() == 2);
  89. testVerifyStr(s, "ab");
  90. testVerifyBool(ib.get() == '\n' && ib.good() && ib.gcount() == 1);
  91. ib.get(s, sizeof(s));
  92. testVerifyBool(ib.good() && ib.gcount() == 3);
  93. testVerifyStr(s, "cde");
  94. ib.get(s, sizeof(s));
  95. testVerifyBool(ib.eof() && ib.gcount() == 1);
  96. testVerifyStr(s, "f");
  97. ibInit(
  98. "short line\n"
  99. "\n"
  100. "17 character line\n"
  101. "too long for buffer\n"
  102. "line with no nl"
  103. );
  104. char buf[18];
  105. ib.getline(buf, sizeof(buf));
  106. testVerifyBool(ib.good() && ib.gcount() == 11);
  107. testVerifyStr(buf, "short line");
  108. ib.getline(buf, sizeof(buf));
  109. testVerifyBool(ib.good() && ib.gcount() == 1 && buf[0] == '\0');
  110. ib.getline(buf, sizeof(buf));
  111. testVerifyBool(ib.good() && ib.gcount() == 18);
  112. testVerifyStr(buf, "17 character line");
  113. ib.getline(buf, sizeof(buf));
  114. testVerifyBool(ib.fail() && !ib.eof() && ib.gcount() == 17);
  115. testVerifyStr(buf, "too long for buff");
  116. ib.clear();
  117. ib.getline(buf, sizeof(buf));
  118. testVerifyBool(ib.good() && !ib.eof() && ib.gcount() == 3);
  119. testVerifyStr(buf, "er");
  120. ib.getline(buf, sizeof(buf));
  121. testVerifyBool(!ib.fail() && ib.eof() && ib.gcount() == 15);
  122. testVerifyStr(buf, "line with no nl");
  123. }
  124. //------------------------------------------------------------------------------
  125. void istreamNumber() {
  126. short s;
  127. signed short ss;
  128. unsigned short us;
  129. int i;
  130. signed int si;
  131. unsigned int ui;
  132. long l;
  133. signed long sl;
  134. unsigned long ul;
  135. ibInit("-32769");
  136. testVerifyBool(!(ib >> s) && ib.fail());
  137. ibInit("-32768 0 32767 32768");
  138. testVerifyBool((ib >> s) && s == -32768 && ib.good());
  139. testVerifyBool((ib >> s) && s == 0 && ib.good());
  140. testVerifyBool((ib >> s) && s == 32767 && ib.good());
  141. testVerifyBool(!(ib >> s) && ib.fail());
  142. ibInit("-32769");
  143. testVerifyBool(!(ib >> ss) && ib.fail());
  144. ibInit("-32768 0 32767 32768");
  145. testVerifyBool((ib >> ss) && ss == -32768 && ib.good());
  146. testVerifyBool((ib >> ss) && ss == 0 && ib.good());
  147. testVerifyBool((ib >> ss) && ss == 32767 && ib.good());
  148. testVerifyBool(!(ib >> ss) && ib.fail());
  149. ibInit("0 65535 65536");
  150. testVerifyBool((ib >> us) && us == 0 && ib.good());
  151. testVerifyBool((ib >> us) && us == 65535 && ib.good());
  152. testVerifyBool(!(ib >> us) && ib.fail());
  153. if (sizeof(int) == 2) {
  154. ibInit("-32769");
  155. testVerifyBool(!(ib >> i) && ib.fail());
  156. ibInit("-32768 0 32767 32768");
  157. testVerifyBool((ib >> i) && i == -32768 && ib.good());
  158. testVerifyBool((ib >> i) && i == 0 && ib.good());
  159. testVerifyBool((ib >> i) && i == 32767 && ib.good());
  160. testVerifyBool(!(ib >> i) && ib.fail());
  161. ibInit("-32769");
  162. testVerifyBool(!(ib >> si) && ib.fail());
  163. ibInit("-32768 0 32767 32768");
  164. testVerifyBool((ib >> si) && si == -32768 && ib.good());
  165. testVerifyBool((ib >> si) && si == 0 && ib.good());
  166. testVerifyBool((ib >> si) && si == 32767 && ib.good());
  167. testVerifyBool(!(ib >> si) && ib.fail());
  168. ibInit("0 65535 65536");
  169. testVerifyBool((ib >> ui) && ui == 0 && ib.good());
  170. testVerifyBool((ib >> ui) && ui == 65535 && ib.good());
  171. testVerifyBool(!(ib >> ui) && ib.fail());
  172. } else {
  173. ibInit("-2147483649");
  174. testVerifyBool(!(ib >> i) && ib.fail());
  175. ibInit("-2147483648 0 2147483647 2147483648");
  176. testVerifyBool((ib >> i) && i == -2147483648 && ib.good());
  177. testVerifyBool((ib >> i) && i == 0 && ib.good());
  178. testVerifyBool((ib >> i) && i == 2147483647 && ib.good());
  179. testVerifyBool(!(ib >> i) && ib.fail());
  180. ibInit("-2147483649");
  181. testVerifyBool(!(ib >> si) && ib.fail());
  182. ibInit("-2147483648 0 2147483647 2147483648");
  183. testVerifyBool((ib >> si) && si == -2147483648 && ib.good());
  184. testVerifyBool((ib >> si) && si == 0 && ib.good());
  185. testVerifyBool((ib >> si) && si == 2147483647 && ib.good());
  186. testVerifyBool(!(ib >> si) && ib.fail());
  187. ibInit("0 4294967295 4294967296");
  188. testVerifyBool((ib >> ui) && ui == 0 && ib.good());
  189. testVerifyBool((ib >> ui) && ui == 4294967295 && ib.good());
  190. testVerifyBool(!(ib >> ui) && ib.fail());
  191. }
  192. ibInit("-2147483649");
  193. testVerifyBool(!(ib >> l) && ib.fail());
  194. ibInit("-2147483648 0 2147483647 2147483648");
  195. testVerifyBool((ib >> l) && l == -2147483648 && ib.good());
  196. testVerifyBool((ib >> l) && l == 0 && ib.good());
  197. testVerifyBool((ib >> l) && l == 2147483647 && ib.good());
  198. testVerifyBool(!(ib >> l) && ib.fail());
  199. ibInit("-2147483649");
  200. testVerifyBool(!(ib >> sl) && ib.fail());
  201. ibInit("-2147483648 0 2147483647 2147483648");
  202. testVerifyBool((ib >> sl) && sl == -2147483648 && ib.good());
  203. testVerifyBool((ib >> sl) && sl == 0 && ib.good());
  204. testVerifyBool((ib >> sl) && sl == 2147483647 && ib.good());
  205. testVerifyBool(!(ib >> sl) && ib.fail());
  206. ibInit("0 4294967295 4294967296");
  207. testVerifyBool((ib >> ul) && ul == 0 && ib.good());
  208. testVerifyBool((ib >> ul) && ul == 4294967295 && ib.good());
  209. testVerifyBool(!(ib >> ul) && ib.fail());
  210. // octal hex
  211. ibInit("123 abc 0xdef 0XABC 567");
  212. testVerifyBool((ib >> oct >> i) && i == 83);
  213. testVerifyBool((ib >> hex >> i) && i == 0xabc);
  214. testVerifyBool((ib >> i) && i == 0xdef);
  215. testVerifyBool((ib >> i) && i == 0xabc);
  216. testVerifyBool((ib >> dec >> i) && i ==567);
  217. }
  218. //------------------------------------------------------------------------------
  219. void istreamStr() {
  220. char str[20];
  221. ibInit("abc def\r\n hij");
  222. testVerifyBool((ib >> str) && ib.good());
  223. testVerifyStr(str, "abc");
  224. testVerifyBool((ib >> str) && ib.good());
  225. testVerifyStr(str, "def");
  226. testVerifyBool((ib >> str) && ib.eof());
  227. testVerifyStr(str, "hij");
  228. }
  229. //------------------------------------------------------------------------------
  230. void setup() {
  231. testBegin();
  232. istreamBool();
  233. istreamChar();
  234. istreamDouble();
  235. istreamFloat();
  236. istreamGet();
  237. istreamNumber();
  238. istreamStr();
  239. testEnd();
  240. }
  241. //------------------------------------------------------------------------------
  242. void loop() {}