private void Validate <T>(IHugeList <T>[] lists) where T : IComparable <T> { T[] template = lists[0].ToArray(); for (int listIndex = 1; listIndex < lists.Length; listIndex++) { IHugeList <T> list = lists[listIndex]; TestTrue("Count - instance " + listIndex.ToString(), delegate() { return(list.Count == template.Length); }); TestTrue( "Equality - instance " + listIndex.ToString(), delegate() { for (int i = 0; i < template.Length; i++) { if (0 != Comparer <T> .Default.Compare(list[i], template[i])) { return(false); } } return(true); }); lastDumpListIndex = listIndex; TestNoThrow("Validate", delegate() { ((IHugeListValidation)list).Validate(out lastDump); }); } }
public void InsertRange(int index, IHugeList <T> items, int offset, int count) { inner.InsertRange( IntLong.ToLong(index), items != null ? new AdaptHugeListLongToHugeList <T>(items) : null, IntLong.ToLong(offset), IntLong.ToLong(count)); }
private void RemoveAllAction(IHugeList <int>[] lists, Random rnd, ref string description) { IHugeList <int> reference = lists[0]; int extent = reference.Count; int modulus, item; switch ((int)(4 * Math.Pow(rnd.NextDouble(), 10))) // reaches '3' in just under 3% of cases { default: Debug.Assert(false); throw new InvalidOperationException(); case 0: // remove exact item if (extent == 0) { return; } modulus = 0; item = reference[rnd.Next() % extent]; break; case 1: // remove non-match modulus = 0; do { item = rnd.Next(); } while ((extent != 0) && reference.Contains(item)); break; case 2: // remove sparse pattern modulus = rnd.Next() % 100 + 100; item = 0; break; case 3: // remove dense pattern modulus = rnd.Next() % 20 + 1; item = 0; break; } string delegateText = String.Format("delegate (int candidate) {{ return ({1} == 0) ? (candidate == {0}) : (candidate % {1} == {0}); }}", item, modulus); description = String.Format("RemoveAll [{0}]", delegateText); script.AppendLine(String.Format("new OpRemoveAll<T>({0}),", delegateText)); for (int i = 0; i < lists.Length; i++) { try { lists[i].RemoveAll(delegate(int candidate) { return((modulus == 0) ? (candidate == item) : (candidate % modulus == item)); }); } catch (Exception exception) { Fault(lists[i], description + " - threw exception", exception); } } }
private void ContainsAction(IHugeList <int>[] lists, Random rnd, ref string description) { IHugeList <int> reference = lists[0]; if ((rnd.Next(2) == 0) && (reference.Count != 0)) { // existing int item = reference[rnd.Next(reference.Count)]; description = String.Format("Contains (existing) [{0}]", item); for (int i = 0; i < lists.Length; i++) { try { bool f = lists[i].Contains(item); if (!f) { Fault(lists[i], description + " - returned false"); } } catch (Exception exception) { Fault(lists[i], description + " - threw exception", exception); } } } else { // missing int item; do { item = rnd.Next(Int32.MinValue, Int32.MaxValue); }while (reference.Contains(item)); description = String.Format("Contains (missing) [{0}]", item); for (int i = 0; i < lists.Length; i++) { try { bool f = lists[i].Contains(item); if (f) { Fault(lists[i], description + " - returned true"); } } catch (Exception exception) { Fault(lists[i], description + " - threw exception", exception); } } } }
private void InsertAction(IHugeList <int>[] lists, Random rnd, ref string description) { IHugeList <int> reference = lists[0]; int extent = reference.Count; int item = rnd.Next(); if (rnd.Next() % 3 != 0) { index = rnd.Next(Math.Min(-1, -extent / 40), extent + Math.Max(1 + 1 /*upper bound is exclusive*/, extent / 40)); } bool valid = (index >= 0) && (index <= extent); description = String.Format("Insert [{0}, {1}: {2}]", index, valid ? "valid" : "invalid", item); script.AppendLine(String.Format("new OpInsert<T>({0}, {1}),", index, item)); for (int i = 0; i < lists.Length; i++) { try { lists[i].Insert(index, item); if (!valid) { Fault(lists[i], description + " - should have thrown exception"); } } catch (ArgumentOutOfRangeException) when(!valid && (index < 0)) { // expected } catch (ArgumentException) when(!valid && !(index < 0)) { // expected } catch (Exception exception) { Fault(lists[i], description + " - threw exception", exception); } } }
public void InsertRange(int index, IHugeList <T> items, int offset, int count) { if (items == null) { throw new ArgumentNullException(); } if ((count < 0) || (offset < 0)) { throw new ArgumentOutOfRangeException(); } if (unchecked ((uint)offset + (uint)count > (uint)items.Count)) { throw new ArgumentException(); } T[] array = new T[count]; for (int i = 0; i < count; i++) { array[i] = items[i + offset]; } InsertRange(index, array, 0, count); }
public override bool Do(int seed, StochasticControls control) { script.AppendLine(String.Format("Seed: {0}", seed)); IHugeList <int>[] lists = new IHugeList <int>[] { new ReferenceHugeList <int>(), // must be first new HugeList <int>(typeof(SplayTreeRangeMap <>), 47), new HugeList <int>(typeof(AVLTreeRangeMap <>), 512), new AdaptHugeListToHugeListLong <int>(new HugeListLong <int>(typeof(RedBlackTreeRangeMapLong <>), 47)), new AdaptHugeListToHugeListLong <int>(new HugeListLong <int>(typeof(SplayTreeRangeMapLong <>), 512)), }; Tuple <Tuple <int, int>, InvokeAction <IHugeList <int> > >[] actions = new Tuple <Tuple <int, int>, InvokeAction <IHugeList <int> > >[] { new Tuple <Tuple <int, int>, InvokeAction <IHugeList <int> > >(new Tuple <int, int>(400, 400), ContainsAction), new Tuple <Tuple <int, int>, InvokeAction <IHugeList <int> > >(new Tuple <int, int>(400, 400), InsertAction), new Tuple <Tuple <int, int>, InvokeAction <IHugeList <int> > >(new Tuple <int, int>(400, 400), RemoveAction), new Tuple <Tuple <int, int>, InvokeAction <IHugeList <int> > >(new Tuple <int, int>(1000, 2000), InsertRangeAction), new Tuple <Tuple <int, int>, InvokeAction <IHugeList <int> > >(new Tuple <int, int>(400, 400), RemoveRangeAction), new Tuple <Tuple <int, int>, InvokeAction <IHugeList <int> > >(new Tuple <int, int>(400, 400), ReplaceRangeAction), new Tuple <Tuple <int, int>, InvokeAction <IHugeList <int> > >(new Tuple <int, int>(400, 200), RemoveAllAction), new Tuple <Tuple <int, int>, InvokeAction <IHugeList <int> > >(new Tuple <int, int>(200, 200), CopyToAction), new Tuple <Tuple <int, int>, InvokeAction <IHugeList <int> > >(new Tuple <int, int>(200, 200), IndexSearchAction), new Tuple <Tuple <int, int>, InvokeAction <IHugeList <int> > >(new Tuple <int, int>(200, 200), LastIndexSearchAction), new Tuple <Tuple <int, int>, InvokeAction <IHugeList <int> > >(new Tuple <int, int>(100, 100), InsertRangeHugeListAction), }; return(StochasticDriver( "HugeList Stochastic Test", seed, control, lists, actions, delegate(IHugeList <int> _list) { return (uint)_list.Count; }, delegate(IHugeList <int>[] _lists) { Validate(_lists); CheckScript(lists[0].Count); })); }
public void AddRange(IHugeList <T> collection) { inner.AddRange(collection); }
public AdaptHugeListLongToHugeList(IHugeList <T> inner) { this.inner = inner; }
private void LastIndexSearchAction(IHugeList <int>[] lists, Random rnd, ref string description) { IHugeList <int> reference = lists[0]; int extent = reference.Count; int method = rnd.Next() % 3; const int IndexOfAnyMethod = 2; int count; switch (rnd.Next() % 3) { default: Debug.Assert(false); throw new InvalidOperationException(); case 0: count = Math.Min(rnd.Next() % 20 + 1, extent); break; case 1: count = Math.Min((int)(Math.Pow(rnd.NextDouble(), 5) * 100 + 1), extent); break; case 2: count = (int)(Math.Pow(rnd.NextDouble(), 5) * extent); break; } int index; switch (rnd.Next() % 10) { case 0: // insert at ending edge index = extent - 1; break; case 1: // insert at leading edge index = count - 1; if ((index == -1) && (extent != 0)) { index = 0; // List<>.LastIndexOf doesn't permit the vacuous case unless empty } break; default: // at random position in middle index = extent - 1 - rnd.Next(0, extent - count); break; } bool existing = (rnd.Next(2) == 0) && (count != 0) && (extent != 0); int item = 0; List <int> itemsForAny = new List <int>(); int countForAny = rnd.Next() % 4; for (int i = 0; i < (method != IndexOfAnyMethod ? 1 : countForAny); i++) { if (existing) { // existing int j = rnd.Next(count); itemsForAny.Add(reference[index - j]); } else { // missing do { item = rnd.Next(Int32.MinValue, Int32.MaxValue); }while (reference.LastIndexOf(item, index, count) >= 0); itemsForAny.Add(item); } } if (method != IndexOfAnyMethod) { Debug.Assert(itemsForAny.Count == 1); item = itemsForAny[0]; itemsForAny = null; } bool valid = true; // TODO: disabled because LastIndexOf and FindLastIndex have different invalid parameter boundaries //if (rnd.Next() % 10 == 0) //{ // // make invalid inputs // valid = false; // switch (rnd.Next() % 4) // { // default: // Debug.Assert(false); // throw new InvalidOperationException(); // case 0: // index = -2; // break; // case 1: // index = extent + 1; // break; // case 2: // count = -1; // break; // case 3: // count = extent + 2; // if (extent == 0) // { // valid = true; // special case: count not validated when extent is 0 // } // break; // } //} description = String.Format( "{0} ({1}) [{2}, {3}, {4}: {5}]", method == 0 ? "LastIndexOf" : (method == 1 ? "FindLastIndex" : "LastIndexOfAny"), existing ? "existing" : "missing", itemsForAny != null ? itemsForAny.ToString() : item.ToString(), index, count, valid ? "valid" : "invalid"); int modelFoundIndex = 0; for (int i = 0; i < lists.Length; i++) { int instanceFoundIndex = 0; try { switch (method) { default: Debug.Assert(false); throw new InvalidOperationException(); case 0: Debug.Assert(itemsForAny == null); instanceFoundIndex = lists[i].LastIndexOf(item, index, count); break; case 1: Debug.Assert(itemsForAny == null); instanceFoundIndex = lists[i].FindLastIndex(index, count, delegate(int candidate) { return(candidate == item); }); break; case 2: Debug.Assert(itemsForAny != null); instanceFoundIndex = lists[i].LastIndexOfAny(itemsForAny.ToArray(), index, count); break; } if (!valid) { Fault(lists[i], description + " - should have thrown exception"); } if (i == 0) { modelFoundIndex = instanceFoundIndex; } else { if (modelFoundIndex != instanceFoundIndex) { Fault(lists[i], description + " - found index differs"); } } } catch (ArgumentOutOfRangeException) when(!valid) { // expected } catch (ArgumentException) when(!valid) { // expected } catch (ArgumentNullException) when(!valid) { // expected } catch (Exception exception) { Fault(lists[i], description + " - threw exception", exception); } } }
private void IndexSearchAction(IHugeList <int>[] lists, Random rnd, ref string description) { IHugeList <int> reference = lists[0]; int extent = reference.Count; int method = rnd.Next() % 3; const int IndexOfAnyMethod = 2; int count; switch (rnd.Next() % 3) { default: Debug.Assert(false); throw new InvalidOperationException(); case 0: count = Math.Min(rnd.Next() % 20 + 1, extent); break; case 1: count = Math.Min((int)(Math.Pow(rnd.NextDouble(), 5) * 100 + 1), extent); break; case 2: count = (int)(Math.Pow(rnd.NextDouble(), 5) * extent); break; } int index; switch (rnd.Next() % 10) { case 0: // insert at starting edge index = 0; break; case 1: // insert at trailing edge index = extent - count; break; default: // at random position in middle index = rnd.Next(0, extent - count); break; } bool existing = (rnd.Next(2) == 0) && (count != 0); int item = 0; List <int> itemsForAny = new List <int>(); int countForAny = rnd.Next() % 4; for (int i = 0; i < (method != IndexOfAnyMethod ? 1 : countForAny); i++) { if (existing) { // existing itemsForAny.Add(reference[index + rnd.Next(count)]); } else { // missing do { item = rnd.Next(Int32.MinValue, Int32.MaxValue); }while (reference.IndexOf(item, index, count) >= 0); itemsForAny.Add(item); } } if (method != IndexOfAnyMethod) { Debug.Assert(itemsForAny.Count == 1); item = itemsForAny[0]; itemsForAny = null; } bool valid = true; if (rnd.Next() % 10 == 0) { // make invalid inputs valid = false; switch (rnd.Next() % 4) { default: Debug.Assert(false); throw new InvalidOperationException(); case 0: index = -1; break; case 1: index = extent + 1; break; case 2: count = -1; break; case 3: count = extent + 1; break; } } description = String.Format( "{0} ({1}) [{2}, {3}, {4}: {5}]", method == 0 ? "IndexOf" : (method == 1 ? "FindIndex" : "IndexOfAny"), existing ? "existing" : "missing", itemsForAny != null ? itemsForAny.ToString() : item.ToString(), index, count, valid ? "valid" : "invalid"); int modelFoundIndex = 0; for (int i = 0; i < lists.Length; i++) { int instanceFoundIndex = 0; try { switch (method) { default: Debug.Assert(false); throw new InvalidOperationException(); case 0: Debug.Assert(itemsForAny == null); instanceFoundIndex = lists[i].IndexOf(item, index, count); break; case 1: Debug.Assert(itemsForAny == null); instanceFoundIndex = lists[i].FindIndex(index, count, delegate(int candidate) { return(candidate == item); }); break; case 2: Debug.Assert(itemsForAny != null); instanceFoundIndex = lists[i].IndexOfAny(itemsForAny.ToArray(), index, count); break; } if (!valid) { Fault(lists[i], description + " - should have thrown exception"); } if (i == 0) { modelFoundIndex = instanceFoundIndex; } else { if (modelFoundIndex != instanceFoundIndex) { Fault(lists[i], description + " - found index differs"); } } } catch (ArgumentOutOfRangeException) when(!valid) { // expected } catch (ArgumentException) when(!valid) { // expected } catch (ArgumentNullException) when(!valid) { // expected } catch (Exception exception) { Fault(lists[i], description + " - threw exception", exception); } } }
private void CopyToAction(IHugeList <int>[] lists, Random rnd, ref string description) { IHugeList <int> reference = lists[0]; int extent = reference.Count; int count; switch (rnd.Next() % 3) { default: Debug.Assert(false); throw new InvalidOperationException(); case 0: count = Math.Min(rnd.Next() % 20 + 1, extent); break; case 1: count = Math.Min((int)(Math.Pow(rnd.NextDouble(), 5) * 100 + 1), extent); break; case 2: count = (int)(Math.Pow(rnd.NextDouble(), 5) * extent); break; } int arrayIndex = rnd.Next() % 4 != 0 ? 0 : 1; int arrayLength = count + arrayIndex; int index; switch (rnd.Next() % 10) { case 0: // insert at starting edge index = 0; break; case 1: // insert at trailing edge index = extent - count; break; default: // at random position in middle index = rnd.Next(0, extent - count); break; } bool valid = true; if (rnd.Next() % 10 == 0) { // make invalid inputs valid = false; switch (rnd.Next() % 6) { default: Debug.Assert(false); throw new InvalidOperationException(); case 0: index = -1; break; case 1: index = extent + 1; break; case 2: count = -1; break; case 3: count = extent + 1; break; case 4: arrayIndex = -1; break; case 5: arrayIndex += 1; break; } } description = String.Format("CopyTo [{0}, {1}, {2}: {3}]", index, count, arrayIndex, valid ? "valid" : "invalid"); int[] refArray = null; for (int i = 0; i < lists.Length; i++) { int[] instanceArray = new int[arrayLength]; try { lists[i].CopyTo(index, instanceArray, arrayIndex, count); if (!valid) { Fault(lists[i], description + " - should have thrown exception"); } if (i == 0) { refArray = instanceArray; } else { for (int j = 0; j < refArray.Length; j++) { if (refArray[j] != instanceArray[j]) { Fault(lists[i], description + " - resulting array data differs"); } } } } catch (ArgumentOutOfRangeException) when(!valid) { // expected } catch (ArgumentException) when(!valid) { // expected } catch (ArgumentNullException) when(!valid) { // expected } catch (Exception exception) { Fault(lists[i], description + " - threw exception", exception); } } }
private void ReplaceRangeAction(IHugeList <int>[] lists, Random rnd, ref string description) { IHugeList <int> reference = lists[0]; int extent = reference.Count; int removeCount = RemoveRangeCountGenerator(rnd, extent); int insertCount = InsertRangeCountGenerator(rnd, extent); int[] items = new int[insertCount]; for (int i = 0; i < items.Length; i++) { items[i] = rnd.Next(); } int offset = 0; if ((rnd.Next() % 3 != 0) || (index == 0) && (index == extent)) { switch (rnd.Next() % 10) { case 0: // insert at starting edge index = 0; break; case 1: // insert at trailing edge index = extent - removeCount; break; default: // at random position in middle index = rnd.Next(0, extent - removeCount); break; } } index = Math.Min(extent - removeCount, Math.Max(0, index)); bool valid = true; if (rnd.Next() % 10 == 0) { // make invalid inputs valid = false; switch (rnd.Next() % 7) { default: Debug.Assert(false); throw new InvalidOperationException(); case 0: index = -1; break; case 1: index = extent + 1; break; case 2: insertCount = -1; break; case 3: insertCount++; break; case 4: offset = -1; break; case 5: offset++; break; case 6: items = null; break; case 7: removeCount = -1; break; case 8: removeCount = extent + 1; break; } } else { // array subset if ((rnd.Next() % 3 == 0) && (insertCount > 0)) { insertCount--; if (rnd.Next() % 2 == 0) { offset++; } } } description = String.Format("ReplaceRange [{0}, {1}: {2}]", index, insertCount, valid ? "valid" : "invalid"); script.AppendLine(String.Format("new OpReplaceRange<T>({0}, {1}, {4}, {2}, {3}),", index, removeCount, offset, insertCount, items != null ? String.Format("int[{0}] {{ {1} }}", items.Length, String.Join(", ", items)) : "null")); for (int i = 0; i < lists.Length; i++) { try { lists[i].ReplaceRange(index, removeCount, items, offset, insertCount); if (!valid) { Fault(lists[i], description + " - should have thrown exception"); } } catch (ArgumentOutOfRangeException) when(!valid) { // expected } catch (ArgumentException) when(!valid) { // expected } catch (ArgumentNullException) when(!valid) { // expected } catch (Exception exception) { Fault(lists[i], description + " - threw exception", exception); } } }
private void RemoveRangeAction(IHugeList <int>[] lists, Random rnd, ref string description) { IHugeList <int> reference = lists[0]; int extent = reference.Count; int count = RemoveRangeCountGenerator(rnd, extent); if ((rnd.Next() % 3 != 0) || (index == 0) && (index == extent)) { switch (rnd.Next() % 10) { case 0: // remove at starting edge index = 0; break; case 1: // remove at trailing edge index = extent - count; break; default: // at random position in middle index = rnd.Next(0, extent - count); break; } } index = Math.Min(extent - count, Math.Max(0, index)); bool valid = true; if (rnd.Next() % 10 == 0) { // make invalid inputs valid = false; switch (rnd.Next() % 4) { default: Debug.Assert(false); throw new InvalidOperationException(); case 0: index = -1; break; case 1: index = extent - count + 1; break; case 2: count = -1; break; case 3: count = extent + 1; break; } } description = String.Format("RemoveRange [{0}, {1}: {2}]", index, count, valid ? "valid" : "invalid"); script.AppendLine(String.Format("new OpRemoveRange<T>({0}, {1}),", index, count)); for (int i = 0; i < lists.Length; i++) { try { lists[i].RemoveRange(index, count); if (!valid) { Fault(lists[i], description + " - should have thrown exception"); } } catch (ArgumentOutOfRangeException) when(!valid) { // expected } catch (ArgumentException) when(!valid) { // expected } catch (Exception exception) { Fault(lists[i], description + " - threw exception", exception); } } }
private void InsertRangeHugeListAction(IHugeList <int>[] lists, Random rnd, ref string description) { IHugeList <int> reference = lists[0]; int extent = reference.Count; int insertAt = rnd.Next() % (extent + 1); int chunkSize = rnd.Next() % 20 + 1; int length = rnd.Next() % 50; int offset = length > 0 ? rnd.Next() % length : 0; int count = length - offset > 0 ? rnd.Next() % (length - offset) : 0; HugeList <int> toAdd = new HugeList <int>(chunkSize); List <int> insertionIndices = new List <int>(); for (int i = 0; i < length; i++) { int index = rnd.Next() % (toAdd.Count + 1); // ensure segment lengths come out randomized insertionIndices.Add(index); toAdd.Insert(index, rnd.Next()); } bool valid = true; if (rnd.Next() % 10 == 0) { valid = false; switch (rnd.Next() % 6) { default: Debug.Assert(false); throw new ArgumentException(); case 0: count = length + 1; break; case 1: count = -1; break; case 2: offset = -1; break; case 3: insertAt = -1; break; case 4: insertAt = extent + 1; break; case 5: toAdd = null; break; } } description = String.Format("InsertHugeList ({0}) ({1}, [{2}, {3}, {4}])", valid ? "valid" : "invalid", insertAt, length, offset, count); script.AppendLine(String.Format("new OpInsertHugeList<T>({0}, {1}, {2}, {3}, {4}, new int[{6}] {{ {5} }})", chunkSize, length, insertAt, offset, count, String.Join(", ", insertionIndices), insertionIndices.Count)); for (int i = 0; i < lists.Length; i++) { try { lists[i].InsertRange(insertAt, toAdd, offset, count); } catch (ArgumentNullException) when(!valid) { // expected } catch (ArgumentOutOfRangeException) when(!valid) { // expected } catch (ArgumentException) when(!valid) { // expected } catch (Exception exception) { Fault(lists[i], description + " - threw exception", exception); } } }
public void AddRange(IHugeList <T> collection) { inner.AddRange(collection != null ? new AdaptHugeListLongToHugeList <T>(collection) : null); }