//+------------------------------------------------------------------+
//|                                              TestRenkoEngine.mq5 |
//+------------------------------------------------------------------+
#property script_show_inputs

#include <RenkoBuilder/RenkoEngine.mqh>

#define ASSERT(condition, message) \
   if(!(condition)) \
     { \
      ::Print("ASSERT FAILED: ", message); \
      g_failures++; \
     } \
   else \
     { \
      ::Print("ASSERT PASSED: ", message); \
     }

int g_failures = 0;

//+------------------------------------------------------------------+
//| TestContinuation                                                 |
//| Drives a steady climb worth exactly two brick sizes and confirms |
//| two continuation bricks emit with exact boundaries, leaving the  |
//| third brick incomplete. Checks open, close, direction, and tick  |
//| count of the first and second bricks, and that the second brick  |
//| opens where the first one closed.                                |
//+------------------------------------------------------------------+
void TestContinuation(void)
  {
   CRenkoEngine engine;
   SRenkoBrick  emitted[];
   int          bricks_seen    = 0;
   double       previous_close = 0.0;
   double       brick_size     = 10.0 * 0.00001;

   engine.Initialize(1.10000, 10.0, 0.00001);

   double   climb_quotes[] = {1.10003, 1.10008, 1.10010, 1.10015, 1.10020, 1.10022, 1.10025};
   int      quote_count    = ::ArraySize(climb_quotes);

   for(int i = 0; i < quote_count; i++)
     {
      int   count_here = engine.ProcessTick(climb_quotes[i], ::TimeCurrent() + i, emitted);

      for(int j = 0; j < count_here; j++)
        {
         bricks_seen++;

         if(bricks_seen == 1)
           {
            ASSERT(::MathAbs(emitted[j].brick_open  - 1.10000) < 0.0000001, "first brick opens at 1.10000");
            ASSERT(::MathAbs(emitted[j].brick_close - 1.10010) < 0.0000001, "first brick closes at 1.10010");
            ASSERT(emitted[j].direction == BRICK_DIRECTION_UP, "first brick direction is up");
            ASSERT(emitted[j].tick_count == 3, "first brick counts the three quotes received before it closed");
           }

         if(bricks_seen == 2)
           {
            ASSERT(::MathAbs(emitted[j].brick_open  - 1.10010) < 0.0000001, "second brick opens at 1.10010");
            ASSERT(::MathAbs(emitted[j].brick_close - 1.10020) < 0.0000001, "second brick closes at 1.10020");
            ASSERT(::MathAbs(emitted[j].brick_open - previous_close) < 0.0000001, "second brick opens where the first one closed");
            ASSERT(emitted[j].direction == BRICK_DIRECTION_UP, "second brick direction is up");
            ASSERT(emitted[j].tick_count == 2, "second brick counts the two quotes received before it closed");
           }

         ASSERT(::MathAbs(emitted[j].brick_close - emitted[j].brick_open - brick_size) < 0.0000001,
                "every continuation brick closes exactly one brick size above its open");

         previous_close = emitted[j].brick_close;
        }
     }

   ASSERT(bricks_seen == 2, "exactly two continuation bricks emitted from the climb");
   ASSERT(engine.CurrentDirection() == BRICK_DIRECTION_UP, "direction is up after the climb");
   ASSERT(::MathAbs(engine.LowerBoundary() - 1.10010) < 0.0000001, "lower boundary sits at 1.10010 with the third brick incomplete");
   ASSERT(::MathAbs(engine.UpperBoundary() - 1.10030) < 0.0000001, "upper boundary sits at 1.10030 with the third brick incomplete");
  }

//+------------------------------------------------------------------+
//| TestRetracementVersusReversal                                    |
//| Confirms a retracement smaller than the reversal threshold emits |
//| nothing, and a further move past the threshold emits exactly one |
//| reversal brick with exact boundaries.                            |
//+------------------------------------------------------------------+
void TestRetracementVersusReversal(void)
  {
   CRenkoEngine engine;
   SRenkoBrick  emitted[];

   engine.Initialize(1.10000, 10.0, 0.00001);

   engine.ProcessTick(1.10010, ::TimeCurrent(), emitted);
   engine.ProcessTick(1.10020, ::TimeCurrent() + 1, emitted);

   ASSERT(engine.CurrentDirection() == BRICK_DIRECTION_UP, "direction is up after two continuation bricks");

   int      retracement_count = engine.ProcessTick(1.10005, ::TimeCurrent() + 2, emitted);
   ASSERT(retracement_count == 0, "a 15 point retracement smaller than two brick sizes emits nothing");
   ASSERT(engine.CurrentDirection() == BRICK_DIRECTION_UP, "direction is unchanged after the retracement");

   int      reversal_count = engine.ProcessTick(1.10000, ::TimeCurrent() + 3, emitted);
   ASSERT(reversal_count == 1, "a full 20 point move from the boundary emits exactly one reversal brick");
   ASSERT(::MathAbs(emitted[0].brick_open  - 1.10010) < 0.0000001, "reversal brick opens at 1.10010");
   ASSERT(::MathAbs(emitted[0].brick_close - 1.10000) < 0.0000001, "reversal brick closes at 1.10000");
   ASSERT(emitted[0].direction == BRICK_DIRECTION_DOWN, "reversal brick direction is down");
   ASSERT(engine.CurrentDirection() == BRICK_DIRECTION_DOWN, "direction flips to down after the reversal brick");
  }

//+------------------------------------------------------------------+
//| TestGapHandling                                                  |
//| Confirms a single quote that jumps three brick sizes in the      |
//| established direction emits three continuation bricks in correct |
//| ascending order with contiguous boundaries.                      |
//+------------------------------------------------------------------+
void TestGapHandling(void)
  {
   CRenkoEngine engine;
   SRenkoBrick  emitted[];

   engine.Initialize(1.10000, 10.0, 0.00001);

   engine.ProcessTick(1.10010, ::TimeCurrent(), emitted);
   engine.ProcessTick(1.10020, ::TimeCurrent() + 1, emitted);
   engine.ProcessTick(1.10030, ::TimeCurrent() + 2, emitted);

   int      gap_count = engine.ProcessTick(1.10062, ::TimeCurrent() + 3, emitted);

   ASSERT(gap_count == 3, "a single gap quote emits exactly three continuation bricks");

   ASSERT(::MathAbs(emitted[0].brick_open  - 1.10030) < 0.0000001, "first gap brick opens at 1.10030");
   ASSERT(::MathAbs(emitted[0].brick_close - 1.10040) < 0.0000001, "first gap brick closes at 1.10040");
   ASSERT(::MathAbs(emitted[1].brick_open  - 1.10040) < 0.0000001, "second gap brick opens at 1.10040");
   ASSERT(::MathAbs(emitted[1].brick_close - 1.10050) < 0.0000001, "second gap brick closes at 1.10050");
   ASSERT(::MathAbs(emitted[2].brick_open  - 1.10050) < 0.0000001, "third gap brick opens at 1.10050");
   ASSERT(::MathAbs(emitted[2].brick_close - 1.10060) < 0.0000001, "third gap brick closes at 1.10060");

   ASSERT(emitted[1].open_time == emitted[0].close_time, "second gap brick opens when the first one closes");
   ASSERT(emitted[2].open_time == emitted[1].close_time, "third gap brick opens when the second one closes");

   for(int i = 0; i < gap_count; i++)
      ASSERT(emitted[i].direction == BRICK_DIRECTION_UP, "every gap brick is a continuation brick in the up direction");
  }

//+------------------------------------------------------------------+
//| TestReversalGapHandling                                          |
//| Confirms a single quote that jumps more than two brick sizes     |
//| against the established direction emits one reversal brick,      |
//| followed by continuation bricks in the new direction, all from   |
//| that one tick.                                                   |
//+------------------------------------------------------------------+
void TestReversalGapHandling(void)
  {
   CRenkoEngine engine;
   SRenkoBrick  emitted[];

   engine.Initialize(1.10000, 10.0, 0.00001);

   engine.ProcessTick(1.10010, ::TimeCurrent(), emitted);   // establish an UP direction

   int      gap_count = engine.ProcessTick(1.09975, ::TimeCurrent() + 1, emitted);   // jump down 3.5 brick sizes in one tick

   ASSERT(gap_count >= 2, "a reversal gap against an established UP direction emits more than one brick");
   ASSERT(emitted[0].direction == BRICK_DIRECTION_DOWN, "first emitted brick is the reversal brick, direction down");
   ASSERT(::MathAbs(emitted[0].brick_open  - 1.10000) < 0.0000001, "reversal brick opens at 1.10000");
   ASSERT(::MathAbs(emitted[0].brick_close - 1.09990) < 0.0000001, "reversal brick closes at 1.09990");

   for(int i = 1; i < gap_count; i++)
      ASSERT(emitted[i].direction == BRICK_DIRECTION_DOWN, "every brick after the reversal continues down");
  }

//+------------------------------------------------------------------+
//| OnStart                                                          |
//| Runs all four verification scenarios in sequence and reports a   |
//| final pass or fail summary based on the accumulated failure count|
//+------------------------------------------------------------------+
void OnStart(void)
  {
   ::Print("--- TestRenkoEngine: starting verification ---");

   TestContinuation();
   TestRetracementVersusReversal();
   TestGapHandling();
   TestReversalGapHandling();

   if(g_failures == 0)
      ::Print("--- TestRenkoEngine: ALL TESTS PASSED ---");
   else
      ::Print("--- TestRenkoEngine: ", g_failures, " ASSERTION(S) FAILED ---");
  }
//+------------------------------------------------------------------+