protected static void EnumeratorTestHelper <T>(System.Collections.Immutable.IImmutableSet <T> emptySet, IComparer <T>?comparer, params T[] values) { var set = emptySet; foreach (T value in values) { set = set.Add(value); } var nonGenericEnumerableList = ToListNonGeneric <T>(set); CollectionAssertAreEquivalent(nonGenericEnumerableList, values); var list = set.ToList(); CollectionAssertAreEquivalent(list, values); if (comparer != null) { Array.Sort(values, comparer); Assert.Equal <T>(values, list); } // Apply some less common uses to the enumerator to test its metal. IEnumerator <T> enumerator; using (enumerator = set.GetEnumerator()) { Assert.Equal(default, enumerator.Current);
/// <summary> /// Tests various aspects of a set. This should be called only from the unordered or sorted overloads of this method. /// </summary> /// <typeparam name="T">The type of element stored in the set.</typeparam> /// <param name="emptySet">The empty set.</param> protected static void EmptyTestHelper <T>(System.Collections.Immutable.IImmutableSet <T> emptySet) { Assert.NotNull(emptySet); Assert.Equal(0, emptySet.Count); //, "Empty set should have a Count of 0"); Assert.Equal(0, emptySet.Count()); //, "Enumeration of an empty set yielded elements."); Assert.True(IsSame(emptySet, emptySet.Clear())); }
/// <inheritdoc/> public IImmutableSet <T> Add(T value) { System.Collections.Immutable.IImmutableSet <T> newSet = this.Set.Add(value); if (object.ReferenceEquals(newSet, this.Set)) { return(this); } else { return(new ImmutableSetAdapter <T>(newSet)); } }
/// <inheritdoc/> public IImmutableSet <T> Union(IEnumerable <T> other) { System.Collections.Immutable.IImmutableSet <T> newSet = this.Set.Union(other); if (object.ReferenceEquals(newSet, this.Set)) { return(this); } else { return(new ImmutableSetAdapter <T>(newSet)); } }
protected static IComparer <T>?GetComparer <T>(System.Collections.Immutable.IImmutableSet <T> set) { return(set switch { // Unordered collections ImmutableSegmentedHashSet <T> => null, System.Collections.Immutable.ImmutableHashSet <T> => null, // Ordered collections System.Collections.Immutable.ImmutableSortedSet <T> s => s.KeyComparer, // Unknown _ => throw ExceptionUtilities.UnexpectedValue(set), });
/// <summary> /// Tests various aspects of an unordered set. /// </summary> /// <typeparam name="T">The type of element stored in the set.</typeparam> /// <param name="emptySet">The empty set.</param> /// <param name="value">A value that could be placed in the set.</param> /// <param name="comparer">The comparer used to obtain the empty set, if any.</param> private static void EmptyTestHelper <T>(System.Collections.Immutable.IImmutableSet <T> emptySet, T value, IEqualityComparer <T>?comparer) { Assert.NotNull(emptySet); EmptyTestHelper(emptySet); Assert.True(IsSame(emptySet, emptySet.ToImmutableSegmentedHashSet(comparer))); Assert.Same(comparer ?? EqualityComparer <T> .Default, GetEqualityComparer(emptySet)); if (comparer == null) { Assert.True(IsSame(emptySet, ImmutableSegmentedHashSet <T> .Empty)); } var reemptied = emptySet.Add(value).Clear(); Assert.True(IsSame(reemptied, reemptied.ToImmutableSegmentedHashSet(comparer))); //, "Getting the empty set from a non-empty instance did not preserve the comparer."); }
public Process(Configuration.MonitorElement configuration, System.Action <System.String> log) : base() { if (null == configuration) { throw new System.ArgumentNullException("configuration"); } myLog = log; this.DestinationPath = configuration.Destination; this.Interval = configuration.Interval * 1000; myTimer = new System.Threading.Timer(this.OnAlarm, this, -1, 0); myFilePathName = System.Collections.Immutable.ImmutableHashSet <System.String> .Empty; myState = 0; myFileSystemWatcher = System.Collections.Immutable.ImmutableHashSet <System.IO.FileSystemWatcher> .Empty; var exec = configuration.Execute; if (null != exec) { this.Arguments = exec.Arguments; this.PathName = exec.Executable; this.WorkingDirectory = exec.WorkingDirectory; } System.IO.FileSystemWatcher fsw; foreach (var path in configuration.Paths.OfType <Configuration.PathElement>()) { foreach (var filter in path.Filters.OfType <Configuration.FilterElement>()) { try { fsw = new System.IO.FileSystemWatcher(System.Environment.ExpandEnvironmentVariables(path.Path), System.Environment.ExpandEnvironmentVariables(filter.Filter)); } catch (System.Exception e) { myLog(System.String.Format("Exception: {0}\r\n{1}\r\nStack Trace follows:\r\n{2}", e.GetType().ToString(), e.Message, e.StackTrace)); throw; } fsw.IncludeSubdirectories = false; fsw.Created += this.OnCreated; fsw.Renamed += this.OnRename; fsw.Deleted += this.OnDeleted; fsw.NotifyFilter = (System.IO.NotifyFilters.CreationTime | System.IO.NotifyFilters.FileName | System.IO.NotifyFilters.LastWrite | System.IO.NotifyFilters.Size); myFileSystemWatcher = myFileSystemWatcher.Add(fsw); } } }
public void RemoveNonExistingTest() { System.Collections.Immutable.IImmutableSet <int> emptySet = this.Empty <int>(); Assert.True(IsSame(emptySet, emptySet.Remove(5))); // Also fill up a set with many elements to build up the tree, then remove from various places in the tree. const int Size = 200; var set = emptySet; for (int i = 0; i < Size; i += 2) { // only even numbers! set = set.Add(i); } // Verify that removing odd numbers doesn't change anything. for (int i = 1; i < Size; i += 2) { var setAfterRemoval = set.Remove(i); Assert.True(IsSame(set, setAfterRemoval)); } }
protected static void CustomSortTestHelper <T>(System.Collections.Immutable.IImmutableSet <T> emptySet, bool matchOrder, T[] injectedValues, T[] expectedValues) { Assert.NotNull(emptySet); Assert.NotNull(injectedValues); Assert.NotNull(expectedValues); var set = emptySet; foreach (T value in injectedValues) { set = set.Add(value); } Assert.Equal(expectedValues.Length, set.Count); if (matchOrder) { Assert.Equal <T>(expectedValues, set.ToList()); } else { CollectionAssertAreEquivalent(expectedValues, set.ToList()); } }
private static void AddRemoveLoadTestHelper <T>(System.Collections.Immutable.IImmutableSet <T> set, T[] data) { Assert.NotNull(set); Assert.NotNull(data); foreach (T value in data) { var newSet = set.Add(value); Assert.NotSame(set, newSet); set = newSet; } foreach (T value in data) { Assert.True(set.Contains(value)); } foreach (T value in data) { var newSet = set.Remove(value); Assert.NotSame(set, newSet); set = newSet; } }
private static void RemoveTestHelper <T>(System.Collections.Immutable.IImmutableSet <T> set, params T[] values) { Assert.NotNull(set); Assert.NotNull(values); Assert.True(IsSame(set, set.Except(Enumerable.Empty <T>()))); int initialCount = set.Count; int removedCount = 0; foreach (T value in values) { var nextSet = set.Remove(value); Assert.NotSame(set, nextSet); Assert.Equal(initialCount - removedCount, set.Count); Assert.Equal(initialCount - removedCount - 1, nextSet.Count); Assert.True(IsSame(nextSet, nextSet.Remove(value))); //, "Removing a non-existing element should not change the set reference."); removedCount++; set = nextSet; } Assert.Equal(initialCount - removedCount, set.Count); }
public MockSystemImmutableSet(System.Collections.Immutable.IImmutableSet <T> set) : this( set.Add, set.Clear, set.Contains, () => set.Count, set.Equals, set.Except, set.GetEnumerator, set.GetHashCode, set.Intersect, set.IsProperSubsetOf, set.IsProperSupersetOf, set.IsSubsetOf, set.IsSupersetOf, set.Overlaps, set.Remove, set.SetEquals, set.SymmetricExcept, set.ToString, set.TryGetValue, set.Union) { }
/// <summary> /// Initializes a new instance of the <see cref="ImmutableSetAdapter{T}"/> class. /// </summary> /// <param name="set"> /// The set to adapt. /// </param> public ImmutableSetAdapter(System.Collections.Immutable.IImmutableSet <T> set) { this.Set = set ?? throw new ArgumentNullException(nameof(set)); this.clearSet = ImmutableSetAdapter <T> .InitialClearSetValue; }
/// <inheritdoc/> public bool Equals(System.Collections.Immutable.IImmutableSet <T> other) { return(this.Set.Equals(other)); }