Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ostreamTest.ino 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #include <SdFat.h>
  2. #include <SdFatTestSuite.h>
  3. //------------------------------------------------------------------------------
  4. void ostreamBool() {
  5. char buf[40];
  6. obufstream ob(buf, sizeof(buf));
  7. bool f = false;
  8. bool t = true;
  9. ob << t << ',' << f << ',' << setw(2) << t << ',' << left << setw(2) << f;
  10. testVerifyStr(buf, "1,0, 1,0 ");
  11. ob.init(buf, sizeof(buf));
  12. ob << boolalpha << t << ',' << f << ',' << setw(5) << t;
  13. ob << ',' << right << setw(6) << f;
  14. testVerifyStr(buf, "true,false,true , false");
  15. }
  16. //------------------------------------------------------------------------------
  17. void ostreamChar() {
  18. char buf[40];
  19. obufstream ob(buf, sizeof(buf));
  20. char c = 'c';
  21. signed char sc = 's';
  22. unsigned char uc = 'u';
  23. ob <<'l' << c << sc << uc;
  24. ob.put('p');
  25. testVerifyStr(buf, "lcsup");
  26. ob.init(buf, sizeof(buf));
  27. ob << 's' << setw(2) << 'r' << 'n' << left << setw(2) << 'l';
  28. ob << 'm' << right << setw(2) << 'e';
  29. testVerifyStr(buf, "s rnl m e");
  30. ob.init(buf, sizeof(buf));
  31. ob << setfill('f') << setw(5) << 'r';
  32. testVerifyStr(buf, "ffffr");
  33. }
  34. //------------------------------------------------------------------------------
  35. void ostreamFloat() {
  36. char buf[50];
  37. obufstream ob(buf, sizeof(buf));
  38. float f = 9.87654;
  39. double d = -123.4567;
  40. ob << f <<',';
  41. ob << internal << setw(10) << d << ',';
  42. ob << setfill('0') << setw(10) << d;
  43. testVerifyStr(buf, "9.88,- 123.46,-000123.46");
  44. ob.init(buf, sizeof(buf));
  45. ob << setw(10) << left << d << ',' << showpos << -d << ',';
  46. ob << setprecision(0) << d;
  47. testVerifyStr(buf, "-123.46000,+123.46,-123");
  48. ob.init(buf, sizeof(buf));
  49. ob << showpoint << d << noshowpoint << ',' << d << ',';
  50. ob << setprecision(4) << f << ',' << setprecision(2) << noshowpos << f;
  51. testVerifyStr(buf, "-123.,-123,+9.8765,9.88");
  52. }
  53. //------------------------------------------------------------------------------
  54. void ostreamNumber() {
  55. char buf[50];
  56. obufstream ob(buf, sizeof(buf));
  57. short s = 1;
  58. short signed ss = 2;
  59. short unsigned su = 3;
  60. int i = 4;
  61. int signed is = 5;
  62. int unsigned iu = 6;
  63. long l = 7;
  64. long signed ls = 8;
  65. long unsigned lu = 9;
  66. ob << s << ss << su << i << is << iu << l <<ls << lu;
  67. s = -1;
  68. ss = -2;
  69. i = -3;
  70. is = -4;
  71. l = -5;
  72. ls = -6;
  73. ob << s << ss << i << is << l <<ls;
  74. testVerifyStr(buf, "123456789-1-2-3-4-5-6");
  75. ob.init(buf, sizeof(buf));
  76. s = ss = 0X8000;
  77. i = is = sizeof(int) == 2 ? 0X8000 : 0X80000000;
  78. l = ls = 0X80000000;
  79. su = iu = lu = 0;
  80. ob << su << iu << lu << s << ss;
  81. testVerifyStr(buf, "000-32768-32768");
  82. ob.init(buf, sizeof(buf));
  83. ob << i << is;
  84. if (sizeof(int) == 2) {
  85. testVerifyStr(buf, "-32768-32768");
  86. } else {
  87. testVerifyStr(buf, "-2147483648-2147483648");
  88. }
  89. ob.init(buf, sizeof(buf));
  90. ob << l << ls;
  91. testVerifyStr(buf, "-2147483648-2147483648");
  92. ob.init(buf, sizeof(buf));
  93. s = ss = i = is = 0X7FFF;
  94. l = ls = 0X7FFFFFFF;
  95. su = iu = 0XFFFF;
  96. lu = 0XFFFFFFFF;
  97. ob << showpos << s << ss << i << is;
  98. testVerifyStr(buf, "+32767+32767+32767+32767");
  99. ob.init(buf, sizeof(buf));
  100. ob << l << ls;
  101. testVerifyStr(buf, "+2147483647+2147483647");
  102. ob.init(buf, sizeof(buf));
  103. ob << su << iu << lu;
  104. testVerifyStr(buf, "+65535+65535+4294967295");
  105. i = 0XABC;
  106. ob.init(buf, sizeof(buf));
  107. ob << noshowpos << hex << i << ',' << dec << i << ',' << oct << i;
  108. testVerifyStr(buf, "abc,2748,5274");
  109. ob.init(buf, sizeof(buf));
  110. ob << showbase << hex << i << ',' << dec << i << ',' << oct << i;
  111. testVerifyStr(buf, "0xabc,2748,05274");
  112. ob.init(buf, sizeof(buf));
  113. ob << showpos << uppercase << hex << i << ',' << dec << i << ',' << oct << i;
  114. testVerifyStr(buf, "0XABC,+2748,05274");
  115. is = -123;
  116. ob.init(buf, sizeof(buf));
  117. ob << internal << setfill('0');
  118. ob << setw(6) << hex << i << ',' << dec << setw(6) << i << ',';
  119. ob << setw(5) << is;
  120. testVerifyStr(buf, "0X0ABC,+02748,-0123");
  121. ob.init(buf, sizeof(buf));
  122. ob << setfill(' ') << left << setw(5) << is << ',' << right << setw(6) << is;
  123. testVerifyStr(buf, "-123 , -123");
  124. }
  125. //------------------------------------------------------------------------------
  126. void ostreamStr() {
  127. char buf[40];
  128. obufstream ob(buf, sizeof(buf));
  129. char* c = "c";
  130. const char* cc = "CC";
  131. signed char* sc = (signed char*)"sc";
  132. const signed char* csc = (const signed char*)"CSC";
  133. unsigned char *uc = (unsigned char *)"uc";
  134. const unsigned char *cuc = (const unsigned char *)"CUC";
  135. ob << "lit" << c << cc << sc << csc << uc << cuc << pstr("pstr") << F("F");
  136. testVerifyStr(buf, "litcCCscCSCucCUCpstrF");
  137. ob.init(buf, sizeof(buf));
  138. ob << setfill('*') << "s" << setw(8) << "right";
  139. ob << " " << left << setw(6) << "left" << "e";
  140. testVerifyStr(buf, "s***right left**e");
  141. }
  142. //------------------------------------------------------------------------------
  143. void setup() {
  144. testBegin();
  145. ostreamBool();
  146. ostreamChar();
  147. ostreamFloat();
  148. ostreamNumber();
  149. ostreamStr();
  150. testEnd();
  151. }
  152. //------------------------------------------------------------------------------
  153. void loop() {}