context: ///////////////////////////////////////////////////////////////////////////////////////
  // Copyright (C) 2006-2015 Esper Team. All rights reserved.                           /
  // http://esper.codehaus.org                                                          /
  // ---------------------------------------------------------------------------------- /
  // The software in this package is published under the terms of the GPL license       /
  // a copy of which has been included with this distribution in the license.txt file.  /
  ///////////////////////////////////////////////////////////////////////////////////////
  
  using System.Collections.Generic;
  
  using com.espertech.esper.common.client.scopetest;
  using com.espertech.esper.common.@internal.support;
  using com.espertech.esper.common.@internal.util;
  using com.espertech.esper.regressionlib.framework;
  using com.espertech.esper.regressionlib.support.bean;
  
  using NUnit.Framework;
  
  using SupportBean_A = com.espertech.esper.regressionlib.support.bean.SupportBean_A;
  
  namespace com.espertech.esper.regressionlib.suite.resultset.querytype
  {
      public class ResultSetQueryTypeRowForAll
      {
          private const string JOIN_KEY = "KEY";
  
          public static IList<RegressionExecution> Executions()
          {
              var execs = new List<RegressionExecution>();
              WithRowForAllSimple(execs);
              WithRowForAllSumMinMax(execs);
              WithRowForAllWWindowAgg(execs);
              WithRowForAllMinMaxWindowed(execs);
              WithSumOneView(execs);
              WithSumJoin(execs);
              WithAvgPerSym(execs);
              WithSelectStarStdGroupBy(execs);
              WithSelectExprGroupWin(execs);
              WithSelectAvgExprStdGroupBy(execs);
              WithSelectAvgStdGroupByUni(execs);
              WithRowForAllNamedWindowWindow(execs);
              WithRowForAllStaticMethodDoubleNested(execs);
              return execs;
          }
  
          public static IList<RegressionExecution> WithRowForAllStaticMethodDoubleNested(IList<RegressionExecution> execs = null)
          {
              execs = execs ?? new List<RegressionExecution>();
              execs.Add(new ResultSetQueryTypeRowForAllStaticMethodDoubleNested());
              return execs;
          }
  
          public static IList<RegressionExecution> WithRowForAllNamedWindowWindow(IList<RegressionExecution> execs = null)
          {
              execs = execs ?? new List<RegressionExecution>();
              execs.Add(new ResultSetQueryTypeRowForAllNamedWindowWindow());
              return execs;
          }
  
          public static IList<RegressionExecution> WithSelectAvgStdGroupByUni(IList<RegressionExecution> execs = null)
          {
              execs = execs ?? new List<RegressionExecution>();
              execs.Add(new ResultSetQueryTypeSelectAvgStdGroupByUni());
              return execs;
          }
  
          public static IList<RegressionExecution> WithSelectAvgExprStdGroupBy(IList<RegressionExecution> execs = null)
          {
              execs = execs ?? new List<RegressionExecution>();
              execs.Add(new ResultSetQueryTypeSelectAvgExprStdGroupBy());
              return execs;
          }
  
          public static IList<RegressionExecution> WithSelectExprGroupWin(IList<RegressionExecution> execs = null)
          {
              execs = execs ?? new List<RegressionExecution>();
              execs.Add(new ResultSetQueryTypeSelectExprGroupWin());
              return execs;
          }
  
          public static IList<RegressionExecution> WithSelectStarStdGroupBy(IList<RegressionExecution> execs = null)
          {
              execs = execs ?? new List<RegressionExecution>();
              execs.Add(new ResultSetQueryTypeSelectStarStdGroupBy());
              return execs;
          }
  
          public static IList<RegressionExecution> WithAvgPerSym(IList<RegressionExecution> execs = null)
          {
              execs = execs ?? new List<RegressionExecution>();
              execs.Add(new ResultSetQueryTypeAvgPerSym());
              return execs;
          }
  
          public static IList<RegressionExecution> WithSumJoin(IList<RegressionExecution> execs = null)
          {
              execs = execs ?? new List<RegressionExecution>();
              execs.Add(new ResultSetQueryTypeSumJoin());
              return execs;
          }
  
          public static IList<RegressionExecution> WithSumOneView(IList<RegressionExecution> execs = null)
          {
              execs = execs ?? new List<RegressionExecution>();
              execs.Add(new ResultSetQueryTypeSumOneView());
              return execs;
          }
  
          public static IList<RegressionExecution> WithRowForAllMinMaxWindowed(IList<RegressionExecution> execs = null)
          {
              execs = execs ?? new List<RegressionExecution>();
              execs.Add(new ResultSetQueryTypeRowForAllMinMaxWindowed());
              return execs;
          }
  
          public static IList<RegressionExecution> WithRowForAllWWindowAgg(IList<RegressionExecution> execs = null)
          {
              execs = execs ?? new List<RegressionExecution>();
              execs.Add(new ResultSetQueryTypeRowForAllWWindowAgg());
              return execs;
          }
  
          public static IList<RegressionExecution> WithRowForAllSumMinMax(IList<RegressionExecution> execs = null)
          {
              execs = execs ?? new List<RegressionExecution>();
              execs.Add(new ResultSetQueryTypeRowForAllSumMinMax());
              return execs;
          }
  
          public static IList<RegressionExecution> WithRowForAllSimple(IList<RegressionExecution> execs = null)
          {
              execs = execs ?? new List<RegressionExecution>();
              execs.Add(new ResultSetQueryTypeRowForAllSimple());
              return execs;
          }
  
          private static void TryAssert(RegressionEnvironment env)
          {
              // assert select result type
              Assert.AreEqual(typeof(long?), env.Statement("s0").EventType.GetPropertyType("mySum"));
              EPAssertionUtil.AssertPropsPerRowAnyOrder(
                  env.Statement("s0").GetEnumerator(),
                  new[] {"mySum"},
                  new[] {new object[] {null}});
  
              SendTimerEvent(env, 0);
              SendEvent(env, 10);
              Assert.AreEqual(10L, env.Listener("s0").GetAndResetLastNewData()[0].Get("mySum"));
              EPAssertionUtil.AssertPropsPerRowAnyOrder(
                  env.Statement("s0").GetEnumerator(),
                  new[] {"mySum"},
                  new[] {new object[] {10L}});
  
              SendTimerEvent(env, 5000);
              SendEvent(env, 15);
              Assert.AreEqual(25L, env.Listener("s0").GetAndResetLastNewData()[0].Get("mySum"));
              EPAssertionUtil.AssertPropsPerRowAnyOrder(
                  env.Statement("s0").GetEnumerator(),
                  new[] {"mySum"},
                  new[] {new object[] {25L}});
  
              SendTimerEvent(env, 8000);
              SendEvent(env, -5);
              Assert.AreEqual(20L, env.Listener("s0").GetAndResetLastNewData()[0].Get("mySum"));
              Assert.IsNull(env.Listener("s0").LastOldData);
              EPAssertionUtil.AssertPropsPerRowAnyOrder(
                  env.Statement("s0").GetEnumerator(),
                  new[] {"mySum"},
                  new[] {new object[] {20L}});
  
              SendTimerEvent(env, 10000);
              Assert.AreEqual(20L, env.Listener("s0").LastOldData[0].Get("mySum"));
              Assert.AreEqual(10L, env.Listener("s0").GetAndResetLastNewData()[0].Get("mySum"));
              EPAssertionUtil.AssertPropsPerRowAnyOrder(
                  env.Statement("s0").GetEnumerator(),
                  new[] {"mySum"},
                  new[] {new object[] {10L}});
  
              SendTimerEvent(env, 15000);
              Assert.AreEqual(10L, env.Listener("s0").LastOldData[0].Get("mySum"));
              Assert.AreEqual(-5L, env.Listener("s0").GetAndResetLastNewData()[0].Get("mySum"));
              EPAssertionUtil.AssertPropsPerRowAnyOrder(
                  env.Statement("s0").GetEnumerator(),
                  new[] {"mySum"},
                  new[] {new object[] {-5L}});
  
              SendTimerEvent(env, 18000);
              Assert.AreEqual(-5L, env.Listener("s0").LastOldData[0].Get("mySum"));
              Assert.IsNull(env.Listener("s0").GetAndResetLastNewData()[0].Get("mySum"));
              EPAssertionUtil.AssertPropsPerRowAnyOrder(
                  env.Statement("s0").GetEnumerator(),
                  new[] {"mySum"},
                  new[] {new object[] {null}});
          }
  
          private static void SendSupportBean_A(
              RegressionEnvironment env,
              string id)
          {
              env.SendEventBean(new SupportBean_A(id));
          }
  
          private static SupportBean SendSupportBean(
              RegressionEnvironment env,
              string theString,
              int intPrimitive)
          {
              var sb = new SupportBean(theString, intPrimitive);
              env.SendEventBean(sb);
              return sb;
          }
  
          private static object SendEvent(
              RegressionEnvironment env,
              string symbol,
              double price)
          {
              object theEvent = new SupportMarketDataBean(symbol, price, null, null);
              env.SendEventBean(theEvent);
              return theEvent;
          }
  
          private static void SendEvent(
              RegressionEnvironment env,
              long longBoxed,
              int intBoxed,
              short shortBoxed)
          {
              var bean = new SupportBean();
              bean.TheString = JOIN_KEY;
              bean.LongBoxed = longBoxed;
              bean.IntBoxed = intBoxed;
              bean.ShortBoxed = shortBoxed;
              env.SendEventBean(bean);
          }
  
          private static void SendEvent(
              RegressionEnvironment env,
              long longBoxed)
          {
              SendEvent(env, longBoxed, 0, 0);
          }
  
          private static SupportMarketDataBean MakeMarketDataEvent(double price)
          {
              return new SupportMarketDataBean("DELL", price, 0L, null);
          }
  
          private static void SendEventSB(
              RegressionEnvironment env,
              string theString,
              int intPrimitive)
          {
              env.SendEventBean(new SupportBean(theString, intPrimitive));
          }
  
          private static void SendTimerEvent(
              RegressionEnvironment env,
              long msec)
          {
              env.AdvanceTime(msec);
          }
  
          public class ResultSetQueryTypeRowForAllStaticMethodDoubleNested : RegressionExecution
          {
              public void Run(RegressionEnvironment env)
              {
                  var epl =
                      $"import {typeof(MyHelper).MaskTypeName()};\n" +
                      "@Name('s0') select MyHelper.DoOuter(MyHelper.DoInner(last(TheString))) as c0 from SupportBean;\n";
                  env.CompileDeploy(epl).AddListener("s0");
  
                  env.SendEventBean(new SupportBean("E1", 1));
                  Assert.AreEqual("oiE1io", env.Listener("s0").AssertOneGetNewAndReset().Get("c0"));
  
                  env.UndeployAll();
              }
          }
  
          public class ResultSetQueryTypeRowForAllSumMinMax : RegressionExecution
          {
              public void Run(RegressionEnvironment env)
              {
                  var fields = new[] {"c0", "c1", "c2", "c3"};
  
                  env.Milestone(0);
                  env.AdvanceTime(0);
                  var epl = "@Name('s0') select TheString as c0, sum(IntPrimitive) as c1," +
                            "min(IntPrimitive) as c2, max(IntPrimitive) as c3 from SupportBean";
                  env.CompileDeploy(epl).AddListener("s0");
  
                  env.Milestone(1);
  
                  SendEventSB(env, "E1", 10);
                  EPAssertionUtil.AssertProps(
                      env.Listener("s0").AssertOneGetNewAndReset(),
                      fields,
                      new object[] {"E1", 10, 10, 10});
  
                  env.Milestone(2);
  
                  SendEventSB(env, "E2", 100);
                  EPAssertionUtil.AssertProps(
                      env.Listener("s0").AssertOneGetNewAndReset(),
                      fields,
                      new object[] {"E2", 10 + 100, 10, 100});
  
                  env.Milestone(3);
  
                  SendEventSB(env, "E3", 11);
                  EPAssertionUtil.AssertProps(
                      env.Listener("s0").AssertOneGetNewAndReset(),
                      fields,
                      new object[] {"E3", 10 + 100 + 11, 10, 100});
  
                  env.Milestone(4);
  
                  env.Milestone(5);
  
                  SendEventSB(env, "E4", 9);
                  EPAssertionUtil.AssertProps(
                      env.Listener("s0").AssertOneGetNewAndReset(),
                      fields,
                      new object[] {"E4", 10 + 100 + 11 + 9, 9, 100});
  
                  SendEventSB(env, "E5", 120);
                  EPAssertionUtil.AssertProps(
                      env.Listener("s0").AssertOneGetNewAndReset(),
                      fields,
                      new object[] {"E5", 10 + 100 + 11 + 9 + 120, 9, 120});
  
                  SendEventSB(env, "E6", 100);
                  EPAssertionUtil.AssertProps(
                      env.Listener("s0").AssertOneGetNewAndReset(),
                      fields,
                      new object[] {"E6", 10 + 100 + 11 + 9 + 120 + 100, 9, 120});
  
                  env.UndeployAll();
              }
          }
  
          public class ResultSetQueryTypeRowForAllWWindowAgg : RegressionExecution
          {
              public void Run(RegressionEnvironment env)
              {
                  var fields = new[] {"c0", "c1", "c2"};
  
                  var epl = "@Name('s0') select irstream TheString as c0, sum(IntPrimitive) as c1," +
                            "window(*) as c2 from SupportBean.win:length(2)";
                  env.CompileDeploy(epl).AddListener("s0");
  
                  env.Milestone(0);
  
                  object e1 = SendSupportBean(env, "E1", 10);
                  EPAssertionUtil.AssertProps(
                      env.Listener("s0").AssertOneGetNewAndReset(),
                      fields,
                      new object[] {
                          "E1", 10,
                          new[] {e1}
                      });
  
                  env.Milestone(1);
  
                  object e2 = SendSupportBean(env, "E2", 100);
                  EPAssertionUtil.AssertProps(
                      env.Listener("s0").AssertOneGetNewAndReset(),
                      fields,
                      new object[] {
                          "E2", 10 + 100,
                          new[] {e1, e2}
                      });
  
                  env.Milestone(2);
  
                  object e3 = SendSupportBean(env, "E3", 11);
                  EPAssertionUtil.AssertProps(
                      env.Listener("s0").AssertGetAndResetIRPair(),
                      fields,
                      new object[] {
                          "E3", 100 + 11,
                          new[] {e2, e3}
                      },
                      new object[] {
                          "E1", 100 + 11,
                          new[] {e2, e3}
                      });
  
                  env.Milestone(3);
  
                  env.Milestone(4);
  
                  object e4 = SendSupportBean(env, "E4", 9);
                  EPAssertionUtil.AssertProps(
                      env.Listener("s0").AssertGetAndResetIRPair(),
                      fields,
                      new object[] {
                          "E4", 11 + 9,
                          new[] {e3, e4}
                      },
                      new object[] {
                          "E2", 11 + 9,
                          new[] {e3, e4}
                      });
  
                  env.UndeployAll();
              }
          }
  
          public class ResultSetQueryTypeRowForAllSimple : RegressionExecution
          {
              public void Run(RegressionEnvironment env)
              {
                  var epl = "@Name('s0') select irstream " +
                            "avg(Price) as avgPrice," +
                            "sum(Price) as sumPrice," +
                            "min(Price) as minPrice," +
                            "max(Price) as maxPrice," +
                            "median(Price) as medianPrice," +
                            "stddev(Price) as stddevPrice," +
                            "avedev(Price) as avedevPrice," +
                            "count(*) as datacount, " +
                            "count(distinct Price) as countDistinctPrice " +
                            "from SupportMarketDataBean";
                  env.CompileDeploy(epl).AddListener("s0");
  
                  env.Milestone(0);
  
                  env.SendEventBean(MakeMarketDataEvent(100));
  
                  env.Listener("s0")
                      .AssertNewOldData(
                          new[] {
                              new object[] {"avgPrice", 100d},
                              new object[] {"sumPrice", 100d},
                              new object[] {"minPrice", 100d},
                              new object[] {"maxPrice", 100d},
                              new object[] {"medianPrice", 100d},
                              new object[] {"stddevPrice", null},
                              new object[] {"avedevPrice", 0.0},
                              new object[] {"datacount", 1L},
                              new object[] {"countDistinctPrice", 1L}
                          }, // new data
                          new[] {
                              new object[] {"avgPrice", null},
                              new object[] {"sumPrice", null},
                              new object[] {"minPrice", null},
                              new object[] {"maxPrice", null},
                              new object[] {"medianPrice", null},
                              new object[] {"stddevPrice", null},
                              new object[] {"avedevPrice", null},
                              new object[] {"datacount", 0L},
                              new object[] {"countDistinctPrice", 0L}
                          } // old data
                      );
  
                  env.Milestone(1);
  
                  env.SendEventBean(MakeMarketDataEvent(200));
                  env.Listener("s0")
                      .AssertNewOldData(
                          new[] {
                              new object[] {"avgPrice", (100 + 200) / 2.0},
                              new object[] {"sumPrice", 100 + 200d},
                              new object[] {"minPrice", 100d},
                              new object[] {"maxPrice", 200d},
                              new object[] {"medianPrice", 150d},
                              new object[] {"stddevPrice", 70.71067811865476},
                              new object[] {"avedevPrice", 50d},
                              new object[] {"datacount", 2L},
                              new object[] {"countDistinctPrice", 2L}
                          }, // new data
                          new[] {
                              new object[] {"avgPrice", 100d},
                              new object[] {"sumPrice", 100d},
                              new object[] {"minPrice", 100d},
                              new object[] {"maxPrice", 100d},
                              new object[] {"medianPrice", 100d},
                              new object[] {"stddevPrice", null},
                              new object[] {"avedevPrice", 0.0},
                              new object[] {"datacount", 1L},
                              new object[] {"countDistinctPrice", 1L}
                          } // old data
                      );
  
                  env.Milestone(2);
  
                  env.SendEventBean(MakeMarketDataEvent(150));
                  env.Listener("s0")
                      .AssertNewOldData(
                          new[] {
                              new object[] {"avgPrice", (150 + 100 + 200) / 3.0},
                              new object[] {"sumPrice", 150 + 100 + 200d},
                              new object[] {"minPrice", 100d},
                              new object[] {"maxPrice", 200d},
                              new object[] {"medianPrice", 150d},
                              new object[] {"stddevPrice", 50d},
  
gt:                             new object[] {"avedevPrice", 33 + 1 / 3d},