|
| 1 | +/* |
| 2 | + * StdioSerialWrite makes ad-hoc calls to various print(), println(), and |
| 3 | + * write() output methods in the `Serial` object to verify that they work as |
| 4 | + * expected. This cannot be written as an AUnit unit test because we want to |
| 5 | + * test the StdioSerial class itself, not the Print parent class. |
| 6 | + * |
| 7 | + * On Linux or Mac, type: |
| 8 | + * * $ make |
| 9 | + * * $ ./StdioSerialWrite.out |
| 10 | + * |
| 11 | + * Should print out the following: |
| 12 | + * |
| 13 | + * ==== Testing print(s) |
| 14 | + * print(s) |
| 15 | + * ==== Testing println(s) |
| 16 | + * println(s) |
| 17 | + * ==== Testing print(F(s)) |
| 18 | + * print(F(s)) |
| 19 | + * ==== Testing println(F(s)) |
| 20 | + * println(F(s)) |
| 21 | + * ==== Testing write(c) |
| 22 | + * w |
| 23 | + * ==== Testing write(s) |
| 24 | + * some string |
| 25 | + * ==== Testing write(buf, n) |
| 26 | + * some |
| 27 | + */ |
| 28 | + |
| 29 | +#include <Arduino.h> |
| 30 | + |
| 31 | +void setup() { |
| 32 | +#if ! defined(EPOXY_DUINO) |
| 33 | + delay(1000); // some boards reboot twice |
| 34 | +#endif |
| 35 | + |
| 36 | + SERIAL_PORT_MONITOR.begin(115200); // baud ignored on EpoxyDuino |
| 37 | + while (!SERIAL_PORT_MONITOR); // Wait on Leonardo/Micro, no-op on EpoxyDuino |
| 38 | + |
| 39 | +#if defined(EPOXY_DUINO) |
| 40 | + SERIAL_PORT_MONITOR.setLineModeUnix(); |
| 41 | +#endif |
| 42 | + |
| 43 | + size_t n; |
| 44 | + |
| 45 | + SERIAL_PORT_MONITOR.println(F("==== Testing print(s)")); |
| 46 | + n = SERIAL_PORT_MONITOR.print("print(s)"); |
| 47 | + SERIAL_PORT_MONITOR.println(); |
| 48 | + if (n != 8) { |
| 49 | + SERIAL_PORT_MONITOR.println("print(s) should have returned 8"); |
| 50 | + } |
| 51 | + |
| 52 | + SERIAL_PORT_MONITOR.println(F("==== Testing println(s)")); |
| 53 | + n = SERIAL_PORT_MONITOR.println("println(s)"); |
| 54 | + if (n != 11) { // 10 + newline |
| 55 | + SERIAL_PORT_MONITOR.println("print(s) should have returned 11"); |
| 56 | + } |
| 57 | + |
| 58 | + SERIAL_PORT_MONITOR.println(F("==== Testing print(F(s))")); |
| 59 | + n = SERIAL_PORT_MONITOR.print(F("print(F(s))")); |
| 60 | + SERIAL_PORT_MONITOR.println(); |
| 61 | + if (n != 11) { |
| 62 | + SERIAL_PORT_MONITOR.println(F("print(F(s)) should have returned 11")); |
| 63 | + } |
| 64 | + |
| 65 | + SERIAL_PORT_MONITOR.println(F("==== Testing println(F(s))")); |
| 66 | + n = SERIAL_PORT_MONITOR.println(F("println(F(s))")); |
| 67 | + if (n != 14) { // 13 + newline |
| 68 | + SERIAL_PORT_MONITOR.println(F("println(F(s)) should have returned 14")); |
| 69 | + } |
| 70 | + |
| 71 | + SERIAL_PORT_MONITOR.println(F("==== Testing write(c)")); |
| 72 | + n = SERIAL_PORT_MONITOR.write('w'); |
| 73 | + SERIAL_PORT_MONITOR.println(); |
| 74 | + if (n != 1) { |
| 75 | + SERIAL_PORT_MONITOR.println(F("write(c) should have returned 1")); |
| 76 | + } |
| 77 | + |
| 78 | + const char buf[] = "some string"; // strlen(buf) == 11 |
| 79 | + SERIAL_PORT_MONITOR.println(F("==== Testing write(s)")); |
| 80 | + n = SERIAL_PORT_MONITOR.write(buf); |
| 81 | + SERIAL_PORT_MONITOR.println(); |
| 82 | + if (n != 11) { |
| 83 | + SERIAL_PORT_MONITOR.println(F("write(s) should have returned 11")); |
| 84 | + } |
| 85 | + |
| 86 | + SERIAL_PORT_MONITOR.println(F("==== Testing write(buf, n)")); |
| 87 | + n = SERIAL_PORT_MONITOR.write((const uint8_t*) buf, 5); |
| 88 | + SERIAL_PORT_MONITOR.println(); |
| 89 | + if (n != 5) { |
| 90 | + SERIAL_PORT_MONITOR.println(F("write(buf, n) should have returned 5")); |
| 91 | + } |
| 92 | + |
| 93 | +#if defined(EPOXY_DUINO) |
| 94 | + exit(0); |
| 95 | +#endif |
| 96 | +} |
| 97 | + |
| 98 | +void loop() {} |
0 commit comments