|
| 1 | +// Copyright (c) Microsoft. All rights reserved. |
| 2 | +// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 3 | + |
| 4 | +using System.Collections; |
| 5 | +using System.Linq; |
| 6 | +using Xunit; |
| 7 | + |
| 8 | +namespace System.Diagnostics.ProcessTests |
| 9 | +{ |
| 10 | + public class ProcessCollectionTests : ProcessTestBase |
| 11 | + { |
| 12 | + [Fact] |
| 13 | + public void TestModuleCollectionBehavior() |
| 14 | + { |
| 15 | + ProcessModule[] mArray = _process.Modules.Cast<ProcessModule>().ToArray(); |
| 16 | + |
| 17 | + // Constructor |
| 18 | + ProcessModuleCollection moduleCollection = new ProcessModuleCollection(mArray); |
| 19 | + |
| 20 | + // Count |
| 21 | + Assert.Equal(mArray.Count(), moduleCollection.Count); |
| 22 | + |
| 23 | + // get_item, Contains, IndexOf |
| 24 | + for (int i = 0; i < mArray.Count(); i++) |
| 25 | + { |
| 26 | + Assert.Equal(mArray[i], moduleCollection[i]); |
| 27 | + Assert.True(moduleCollection.Contains(mArray[i])); |
| 28 | + Assert.Equal(i, moduleCollection.IndexOf(mArray[i])); |
| 29 | + } |
| 30 | + |
| 31 | + // CopyTo |
| 32 | + ProcessModule[] moduleArray = new ProcessModule[moduleCollection.Count + 1]; |
| 33 | + moduleCollection.CopyTo(moduleArray, 1); |
| 34 | + for (int i = 0; i < mArray.Count(); i++) |
| 35 | + { |
| 36 | + Assert.Equal(mArray[i], moduleArray[i + 1]); |
| 37 | + } |
| 38 | + |
| 39 | + Assert.Throws<ArgumentOutOfRangeException>(() => moduleCollection.CopyTo(moduleArray, -1)); |
| 40 | + |
| 41 | + // Explicit interface implementations |
| 42 | + Assert.False(((ICollection)moduleCollection).IsSynchronized); |
| 43 | + Assert.NotNull(((ICollection)moduleCollection).SyncRoot); |
| 44 | + |
| 45 | + moduleArray = new ProcessModule[moduleCollection.Count]; |
| 46 | + ((ICollection)moduleCollection).CopyTo(moduleArray, 0); |
| 47 | + Assert.Equal(moduleCollection.Cast<ProcessModule>().ToArray(), moduleArray); |
| 48 | + |
| 49 | + // GetEnumerator |
| 50 | + IEnumerator enumerator = moduleCollection.GetEnumerator(); |
| 51 | + Assert.Throws<InvalidOperationException>(() => enumerator.Current); |
| 52 | + |
| 53 | + for (int i = 0; i < moduleCollection.Count; i++) |
| 54 | + { |
| 55 | + enumerator.MoveNext(); |
| 56 | + Assert.Equal(moduleCollection[i], enumerator.Current); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + [Fact] |
| 61 | + public void TestThreadCollectionBehavior() |
| 62 | + { |
| 63 | + ProcessThread[] tArray = _process.Threads.Cast<ProcessThread>().ToArray(); |
| 64 | + int countOfTArray = tArray.Count(); |
| 65 | + |
| 66 | + // constructor |
| 67 | + ProcessThreadCollection threadCollection = new ProcessThreadCollection(tArray); |
| 68 | + |
| 69 | + // Count |
| 70 | + Assert.Equal(countOfTArray, threadCollection.Count); |
| 71 | + |
| 72 | + // get_item, Contains, IndexOf |
| 73 | + for (int i = 0; i < countOfTArray; i++) |
| 74 | + { |
| 75 | + Assert.Equal(tArray[i], threadCollection[i]); |
| 76 | + Assert.True(threadCollection.Contains(tArray[i])); |
| 77 | + Assert.Equal(i, threadCollection.IndexOf(tArray[i])); |
| 78 | + } |
| 79 | + |
| 80 | + // CopyTo |
| 81 | + ProcessThread[] threadArray = new ProcessThread[threadCollection.Count + 1]; |
| 82 | + threadCollection.CopyTo(threadArray, 1); |
| 83 | + for (int i = 0; i < countOfTArray; i++) |
| 84 | + { |
| 85 | + Assert.Equal(tArray[i], threadArray[i + 1]); |
| 86 | + } |
| 87 | + |
| 88 | + Assert.Throws<ArgumentOutOfRangeException>(() => threadCollection.CopyTo(threadArray, -1)); |
| 89 | + |
| 90 | + // Remove |
| 91 | + threadCollection.Remove(tArray[0]); |
| 92 | + Assert.Equal(-1, threadCollection.IndexOf(tArray[0])); |
| 93 | + Assert.False(threadCollection.Contains(tArray[0])); |
| 94 | + // Try remove non existent member |
| 95 | + threadCollection.Remove(tArray[0]); |
| 96 | + // Cleanup after remove |
| 97 | + threadCollection.Insert(0, tArray[0]); |
| 98 | + |
| 99 | + // Add |
| 100 | + threadCollection.Add(default(ProcessThread)); |
| 101 | + Assert.Equal(threadCollection.Count - 1, threadCollection.IndexOf(default(ProcessThread))); |
| 102 | + // Add same member again |
| 103 | + threadCollection.Add(default(ProcessThread)); |
| 104 | + Assert.Equal(threadCollection.Count - 2, threadCollection.IndexOf(default(ProcessThread))); |
| 105 | + Assert.Equal(default(ProcessThread), threadCollection[threadCollection.Count - 1]); |
| 106 | + // Cleanup after Add. |
| 107 | + threadCollection.Remove(default(ProcessThread)); |
| 108 | + threadCollection.Remove(default(ProcessThread)); |
| 109 | + Assert.False(threadCollection.Contains(default(ProcessThread))); |
| 110 | + |
| 111 | + // Insert |
| 112 | + int index = threadCollection.Count / 2; |
| 113 | + int initialCount = threadCollection.Count; |
| 114 | + threadCollection.Insert(index, null); |
| 115 | + Assert.Equal(index, threadCollection.IndexOf(null)); |
| 116 | + Assert.Equal(initialCount + 1, threadCollection.Count); |
| 117 | + // Insert at invalid index |
| 118 | + Assert.Throws<ArgumentOutOfRangeException>(() => threadCollection.Insert(-1, tArray[0])); |
| 119 | + |
| 120 | + // Explicit interface implementations |
| 121 | + Assert.False(((ICollection)threadCollection).IsSynchronized); |
| 122 | + Assert.NotNull(((ICollection)threadCollection).SyncRoot); |
| 123 | + |
| 124 | + threadArray = new ProcessThread[threadCollection.Count]; |
| 125 | + ((ICollection)threadCollection).CopyTo(threadArray, 0); |
| 126 | + Assert.Equal(threadCollection.Cast<ProcessThread>().ToArray(), threadArray); |
| 127 | + |
| 128 | + // GetEnumerator |
| 129 | + IEnumerator enumerator = threadCollection.GetEnumerator(); |
| 130 | + Assert.Throws<InvalidOperationException>(() => enumerator.Current); |
| 131 | + |
| 132 | + for (int i = 0; i < threadCollection.Count; i++) |
| 133 | + { |
| 134 | + enumerator.MoveNext(); |
| 135 | + Assert.Equal(threadCollection[i], enumerator.Current); |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments