예제 #1
0
        protected bool CheckSameValues(DataViewRowCursor curs1, IDataView view2, bool exactTypes = true, bool exactDoubles = true, bool checkId = true)
        {
            Contracts.Assert(curs1.Schema.Count == view2.Schema.Count);

            // Get a cursor for each column.
            int colLim  = curs1.Schema.Count;
            var cursors = new DataViewRowCursor[colLim];

            try
            {
                for (int col = 0; col < colLim; col++)
                {
                    // curs1 should have all columns active (for simplicity of the code here).
                    Contracts.Assert(curs1.IsColumnActive(curs1.Schema[col]));
                    cursors[col] = view2.GetRowCursor(view2.Schema[col]);
                }

                // Get the comparison delegates for each column.
                Func <bool>[] comps = new Func <bool> [colLim];
                // We have also one ID comparison delegate for each cursor.
                Func <bool>[] idComps = new Func <bool> [cursors.Length];
                for (int col = 0; col < colLim; col++)
                {
                    Contracts.Assert(cursors[col] != null);
                    var type1 = curs1.Schema[col].Type;
                    var type2 = cursors[col].Schema[col].Type;
                    if (!TestCommon.EqualTypes(type1, type2, exactTypes))
                    {
                        Fail("Different types");
                        return(Failed());
                    }
                    comps[col] = GetColumnComparer(curs1, cursors[col], col, type1, exactDoubles);
                    ValueGetter <DataViewRowId> idGetter;
                    idComps[col] = checkId ? GetIdComparer(curs1, cursors[col], out idGetter) : null;
                }

                for (; ;)
                {
                    bool f1 = curs1.MoveNext();
                    for (int col = 0; col < colLim; col++)
                    {
                        bool f2 = cursors[col].MoveNext();
                        if (f1 != f2)
                        {
                            if (f1)
                            {
                                Fail("Left has more rows at position: {0}", curs1.Position);
                            }
                            else
                            {
                                Fail("Right {0} has more rows at position: {1}", col, cursors[2].Position);
                            }
                            return(Failed());
                        }
                    }

                    if (!f1)
                    {
                        return(true);
                    }

                    for (int col = 0; col < colLim; col++)
                    {
                        Contracts.Assert(curs1.Position == cursors[col].Position);
                        var comp = comps[col];
                        if (comp != null && !comp())
                        {
                            Fail("Different values in column {0} of row {1}", col, curs1.Position);
                            return(Failed());
                        }
                        comp = idComps[col];
                        if (comp != null && !comp())
                        {
                            Fail("Different values in ID values for column {0} cursor of row {1}", col, curs1.Position);
                            return(Failed());
                        }
                    }
                }
            }
            finally
            {
                for (int col = 0; col < colLim; col++)
                {
                    var c = cursors[col];
                    if (c != null)
                    {
                        c.Dispose();
                    }
                }
            }
        }
예제 #2
0
        protected bool CheckSameValues(DataViewRowCursor curs1, DataViewRowCursor curs2, bool exactTypes, bool exactDoubles, bool checkId, bool checkIdCollisions = true)
        {
            Contracts.Assert(curs1.Schema.Count == curs2.Schema.Count);

            // Get the comparison delegates for each column.
            int colLim = curs1.Schema.Count;

            Func <bool>[] comps = new Func <bool> [colLim];
            for (int col = 0; col < colLim; col++)
            {
                var f1 = curs1.IsColumnActive(curs1.Schema[col]);
                var f2 = curs2.IsColumnActive(curs2.Schema[col]);

                if (f1 && f2)
                {
                    var type1 = curs1.Schema[col].Type;
                    var type2 = curs2.Schema[col].Type;
                    if (!TestCommon.EqualTypes(type1, type2, exactTypes))
                    {
                        Fail($"Different types {type1} and {type2}");
                        return(Failed());
                    }
                    comps[col] = GetColumnComparer(curs1, curs2, col, type1, exactDoubles);
                }
            }
            ValueGetter <DataViewRowId> idGetter = null;
            Func <bool>             idComp       = checkId ? GetIdComparer(curs1, curs2, out idGetter) : null;
            HashSet <DataViewRowId> idsSeen      = null;

            if (checkIdCollisions && idGetter == null)
            {
                idGetter = curs1.GetIdGetter();
            }
            long          idCollisions = 0;
            DataViewRowId id           = default(DataViewRowId);

            for (; ;)
            {
                bool f1 = curs1.MoveNext();
                bool f2 = curs2.MoveNext();
                if (f1 != f2)
                {
                    if (f1)
                    {
                        Fail("Left has more rows at position: {0}", curs1.Position);
                    }
                    else
                    {
                        Fail("Right has more rows at position: {0}", curs2.Position);
                    }
                    return(Failed());
                }

                if (!f1)
                {
                    if (idCollisions > 0)
                    {
                        Fail("{0} id collisions among {1} items", idCollisions, Utils.Size(idsSeen) + idCollisions);
                    }
                    return(idCollisions == 0);
                }
                else if (checkIdCollisions)
                {
                    idGetter(ref id);
                    if (!Utils.Add(ref idsSeen, id))
                    {
                        if (idCollisions == 0)
                        {
                            idCollisions++;
                        }
                    }
                }

                Contracts.Assert(curs1.Position == curs2.Position);

                for (int col = 0; col < colLim; col++)
                {
                    var comp = comps[col];
                    if (comp != null && !comp())
                    {
                        Fail("Different values in column {0} of row {1}", col, curs1.Position);
                        return(Failed());
                    }
                    if (idComp != null && !idComp())
                    {
                        Fail("Different values in ID of row {0}", curs1.Position);
                        return(Failed());
                    }
                }
            }
        }