public void IndexOf_OrderIsCorrect( IndexOfMethod indexOfMethod, int count, bool frontToBackOrder ) { SegmentedList <T> list = GenericListFactory(count); SegmentedList <T> withoutDuplicates = list.ToSegmentedList(); list.AddRange(list); IndexOfDelegate IndexOf = IndexOfDelegateFromType(indexOfMethod); Assert.All( Enumerable.Range(0, count), i => { if (frontToBackOrder) { Assert.Equal(i, IndexOf(list, withoutDuplicates[i])); } else { Assert.Equal(count + i, IndexOf(list, withoutDuplicates[i])); } } ); }
public void IndexOf_NonExistingValues( IndexOfMethod indexOfMethod, int count, bool frontToBackOrder ) { _ = frontToBackOrder; SegmentedList <T> list = GenericListFactory(count); IEnumerable <T> nonexistentValues = CreateEnumerable( EnumerableType.List, list, count: count, numberOfMatchingElements: 0, numberOfDuplicateElements: 0 ); IndexOfDelegate IndexOf = IndexOfDelegateFromType(indexOfMethod); Assert.All( nonexistentValues, nonexistentValue => { Assert.Equal(-1, IndexOf(list, nonexistentValue)); } ); }
public void IndexOf_NoDuplicates(IndexOfMethod indexOfMethod, int count, bool frontToBackOrder) { List <T> list = GenericListFactory(count); List <T> expectedList = list.ToList(); IndexOfDelegate IndexOf = IndexOfDelegateFromType(indexOfMethod); Assert.All(Enumerable.Range(0, count), i => { Assert.Equal(i, IndexOf(list, expectedList[i])); }); }
public void IndexOf_DefaultValue(IndexOfMethod indexOfMethod, int count, bool frontToBackOrder) { T defaultValue = default(T); List <T> list = GenericListFactory(count); IndexOfDelegate IndexOf = IndexOfDelegateFromType(indexOfMethod); while (list.Remove(defaultValue)) { count--; } list.Add(defaultValue); Assert.Equal(count, IndexOf(list, defaultValue)); }
public void IndexOf_DefaultValue(IndexOfMethod indexOfMethod, int count, bool frontToBackOrder) { _ = frontToBackOrder; T?defaultValue = default; SegmentedList <T?> list = GenericListFactory(count) !; IndexOfDelegate IndexOf = IndexOfDelegateFromType(indexOfMethod); while (list.Remove(defaultValue)) { count--; } list.Add(defaultValue); Assert.Equal(count, IndexOf(list !, defaultValue !)); }
static SqlDataReaderExtensions() { Type typeSqlDataReader = typeof(SqlDataReader); Type typeSqlStatistics = typeSqlDataReader.Assembly.GetType("System.Data.SqlClient.SqlStatistics", true); Type typeFieldNameLookup = typeSqlDataReader.Assembly.GetType("System.Data.ProviderBase.FieldNameLookup", true); BindingFlags staticflags = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Static; BindingFlags instflags = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance; DynamicMethod dynmethod = new DynamicMethod("SqlDataReader_IndexOf", typeof(int), new Type[2] { typeSqlDataReader, typeof(string) }, true); ILGenerator gen = dynmethod.GetILGenerator(); gen.DeclareLocal(typeSqlStatistics); gen.DeclareLocal(typeof(int)); // SqlStatistics statistics = (SqlStatistics) null; gen.Emit(OpCodes.Ldnull); gen.Emit(OpCodes.Stloc_0); // try { gen.BeginExceptionBlock(); // statistics = SqlStatistics.StartTimer(this.Statistics); gen.Emit(OpCodes.Ldarg_0); //this gen.Emit(OpCodes.Call, typeSqlDataReader.GetProperty("Statistics", instflags | BindingFlags.GetProperty, null, typeSqlStatistics, Type.EmptyTypes, null).GetMethod); gen.Emit(OpCodes.Call, typeSqlStatistics.GetMethod("StartTimer", staticflags | BindingFlags.InvokeMethod, null, new Type[] { typeSqlStatistics }, null)); gen.Emit(OpCodes.Stloc_0); //statistics // if(this._fieldNameLookup == null) { Label branchTarget = gen.DefineLabel(); gen.Emit(OpCodes.Ldarg_0); //this gen.Emit(OpCodes.Ldfld, typeSqlDataReader.GetField("_fieldNameLookup", instflags | BindingFlags.GetField)); gen.Emit(OpCodes.Brtrue_S, branchTarget); // this.CheckMetaDataIsReady(); gen.Emit(OpCodes.Ldarg_0); //this gen.Emit(OpCodes.Call, typeSqlDataReader.GetMethod("CheckMetaDataIsReady", instflags | BindingFlags.InvokeMethod, null, Type.EmptyTypes, null)); // this._fieldNameLookup = new FieldNameLookup((IDataRecord)this, this._defaultLCID); gen.Emit(OpCodes.Ldarg_0); //this gen.Emit(OpCodes.Ldarg_0); //this gen.Emit(OpCodes.Ldarg_0); //this gen.Emit(OpCodes.Ldfld, typeSqlDataReader.GetField("_defaultLCID", instflags | BindingFlags.GetField)); gen.Emit(OpCodes.Newobj, typeFieldNameLookup.GetConstructor(instflags, null, new Type[] { typeof(IDataReader), typeof(int) }, null)); gen.Emit(OpCodes.Stfld, typeSqlDataReader.GetField("_fieldNameLookup", instflags | BindingFlags.SetField)); // } gen.MarkLabel(branchTarget); gen.Emit(OpCodes.Ldarg_0); //this gen.Emit(OpCodes.Ldfld, typeSqlDataReader.GetField("_fieldNameLookup", instflags | BindingFlags.GetField)); gen.Emit(OpCodes.Ldarg_1); //name gen.Emit(OpCodes.Call, typeFieldNameLookup.GetMethod("IndexOf", instflags | BindingFlags.InvokeMethod, null, new Type[] { typeof(string) }, null)); gen.Emit(OpCodes.Stloc_1); //int output Label leaveProtectedRegion = gen.DefineLabel(); gen.Emit(OpCodes.Leave_S, leaveProtectedRegion); // } finally { gen.BeginFaultBlock(); // SqlStatistics.StopTimer(statistics); gen.Emit(OpCodes.Ldloc_0); //statistics gen.Emit(OpCodes.Call, typeSqlStatistics.GetMethod("StopTimer", staticflags | BindingFlags.InvokeMethod, null, new Type[] { typeSqlStatistics }, null)); // } gen.EndExceptionBlock(); gen.MarkLabel(leaveProtectedRegion); gen.Emit(OpCodes.Ldloc_1); gen.Emit(OpCodes.Ret); IndexOf = (IndexOfDelegate)dynmethod.CreateDelegate(typeof(IndexOfDelegate)); }