From da9d4eed5e4e4cc80d1e2f524887a706c592f768 Mon Sep 17 00:00:00 2001 From: Jim Knowler Date: Mon, 3 Jan 2022 14:31:34 +0000 Subject: [PATCH] Trace: truncate large traces when rendering them (including diffs for MatchTrace) --- gtestverilog/example/Counter.test.cpp | 12 +++++++++++ gtestverilog/lib/MatchesTrace.cpp | 30 +++++++++++++++++++++++---- gtestverilog/lib/Trace.cpp | 16 ++++++++++++-- 3 files changed, 52 insertions(+), 6 deletions(-) diff --git a/gtestverilog/example/Counter.test.cpp b/gtestverilog/example/Counter.test.cpp index 84fcf26..35e3f27 100644 --- a/gtestverilog/example/Counter.test.cpp +++ b/gtestverilog/example/Counter.test.cpp @@ -75,5 +75,17 @@ TEST_F(Counter, ShouldSimulate) { .port(i_simulate_combinatorial).signal( "01" ).repeat(11) .port(i_simulate_sequential).signal( "10" ).repeat(11); + ASSERT_THAT(testBench.trace, MatchesTrace(traceExpected)); +} + +TEST_F(Counter, ShouldHandleLargeTraces) { + testBench.reset(); + testBench.tick(1000); + + const Trace traceExpected = TraceBuilder() + .port(i_clk).signal( "10" ).repeat(1001) + .port(i_simulate_combinatorial).signal( "01" ).repeat(1001) + .port(i_simulate_sequential).signal( "10" ).repeat(1001); + ASSERT_THAT(testBench.trace, MatchesTrace(traceExpected)); } \ No newline at end of file diff --git a/gtestverilog/lib/MatchesTrace.cpp b/gtestverilog/lib/MatchesTrace.cpp index 59a082c..b431a02 100644 --- a/gtestverilog/lib/MatchesTrace.cpp +++ b/gtestverilog/lib/MatchesTrace.cpp @@ -1,7 +1,10 @@ #include "MatchesTrace.h" +#include + namespace gtestverilog { namespace matches_trace { + const size_t kMaxTraceRenderSize = 80; bool compare(const Trace& actual, const Trace& expected, ::testing::MatchResultListener& listener) { bool hasMatched = true; @@ -43,18 +46,37 @@ namespace matches_trace { << ConsoleColour().reset() << "\n"; + std::vector stepsExpectedRender; + std::vector stepsActualRender; + + size_t stepStart = 0; + size_t stepEnd = sizeActual; + + if (sizeActual > kMaxTraceRenderSize) { + stepStart = (step > (kMaxTraceRenderSize/2)) ? step - (kMaxTraceRenderSize/2) : 0; + stepEnd = std::min(stepEnd, stepStart + kMaxTraceRenderSize); + + listener << "Note: Diff truncated to steps " << stepStart << " to " << stepEnd << "\n"; + + stepsExpectedRender = std::vector(stepsExpected.begin() + stepStart, stepsExpected.begin() + stepEnd); + stepsActualRender = std::vector(stepsActual.begin() + stepStart, stepsActual.begin() + stepEnd); + } else { + stepsExpectedRender = stepsExpected; + stepsActualRender = stepsActual; + } + size_t maxPortLabelSize = expected.getMaxPortLabelSize(); listener << "\n"; listener << "Expected:"; - Trace::renderPortDiff(*(listener.stream()), 'v', ConsoleColour::kGreen, maxPortLabelSize-9, portDesc, stepsExpected, stepsActual); + Trace::renderPortDiff(*(listener.stream()), 'v', ConsoleColour::kGreen, maxPortLabelSize-9, portDesc, stepsExpectedRender, stepsActualRender); - Trace::renderPort(*(listener.stream()), maxPortLabelSize, portDesc, stepsExpected); + Trace::renderPort(*(listener.stream()), maxPortLabelSize, portDesc, stepsExpectedRender); listener << " Actual:"; - Trace::renderPortDiff(*(listener.stream()), 'v', ConsoleColour::kRed, maxPortLabelSize-9, portDesc, stepsExpected, stepsActual); + Trace::renderPortDiff(*(listener.stream()), 'v', ConsoleColour::kRed, maxPortLabelSize-9, portDesc, stepsExpectedRender, stepsActualRender); - Trace::renderPort(*(listener.stream()), maxPortLabelSize, portDesc, stepsActual); + Trace::renderPort(*(listener.stream()), maxPortLabelSize, portDesc, stepsActualRender); } hasMatched = false; diff --git a/gtestverilog/lib/Trace.cpp b/gtestverilog/lib/Trace.cpp index afd34c6..a3e9c26 100644 --- a/gtestverilog/lib/Trace.cpp +++ b/gtestverilog/lib/Trace.cpp @@ -2,8 +2,11 @@ #include #include +#include namespace gtestverilog { + const size_t kMaxTraceRenderSize = 80; + Trace::Trace() { } @@ -137,10 +140,19 @@ namespace gtestverilog { os << ConsoleColour().reset(); auto& steps = trace.getSteps(); + + std::vector stepsRender; + + if (steps.size() <= kMaxTraceRenderSize) { + stepsRender = steps; + } else { + stepsRender = std::vector(steps.begin(), steps.begin() + kMaxTraceRenderSize); + os << "Note: Trace truncated to first " << kMaxTraceRenderSize << " steps\n"; + } size_t maxPortLabelSize = trace.getMaxPortLabelSize(); - renderTimeline(os, maxPortLabelSize + 11, steps.size()); + renderTimeline(os, maxPortLabelSize + 11, stepsRender.size()); for (uint32_t portId=0; portId<64; portId++) { if (!trace.hasPort(portId)) { @@ -149,7 +161,7 @@ namespace gtestverilog { const PortDescription& portDesc = trace.getPortDescription(portId); - Trace::renderPort(os, maxPortLabelSize, portDesc, steps); + Trace::renderPort(os, maxPortLabelSize, portDesc, stepsRender); } return os;