Esempio n. 1
0
        public static void DbAggregators_COUNT()
        {
            DbFunctionTools tools    = new DbFunctionTools();
            Random          rnd      = new Random();
            const int       rowcount = 20;

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

                DbFunctionArguments[] fargs = new DbFunctionArguments[rowcount];
                for (int i = 0; i < fargs.Length; i++)
                {
                    double input = rnd.NextDouble();
                    if (input < 0.5)
                    {
                        input = input * -1d;
                    }
                    List <DbValue> args = new List <DbValue>();
                    args.Add(tools.AllocValue(input));
                    fargs[i] = new DbFunctionArguments(args);
                }
                DbValue   valOutput = DbAggregators.COUNT(tools, new DbAggregatorArguments(fargs));
                ByteSlice bs        = valOutput.Eval();
                int       output    = (int)tools.GetLong(bs);
                int       expected  = rowcount;
                if (expected != output)
                {
                    throw new Exception("DbAggregators_COUNT(Double) has failed.  Expected result: " + expected.ToString() + ", but received: " + output.ToString());
                }
                else
                {
                    Console.WriteLine("Expected results received.");
                }
            }

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

                DbFunctionArguments[] fargs = new DbFunctionArguments[rowcount];
                for (int i = 0; i < fargs.Length; i++)
                {
                    int            input = rnd.Next(Int32.MinValue, Int32.MaxValue);
                    List <DbValue> args  = new List <DbValue>();
                    args.Add(tools.AllocValue(input));
                    fargs[i] = new DbFunctionArguments(args);
                }
                DbValue   valOutput = DbAggregators.COUNT(tools, new DbAggregatorArguments(fargs));
                ByteSlice bs        = valOutput.Eval();
                int       output    = (int)tools.GetLong(bs);
                int       expected  = rowcount;
                if (expected != output)
                {
                    throw new Exception("DbAggregators_COUNT(Int32) has failed.  Expected result: " + expected.ToString() + ", but received: " + output.ToString());
                }
                else
                {
                    Console.WriteLine("Expected results received.");
                }
            }

            {
                Console.WriteLine("Testing DbAggregators_COUNT(with NULL)...");

                DbFunctionArguments[] fargs = new DbFunctionArguments[rowcount];
                int expected = 0;
                for (int i = 0; i < fargs.Length; i++)
                {
                    int            input = rnd.Next(Int32.MinValue, Int32.MaxValue);
                    List <DbValue> args  = new List <DbValue>();
                    if (1 == i % 8)
                    {
                        args.Add(tools.AllocNullValue());
                    }
                    else
                    {
                        args.Add(tools.AllocValue(input));
                        expected++;
                    }
                    fargs[i] = new DbFunctionArguments(args);
                }
                DbValue   valOutput = DbAggregators.COUNT(tools, new DbAggregatorArguments(fargs));
                ByteSlice bs        = valOutput.Eval();
                int       output    = (int)tools.GetLong(bs);
                if (expected != output)
                {
                    throw new Exception("DbAggregators_COUNT(with NULL) has failed.  Expected result: " + expected.ToString() + ", but received: " + output.ToString());
                }
                else
                {
                    Console.WriteLine("Expected results received.");
                }
            }

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

                DbFunctionArguments[] fargs = new DbFunctionArguments[rowcount];

                for (int i = 0; i < fargs.Length; i++)
                {
                    long input = DateTime.Now.Ticks;
                    if (input % 2 == 0)
                    {
                        input = input * -1;
                    }
                    List <DbValue> args = new List <DbValue>();
                    args.Add(tools.AllocValue(input));
                    fargs[i] = new DbFunctionArguments(args);
                }
                DbValue   valOutput = DbAggregators.COUNT(tools, new DbAggregatorArguments(fargs));
                ByteSlice bs        = valOutput.Eval();
                int       output    = (int)tools.GetLong(bs);
                int       expected  = rowcount;
                if (expected != output)
                {
                    throw new Exception("DbAggregators_COUNT(Int64) has failed.  Expected result: " + expected.ToString() + ", but received: " + output.ToString());
                }
                else
                {
                    Console.WriteLine("Expected results received.");
                }
            }

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

                DbFunctionArguments[] fargs = new DbFunctionArguments[rowcount];
                for (int i = 0; i < fargs.Length; i++)
                {
                    int            strlen = rnd.Next(1, 100);
                    mstring        input  = Utils.GenString(strlen);
                    List <DbValue> args   = new List <DbValue>();
                    args.Add(tools.AllocValue(input));
                    fargs[i] = new DbFunctionArguments(args);
                }
                DbValue   valOutput = DbAggregators.COUNT(tools, new DbAggregatorArguments(fargs));
                ByteSlice bs        = valOutput.Eval();
                int       output    = (int)tools.GetLong(bs);
                int       expected  = rowcount;
                if (expected != output)
                {
                    throw new Exception("DbAggregators_COUNT(char(n)) has failed.  Expected result: " + expected.ToString() + ", but received: " + output.ToString());
                }
                else
                {
                    Console.WriteLine("Expected results received.");
                }
            }
        }
Esempio n. 2
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.");
                }
            }
        }