public static void TestDebuggerAttributes_Null() { Type proxyType = DebuggerAttributes.GetProxyType(ImmutableSegmentedList.Create <double>()); TargetInvocationException tie = Assert.Throws <TargetInvocationException>(() => Activator.CreateInstance(proxyType, (object?)null)); Assert.IsType <ArgumentNullException>(tie.InnerException); }
public void RemoveRangeEnumerableTest() { var list = ImmutableSegmentedList.Create(1, 2, 3); Assert.Throws <ArgumentNullException>("items", () => list.RemoveRange(null !)); ImmutableSegmentedList <int> removed2 = list.RemoveRange(new[] { 2 }); Assert.Equal(2, removed2.Count); Assert.Equal(new[] { 1, 3 }, removed2); ImmutableSegmentedList <int> removed13 = list.RemoveRange(new[] { 1, 3, 5 }); Assert.Equal(1, removed13.Count); Assert.Equal(new[] { 2 }, removed13); Assert.Equal(new[] { 2 }, System.Collections.Immutable.ImmutableList.RemoveRange((System.Collections.Immutable.IImmutableList <int>)list, new[] { 1, 3, 5 })); Assert.True(IsSame(list, list.RemoveRange(new[] { 5 }))); Assert.True(IsSame(ImmutableSegmentedList.Create <int>(), ImmutableSegmentedList.Create <int>().RemoveRange(new[] { 1 }))); var listWithDuplicates = ImmutableSegmentedList.Create(1, 2, 2, 3); Assert.Equal(new[] { 1, 2, 3 }, listWithDuplicates.RemoveRange(new[] { 2 })); Assert.Equal(new[] { 1, 3 }, listWithDuplicates.RemoveRange(new[] { 2, 2 })); Assert.Throws <ArgumentNullException>("items", () => System.Collections.Immutable.ImmutableList.RemoveRange((System.Collections.Immutable.IImmutableList <int>)ImmutableSegmentedList.Create(1, 2, 3), null !)); Assert.Equal(new[] { 1, 3 }, System.Collections.Immutable.ImmutableList.RemoveRange((System.Collections.Immutable.IImmutableList <int>)ImmutableSegmentedList.Create(1, 2, 3), new[] { 2 })); }
public void InsertRangeRandomBalanceTest() { int randSeed = unchecked ((int)DateTime.Now.Ticks); Debug.WriteLine("Random seed: {0}", randSeed); var random = new Random(randSeed); var immutableList = ImmutableSegmentedList.CreateBuilder <int>(); var list = new List <int>(); const int maxBatchSize = 32; int valueCounter = 0; for (int i = 0; i < 24; i++) { int startPosition = random.Next(list.Count + 1); int length = random.Next(maxBatchSize + 1); int[] values = new int[length]; for (int j = 0; j < length; j++) { values[j] = ++valueCounter; } immutableList.InsertRange(startPosition, values); list.InsertRange(startPosition, values); Assert.Equal(list, immutableList); } }
public void InsertBalanceTest() { var list = ImmutableSegmentedList.Create(1); list = list.Insert(0, 2); list = list.Insert(1, 3); }
public void CopyToTest() { var list = ImmutableSegmentedList.Create(1, 2); var enumerable = (IEnumerable <int>)list; var array = new int[2]; CopyToImpl(list, array); Assert.Equal(enumerable, array); array = new int[2]; CopyToImpl(list, array, 0); Assert.Equal(enumerable, array); array = new int[2]; CopyToImpl(list, 0, array, 0, list.Count); Assert.Equal(enumerable, array); array = new int[1]; // shorter than source length CopyToImpl(list, 0, array, 0, array.Length); Assert.Equal(enumerable.Take(array.Length), array); array = new int[3]; CopyToImpl(list, 1, array, 2, 1); Assert.Equal(new[] { 0, 0, 2 }, array); array = new int[2]; ((ICollection)GetListQuery(list)).CopyTo(array, 0); Assert.Equal(enumerable, array); }
private ValueTask ProcessTagsChangedAsync( ImmutableSegmentedList <NormalizedSnapshotSpanCollection> snapshotSpans, CancellationToken cancellationToken) { var tagsChanged = this.TagsChanged; if (tagsChanged == null) { return(ValueTaskFactory.CompletedTask); } foreach (var collection in snapshotSpans) { if (collection.Count == 0) { continue; } var snapshot = collection.First().Snapshot; // Coalesce the spans if there are a lot of them. var coalesced = collection.Count > CoalesceDifferenceCount ? new NormalizedSnapshotSpanCollection(snapshot.GetSpanFromBounds(collection.First().Start, collection.Last().End)) : collection; foreach (var span in coalesced) { tagsChanged(this, new SnapshotSpanEventArgs(span)); } } return(ValueTaskFactory.CompletedTask); }
public void GetEnumeratorExplicit() { ICollection <int> builder = ImmutableSegmentedList.Create <int>().ToBuilder(); var enumerator = builder.GetEnumerator(); Assert.NotNull(enumerator); }
private async ValueTask ProcessBatchAsync(ImmutableSegmentedList <WatcherOperation> workItems, CancellationToken cancellationToken) { var service = await _fileChangeService.ConfigureAwait(false); var prior = WatcherOperation.Empty; for (var i = 0; i < workItems.Count; i++) { if (prior.TryCombineWith(workItems[i], out var combined)) { prior = combined; continue; } // The current item can't be combined with the prior item. Process the prior item before marking the // current item as the new prior item. await prior.ApplyAsync(service, cancellationToken).ConfigureAwait(false); prior = workItems[i]; } // The last item is always stored in prior rather than processing it directly. Make sure to process it // before returning from the batch. await prior.ApplyAsync(service, cancellationToken).ConfigureAwait(false); }
public void Reverse() { var mutable = ImmutableSegmentedList.CreateRange(Enumerable.Range(1, 3)).ToBuilder(); mutable.Reverse(); Assert.Equal(Enumerable.Range(1, 3).Reverse(), mutable); }
private void TrueForAllTestHelper <T>(ImmutableSegmentedList <T> list, Predicate <T> test) { var bclList = list.ToList(); var expected = bclList.TrueForAll(test); var actual = TrueForAllImpl(list, test); Assert.Equal(expected, actual); }
public static T Last <T>(this ImmutableSegmentedList <T> immutableList) { // In the event of an empty list, generate the same exception // that the linq extension method would. return(immutableList.Count > 0 ? immutableList[immutableList.Count - 1] : Enumerable.Last(immutableList)); }
public void RemoveRange_EnumerableEqualityComparer_AcceptsNullEQ() { var list = ImmutableSegmentedList.Create(1, 2, 3); var removed2eq = list.RemoveRange(new[] { 2 }, null); Assert.Equal(2, removed2eq.Count); Assert.Equal(new[] { 1, 3 }, removed2eq); }
/// <summary> /// Starts a new task to compute the model based on the current text. /// </summary> private async ValueTask <NavigationBarModel?> ComputeModelAndSelectItemAsync(ImmutableSegmentedList <bool> unused, CancellationToken cancellationToken) { // Jump back to the UI thread to determine what snapshot the user is processing. await _threadingContext.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken); var textSnapshot = _subjectBuffer.CurrentSnapshot; // Ensure we switch to the threadpool before calling GetDocumentWithFrozenPartialSemantics. It ensures // that any IO that performs is not potentially on the UI thread. await TaskScheduler.Default; var model = await ComputeModelAsync().ConfigureAwait(false); // Now, enqueue work to select the right item in this new model. if (model != null) { StartSelectedItemUpdateTask(); } return(model); async Task <NavigationBarModel?> ComputeModelAsync() { // When computing items just get the partial semantics workspace. This will ensure we can get data for this // file, and hopefully have enough loaded to get data for other files in the case of partial types. In the // event the other files aren't available, then partial-type information won't be correct. That's ok though // as this is just something that happens during solution load and will pass once that is over. By using // partial semantics, we can ensure we don't spend an inordinate amount of time computing and using full // compilation data (like skeleton assemblies). var forceFrozenPartialSemanticsForCrossProcessOperations = true; var document = textSnapshot.AsText().GetDocumentWithFrozenPartialSemantics(cancellationToken); if (document == null) { return(null); } var itemService = document.GetLanguageService <INavigationBarItemService>(); if (itemService == null) { return(null); } // If these are navbars for a file that isn't even visible, then avoid doing any unnecessary computation // work until far in teh future (or if visibility changes). This ensures our non-visible docs do settle // once enough time has passed, while greatly reducing their impact on the system. await _visibilityTracker.DelayWhileNonVisibleAsync( _threadingContext, _subjectBuffer, DelayTimeSpan.NonFocus, cancellationToken).ConfigureAwait(false); using (Logger.LogBlock(FunctionId.NavigationBar_ComputeModelAsync, cancellationToken)) { var items = await itemService.GetItemsAsync(document, forceFrozenPartialSemanticsForCrossProcessOperations, textSnapshot.Version, cancellationToken).ConfigureAwait(false); return(new NavigationBarModel(itemService, items)); } } }
public static bool Any <T>(this ImmutableSegmentedList <T> .Builder builder) { if (builder is null) { throw new ArgumentNullException(nameof(builder)); } return(builder.Count > 0); }
public void IndexOf() { IndexOfTests.IndexOfTest( seq => ImmutableSegmentedList.CreateRange(seq).ToBuilder(), (b, v) => b.IndexOf(v), (b, v, i) => b.IndexOf(v, i), (b, v, i, c) => b.IndexOf(v, i, c), (b, v, i, c, eq) => b.IndexOf(v, i, c, eq)); }
public IncludeOperation(IncludeOperationBuilder builder, LazyItemEvaluator <P, I, M, D> lazyEvaluator) : base(builder, lazyEvaluator) { _elementOrder = builder.ElementOrder; _rootDirectory = builder.RootDirectory; _excludes = builder.Excludes.ToImmutable(); _metadata = builder.Metadata.ToImmutable(); }
public static bool Any <T>(this ImmutableSegmentedList <T> immutableList) { if (immutableList.IsDefault) { throw new ArgumentNullException(nameof(immutableList)); } return(!immutableList.IsEmpty); }
public static async ValueTask BatchUpdateCacheAsync(ImmutableSegmentedList <Project> projects, CancellationToken cancellationToken) { var latestProjects = CompletionUtilities.GetDistinctProjectsFromLatestSolutionSnapshot(projects); foreach (var project in latestProjects) { await SymbolComputer.UpdateCacheAsync(project, cancellationToken).ConfigureAwait(false); } }
public void AddRange_IOrderedCollection() { var list = ImmutableSegmentedList <int> .Empty; ImmutableSegmentedList <int> .Builder builder = ImmutableSegmentedList.CreateBuilder <int>(); builder.Add(1); list = list.AddRange(builder); Assert.Equal(new int[] { 1 }, list); }
public void LastIndexOf() { IndexOfTests.LastIndexOfTest( seq => ImmutableSegmentedList.CreateRange(seq).ToBuilder(), (b, v) => b.LastIndexOf(v), (b, v, eq) => b.LastIndexOf(v, b.Count > 0 ? b.Count - 1 : 0, b.Count, eq), (b, v, i) => b.LastIndexOf(v, i), (b, v, i, c) => b.LastIndexOf(v, i, c), (b, v, i, c, eq) => b.LastIndexOf(v, i, c, eq)); }
public void ReverseTest2() { var emptyList = ImmutableSegmentedList.Create <int>(); Assert.True(IsSame(emptyList, emptyList.Reverse())); var populatedList = ImmutableSegmentedList.Create(3, 2, 1); Assert.Equal(Enumerable.Range(1, 3), populatedList.Reverse()); }
public void Clear() { var mutable = ImmutableSegmentedList.CreateRange(Enumerable.Range(1, 3)).ToBuilder(); mutable.Clear(); Assert.Equal(0, mutable.Count); // Do it again for good measure. :) mutable.Clear(); Assert.Equal(0, mutable.Count); }
public static async ValueTask BatchUpdateCacheAsync(ImmutableSegmentedList <Project> projects, CancellationToken cancellationToken) { var latestProjects = CompletionUtilities.GetDistinctProjectsFromLatestSolutionSnapshot(projects); foreach (var project in latestProjects) { cancellationToken.ThrowIfCancellationRequested(); var service = (AbstractTypeImportCompletionService)project.GetRequiredLanguageService <ITypeImportCompletionService>(); _ = await service.GetCacheEntriesAsync(project, forceCacheCreation : true, cancellationToken).ConfigureAwait(false); } }
public void DebuggerAttributesValid() { DebuggerAttributes.ValidateDebuggerDisplayReferences(ImmutableSegmentedList.CreateBuilder <int>()); ImmutableSegmentedList <string> .Builder builder = ImmutableSegmentedList.CreateBuilder <string>(); builder.Add("One"); builder.Add("Two"); DebuggerAttributeInfo info = DebuggerAttributes.ValidateDebuggerTypeProxyProperties(builder); PropertyInfo itemProperty = info.Properties.Single(pr => pr.GetCustomAttribute <DebuggerBrowsableAttribute>() !.State == DebuggerBrowsableState.RootHidden); string[]? items = itemProperty.GetValue(info.Instance) as string[]; Assert.Equal(builder, items); }
/// <summary> /// Asserts that the <see cref="ImmutableSegmentedList{T}"/> or <see cref="ImmutableSegmentedList{T}.Builder"/>'s /// implementation of <see cref="IList"/> behave the same way <see cref="List{T}"/> does. /// </summary> /// <typeparam name="T">The type of the element for one collection to test with.</typeparam> /// <param name="operation"> /// The <see cref="IList"/> operation to perform. /// The function is provided with the <see cref="IList"/> implementation to test /// and the item to use as the argument to the operation. /// The function should return some equatable value by which to compare the effects /// of the operation across <see cref="IList"/> implementations. /// </param> /// <param name="item">The item to add to the collection.</param> /// <param name="other">The item to pass to the <paramref name="operation"/> function as the second parameter.</param> protected void AssertIListBaseline <T>(Func <IList, object?, object> operation, T item, object?other) { IList bclList = new List <T> { item }; IList testedList = (IList)this.GetListQuery(ImmutableSegmentedList.Create(item)); object expected = operation(bclList, other); object actual = operation(testedList, other); Assert.Equal(expected, actual); }
public void IListOfTIsReadOnly() { IList <int> list = ImmutableSegmentedList.Create <int>(); Assert.True(list.IsReadOnly); Assert.Throws <NotSupportedException>(() => list.Add(1)); Assert.Throws <NotSupportedException>(() => list.Clear()); Assert.Throws <NotSupportedException>(() => list.Insert(0, 1)); Assert.Throws <NotSupportedException>(() => list.Remove(1)); Assert.Throws <NotSupportedException>(() => list.RemoveAt(0)); Assert.Throws <NotSupportedException>(() => list[0] = 1); }
public void RemoveRangeArrayTest() { Assert.True(ImmutableSegmentedList <int> .Empty.RemoveRange(0, 0).IsEmpty); var list = ImmutableSegmentedList.Create(1, 2, 3); Assert.Throws <ArgumentOutOfRangeException>("index", () => list.RemoveRange(-1, 0)); Assert.Throws <ArgumentOutOfRangeException>("count", () => list.RemoveRange(0, -1)); Assert.Throws <ArgumentException>(() => list.RemoveRange(4, 0)); Assert.Throws <ArgumentException>(() => list.RemoveRange(0, 4)); Assert.Throws <ArgumentException>(() => list.RemoveRange(2, 2)); Assert.Equal(list, list.RemoveRange(3, 0)); }
public static T Last <T>(this ImmutableSegmentedList <T> .Builder builder) { if (builder is null) { throw new ArgumentNullException(nameof(builder)); } // In the event of an empty list, generate the same exception // that the linq extension method would. return(builder.Count > 0 ? builder[builder.Count - 1] : Enumerable.Last(builder)); }
public void Remove_NullEqualityComparer() { var collection = ImmutableSegmentedList.Create(1, 2, 3); var modified = collection.Remove(2, null); Assert.Equal(new[] { 1, 3 }, modified); // Try again through the explicit interface implementation. System.Collections.Immutable.IImmutableList <int> collectionIface = collection; var modified2 = collectionIface.Remove(2, null); Assert.Equal(new[] { 1, 3 }, modified2); }
public void RemoveTest() { ImmutableSegmentedList <int> list = ImmutableSegmentedList <int> .Empty; for (int i = 1; i <= 10; i++) { list = list.Add(i * 10); } list = list.Remove(30); Assert.Equal(9, list.Count); Assert.False(list.Contains(30)); list = list.Remove(100); Assert.Equal(8, list.Count); Assert.False(list.Contains(100)); list = list.Remove(10); Assert.Equal(7, list.Count); Assert.False(list.Contains(10)); var removeList = new int[] { 20, 70 }; list = list.RemoveAll(removeList.Contains); Assert.Equal(5, list.Count); Assert.False(list.Contains(20)); Assert.False(list.Contains(70)); System.Collections.Immutable.IImmutableList <int> list2 = ImmutableSegmentedList <int> .Empty; for (int i = 1; i <= 10; i++) { list2 = list2.Add(i * 10); } list2 = System.Collections.Immutable.ImmutableList.Remove(list2, 30); Assert.Equal(9, list2.Count); Assert.False(list2.Contains(30)); list2 = System.Collections.Immutable.ImmutableList.Remove(list2, 100); Assert.Equal(8, list2.Count); Assert.False(list2.Contains(100)); list2 = System.Collections.Immutable.ImmutableList.Remove(list2, 10); Assert.Equal(7, list2.Count); Assert.False(list2.Contains(10)); list2 = list2.RemoveAll(removeList.Contains); Assert.Equal(5, list2.Count); Assert.False(list2.Contains(20)); Assert.False(list2.Contains(70)); }