Files
VCCPlusOSS/tests/macos/drivewire-cowin-probe.cpp

101 lines
3.4 KiB
C++

// Replay encoded updates with the documented CoWin cursor, immediate wrapping
// and insert-line behavior. A scroll on the last row is always a test failure.
#include "DriveWireCoWin.h"
#include <cassert>
#include <iostream>
#include <random>
using namespace VCC::Mac;
struct GuestWindow {
TerminalGrid cells{};
int x = 0, y = 0;
bool visible = true;
void receive(const std::vector<std::uint8_t>& bytes) {
for (std::size_t i = 0; i < bytes.size(); ++i) {
const auto ch = bytes.at(i);
switch (ch) {
case 0x0c:
for (auto& row : cells) row.fill(' ');
x = y = 0;
break;
case 0x02:
x = bytes.at(++i) - 32;
y = bytes.at(++i) - 32;
assert(x >= 0 && x < 80 && y >= 0 && y < 24);
break;
case 0x05: {
const auto mode = bytes.at(++i);
assert(mode == 0x20 || mode == 0x21);
visible = mode == 0x21;
break;
}
case 0x1f:
assert(bytes.at(++i) == 0x30);
for (int r = 23; r > y; --r) cells[r] = cells[r - 1];
cells[y].fill(' ');
break;
default:
assert(ch >= 0x20 && ch <= 0x7e);
cells.at(y).at(x++) = static_cast<char>(ch);
if (x == 80) {
x = 0;
++y;
assert(y < 24);
}
}
}
}
};
int main() {
DriveWireTerminalOutput display;
GuestWindow guest;
for (auto& row : guest.cells) row.fill('@'); // unknown console before vi
TerminalGrid desired;
for (auto& row : desired) row.fill(' ');
auto roundTrip = [&](int x, int y, bool visible, bool clear = false) {
std::vector<std::uint8_t> bytes;
display.draw(desired, x, y, visible, clear, bytes);
assert(bytes.size() <= DriveWireTerminalOutput::maxDrawBytes);
guest.receive(bytes);
assert(guest.cells == desired);
assert(guest.x == x && guest.y == y && guest.visible == visible);
return bytes.size();
};
roundTrip(0, 0, true, true);
desired[0][0] = 'A';
desired[22][79] = 'B';
desired[23][79] = 'C';
roundTrip(79, 23, false);
assert(roundTrip(79, 23, false) == 0);
assert(roundTrip(0, 0, false) == 3);
assert(roundTrip(0, 0, true) == 2);
desired[23][0] = 'D';
assert(roundTrip(0, 0, true) == 7);
// Exercise runs separated by every possible small gap, dense repaints,
// right-edge clearing and cursor-only changes.
std::mt19937 random(789);
for (int frame = 0; frame < 400; ++frame) {
const int spacing = 1 + frame % 8;
for (auto& row : desired) {
for (int x = frame % spacing; x < 80; x += spacing) {
row[x] = static_cast<char>(' ' + random() % 95);
}
}
roundTrip(random() % 80, random() % 24, frame % 2 != 0, frame % 41 == 0);
}
// A malformed parser cell must never inject Telnet IAC or a CoWin control.
desired[0][0] = static_cast<char>(255);
desired[0][1] = 27;
std::vector<std::uint8_t> bytes;
display.draw(desired, 0, 0, true, false, bytes);
guest.receive(bytes);
assert(guest.cells[0][0] == '?' && guest.cells[0][1] == '?');
std::cout << "CoWin bounded differential output passed\n";
}