Exemple #1
0
        public static void DbFunctions_RIGHT()
        {
            DbFunctionTools tools = new DbFunctionTools();

            //String,Int32.
            {
                Console.WriteLine("Testing DbFunctions.RIGHT(String,Int32)...");

                mstring        input = Utils.GenString(100);
                List <DbValue> args  = new List <DbValue>();
                args.Add(tools.AllocValue(input));
                args.Add(tools.AllocValue(5));
                DbFunctionArguments fargs = new DbFunctionArguments(args);

                DbValue   valOutput = DbFunctions.RIGHT(tools, fargs);
                ByteSlice bs        = valOutput.Eval();
                mstring   output    = tools.GetString(bs);

                string str      = input.ToString();
                string expected = str.Substring(str.Length - 5, 5);

                if (expected != output.ToString())
                {
                    throw new Exception("DbFunctions.RIGHT(String,Int32) has failed.  Expected result: " + expected.ToString() + ", but received: " + output.ToString());
                }
                else
                {
                    Console.WriteLine("Expected results received.");
                }
            }
        }
Exemple #2
0
        public void PutString(mstring s)
        {
            ContainsString = true;

            int endIndex = s.StartIndex + s.ByteCount - 1;

            for (int i = s.StartIndex; i <= endIndex; i++)
            {
                Buffer.Add(s.Buffer[i]);
            }

            Buffer.Add(0x0);
            Buffer.Add(0x0);

            if (StaticGlobals.ExecutionMode == ExecutionMode.DEBUG)
            {
                PutToDummy("[(mstring) " + s.ToString() + "]");
            }
        }
        public static void DbAggregators_LAST()
        {
            DbFunctionTools tools    = new DbFunctionTools();
            Random          rnd      = new Random();
            const int       rowcount = 20;

            //Double.
            {
                Console.WriteLine("Testing DbAggregators_LAST(Double)...");

                DbFunctionArguments[] fargs = new DbFunctionArguments[rowcount];
                double expected             = 0;

                for (int i = 0; i < fargs.Length; i++)
                {
                    double input = rnd.NextDouble();
                    if (input < 0.5)
                    {
                        input = input * -1d;
                    }
                    if (i == fargs.Length - 1)
                    {
                        expected = input;
                    }

                    List <DbValue> args = new List <DbValue>();
                    args.Add(tools.AllocValue(input));
                    fargs[i] = new DbFunctionArguments(args);
                }
                DbValue   valOutput = DbAggregators.LAST(tools, new DbAggregatorArguments(fargs));
                ByteSlice bs        = valOutput.Eval();
                double    output    = tools.GetDouble(bs);

                if (expected != output)
                {
                    throw new Exception("DbAggregators_LAST(Double) has failed.  Expected result: " + expected.ToString() + ", but received: " + output.ToString());
                }
                else
                {
                    Console.WriteLine("Expected results received.");
                }
            }

            {
                Console.WriteLine("Testing DbAggregators_LAST(Int32)...");

                DbFunctionArguments[] fargs = new DbFunctionArguments[rowcount];
                int expected = Int32.MaxValue;

                for (int i = 0; i < fargs.Length; i++)
                {
                    int input = rnd.Next(Int32.MinValue, Int32.MaxValue);
                    if (i == fargs.Length - 1)
                    {
                        expected = input;
                    }
                    List <DbValue> args = new List <DbValue>();
                    args.Add(tools.AllocValue(input));
                    fargs[i] = new DbFunctionArguments(args);
                }
                DbValue   valOutput = DbAggregators.LAST(tools, new DbAggregatorArguments(fargs));
                ByteSlice bs        = valOutput.Eval();
                int       output    = tools.GetInt(bs);

                if (expected != output)
                {
                    throw new Exception("DbAggregators_LAST(Int32) has failed.  Expected result: " + expected.ToString() + ", but received: " + output.ToString());
                }
                else
                {
                    Console.WriteLine("Expected results received.");
                }
            }

            {
                Console.WriteLine("Testing DbAggregators_LAST(Int64)...");

                DbFunctionArguments[] fargs = new DbFunctionArguments[rowcount];
                long expected = Int64.MaxValue;

                for (int i = 0; i < fargs.Length; i++)
                {
                    long input = DateTime.Now.Ticks;
                    if (input % 2 == 0)
                    {
                        input = input * -1;
                    }
                    if (i == fargs.Length - 1)
                    {
                        expected = input;
                    }
                    List <DbValue> args = new List <DbValue>();
                    args.Add(tools.AllocValue(input));
                    fargs[i] = new DbFunctionArguments(args);
                }
                DbValue   valOutput = DbAggregators.LAST(tools, new DbAggregatorArguments(fargs));
                ByteSlice bs        = valOutput.Eval();
                long      output    = tools.GetLong(bs);

                if (expected != output)
                {
                    throw new Exception("DbAggregators_LAST(Int64) has failed.  Expected result: " + expected.ToString() + ", but received: " + output.ToString());
                }
                else
                {
                    Console.WriteLine("Expected results received.");
                }
            }

            {
                Console.WriteLine("Testing DbAggregators_LAST(char(n))...");

                DbFunctionArguments[] fargs = new DbFunctionArguments[rowcount];
                string expected             = null;

                for (int i = 0; i < fargs.Length; i++)
                {
                    int     strlen = rnd.Next(1, 100);
                    mstring input  = Utils.GenString(strlen);
                    if (i == fargs.Length - 1)
                    {
                        expected = input.ToString();
                    }
                    List <DbValue> args = new List <DbValue>();
                    args.Add(tools.AllocValue(input));
                    fargs[i] = new DbFunctionArguments(args);
                }
                DbValue   valOutput = DbAggregators.LAST(tools, new DbAggregatorArguments(fargs));
                ByteSlice bs        = valOutput.Eval();
                mstring   output    = tools.GetString(bs);

                if (expected != output.ToString())
                {
                    throw new Exception("DbAggregators_LAST(char(n)) has failed.  Expected result: " + expected.ToString() + ", but received: " + output.ToString());
                }
                else
                {
                    Console.WriteLine("Expected results received.");
                }
            }
        }
        public static void DbFunctions_CONCAT()
        {
            DbFunctionTools tools = new DbFunctionTools();

            //String,Int32.
            {
                Console.WriteLine("Testing DbFunctions.CONCAT(String,String)...");

                List <DbValue> args = new List <DbValue>();
                args.Add(tools.AllocValue(mstring.Prepare("hello ")));
                args.Add(tools.AllocValue(mstring.Prepare("world")));
                DbFunctionArguments fargs = new DbFunctionArguments(args);

                DbValue   valOutput = DbFunctions.CONCAT(tools, fargs);
                ByteSlice bs        = valOutput.Eval();
                mstring   output    = tools.GetString(bs);

                mstring expected = mstring.Prepare("hello world");

                if (expected != output)
                {
                    throw new Exception("DbFunctions.CONCAT(String,String) has failed.  Expected result: " + expected.ToString() + ", but received: " + output.ToString());
                }
                else
                {
                    Console.WriteLine("Expected results received.");
                }
            }
        }
        public static void DbFunctions_ISNOTNULL()
        {
            DbFunctionTools tools = new DbFunctionTools();
            Random          rnd   = new Random();

            {
                Console.WriteLine("Testing DbFunctions.ISNOTNULL(char(n))...");

                {
                    mstring s1 = Utils.GenString(rnd.Next(1, 200));

                    List <DbValue> args = new List <DbValue>();
                    args.Add(tools.AllocValue(s1));
                    DbFunctionArguments fargs = new DbFunctionArguments(args);

                    DbValue   valOutput = DbFunctions.ISNOTNULL(tools, fargs);
                    ByteSlice bs        = valOutput.Eval();
                    bool      output    = tools.GetInt(bs) != 0;

                    bool expected = !false;

                    if (expected != output)
                    {
                        throw new Exception("DbFunctions.ISNOTNULL(char(n)='" + s1.ToString() + "') has failed.  Expected result: " + expected.ToString() + ", but received: " + output.ToString());
                    }
                    else
                    {
                        Console.WriteLine("Expected results received.");
                    }
                }

                {
                    List <DbValue> args = new List <DbValue>();
                    args.Add(tools.AllocValue(ByteSlice.Prepare(new byte[] { 1 }), DbTypeID.CHARS));
                    DbFunctionArguments fargs = new DbFunctionArguments(args);

                    DbValue   valOutput = DbFunctions.ISNOTNULL(tools, fargs);
                    ByteSlice bs        = valOutput.Eval();
                    bool      output    = tools.GetInt(bs) != 0;

                    bool expected = !true;

                    if (expected != output)
                    {
                        throw new Exception("DbFunctions.ISNOTNULL(char(n)=NULL) has failed.  Expected result: " + expected.ToString() + ", but received: " + output.ToString());
                    }
                    else
                    {
                        Console.WriteLine("Expected results received.");
                    }
                }
            }

            {
                Console.WriteLine("Testing DbFunctions.ISNOTNULL(Int32)...");

                {
                    int x = rnd.Next(Int32.MinValue, Int32.MaxValue);

                    List <DbValue> args = new List <DbValue>();
                    args.Add(tools.AllocValue(x));
                    DbFunctionArguments fargs = new DbFunctionArguments(args);

                    DbValue   valOutput = DbFunctions.ISNOTNULL(tools, fargs);
                    ByteSlice bs        = valOutput.Eval();
                    bool      output    = tools.GetInt(bs) != 0;

                    bool expected = !false;

                    if (expected != output)
                    {
                        throw new Exception("DbFunctions.ISNOTNULL(Int32=" + x.ToString() + ") has failed.  Expected result: " + expected.ToString() + ", but received: " + output.ToString());
                    }
                    else
                    {
                        Console.WriteLine("Expected results received.");
                    }
                }

                {
                    List <DbValue> args = new List <DbValue>();
                    args.Add(tools.AllocValue(ByteSlice.Prepare(new byte[] { 1 }), DbTypeID.INT));
                    DbFunctionArguments fargs = new DbFunctionArguments(args);

                    DbValue   valOutput = DbFunctions.ISNOTNULL(tools, fargs);
                    ByteSlice bs        = valOutput.Eval();
                    bool      output    = tools.GetInt(bs) != 0;

                    bool expected = !true;

                    if (expected != output)
                    {
                        throw new Exception("DbFunctions.ISNOTNULL(Int32=NULL) has failed.  Expected result: " + expected.ToString() + ", but received: " + output.ToString());
                    }
                    else
                    {
                        Console.WriteLine("Expected results received.");
                    }
                }
            }

            {
                Console.WriteLine("Testing DbFunctions.ISNOTNULL(Double)...");

                {
                    double x = rnd.NextDouble();

                    List <DbValue> args = new List <DbValue>();
                    args.Add(tools.AllocValue(x));
                    DbFunctionArguments fargs = new DbFunctionArguments(args);

                    DbValue   valOutput = DbFunctions.ISNOTNULL(tools, fargs);
                    ByteSlice bs        = valOutput.Eval();
                    bool      output    = tools.GetInt(bs) != 0;

                    bool expected = !false;

                    if (expected != output)
                    {
                        throw new Exception("DbFunctions.ISNOTNULL(Double=" + x.ToString() + ") has failed.  Expected result: " + expected.ToString() + ", but received: " + output.ToString());
                    }
                    else
                    {
                        Console.WriteLine("Expected results received.");
                    }
                }

                {
                    List <DbValue> args = new List <DbValue>();
                    args.Add(tools.AllocValue(ByteSlice.Prepare(new byte[] { 1 }), DbTypeID.DOUBLE));
                    DbFunctionArguments fargs = new DbFunctionArguments(args);

                    DbValue   valOutput = DbFunctions.ISNOTNULL(tools, fargs);
                    ByteSlice bs        = valOutput.Eval();
                    bool      output    = tools.GetInt(bs) != 0;

                    bool expected = !true;

                    if (expected != output)
                    {
                        throw new Exception("DbFunctions.ISNOTNULL(Double=NULL) has failed.  Expected result: " + expected.ToString() + ", but received: " + output.ToString());
                    }
                    else
                    {
                        Console.WriteLine("Expected results received.");
                    }
                }
            }

            {
                Console.WriteLine("Testing DbFunctions.ISNOTNULL(Long)...");

                {
                    long x = DateTime.Now.Ticks;

                    List <DbValue> args = new List <DbValue>();
                    args.Add(tools.AllocValue(x));
                    DbFunctionArguments fargs = new DbFunctionArguments(args);

                    DbValue   valOutput = DbFunctions.ISNOTNULL(tools, fargs);
                    ByteSlice bs        = valOutput.Eval();
                    bool      output    = tools.GetInt(bs) != 0;

                    bool expected = !false;

                    if (expected != output)
                    {
                        throw new Exception("DbFunctions.ISNOTNULL(Long=" + x.ToString() + ") has failed.  Expected result: " + expected.ToString() + ", but received: " + output.ToString());
                    }
                    else
                    {
                        Console.WriteLine("Expected results received.");
                    }
                }

                {
                    List <DbValue> args = new List <DbValue>();
                    args.Add(tools.AllocValue(ByteSlice.Prepare(new byte[] { 1 }), DbTypeID.LONG));
                    DbFunctionArguments fargs = new DbFunctionArguments(args);

                    DbValue   valOutput = DbFunctions.ISNOTNULL(tools, fargs);
                    ByteSlice bs        = valOutput.Eval();
                    bool      output    = tools.GetInt(bs) != 0;

                    bool expected = !true;

                    if (expected != output)
                    {
                        throw new Exception("DbFunctions.ISNOTNULL(Long=NULL) has failed.  Expected result: " + expected.ToString() + ", but received: " + output.ToString());
                    }
                    else
                    {
                        Console.WriteLine("Expected results received.");
                    }
                }
            }
        }
        public static void DbFunctions_FORMAT()
        {
            DbFunctionTools tools = new DbFunctionTools();

            {
                Console.WriteLine("Testing DbFunctions.FORMAT('MM')...");

                List <DbValue> args      = new List <DbValue>();
                mstring        formatstr = mstring.Prepare("MM");
                args.Add(tools.AllocValue(formatstr));
                DateTime dt = new DateTime(2000, 9, 14, 12, 0, 0);
                args.Add(tools.AllocValue(dt));
                DbFunctionArguments fargs = new DbFunctionArguments(args);

                DbValue   valOutput = DbFunctions.FORMAT(tools, fargs);
                ByteSlice bs        = valOutput.Eval();
                mstring   output    = tools.GetString(bs);
                mstring   expected  = mstring.Prepare(dt.ToString(formatstr.ToString()));

                if (expected != output)
                {
                    throw new Exception("DbFunctions.FORMAT('MM') has failed.  Expected result: " + expected.ToString() + ", but received: " + output.ToString());
                }
                else
                {
                    Console.WriteLine("Expected results received.");
                }
            }

            {
                Console.WriteLine("Testing DbFunctions.FORMAT('dd')...");

                List <DbValue> args      = new List <DbValue>();
                mstring        formatstr = mstring.Prepare("dd");
                args.Add(tools.AllocValue(formatstr));
                DateTime dt = new DateTime(2000, 9, 14, 12, 0, 0);
                args.Add(tools.AllocValue(dt));
                DbFunctionArguments fargs = new DbFunctionArguments(args);

                DbValue   valOutput = DbFunctions.FORMAT(tools, fargs);
                ByteSlice bs        = valOutput.Eval();
                mstring   output    = tools.GetString(bs);
                mstring   expected  = mstring.Prepare(dt.ToString(formatstr.ToString()));

                if (expected != output)
                {
                    throw new Exception("DbFunctions.FORMAT('dd') has failed.  Expected result: " + expected.ToString() + ", but received: " + output.ToString());
                }
                else
                {
                    Console.WriteLine("Expected results received.");
                }
            }

            {
                Console.WriteLine("Testing DbFunctions.FORMAT('t')...");

                List <DbValue> args      = new List <DbValue>();
                mstring        formatstr = mstring.Prepare("t");
                args.Add(tools.AllocValue(formatstr));
                DateTime dt = new DateTime(2000, 9, 14, 12, 0, 0);
                args.Add(tools.AllocValue(dt));
                DbFunctionArguments fargs = new DbFunctionArguments(args);

                DbValue   valOutput = DbFunctions.FORMAT(tools, fargs);
                ByteSlice bs        = valOutput.Eval();
                mstring   output    = tools.GetString(bs);
                mstring   expected  = mstring.Prepare(dt.ToString(formatstr.ToString()));

                if (expected != output)
                {
                    throw new Exception("DbFunctions.FORMAT('t') has failed.  Expected result: " + expected.ToString() + ", but received: " + output.ToString());
                }
                else
                {
                    Console.WriteLine("Expected results received.");
                }
            }
        }
Exemple #7
0
        public void PutString(mstring s)
        {          
            ContainsString = true;

            int endIndex = s.StartIndex + s.ByteCount - 1;

            for (int i = s.StartIndex; i <= endIndex; i++)
            {
                Buffer.Add(s.Buffer[i]);
            }

            Buffer.Add(0x0);
            Buffer.Add(0x0);

            if (StaticGlobals.ExecutionMode == ExecutionMode.DEBUG)
            {
                PutToDummy("[(mstring) " + s.ToString() + "]");
            }
        }
        public static void DbFunctions_GREATEREQUAL()
        {
            DbFunctionTools tools = new DbFunctionTools();
            Random          rnd   = new Random();

            {
                Console.WriteLine("Testing DbFunctions.GREATEREQUAL(char(n), char(n))...");

                mstring s1 = Utils.GenString(rnd.Next(1, 200));
                mstring s2 = Utils.GenString(rnd.Next(1, 200));

                List <DbValue> args = new List <DbValue>();
                args.Add(tools.AllocValue(s1));
                args.Add(tools.AllocValue(s2));
                DbFunctionArguments fargs = new DbFunctionArguments(args);

                DbValue   valOutput = DbFunctions.GREATEREQUAL(tools, fargs);
                ByteSlice bs        = valOutput.Eval();
                int       output    = tools.GetInt(bs);

                int expected = (s1.ToString().CompareTo(s2.ToString()) >= 0 ? 1 : 0);

                if (expected != output)
                {
                    throw new Exception("DbFunctions.GREATEREQUAL(char(n), char(n)) has failed.  Expected result: " + expected.ToString() + ", but received: " + output.ToString());
                }
                else
                {
                    Console.WriteLine("Expected results received.");
                }
            }

            {
                Console.WriteLine("Testing DbFunctions.GREATEREQUAL(Int32, Int32)...");

                int x = rnd.Next(Int32.MinValue, Int32.MaxValue);
                int y = rnd.Next(Int32.MinValue, Int32.MaxValue);

                List <DbValue> args = new List <DbValue>();
                args.Add(tools.AllocValue(x));
                args.Add(tools.AllocValue(y));
                DbFunctionArguments fargs = new DbFunctionArguments(args);

                DbValue   valOutput = DbFunctions.GREATEREQUAL(tools, fargs);
                ByteSlice bs        = valOutput.Eval();
                int       output    = tools.GetInt(bs);

                int expected = (x >= y ? 1 : 0);

                if (expected != output)
                {
                    throw new Exception("DbFunctions.GREATEREQUAL(Int32, Int32) has failed.  Expected result: " + expected.ToString() + ", but received: " + output.ToString());
                }
                else
                {
                    Console.WriteLine("Expected results received.");
                }
            }

            {
                Console.WriteLine("Testing DbFunctions.GREATEREQUAL(Double, Double)...");

                double x = rnd.NextDouble();
                double y = rnd.NextDouble();

                List <DbValue> args = new List <DbValue>();
                args.Add(tools.AllocValue(x));
                args.Add(tools.AllocValue(y));
                DbFunctionArguments fargs = new DbFunctionArguments(args);

                DbValue   valOutput = DbFunctions.GREATEREQUAL(tools, fargs);
                ByteSlice bs        = valOutput.Eval();
                int       output    = tools.GetInt(bs);

                int expected = (x >= y ? 1 : 0);

                if (expected != output)
                {
                    throw new Exception("DbFunctions.GREATEREQUAL(Double, Double) has failed.  Expected result: " + expected.ToString() + ", but received: " + output.ToString());
                }
                else
                {
                    Console.WriteLine("Expected results received.");
                }
            }

            {
                Console.WriteLine("Testing DbFunctions.GREATEREQUAL(Long, Long)...");

                long x = DateTime.Now.Ticks;
                long y = DateTime.Now.Ticks;

                List <DbValue> args = new List <DbValue>();
                args.Add(tools.AllocValue(x));
                args.Add(tools.AllocValue(y));
                DbFunctionArguments fargs = new DbFunctionArguments(args);

                DbValue   valOutput = DbFunctions.GREATEREQUAL(tools, fargs);
                ByteSlice bs        = valOutput.Eval();
                int       output    = tools.GetInt(bs);

                int expected = (x >= y ? 1 : 0);

                if (expected != output)
                {
                    throw new Exception("DbFunctions.GREATEREQUAL(Long, Long) has failed.  Expected result: " + expected.ToString() + ", but received: " + output.ToString());
                }
                else
                {
                    Console.WriteLine("Expected results received.");
                }
            }

            {
                Console.WriteLine("Testing DbFunctions.GREATEREQUAL(Int32, Double)...");

                int    x = rnd.Next(Int32.MinValue, Int32.MaxValue);
                double y = rnd.NextDouble();

                List <DbValue> args = new List <DbValue>();
                args.Add(tools.AllocValue(x));
                args.Add(tools.AllocValue(y));
                DbFunctionArguments fargs = new DbFunctionArguments(args);

                DbValue   valOutput = DbFunctions.GREATEREQUAL(tools, fargs);
                ByteSlice bs        = valOutput.Eval();
                int       output    = tools.GetInt(bs);

                int expected = (x >= y ? 1 : 0);

                if (expected != output)
                {
                    throw new Exception("DbFunctions.GREATEREQUAL(Int32, Double) has failed.  Expected result: " + expected.ToString() + ", but received: " + output.ToString());
                }
                else
                {
                    Console.WriteLine("Expected results received.");
                }
            }

            {
                Console.WriteLine("Testing DbFunctions.GREATEREQUAL(Int32, Long)...");

                int  x = rnd.Next(Int32.MinValue, Int32.MaxValue);
                long y = DateTime.Now.Ticks;

                List <DbValue> args = new List <DbValue>();
                args.Add(tools.AllocValue(x));
                args.Add(tools.AllocValue(y));
                DbFunctionArguments fargs = new DbFunctionArguments(args);

                DbValue   valOutput = DbFunctions.GREATEREQUAL(tools, fargs);
                ByteSlice bs        = valOutput.Eval();
                int       output    = tools.GetInt(bs);

                int expected = (x >= y ? 1 : 0);

                if (expected != output)
                {
                    throw new Exception("DbFunctions.GREATEREQUAL(Int32, Long) has failed.  Expected result: " + expected.ToString() + ", but received: " + output.ToString());
                }
                else
                {
                    Console.WriteLine("Expected results received.");
                }
            }
        }
        public static void DbFunctions_COMPARE()
        {
            DbFunctionTools tools = new DbFunctionTools();
            Random          rnd   = new Random();

            const int NUM_RUNS_EACH = 4;

            for (int it = 0; it < NUM_RUNS_EACH; it++)
            {
                Console.WriteLine("Testing DbFunctions.COMPARE(char(n), char(n)) ({0} of {1})...", it + 1, NUM_RUNS_EACH);

                mstring x = Utils.GenString(rnd.Next(1, 200));
                mstring y = Utils.GenString(rnd.Next(1, 200));
                if (1 == it)
                {
                    y = x;
                }

                List <DbValue> args = new List <DbValue>();
                args.Add(tools.AllocValue(x));
                args.Add(tools.AllocValue(y));
                DbFunctionArguments fargs = new DbFunctionArguments(args);

                DbValue   valOutput = DbFunctions.COMPARE(tools, fargs);
                ByteSlice bs        = valOutput.Eval();
                int       output    = tools.GetInt(bs);

                int expected = string.Compare(x.ToString(), y.ToString(), StringComparison.OrdinalIgnoreCase);

                if (!_SameCompareRank(expected, output))
                {
                    if (0 == expected || 0 == output)
                    {
                        throw new Exception("DbFunctions.COMPARE(char(n), char(n)) has failed.  Expected result: " + _CompareRankToString(expected) + ", but received: " + _CompareRankToString(output));
                    }
                    Console.WriteLine("Warning: C# StringComparison.OrdinalIgnoreCase and DbFunctions.COMPARE do not agree on string ordering: C# says " + _CompareRankToString(expected) + " but I say " + _CompareRankToString(output) + " for:\r\n\t'{0}'\r\n\t'{1}'", x.ToString(), y.ToString());
                    System.Threading.Thread.Sleep(1000 * 3);
                }
                else
                {
                    Console.WriteLine("Expected results received: {0}", _CompareRankToString(expected));
                }
            }

            for (int it = 0; it < NUM_RUNS_EACH; it++)
            {
                Console.WriteLine("Testing DbFunctions.COMPARE(Int32, Int32) ({0} of {1})...", it + 1, NUM_RUNS_EACH);

                int x = rnd.Next(Int32.MinValue, Int32.MaxValue);
                int y = rnd.Next(Int32.MinValue, Int32.MaxValue);
                if (1 == it)
                {
                    y = x;
                }

                List <DbValue> args = new List <DbValue>();
                args.Add(tools.AllocValue(x));
                args.Add(tools.AllocValue(y));
                DbFunctionArguments fargs = new DbFunctionArguments(args);

                DbValue   valOutput = DbFunctions.COMPARE(tools, fargs);
                ByteSlice bs        = valOutput.Eval();
                int       output    = tools.GetInt(bs);

                int expected = x.CompareTo(y);

                if (!_SameCompareRank(expected, output))
                {
                    throw new Exception("DbFunctions.COMPARE(Int32, Int32) has failed.  Expected result: " + _CompareRankToString(expected) + ", but received: " + _CompareRankToString(output));
                }
                else
                {
                    Console.WriteLine("Expected results received: {0}", _CompareRankToString(expected));
                }
            }

            for (int it = 0; it < NUM_RUNS_EACH; it++)
            {
                Console.WriteLine("Testing DbFunctions.COMPARE(Double, Double) ({0} of {1})...", it + 1, NUM_RUNS_EACH);

                double x = rnd.NextDouble();
                double y = rnd.NextDouble();
                if (1 == it)
                {
                    y = x;
                }

                List <DbValue> args = new List <DbValue>();
                args.Add(tools.AllocValue(x));
                args.Add(tools.AllocValue(y));
                DbFunctionArguments fargs = new DbFunctionArguments(args);

                DbValue   valOutput = DbFunctions.COMPARE(tools, fargs);
                ByteSlice bs        = valOutput.Eval();
                int       output    = tools.GetInt(bs);

                int expected = x.CompareTo(y);

                if (!_SameCompareRank(expected, output))
                {
                    throw new Exception("DbFunctions.COMPARE(Double, Double) has failed.  Expected result: " + _CompareRankToString(expected) + ", but received: " + _CompareRankToString(output));
                }
                else
                {
                    Console.WriteLine("Expected results received: {0}", _CompareRankToString(expected));
                }
            }

            for (int it = 0; it < NUM_RUNS_EACH; it++)
            {
                Console.WriteLine("Testing DbFunctions.COMPARE(Long, Long) ({0} of {1})...", it + 1, NUM_RUNS_EACH);

                long x = unchecked ((long)rnd.Next() * (long)rnd.Next());
                long y = unchecked ((long)rnd.Next() * (long)rnd.Next());
                if (1 == it)
                {
                    y = x;
                }

                List <DbValue> args = new List <DbValue>();
                args.Add(tools.AllocValue(x));
                args.Add(tools.AllocValue(y));
                DbFunctionArguments fargs = new DbFunctionArguments(args);

                DbValue   valOutput = DbFunctions.COMPARE(tools, fargs);
                ByteSlice bs        = valOutput.Eval();
                int       output    = tools.GetInt(bs);

                int expected = x.CompareTo(y);

                if (!_SameCompareRank(expected, output))
                {
                    throw new Exception("DbFunctions.COMPARE(Long, Long) has failed.  Expected result: " + _CompareRankToString(expected) + ", but received: " + _CompareRankToString(output));
                }
                else
                {
                    Console.WriteLine("Expected results received: {0}", _CompareRankToString(expected));
                }
            }

            for (int it = 0; it < NUM_RUNS_EACH; it++)
            {
                Console.WriteLine("Testing DbFunctions.COMPARE(Double, Int32) ({0} of {1})...", it + 1, NUM_RUNS_EACH);

                double x = rnd.NextDouble();
                int    y = rnd.Next();
                if (1 == it)
                {
                    //y = x;
                    x = y;
                }

                List <DbValue> args = new List <DbValue>();
                args.Add(tools.AllocValue(x));
                args.Add(tools.AllocValue(y));
                DbFunctionArguments fargs = new DbFunctionArguments(args);

                DbValue   valOutput = DbFunctions.COMPARE(tools, fargs);
                ByteSlice bs        = valOutput.Eval();
                int       output    = tools.GetInt(bs);

                int expected = x.CompareTo((double)y);

                if (!_SameCompareRank(expected, output))
                {
                    throw new Exception("DbFunctions.COMPARE(Double, Int32) has failed.  Expected result: " + _CompareRankToString(expected) + ", but received: " + _CompareRankToString(output));
                }
                else
                {
                    Console.WriteLine("Expected results received: {0}", _CompareRankToString(expected));
                }
            }

            for (int it = 0; it < NUM_RUNS_EACH; it++)
            {
                Console.WriteLine("Testing DbFunctions.COMPARE(Double, Long) ({0} of {1})...", it + 1, NUM_RUNS_EACH);

                double x = rnd.NextDouble();
                long   y = unchecked ((long)rnd.Next() * (long)rnd.Next());
                if (1 == it)
                {
                    //y = x;
                    x = y;
                }

                List <DbValue> args = new List <DbValue>();
                args.Add(tools.AllocValue(x));
                args.Add(tools.AllocValue(y));
                DbFunctionArguments fargs = new DbFunctionArguments(args);

                DbValue   valOutput = DbFunctions.COMPARE(tools, fargs);
                ByteSlice bs        = valOutput.Eval();
                int       output    = tools.GetInt(bs);

                int expected = x.CompareTo((double)y);

                if (!_SameCompareRank(expected, output))
                {
                    throw new Exception("DbFunctions.COMPARE(Double, Long) has failed.  Expected result: " + _CompareRankToString(expected) + ", but received: " + _CompareRankToString(output));
                }
                else
                {
                    Console.WriteLine("Expected results received: {0}", _CompareRankToString(expected));
                }
            }

            for (int it = 0; it < NUM_RUNS_EACH; it++)
            {
                Console.WriteLine("Testing DbFunctions.COMPARE(DateTime, DateTime) ({0} of {1})...", it + 1, NUM_RUNS_EACH);

                DateTime x = DateTime.Now;
                DateTime y = x.AddDays(rnd.Next(-365, 365));
                if (1 == it)
                {
                    y = x;
                }

                List <DbValue> args = new List <DbValue>();
                args.Add(tools.AllocValue(x));
                args.Add(tools.AllocValue(y));
                DbFunctionArguments fargs = new DbFunctionArguments(args);

                DbValue   valOutput = DbFunctions.COMPARE(tools, fargs);
                ByteSlice bs        = valOutput.Eval();
                int       output    = tools.GetInt(bs);

                int expected = x.CompareTo(y);

                if (!_SameCompareRank(expected, output))
                {
                    throw new Exception("DbFunctions.COMPARE(DateTime, DateTime) has failed.  Expected result: " + _CompareRankToString(expected) + ", but received: " + _CompareRankToString(output));
                }
                else
                {
                    Console.WriteLine("Expected results received: {0}", _CompareRankToString(expected));
                }
            }

            for (int it = 0; it < NUM_RUNS_EACH; it++)
            {
                Console.WriteLine("Testing DbFunctions.COMPARE(DateTime, Char(n)) ({0} of {1})...", it + 1, NUM_RUNS_EACH);

                DateTime x  = DateTime.Now;
                DateTime dt = x.AddDays(rnd.Next(-365, 365));
                mstring  y  = mstring.Prepare(dt.ToString());

                if (1 == it)
                {
                    y = mstring.Prepare(x.ToString());
                }

                List <DbValue> args = new List <DbValue>();
                args.Add(tools.AllocValue(x));
                args.Add(tools.AllocValue(y));
                DbFunctionArguments fargs = new DbFunctionArguments(args);

                DbValue   valOutput = DbFunctions.COMPARE(tools, fargs);
                ByteSlice bs        = valOutput.Eval();
                int       output    = tools.GetInt(bs);

                int expected = x.CompareTo(DateTime.Parse(y.ToString()));

                if (!_SameCompareRank(expected, output))
                {
                    throw new Exception("DbFunctions.COMPARE(DateTime, Char(n)) has failed.  Expected result: " + _CompareRankToString(expected) + ", but received: " + _CompareRankToString(output));
                }
                else
                {
                    Console.WriteLine("Expected results received: {0}", _CompareRankToString(expected));
                }
            }

            for (int it = 0; it < NUM_RUNS_EACH; it++)
            {
                Console.WriteLine("Testing DbFunctions.COMPARE(Char(n), DateTime) ({0} of {1})...", it + 1, NUM_RUNS_EACH);

                DateTime y  = DateTime.Now;
                DateTime dt = y.AddDays(rnd.Next(-365, 365));
                mstring  x  = mstring.Prepare(dt.ToString());

                if (1 == it)
                {
                    x = mstring.Prepare(y.ToString());
                }

                List <DbValue> args = new List <DbValue>();
                args.Add(tools.AllocValue(x));
                args.Add(tools.AllocValue(y));
                DbFunctionArguments fargs = new DbFunctionArguments(args);

                DbValue   valOutput = DbFunctions.COMPARE(tools, fargs);
                ByteSlice bs        = valOutput.Eval();
                int       output    = tools.GetInt(bs);

                int expected = DateTime.Parse(x.ToString()).CompareTo(y);

                if (!_SameCompareRank(expected, output))
                {
                    throw new Exception("DbFunctions.COMPARE(Char(n), DateTime) has failed.  Expected result: " + _CompareRankToString(expected) + ", but received: " + _CompareRankToString(output));
                }
                else
                {
                    Console.WriteLine("Expected results received: {0}", _CompareRankToString(expected));
                }
            }
        }
        public static void DbAggregators_CHOOSERND()
        {
            DbFunctionTools tools    = new DbFunctionTools();
            Random          rnd      = new Random();
            const int       rowcount = 20;

            //Double.
            {
                Console.WriteLine("Testing DbAggregators_CHOOSERND(Double)...");

                DbFunctionArguments[]    fargs = new DbFunctionArguments[rowcount];
                Dictionary <double, int> dict  = new Dictionary <double, int>();

                for (int i = 0; i < fargs.Length; i++)
                {
                    double input = rnd.NextDouble();
                    if (input < 0.5)
                    {
                        input = input * -1d;
                    }
                    dict[input] = 1;
                    List <DbValue> args = new List <DbValue>();
                    args.Add(tools.AllocValue(input));
                    fargs[i] = new DbFunctionArguments(args);
                }
                DbValue   valOutput = DbAggregators.CHOOSERND(tools, new DbAggregatorArguments(fargs));
                ByteSlice bs        = valOutput.Eval();
                double    output    = tools.GetDouble(bs);

                if (!dict.ContainsKey(output))
                {
                    throw new Exception("DbAggregators_CHOOSERND(Double) has failed.  Value not found: " + output.ToString());
                }
                else
                {
                    Console.WriteLine("Expected results received.");
                }
            }

            {
                Console.WriteLine("Testing DbAggregators_CHOOSERND(Int32)...");

                DbFunctionArguments[] fargs = new DbFunctionArguments[rowcount];
                Dictionary <int, int> dict  = new Dictionary <int, int>();

                for (int i = 0; i < fargs.Length; i++)
                {
                    int input = rnd.Next(Int32.MinValue, Int32.MaxValue);
                    dict[input] = 1;
                    List <DbValue> args = new List <DbValue>();
                    args.Add(tools.AllocValue(input));
                    fargs[i] = new DbFunctionArguments(args);
                }
                DbValue   valOutput = DbAggregators.CHOOSERND(tools, new DbAggregatorArguments(fargs));
                ByteSlice bs        = valOutput.Eval();
                int       output    = tools.GetInt(bs);

                if (!dict.ContainsKey(output))
                {
                    throw new Exception("DbAggregators_CHOOSERND(Int32) has failed.  Value not found: " + output.ToString());
                }
                else
                {
                    Console.WriteLine("Expected results received.");
                }
            }

            {
                Console.WriteLine("Testing DbAggregators_CHOOSERND(Int64)...");

                DbFunctionArguments[]  fargs = new DbFunctionArguments[rowcount];
                Dictionary <long, int> dict  = new Dictionary <long, int>();

                for (int i = 0; i < fargs.Length; i++)
                {
                    long input = DateTime.Now.Ticks;
                    if (input % 2 == 0)
                    {
                        input = input * -1;
                    }
                    dict[input] = 1;
                    List <DbValue> args = new List <DbValue>();
                    args.Add(tools.AllocValue(input));
                    fargs[i] = new DbFunctionArguments(args);
                }
                DbValue   valOutput = DbAggregators.CHOOSERND(tools, new DbAggregatorArguments(fargs));
                ByteSlice bs        = valOutput.Eval();
                long      output    = tools.GetLong(bs);

                if (!dict.ContainsKey(output))
                {
                    throw new Exception("DbAggregators_CHOOSERND(Int64) has failed.  Value not found: " + output.ToString());
                }
                else
                {
                    Console.WriteLine("Expected results received.");
                }
            }

            {
                Console.WriteLine("Testing DbAggregators_CHOOSERND(char(n))...");

                DbFunctionArguments[]    fargs = new DbFunctionArguments[rowcount];
                Dictionary <string, int> dict  = new Dictionary <string, int>();

                for (int i = 0; i < fargs.Length; i++)
                {
                    int     strlen = rnd.Next(1, 100);
                    mstring input  = Utils.GenString(strlen);
                    dict[input.ToString()] = 1;
                    List <DbValue> args = new List <DbValue>();
                    args.Add(tools.AllocValue(input));
                    fargs[i] = new DbFunctionArguments(args);
                }
                DbValue   valOutput = DbAggregators.CHOOSERND(tools, new DbAggregatorArguments(fargs));
                ByteSlice bs        = valOutput.Eval();
                mstring   output    = tools.GetString(bs);

                if (!dict.ContainsKey(output.ToString()))
                {
                    throw new Exception("DbAggregators_CHOOSERND(char(n)) has failed.  Value not found: " + output.ToString());
                }
                else
                {
                    Console.WriteLine("Expected results received.");
                }
            }
        }
Exemple #11
0
        public static DbValue CAST(DbFunctionTools tools, DbFunctionArguments args)
        {
#if DEBUG
            //System.Diagnostics.Debugger.Launch();
#endif

            string FunctionName = "CAST";

            args.EnsureCount(FunctionName, 2);

            DbType    type;
            ByteSlice bs = args[0].Eval(out type);

            mstring xas = tools.GetString(args[1]);
            if (!xas.StartsWith("AS "))
            {
#if DEBUG
                throw new Exception("Expected AS <type> in CAST, not: " + xas);
#endif
                throw new Exception("Expected AS <type> in CAST");
            }
            mstring msastype = xas.SubstringM(3);
            string  sastype  = msastype.ToString();  // Alloc.
            sastype = DbType.NormalizeName(sastype); // Alloc if char(n).
            DbType astype = DbType.Prepare(sastype); // Alloc if char(n).
            if (DbTypeID.NULL == astype.ID)
            {
                throw new Exception("Unknown AS type in CAST: " + sastype);
            }

            switch (astype.ID)
            {
            case DbTypeID.INT:
                switch (type.ID)
                {
                case DbTypeID.INT:         // as INT
                    if (astype.Size > type.Size)
                    {
                        throw new Exception("CAST: source value buffer too small");
                    }
                    return(tools.AllocValue(ByteSlice.Prepare(bs, 0, astype.Size), astype));

                case DbTypeID.LONG:         // as INT
                    return(tools.AllocValue((int)tools.GetLong(bs)));

                case DbTypeID.DOUBLE:         // as INT
                    return(tools.AllocValue((int)tools.GetDouble(bs)));

                case DbTypeID.DATETIME:         // as INT
                    return(tools.AllocValue((int)tools.GetDateTime(bs).Ticks));

                case DbTypeID.CHARS:         // as INT
                {
                    int to = tools.GetString(bs).NextItemToInt32(' ');
                    return(tools.AllocValue(to));
                }

                default:
                    throw new Exception("Cannot handle CAST value of type " + type.Name + " AS " + astype.Name);
                }
                break;

            case DbTypeID.LONG:
                switch (type.ID)
                {
                case DbTypeID.INT:         // as LONG
                    return(tools.AllocValue((long)tools.GetInt(bs)));

                case DbTypeID.LONG:         // as LONG
                    if (astype.Size > type.Size)
                    {
                        throw new Exception("CAST: source value buffer too small");
                    }
                    return(tools.AllocValue(ByteSlice.Prepare(bs, 0, astype.Size), astype));

                case DbTypeID.DOUBLE:         // as LONG
                    return(tools.AllocValue((long)tools.GetDouble(bs)));

                case DbTypeID.DATETIME:         // as LONG
                    return(tools.AllocValue((long)tools.GetDateTime(bs).Ticks));

                case DbTypeID.CHARS:         // as LONG
                {
                    long to = tools.GetString(bs).NextItemToInt64(' ');
                    return(tools.AllocValue(to));
                }

                default:
                    throw new Exception("Cannot handle CAST value of type " + type.Name + " AS " + astype.Name);
                }
                break;

            case DbTypeID.DOUBLE:
                switch (type.ID)
                {
                case DbTypeID.INT:         // as DOUBLE
                    return(tools.AllocValue((double)tools.GetInt(bs)));

                case DbTypeID.LONG:         // as DOUBLE
                    return(tools.AllocValue((double)tools.GetLong(bs)));

                case DbTypeID.DOUBLE:         // as DOUBLE
                    if (astype.Size > type.Size)
                    {
                        throw new Exception("CAST: source value buffer too small");
                    }
                    return(tools.AllocValue(ByteSlice.Prepare(bs, 0, astype.Size), astype));

                case DbTypeID.DATETIME:         // as DOUBLE
                    return(tools.AllocValue((double)tools.GetDateTime(bs).Ticks));

                case DbTypeID.CHARS:         // as DOUBLE
                {
                    double to = tools.GetString(bs).NextItemToDouble(' ');
                    return(tools.AllocValue(to));
                }

                default:
                    throw new Exception("Cannot handle CAST value of type " + type.Name + " AS " + astype.Name);
                }
                break;

            case DbTypeID.DATETIME:
                switch (type.ID)
                {
                case DbTypeID.INT:         // as DATETIME
                    return(tools.AllocValue(new DateTime((long)tools.GetInt(bs))));

                case DbTypeID.LONG:         // as DATETIME
                    return(tools.AllocValue(new DateTime((long)tools.GetLong(bs))));

                case DbTypeID.DOUBLE:         // as DATETIME
                    return(tools.AllocValue(new DateTime((long)tools.GetDouble(bs))));

                case DbTypeID.DATETIME:         // as DATETIME
                    if (astype.Size > type.Size)
                    {
                        throw new Exception("CAST: source value buffer too small");
                    }
                    return(tools.AllocValue(ByteSlice.Prepare(bs, 0, astype.Size), astype));

                case DbTypeID.CHARS:         // as DATETIME
                {
                    mstring to = tools.GetString(bs);
                    return(tools.AllocValue(DateTime.Parse(to.ToString())));
                }

                default:
                    throw new Exception("Cannot handle CAST value of type " + type.Name + " AS " + astype.Name);
                }
                break;

            case DbTypeID.CHARS:
                switch (type.ID)
                {
                case DbTypeID.INT:         // as CHAR(n)
                {
                    mstring ms = mstring.Prepare(tools.GetInt(bs));
                    return(tools.AllocValue(ms, astype.Size));
                }

                case DbTypeID.LONG:         // as CHAR(n)
                {
                    mstring ms = mstring.Prepare(tools.GetLong(bs));
                    return(tools.AllocValue(ms, astype.Size));
                }

                case DbTypeID.DOUBLE:         // as CHAR(n)
                {
                    mstring ms = mstring.Prepare(tools.GetDouble(bs));
                    return(tools.AllocValue(ms, astype.Size));
                }

                case DbTypeID.DATETIME:         // as CHAR(n)
                {
                    mstring ms = mstring.Prepare(tools.GetDateTime(bs).ToString());
                    return(tools.AllocValue(ms, astype.Size));
                }

                case DbTypeID.CHARS:         // as CHAR(n)
                    if (astype.Size > type.Size)
                    {
                        //throw new Exception("CAST: source value buffer too small");
                        return(tools.AllocValue(tools.GetString(bs), astype.Size));
                    }
                    return(tools.AllocValue(ByteSlice.Prepare(bs, 0, astype.Size), astype));

                default:
                    throw new Exception("Cannot handle CAST value of type " + type.Name + " AS " + astype.Name);
                }
                break;

            default:
                throw new Exception("Cannot handle CAST value of type " + type.Name + " AS " + astype.Name);
            }
        }
        public static void DbFunctions_SUBSTRING()
        {
            DbFunctionTools tools = new DbFunctionTools();

            //String,Int32.
            {
                Console.WriteLine("Testing DbFunctions.SUBSTRING(String, int, int)...");

                List <DbValue> args = new List <DbValue>();
                args.Add(tools.AllocValue(mstring.Prepare("HELLO WORLD")));
                int si  = 6;
                int len = 5;
                args.Add(tools.AllocValue(si));
                args.Add(tools.AllocValue(len));
                DbFunctionArguments fargs = new DbFunctionArguments(args);

                DbValue   valOutput = DbFunctions.SUBSTRING(tools, fargs);
                ByteSlice bs        = valOutput.Eval();
                mstring   output    = tools.GetString(bs);

                mstring expected = mstring.Prepare("WORLD");

                if (expected != output)
                {
                    throw new Exception("DbFunctions.SUBSTRING(String, int, int) has failed.  Expected result: " + expected.ToString() + ", but received: " + output.ToString());
                }
                else
                {
                    Console.WriteLine("Expected results received.");
                }
            }
        }
Exemple #13
0
        public static void DbFunctions_IIF()
        {
            DbFunctionTools tools = new DbFunctionTools();
            Random          rnd   = new Random();

            // int
            {
                Console.WriteLine("Testing DbFunctions.IIF(1, 'First', 'Second')...");

                List <DbValue> args = new List <DbValue>();
                args.Add(tools.AllocValue(1));
                args.Add(tools.AllocValue(mstring.Prepare("First")));
                args.Add(tools.AllocValue(mstring.Prepare("Second")));
                DbFunctionArguments fargs = new DbFunctionArguments(args);

                DbValue   valOutput = DbFunctions.IIF(tools, fargs);
                ByteSlice bs        = valOutput.Eval();
                mstring   output    = tools.GetString(bs);

                mstring expected = mstring.Prepare("First");

                if (expected != output)
                {
                    throw new Exception("DbFunctions.IIF(1, 'First', 'Second') has failed.  Expected result: " + expected.ToString() + ", but received: " + output.ToString());
                }
                else
                {
                    Console.WriteLine("Expected results received.");
                }
            }
            {
                Console.WriteLine("Testing DbFunctions.IIF(0, 'First', 'Second')...");

                List <DbValue> args = new List <DbValue>();
                args.Add(tools.AllocValue(0));
                args.Add(tools.AllocValue(mstring.Prepare("First")));
                args.Add(tools.AllocValue(mstring.Prepare("Second")));
                DbFunctionArguments fargs = new DbFunctionArguments(args);

                DbValue   valOutput = DbFunctions.IIF(tools, fargs);
                ByteSlice bs        = valOutput.Eval();
                mstring   output    = tools.GetString(bs);

                mstring expected = mstring.Prepare("Second");

                if (expected != output)
                {
                    throw new Exception("DbFunctions.IIF(0, 'First', 'Second') has failed.  Expected result: " + expected.ToString() + ", but received: " + output.ToString());
                }
                else
                {
                    Console.WriteLine("Expected results received.");
                }
            }
            {
                Console.WriteLine("Testing DbFunctions.IIF(42, 'First', 'Second')...");

                List <DbValue> args = new List <DbValue>();
                args.Add(tools.AllocValue(42));
                args.Add(tools.AllocValue(mstring.Prepare("First")));
                args.Add(tools.AllocValue(mstring.Prepare("Second")));
                DbFunctionArguments fargs = new DbFunctionArguments(args);

                DbValue   valOutput = DbFunctions.IIF(tools, fargs);
                ByteSlice bs        = valOutput.Eval();
                mstring   output    = tools.GetString(bs);

                mstring expected = mstring.Prepare("First");

                if (expected != output)
                {
                    throw new Exception("DbFunctions.IIF(42, 'First', 'Second') has failed.  Expected result: " + expected.ToString() + ", but received: " + output.ToString());
                }
                else
                {
                    Console.WriteLine("Expected results received.");
                }
            }

            // NULL
            {
                Console.WriteLine("Testing DbFunctions.IIF(NULL, 'First', 'Second')...");

                List <DbValue> args = new List <DbValue>();
                args.Add(tools.AllocNullValue());
                args.Add(tools.AllocValue(mstring.Prepare("First")));
                args.Add(tools.AllocValue(mstring.Prepare("Second")));
                DbFunctionArguments fargs = new DbFunctionArguments(args);

                DbValue   valOutput = DbFunctions.IIF(tools, fargs);
                ByteSlice bs        = valOutput.Eval();
                mstring   output    = tools.GetString(bs);

                mstring expected = mstring.Prepare("Second");

                if (expected != output)
                {
                    throw new Exception("DbFunctions.IIF(NULL, 'First', 'Second') has failed.  Expected result: " + expected.ToString() + ", but received: " + output.ToString());
                }
                else
                {
                    Console.WriteLine("Expected results received.");
                }
            }
        }
Exemple #14
0
        public static void DbAggregators_COUNTDISTINCT()
        {
            DbFunctionTools tools    = new DbFunctionTools();
            Random          rnd      = new Random();
            const int       rowcount = 20;

            //Double.
            {
                Console.WriteLine("Testing DbAggregators_COUNTDISTINCT(Double)...");

                DbFunctionArguments[]      fargs = new DbFunctionArguments[rowcount];
                Dictionary <double, short> dict  = new Dictionary <double, short>();
                double repeat = 0;
                for (int i = 0; i < fargs.Length; i++)
                {
                    double input = rnd.NextDouble();
                    if (input < 0.5)
                    {
                        input = input * -1d;
                    }
                    if (i == 0)
                    {
                        repeat = input;
                    }

                    if (i % 3 == 1)
                    {
                        input = repeat;
                    }

                    dict[input] = 0;
                    List <DbValue> args = new List <DbValue>();
                    args.Add(tools.AllocValue(input));
                    fargs[i] = new DbFunctionArguments(args);
                }
                DbValue   valOutput = DbAggregators.COUNTDISTINCT(tools, new DbAggregatorArguments(fargs));
                ByteSlice bs        = valOutput.Eval();
                int       output    = (int)tools.GetLong(bs);
                int       expected  = dict.Count;
                if (expected != output)
                {
                    throw new Exception("DbAggregators_COUNTDISTINCT(Double) has failed.  Expected result: " + expected.ToString() + ", but received: " + output.ToString());
                }
                else
                {
                    Console.WriteLine("Expected results received.");
                }
            }

            {
                Console.WriteLine("Testing DbAggregators_COUNTDISTINCT(Int32)...");

                DbFunctionArguments[]   fargs = new DbFunctionArguments[rowcount];
                Dictionary <int, short> dict  = new Dictionary <int, short>();
                int repeat = 0;
                for (int i = 0; i < fargs.Length; i++)
                {
                    int input = rnd.Next(Int32.MinValue, Int32.MaxValue);
                    if (i == 0)
                    {
                        repeat = input;
                    }

                    if (i % 3 == 1)
                    {
                        input = repeat;
                    }
                    dict[input] = 0;
                    List <DbValue> args = new List <DbValue>();
                    args.Add(tools.AllocValue(input));
                    fargs[i] = new DbFunctionArguments(args);
                }
                DbValue   valOutput = DbAggregators.COUNTDISTINCT(tools, new DbAggregatorArguments(fargs));
                ByteSlice bs        = valOutput.Eval();
                int       output    = (int)tools.GetLong(bs);
                int       expected  = dict.Count;
                if (expected != output)
                {
                    throw new Exception("DbAggregators_COUNTDISTINCT(Int32) has failed.  Expected result: " + expected.ToString() + ", but received: " + output.ToString());
                }
                else
                {
                    Console.WriteLine("Expected results received.");
                }
            }

            {
                Console.WriteLine("Testing DbAggregators_COUNTDISTINCT(Int64)...");

                DbFunctionArguments[]    fargs = new DbFunctionArguments[rowcount];
                Dictionary <long, short> dict  = new Dictionary <long, short>();
                long repeat = 0;
                for (int i = 0; i < fargs.Length; i++)
                {
                    long input = DateTime.Now.Ticks;
                    if (input % 2 == 0)
                    {
                        input = input * -1;
                    }
                    if (i == 0)
                    {
                        repeat = input;
                    }

                    if (i % 3 == 1)
                    {
                        input = repeat;
                    }
                    dict[input] = 0;
                    List <DbValue> args = new List <DbValue>();
                    args.Add(tools.AllocValue(input));
                    fargs[i] = new DbFunctionArguments(args);
                }
                DbValue   valOutput = DbAggregators.COUNTDISTINCT(tools, new DbAggregatorArguments(fargs));
                ByteSlice bs        = valOutput.Eval();
                int       output    = (int)tools.GetLong(bs);
                int       expected  = dict.Count;
                if (expected != output)
                {
                    throw new Exception("DbAggregators_COUNTDISTINCT(Int64) has failed.  Expected result: " + expected.ToString() + ", but received: " + output.ToString());
                }
                else
                {
                    Console.WriteLine("Expected results received.");
                }
            }

            {
                Console.WriteLine("Testing DbAggregators_COUNTDISTINCT(char(n))...");

                DbFunctionArguments[]      fargs = new DbFunctionArguments[rowcount];
                Dictionary <string, short> dict  = new Dictionary <string, short>();
                mstring repeat = mstring.Prepare();
                for (int i = 0; i < fargs.Length; i++)
                {
                    int     strlen = rnd.Next(1, 100);
                    mstring input  = Utils.GenString(strlen);
                    if (i == 0)
                    {
                        repeat = input;
                    }

                    if (i % 3 == 1)
                    {
                        input = repeat;
                    }
                    dict[input.ToString()] = 0;
                    List <DbValue> args = new List <DbValue>();
                    args.Add(tools.AllocValue(input));
                    fargs[i] = new DbFunctionArguments(args);
                }
                DbValue   valOutput = DbAggregators.COUNTDISTINCT(tools, new DbAggregatorArguments(fargs));
                ByteSlice bs        = valOutput.Eval();
                int       output    = (int)tools.GetLong(bs);
                int       expected  = dict.Count;
                if (expected != output)
                {
                    throw new Exception("DbAggregators_COUNTDISTINCT(char(n)) has failed.  Expected result: " + expected.ToString() + ", but received: " + output.ToString());
                }
                else
                {
                    Console.WriteLine("Expected results received.");
                }
            }
        }