diff --git a/AGENTS.md b/AGENTS.md index 64c3b83..b35b1ef 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -161,8 +161,8 @@ exception goes at the end of a list, with its reason. loopback, one client at a time, for the control program, which has no WebSocket. - `SimulatorSocket` — what both hosts do with a socket: listen on loopback, never wait, send all, and tell a connection that is gone. - - `SimulatorFile` — a whole file read into a string, the page the web host serves and the - panel `--expect` names. + - `SimulatorFile` — a whole file read into a string, the page the web host serves, the + panel `--expect` names and the pictures `DocuPictures --check` compares. - `Avr/` — `mainAvr.cpp` and `SimulatorAvr`, the executable `SimulatorAvr`: the same simulator for the `.elf` `avr-gcc` makes of an example (`--elf`). simavr runs it on an ATmega328P, its pins 10, 11 and 13 and its SPI output drive the model, its UART is `Serial`. It writes the diff --git a/CMakeLists.txt b/CMakeLists.txt index 4980c00..51e6f17 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -196,7 +196,7 @@ add_compile_fail_tests(DIRECTORY ${PROJECT_SOURCE_DIR}/test/compile_fail STANDAR # The pictures in docu/ are drawn from the library and the model rather than by hand: DocuPictures writes them, and the # test fails when one in docu/ is no longer what the library would draw. Once, as the transfer path does not matter. add_executable(DocuPictures docu/DocuPictures.cpp docu/DocuSvg.cpp) -target_link_libraries(DocuPictures PRIVATE MaxMatrix-bitbang) +target_link_libraries(DocuPictures PRIVATE MaxMatrix-bitbang SimulatorHosts) add_test(NAME docu/pictures COMMAND DocuPictures --check ${PROJECT_SOURCE_DIR}/docu) # The examples as the Uno runs them: the .elf avr-gcc makes of each, with bit banging and with SPI, in simavr against the diff --git a/OIL.md b/OIL.md index 6ecdc39..b9c6e89 100644 --- a/OIL.md +++ b/OIL.md @@ -50,7 +50,7 @@ Status: `open`, `in progress`, `done`, `dropped` (with the reason in the item). | 38 | Results that must not be ignored | 36 | done | #94 | | 39 | MC/DC coverage | 34 | done | #95, #96 | | 40 | Braced initialization checked | 34 | done | #93 | -| 41 | The template's rules that review checks | 36 | in progress | #99, #100, #101, #102, #104, #105, #106, #107, #113, #115, #116, #117, #118, #121, #122, #124, #125, #126, #127 | +| 41 | The template's rules that review checks | 36 | in progress | #99, #100, #101, #102, #104, #105, #106, #107, #113, #115, #116, #117, #118, #121, #122, #124, #125, #126, #127, #128 | | 42 | A newer avr-gcc as a cross-check | 41 | done | | | 43 | Bit operations in one class, from the template | 41 | done | #109 | @@ -1190,6 +1190,23 @@ whether something changed, as wxWidgets' `Enable()`, goes to `Discard()` as well finds is `MAXMATRIX_NODISCARD` now, some thirty functions. A reference to the object itself or a handle to what was just added is no result, AGENTS.md says why. +As built for the rest of the chapter: printing the panel at the end of a run and comparing it +with `--expect` are two functions (FN-1), as are writing the pictures of `DocuPictures` and +checking them, which reports a picture it cannot write now; `IsUpright()` returns at the first +dot that differs, and the name MD_MAX72XX gives a wiring is found by a function of its own +(FN-14); `DocuSvg` takes its paint and font by `const&`, the control program a `wxPoint` by +value (FN-7); a few helpers of `control/` and `docu/` are `constexpr` (FN-9); the panel of the +simulator holds its wiring by `std::reference_wrapper`, as it always has one (FN-15); +`SimulatorAvr::TakeByte()` is not `const`, as it writes the frame (FN-11); and `DocuPictures` +reads a file through `SimulatorFile` (FN-13). Left as they are for FN-16, as clang-tidy does not +see them and a test fails on a swap of each: `ControlCommand::MakeDot(column, row)` and the +column and row of `ControlDotPanel`, which `ControlCommandTest` and the order of the line pin; +`DocuSvg::AddArrow(from, to_x, thickness)`, `CountLit(chip, digit)` and `FindLedOfDot(x, y)`, which +`docu/pictures` pins; and `SimulatorWebHost::SendResponse(status, content_type)`, which +`simulator/web` pins for the page and a 404, the page's now as well. `SimulatorWeb::ConvertToHex(value, +digits)` is left too: `SimulatorTest` pins the call that escapes a char, the frame log of the page +only the eye. + ### 42 A newer avr-gcc as a cross-check `max_matrix::Byte` (41) made the examples 30 to 50 bytes of flash larger on the Uno at first, diff --git a/control/ControlDotPanel.cpp b/control/ControlDotPanel.cpp index b253821..654e944 100644 --- a/control/ControlDotPanel.cpp +++ b/control/ControlDotPanel.cpp @@ -205,7 +205,7 @@ bool ControlDotPanel::IsLit(uint8_t column, uint8_t row) const { * Finds the LED at `point`, in pixels of the panel; none for a point in the * margin or in the gap between two modules, which belongs to no LED. */ -ControlDotPanel::Dot ControlDotPanel::GetDotAt(wxPoint const& point) const { +ControlDotPanel::Dot ControlDotPanel::GetDotAt(wxPoint point) const { Dot const none{false, 0, 0}; wxCoord const cell{FromDIP(kCell)}; wxCoord const module_width{(max_matrix::kColumnNumberOfModule * cell) + diff --git a/control/ControlDotPanel.h b/control/ControlDotPanel.h index 8fcc0be..58aab40 100644 --- a/control/ControlDotPanel.h +++ b/control/ControlDotPanel.h @@ -59,7 +59,7 @@ class ControlDotPanel : public wxPanel { void OnCaptureLost(wxMouseCaptureLostEvent& event); void PaintDot(uint8_t column, uint8_t row); MAXMATRIX_NODISCARD bool IsLit(uint8_t column, uint8_t row) const; - MAXMATRIX_NODISCARD Dot GetDotAt(wxPoint const& point) const; + MAXMATRIX_NODISCARD Dot GetDotAt(wxPoint point) const; public: /*! diff --git a/control/ControlTcpPort.cpp b/control/ControlTcpPort.cpp index 8d052eb..86bb825 100644 --- a/control/ControlTcpPort.cpp +++ b/control/ControlTcpPort.cpp @@ -78,8 +78,10 @@ bool SetNonBlocking(Socket socket) { #endif // The socket is kept as an intptr_t, which holds both kinds. -Socket ToSocket(intptr_t socket) { return static_cast(socket); } -Length ToLength(size_t length) { return static_cast(length); } +constexpr Socket ToSocket(intptr_t socket) { + return static_cast(socket); +} +constexpr Length ToLength(size_t length) { return static_cast(length); } } // namespace diff --git a/docu/DocuPictures.cpp b/docu/DocuPictures.cpp index 608bcc4..4a6d3ae 100644 --- a/docu/DocuPictures.cpp +++ b/docu/DocuPictures.cpp @@ -23,13 +23,13 @@ #include #include #include -#include #include #include #include #include "Arduino.h" #include "DocuSvg.h" +#include "Host/SimulatorFile.h" #include "Max7219.h" #include "MaxMatrix.h" #include "Utility/Bit.h" @@ -169,6 +169,9 @@ template using PerDot = std::array, kNumberOfColumns>; +/* each picture's file name and its SVG text */ +using Pictures = std::vector>; + /*! \brief The LED of every dot, if each lit exactly one. */ struct FoundLeds { bool found; /*!< Whether every dot lit exactly one LED. */ @@ -261,8 +264,6 @@ constexpr bool IsPatternDot(uint8_t x, uint8_t y) { * \return True if every dot is lit where it was set. */ bool IsUpright(Max7219Wiring const& wiring) { - bool upright{true}; - matrix.Clear(); for (uint8_t x{0}; x < kNumberOfColumns; x++) { for (uint8_t y{0}; y < max_matrix::kRowNumberOfModule; y++) { @@ -273,11 +274,28 @@ bool IsUpright(Max7219Wiring const& wiring) { for (uint8_t x{0}; x < kNumberOfColumns; x++) { for (uint8_t y{0}; y < max_matrix::kRowNumberOfModule; y++) { if (mock::chain.IsLit(wiring, x, y) != IsPatternDot(x, y)) { - upright = false; + return false; } } } - return upright; + return true; +} + +/*! + * \brief Finds the name MD_MAX72XX gives a wiring, if it gives it one. + * + * \param[in] wiring The wiring. + * \return The name, as the README uses it, or nullptr for a wiring without one. + */ +char const* FindModuleName(Max7219Wiring const& wiring) { + for (auto const& named : kModuleNames) { + if ((named.wiring->digits_as_rows == wiring.digits_as_rows) && + (named.wiring->reversed_columns == wiring.reversed_columns) && + (named.wiring->reversed_rows == wiring.reversed_rows)) { + return named.name; + } + } + return nullptr; } /*! @@ -299,12 +317,9 @@ std::string GetModules() { modules += ", "; } modules += wiring.name; - for (auto const& named : kModuleNames) { - if ((named.wiring->digits_as_rows == wiring.digits_as_rows) && - (named.wiring->reversed_columns == wiring.reversed_columns) && - (named.wiring->reversed_rows == wiring.reversed_rows)) { - modules += std::string(" (") + named.name + ")"; - } + char const* const name{FindModuleName(wiring)}; + if (name != nullptr) { + modules += std::string(" (") + name + ")"; } } return modules.empty() ? "no module" : modules; @@ -317,7 +332,7 @@ std::string GetModules() { * \param[in] x The column, as the viewer sees it. * \return The pixels from the left. */ -int32_t GetCellX(uint8_t x) { +constexpr int32_t GetCellX(uint8_t x) { return kLeft + ((x / max_matrix::kColumnNumberOfModule) * ((max_matrix::kColumnNumberOfModule * kCell) + kModuleGap)) + @@ -331,7 +346,7 @@ int32_t GetCellX(uint8_t x) { * \param[in] y The row, as the viewer sees it. * \return The pixels from the top. */ -int32_t GetCellY(uint8_t y) { return kTop + (y * kCell); } +constexpr int32_t GetCellY(uint8_t y) { return kTop + (y * kCell); } /*! * \brief Returns the width of a picture of the whole panel. @@ -340,7 +355,7 @@ int32_t GetCellY(uint8_t y) { return kTop + (y * kCell); } * * \return The width in pixels. */ -uint16_t GetPictureWidth() { +constexpr uint16_t GetPictureWidth() { return static_cast(GetCellX(kNumberOfColumns - 1) + kCell + kRight); } @@ -351,7 +366,7 @@ uint16_t GetPictureWidth() { * * \return The height in pixels. */ -uint16_t GetPictureHeight() { +constexpr uint16_t GetPictureHeight() { return static_cast(GetCellY(max_matrix::kRowNumberOfModule - 1) + kCell + kBottom); } @@ -641,29 +656,14 @@ std::string DrawCoordinates() { return svg.GetDocument(); } -/*! - * \brief Reads a whole file. - * - * \param[in] path The file. - * \return What it holds, or an empty text if it cannot be read, which no - * picture equals. - */ -std::string ReadFile(std::string const& path) { - std::ifstream const file{path}; - std::stringstream content; - - content << file.rdbuf(); - return content.str(); -} - /*! * \brief Draws every picture. * * \return Each picture's file name and its SVG text, which is empty if a dot * did not light exactly one LED. */ -std::vector> DrawPictures() { - std::vector> pictures; +Pictures DrawPictures() { + Pictures pictures; pictures.emplace_back("Coordinates.svg", DrawCoordinates()); // The module types in the order of the model's wirings, whose names the files @@ -677,26 +677,59 @@ std::vector> DrawPictures() { } /*! - * \brief Writes the pictures into a folder, or checks that it holds them. + * \brief Counts the pictures that could not be drawn, and names each. + * + * \param[in] pictures Each picture's file name and its SVG text, empty for one + * that could not be drawn. + * \return How many could not be drawn. + */ +uint8_t CountUndrawn(Pictures const& pictures) { + uint8_t undrawn{0}; + + for (auto const& picture : pictures) { + if (picture.second.empty()) { + std::cerr << picture.first << ": a dot did not light exactly one LED\n"; + undrawn++; + } + } + return undrawn; +} + +/*! + * \brief Writes the pictures that were drawn into a folder. * * \param[in] pictures Each picture's file name and its SVG text. * \param[in] folder The folder. - * \param[in] write True to write, false to check. - * \return How many pictures could not be drawn or differ. + * \return How many could not be written. */ -uint8_t WriteOrCheck( - std::vector> const& pictures, - std::string const& folder, bool write) { +uint8_t WritePictures(Pictures const& pictures, std::string const& folder) { uint8_t failed{0}; for (auto const& picture : pictures) { std::string const path = folder + "/" + picture.first; - if (picture.second.empty()) { - std::cerr << picture.first << ": a dot did not light exactly one LED\n"; + if (!picture.second.empty() && !(std::ofstream{path} << picture.second)) { + std::cerr << path << " cannot be written\n"; failed++; - } else if (write) { - std::ofstream(path) << picture.second; - } else if (ReadFile(path) != picture.second) { + } + } + return failed; +} + +/*! + * \brief Checks that a folder holds the pictures that were drawn. + * + * \param[in] pictures Each picture's file name and its SVG text. + * \param[in] folder The folder. + * \return How many differ from the files, or have none. + */ +uint8_t CheckPictures(Pictures const& pictures, std::string const& folder) { + uint8_t failed{0}; + + for (auto const& picture : pictures) { + std::string const path = folder + "/" + picture.first; + SimulatorFileText const file{SimulatorFile::Read(path.c_str())}; + if (!picture.second.empty() && + (!file.complete || (file.text != picture.second))) { std::cerr << path << " is not what the library draws; run DocuPictures --write\n"; failed++; @@ -736,5 +769,9 @@ int main(int argc, char** argv) { // NOLINT(misc-const-correctness): main() std::cerr << "the matrix does not start on the pins of the examples\n"; return 1; } - return (WriteOrCheck(DrawPictures(), arguments[2], write) == 0) ? 0 : 1; + Pictures const pictures{DrawPictures()}; + uint8_t const undrawn{CountUndrawn(pictures)}; + uint8_t const failed{write ? WritePictures(pictures, arguments[2]) + : CheckPictures(pictures, arguments[2])}; + return ((undrawn == 0) && (failed == 0)) ? 0 : 1; } diff --git a/docu/DocuSvg.cpp b/docu/DocuSvg.cpp index e512609..223ce61 100644 --- a/docu/DocuSvg.cpp +++ b/docu/DocuSvg.cpp @@ -45,14 +45,15 @@ std::string DocuSvg::GetDocument() const { return document.str(); } -void DocuSvg::AddRect(DocuPoint corner, DocuSize size, DocuPaint paint) { +void DocuSvg::AddRect(DocuPoint corner, DocuSize size, DocuPaint const& paint) { body_ << " (paint.radius) << "\" fill=\"" << paint.fill << "\" stroke=\"" << paint.stroke << "\" stroke-width=\"2\"/>\n"; } -void DocuSvg::AddText(DocuPoint at, std::string const& text, DocuFont font) { +void DocuSvg::AddText(DocuPoint at, std::string const& text, + DocuFont const& font) { body_ << " (font.size) << "\" fill=\"" << font.fill << "\" text-anchor=\"" << font.anchor << "\"" diff --git a/docu/DocuSvg.h b/docu/DocuSvg.h index 6bd2445..0164945 100644 --- a/docu/DocuSvg.h +++ b/docu/DocuSvg.h @@ -86,7 +86,7 @@ class DocuSvg { * \param[in] size The width and the height. * \param[in] paint Its fill, its outline and its corners. */ - void AddRect(DocuPoint corner, DocuSize size, DocuPaint paint); + void AddRect(DocuPoint corner, DocuSize size, DocuPaint const& paint); /*! * \brief Adds a line of text, with its baseline at the point given. @@ -96,7 +96,7 @@ class DocuSvg { * \param[in] font How it is set. * \pre `text` holds no `&`, `<` or `>`. */ - void AddText(DocuPoint at, std::string const& text, DocuFont font); + void AddText(DocuPoint at, std::string const& text, DocuFont const& font); /*! * \brief Adds a horizontal arrow: a line from `from` to `to_x` at the height diff --git a/simulator/Avr/SimulatorAvr.cpp b/simulator/Avr/SimulatorAvr.cpp index f18ef0c..8fbecb7 100644 --- a/simulator/Avr/SimulatorAvr.cpp +++ b/simulator/Avr/SimulatorAvr.cpp @@ -165,8 +165,10 @@ void SimulatorAvr::ReceiveSerial() { } // Adds `value`, the byte on the wire, to the frame as the page shows it; a byte -// outside a frame is an error, as in the Arduino stand-in. -void SimulatorAvr::TakeByte(uint8_t value) const { +// outside a frame is an error, as in the Arduino stand-in. FN-11: not const, as +// it writes the frame the chain receives, which this object follows. +// NOLINTNEXTLINE(readability-make-member-function-const) +void SimulatorAvr::TakeByte(uint8_t value) { if (chip_select_) { mock::errors++; } else { diff --git a/simulator/Avr/SimulatorAvr.h b/simulator/Avr/SimulatorAvr.h index c6b9194..d14cb2e 100644 --- a/simulator/Avr/SimulatorAvr.h +++ b/simulator/Avr/SimulatorAvr.h @@ -49,7 +49,7 @@ class SimulatorAvr { bool receive_ready_{false}; void ReceiveSerial(); - void TakeByte(uint8_t value) const; + void TakeByte(uint8_t value); /* * simavr reports a pin or a byte through a C function and a pointer it was * given, so each of those is a static function that is handed the object. The diff --git a/simulator/SimulatorPanel.cpp b/simulator/SimulatorPanel.cpp index 2845e44..a4ad63c 100644 --- a/simulator/SimulatorPanel.cpp +++ b/simulator/SimulatorPanel.cpp @@ -41,7 +41,8 @@ constexpr std::array kNamedWirings{{ SimulatorPanel::SimulatorPanel() : wiring_name_(kSimulatorPanelDefaultWiring), - wiring_{FindWiring(wiring_name_)} {} + // the default is one of the table's, so it is always found + wiring_{*FindWiring(wiring_name_)} {} // FN-16: the texts of a lit and a dark LED and of the end of a row; the tests // of the simulator compare what it draws with simulator/Expected/ @@ -52,7 +53,7 @@ std::string SimulatorPanel::GetText(char const* lit, char const* dark, for (uint8_t y{0}; y < max_matrix::kRowNumberOfModule; y++) { for (uint8_t x{0}; x < GetNumberOfColumns(); x++) { - text += mock::chain.IsLit(*wiring_, x, y, upside_down_) ? lit : dark; + text += mock::chain.IsLit(wiring_.get(), x, y, upside_down_) ? lit : dark; } text += end_of_row; } @@ -69,7 +70,7 @@ bool SimulatorPanel::SetWiring(std::string const& name) { Max7219Wiring const* found{FindWiring(name)}; if (found != nullptr) { - wiring_ = found; + wiring_ = *found; wiring_name_ = name; draw_requested_ = true; return true; diff --git a/simulator/SimulatorPanel.h b/simulator/SimulatorPanel.h index 654badc..d94d4d7 100644 --- a/simulator/SimulatorPanel.h +++ b/simulator/SimulatorPanel.h @@ -14,6 +14,7 @@ #ifndef MAXMATRIX_SIMULATOR_SIMULATORPANEL_H_ #define MAXMATRIX_SIMULATOR_SIMULATORPANEL_H_ +#include #include #include "Arduino.h" @@ -36,7 +37,7 @@ constexpr uint32_t kSimulatorPanelRedrawInterval{33u}; class SimulatorPanel { private: std::string wiring_name_; - Max7219Wiring const* wiring_; + std::reference_wrapper wiring_; bool upside_down_{false}; int32_t drawn_frames_{-1}; uint32_t last_draw_{0}; diff --git a/simulator/SimulatorRun.cpp b/simulator/SimulatorRun.cpp index f799dda..36e0ae4 100644 --- a/simulator/SimulatorRun.cpp +++ b/simulator/SimulatorRun.cpp @@ -27,8 +27,9 @@ bool SimulatorRun::SetUp(int argc, char** argv, SimulatorOptions& options, int SimulatorRun::End(SimulatorOptions const& options, SimulatorPanel const& panel) { + SimulatorTerminal::PrintResult(panel); bool const as_expected{ - SimulatorTerminal::PrintResult(panel, options.GetExpectFile())}; + SimulatorTerminal::IsExpected(panel, options.GetExpectFile())}; bool const passed{as_expected && (mock::errors == 0)}; return passed ? kSimulatorRunExitOk : kSimulatorRunExitFailed; } diff --git a/simulator/SimulatorTerminal.cpp b/simulator/SimulatorTerminal.cpp index 1b8111b..282865a 100644 --- a/simulator/SimulatorTerminal.cpp +++ b/simulator/SimulatorTerminal.cpp @@ -66,15 +66,16 @@ void SimulatorTerminal::Draw(SimulatorPanel const& panel) { std::cout.flush(); } -bool SimulatorTerminal::PrintResult(SimulatorPanel const& panel, - char const* expect_file) { - std::string const result = panel.GetText(kResultLit, kResultDark, "\n"); +void SimulatorTerminal::PrintResult(SimulatorPanel const& panel) { + std::cout << panel.GetText(kResultLit, kResultDark, "\n"); +} - std::cout << result; +bool SimulatorTerminal::IsExpected(SimulatorPanel const& panel, + char const* expect_file) { if (expect_file == nullptr) { return true; } - + std::string const result = panel.GetText(kResultLit, kResultDark, "\n"); SimulatorFileText const expected{SimulatorFile::Read(expect_file)}; if (!expected.complete || (expected.text != result)) { std::cerr << "differs from " << expect_file << ":\n" << expected.text; diff --git a/simulator/SimulatorTerminal.h b/simulator/SimulatorTerminal.h index 609374c..3157c30 100644 --- a/simulator/SimulatorTerminal.h +++ b/simulator/SimulatorTerminal.h @@ -56,16 +56,24 @@ class SimulatorTerminal { static void Draw(SimulatorPanel const& panel); /*! - * \brief Prints the panel once at the end of a run, in ASCII, and compares it - * with the expected panel if there is one. + * \brief Prints the panel once at the end of a run, in ASCII. * * \param[in] panel The panel to print. + */ + static void PrintResult(SimulatorPanel const& panel); + + /*! + * \brief Returns whether the panel is the one expected, and shows the + * expected one on the error stream if it is not. + * + * \param[in] panel The panel, as PrintResult() prints it. * \param[in] expect_file The file with the expected panel, or nullptr for * none. * \return True if the panel is as expected or none is expected, or false if * it differs from the file or the file cannot be read. */ - static bool PrintResult(SimulatorPanel const& panel, char const* expect_file); + MAXMATRIX_NODISCARD static bool IsExpected(SimulatorPanel const& panel, + char const* expect_file); }; #endif // MAXMATRIX_SIMULATOR_SIMULATORTERMINAL_H_ diff --git a/simulator/Test/WebTest.sh b/simulator/Test/WebTest.sh index e9605de..c651d3d 100755 --- a/simulator/Test/WebTest.sh +++ b/simulator/Test/WebTest.sh @@ -12,6 +12,8 @@ startSimulator "$1" "$Port" 3 Page=$(curl -s "http://localhost:$Port/" || true) grep -q 'MaxMatrix Simulator' <<< "$Page" || fail "the page did not arrive" +PageHead=$(curl -s -o /dev/null -w '%{http_code} %{content_type}' "http://localhost:$Port/" || true) +[ "$PageHead" = "200 text/html; charset=utf-8" ] || fail "the page came as $PageHead" Status=$(curl -s -o /dev/null -w '%{http_code}' "http://localhost:$Port/nothing" || true) [ "$Status" = 404 ] || fail "an unknown path got $Status rather than 404"