public void ItemRemove_Test() { treeDict = new TreeDictionary <int, int>(true); treeDict.Add(0, 44); treeDict.Add(1, 55); treeDict.Add(2, 66); treeDict.Add(3, 99); treeDict.Add(3, 101); treeDict.Add(4, 88); treeDict.Add(5, 77); treeDict.Add(8, 45); treeDict.Add(9, 34); treeDict.Remove(new KeyValuePair <int, int>(8, 45)); treeDict.Remove(new KeyValuePair <int, int>(3, 99)); treeDict.Remove(new KeyValuePair <int, int>(9, 34)); Assert.AreEqual(6, treeDict.Count); Assert.AreEqual(false, treeDict.ContainsKey(6), "Contains"); Assert.AreEqual(true, treeDict.Contains(new KeyValuePair <int, int>(0, 44))); Assert.AreEqual(true, treeDict.Contains(new KeyValuePair <int, int>(1, 55))); Assert.AreEqual(false, treeDict.Contains(new KeyValuePair <int, int>(8, 45)), "8, 45"); Assert.AreEqual(false, treeDict.Contains(new KeyValuePair <int, int>(3, 99)), "3, 99"); Assert.AreEqual(false, treeDict.Contains(new KeyValuePair <int, int>(9, 34)), "9, 34"); }
/* * this whole thing is meant for fully qualified type names * since c# has inner types, both A and A.B could be a valid target for a completion with payload * -> an optional payload on inner nodes must be supported * -> leafs must be non-empty */ public bool Add(SCG.IEnumerable <string> edges, Pay payload) { // TODO: exception on null payload, want null to be used internally and get away with not doing the maybe<T> dance if (!edges.Any()) { throw new InterfaceE(); } if (edges.Any(e => string.IsNullOrEmpty(e) /*|| string.IsNullOrWhiteSpace(e)*/)) { throw new InterfaceE(); // <- empty strings as keys destroy pretty much all assumptions } // TODO !!! ^._____ not available in Unity string first = edges.First(); var Rest = edges.Skip(1); if (!Rest.Any()) { return(AddLeaf(first, payload)); } if (!D.Contains(first)) { D[first] = new SuggestionTree <Pay>(); } // D[first] is guaranteed to exist at this point, but it might be a leaf -> convert to inner node // TODO this is prob. still not enough, depending on how this is supposed to behave upon input in which a path is duplicate // for example when simulating "using directives", overrriding paths and even replacing whole subtrees must be supported // ( das braucht wahrscheinlich einen Join( SuggTree1 , SuggTree2 ) siehe lustig bunte A4 blaetter ) if (D[first] is SuggestionLeaf <Pay> ) { var nu_tree = new SuggestionTree <Pay>(); nu_tree.payload = D[first].payload; D[first] = nu_tree; } return(D[first].Add(Rest, payload)); }
public void AddPair_Test() { KeyValuePair <int, int> pairOne = new KeyValuePair <int, int>(0, 00); KeyValuePair <int, int> pairTwo = new KeyValuePair <int, int>(1, 11); KeyValuePair <int, int> pairThree = new KeyValuePair <int, int>(2, 22); treeDict.Add(pairOne); treeDict.Add(pairTwo); treeDict.Add(pairThree); Assert.AreEqual(true, treeDict.Contains(pairOne)); Assert.AreEqual(true, treeDict.Contains(pairTwo)); Assert.AreEqual(true, treeDict.Contains(pairThree)); }
public void Add(TChild item) { var propValue = (TProperty)_propertyReader.ReadValue(item); if (_index.Contains(propValue)) { _index[propValue].Add(item); } else { _index.Add(propValue, new List <TChild> { item }); } }
static IDictionary <String, TreeSet <int> > IndexFile(String filename) { IDictionary <String, TreeSet <int> > index = new TreeDictionary <String, TreeSet <int> >(); Regex delim = new Regex("[^a-zA-Z0-9]+"); using (TextReader rd = new StreamReader(filename)) { int lineno = 0; for (String line = rd.ReadLine(); line != null; line = rd.ReadLine()) { String[] res = delim.Split(line); lineno++; foreach (String s in res) { if (s != "") { if (!index.Contains(s)) { index[s] = new TreeSet <int>(); } index[s].Add(lineno); } } } } return(index); }
public void TestEmptyDictionary() { var dictionary = new TreeDictionary <int, int>(); TreeDictionary <int, int> .KeyCollection keys = dictionary.Keys; TreeDictionary <int, int> .ValueCollection values = dictionary.Values; Assert.Empty(dictionary); Assert.Empty(keys); Assert.Empty(values); #pragma warning disable xUnit2013 // Do not use equality check to check for collection size. Assert.Equal(0, dictionary.Count); Assert.Equal(0, keys.Count); Assert.Equal(0, values.Count); #pragma warning restore xUnit2013 // Do not use equality check to check for collection size. Assert.False(dictionary.ContainsKey(0)); Assert.False(dictionary.ContainsValue(0)); Assert.False(dictionary.TryGetValue(0, out _)); Assert.Throws <KeyNotFoundException>(() => dictionary[0]); #pragma warning disable xUnit2017 // Do not use Contains() to check if a value exists in a collection Assert.False(keys.Contains(0)); Assert.False(values.Contains(0)); #pragma warning restore xUnit2017 // Do not use Contains() to check if a value exists in a collection }
private static IDictionary <string, TreeSet <int> > IndexFile(string filename) { var index = new TreeDictionary <string, TreeSet <int> >(); var delimiter = new Regex("[^a-zA-Z0-9]+"); using (var reader = File.OpenText(filename)) { var lineNumber = 0; for (var line = reader.ReadLine(); line != null; line = reader.ReadLine()) { var res = delimiter.Split(line); lineNumber++; foreach (var s in res) { if (s != "") { if (!index.Contains(s)) { index[s] = new TreeSet <int>(); } index[s].Add(lineNumber); } } } } return(index); }
public void KeyRemove_Test() { treeDict = new TreeDictionary <int, int>(true); treeDict.Add(0, 44); treeDict.Add(1, 55); treeDict.Add(1, 56); treeDict.Add(2, 66); treeDict.Add(3, 99); treeDict.Remove(1); Assert.AreEqual(3, treeDict.Count); Assert.AreEqual(true, treeDict.ContainsKey(2)); Assert.AreEqual(false, treeDict.Contains(new KeyValuePair <int, int>(3, 98))); Assert.AreEqual(true, treeDict.Contains(new KeyValuePair <int, int>(3, 99))); Assert.AreEqual(66, treeDict[2]); }
public void Add(T item) { Q key = toKey(item); if (!dict.Contains(key)) { dict.Add(key, new HashSet <T>(ReferenceEqualityComparer <T> .Default)); } dict[key].Add(item); }
public void Add(T item) { var key = _toKey(item); if (!_dictionary.Contains(key)) { _dictionary.Add(key, new HashSet <T>(EqualityComparer <T> .Default)); } _dictionary[key].Add(item); }
static IDictionary<String, TreeSet<int>> IndexFile(String filename) { IDictionary<String, TreeSet<int>> index = new TreeDictionary<String, TreeSet<int>>(); Regex delim = new Regex("[^a-zA-Z0-9]+"); using (TextReader rd = new StreamReader(filename)) { int lineno = 0; for (String line = rd.ReadLine(); line != null; line = rd.ReadLine()) { String[] res = delim.Split(line); lineno++; foreach (String s in res) if (s != "") { if (!index.Contains(s)) index[s] = new TreeSet<int>(); index[s].Add(lineno); } } } return index; }
public void Contains() { dict.Add("C", "D"); Assert.IsTrue(dict.Contains("C")); Assert.IsFalse(dict.Contains("D")); }
bool Exists(T Key) { return(ColumnMapper.Contains(Key)); }
public void TestIDictionaryT() { IDictionary <int, int> dictionary = new TreeDictionary <int, int>(branchingFactor: 4); for (int i = 0; i < 10; i++) { dictionary.Add(i, i); } Assert.Equal(10, dictionary.Count); Assert.False(dictionary.IsReadOnly); Assert.Equal(10, dictionary.Keys.Count); Assert.False(dictionary.Keys.IsReadOnly); Assert.Equal(10, dictionary.Values.Count); Assert.False(dictionary.Values.IsReadOnly); Assert.Equal(Enumerable.Range(0, 10), dictionary.Keys); Assert.Equal(Enumerable.Range(0, 10), dictionary.Values); Assert.Throws <NotSupportedException>(() => dictionary.Values.Remove(9)); Assert.False(dictionary.Keys.Remove(10)); Assert.True(dictionary.Keys.Remove(9)); Assert.Equal(9, dictionary.Count); Assert.Equal(9, dictionary.Keys.Count); Assert.False(dictionary.Keys.IsReadOnly); Assert.Equal(9, dictionary.Values.Count); Assert.False(dictionary.Values.IsReadOnly); Assert.Equal(Enumerable.Range(0, 9), dictionary.Keys); Assert.Equal(Enumerable.Range(0, 9), dictionary.Values); Assert.Throws <NotSupportedException>(() => dictionary.Keys.Add(0)); Assert.Throws <ArgumentNullException>("array", () => dictionary.Keys.CopyTo(null !, 0)); Assert.Throws <ArgumentOutOfRangeException>("arrayIndex", () => dictionary.Keys.CopyTo(new int[dictionary.Count], -1)); Assert.Throws <ArgumentOutOfRangeException>("arrayIndex", () => dictionary.Keys.CopyTo(new int[dictionary.Count], dictionary.Count + 1)); Assert.Throws <ArgumentException>(() => dictionary.Keys.CopyTo(new int[dictionary.Count], 1)); IEnumerator <int> keyEnumerator = dictionary.Keys.GetEnumerator(); Assert.NotNull(keyEnumerator); Assert.True(keyEnumerator.MoveNext()); Assert.Equal(0, keyEnumerator.Current); Assert.True(keyEnumerator.MoveNext()); Assert.Equal(1, keyEnumerator.Current); keyEnumerator.Reset(); Assert.True(keyEnumerator.MoveNext()); Assert.Equal(0, keyEnumerator.Current); Assert.Throws <NotSupportedException>(() => dictionary.Values.Add(0)); Assert.Throws <ArgumentNullException>("array", () => dictionary.Values.CopyTo(null !, 0)); Assert.Throws <ArgumentOutOfRangeException>("arrayIndex", () => dictionary.Values.CopyTo(new int[dictionary.Count], -1)); Assert.Throws <ArgumentOutOfRangeException>("arrayIndex", () => dictionary.Values.CopyTo(new int[dictionary.Count], dictionary.Count + 1)); Assert.Throws <ArgumentException>(() => dictionary.Values.CopyTo(new int[dictionary.Count], 1)); IEnumerator <int> valueEnumerator = dictionary.Values.GetEnumerator(); Assert.NotNull(valueEnumerator); Assert.True(valueEnumerator.MoveNext()); Assert.Equal(0, valueEnumerator.Current); Assert.True(valueEnumerator.MoveNext()); Assert.Equal(1, valueEnumerator.Current); valueEnumerator.Reset(); Assert.True(valueEnumerator.MoveNext()); Assert.Equal(0, valueEnumerator.Current); IReadOnlyDictionary <int, int> readOnlyDictionary = (IReadOnlyDictionary <int, int>)dictionary; Assert.Equal(dictionary.Keys, readOnlyDictionary.Keys); Assert.Equal(dictionary.Values, readOnlyDictionary.Values); dictionary.Add(new KeyValuePair <int, int>(11, 11)); Assert.Equal(11, dictionary[11]); Assert.True(dictionary.Contains(new KeyValuePair <int, int>(11, 11))); Assert.False(dictionary.Contains(new KeyValuePair <int, int>(11, 12))); Assert.False(dictionary.Contains(new KeyValuePair <int, int>(12, 12))); Assert.False(dictionary.Remove(new KeyValuePair <int, int>(11, 12))); Assert.True(dictionary.Contains(new KeyValuePair <int, int>(11, 11))); Assert.True(dictionary.Remove(new KeyValuePair <int, int>(11, 11))); Assert.False(dictionary.Contains(new KeyValuePair <int, int>(11, 11))); Assert.NotEmpty(dictionary); dictionary.Keys.Clear(); Assert.Empty(dictionary); Assert.Empty(dictionary.Keys); Assert.Empty(dictionary.Values); dictionary[0] = 1; Assert.NotEmpty(dictionary); dictionary.Values.Clear(); Assert.Empty(dictionary); Assert.Empty(dictionary.Keys); Assert.Empty(dictionary.Values); }
public void TestIDictionary() { IDictionary dictionary = new TreeDictionary <int, int>(branchingFactor: 4); Assert.False(dictionary.IsFixedSize); Assert.False(dictionary.IsReadOnly); Assert.False(dictionary.IsSynchronized); Assert.Throws <ArgumentNullException>("key", () => dictionary.Add(key: null, value: 1)); Assert.Throws <ArgumentException>("value", () => dictionary.Add(key: 1, value: null)); Assert.Throws <ArgumentException>("key", () => dictionary.Add(key: "string value", value: 0)); Assert.Throws <ArgumentException>("value", () => dictionary.Add(key: 0, value: "string value")); for (int i = 0; i < 11; i++) { dictionary.Add(i, i + 1); } Assert.Throws <ArgumentNullException>("key", () => dictionary[key: null]); Assert.Null(dictionary["string key"]); Assert.Equal(11, dictionary[10]); Assert.Throws <ArgumentNullException>("key", () => dictionary[key: null] = 12); Assert.Throws <ArgumentException>("key", () => dictionary["string key"] = 12); Assert.Throws <ArgumentException>("value", () => dictionary[10] = null); Assert.Throws <ArgumentException>("value", () => dictionary[10] = "string value"); dictionary[10] = 12; Assert.Equal(12, dictionary[10]); dictionary[10] = 11; TestCollection(dictionary, i => new KeyValuePair <int, int>(i, i + 1)); var entries = new DictionaryEntry[dictionary.Count]; dictionary.CopyTo(entries, 0); Assert.Equal(entries.Select(i => i.Key), dictionary.Keys.Cast <object>()); Assert.Equal(entries.Select(i => i.Value), dictionary.Values.Cast <object>()); Assert.Throws <ArgumentNullException>(() => dictionary.Contains(null)); Assert.False(dictionary.Contains("string value")); Assert.True(dictionary.Contains(10)); Assert.Throws <ArgumentNullException>(() => dictionary.Remove(null)); Assert.Equal(11, dictionary.Count); dictionary.Remove("string value"); Assert.Equal(11, dictionary.Count); dictionary.Remove(10); Assert.Equal(10, dictionary.Count); Assert.False(dictionary.Contains(10)); IDictionaryEnumerator enumerator = dictionary.GetEnumerator(); Assert.NotNull(enumerator); Assert.True(enumerator.MoveNext()); Assert.Equal(0, enumerator.Key); Assert.Equal(1, enumerator.Value); Assert.Equal(enumerator.Key, enumerator.Entry.Key); Assert.Equal(enumerator.Value, enumerator.Entry.Value); Assert.Equal(enumerator.Entry, enumerator.Current); Assert.True(enumerator.MoveNext()); Assert.Equal(1, enumerator.Key); Assert.Equal(2, enumerator.Value); Assert.Equal(enumerator.Key, enumerator.Entry.Key); Assert.Equal(enumerator.Value, enumerator.Entry.Value); Assert.Equal(enumerator.Entry, enumerator.Current); enumerator.Reset(); Assert.True(enumerator.MoveNext()); Assert.Equal(0, enumerator.Key); Assert.Equal(1, enumerator.Value); Assert.Equal(enumerator.Key, enumerator.Entry.Key); Assert.Equal(enumerator.Value, enumerator.Entry.Value); Assert.Equal(enumerator.Entry, enumerator.Current); ICollection keys = dictionary.Keys; TestCollection(keys, i => i); ICollection values = dictionary.Values; TestCollection(values, i => i + 1); dictionary.Clear(); Assert.Empty(dictionary); Assert.Empty(keys); Assert.Empty(values); void TestCollection <T>(ICollection collection, Func <int, T> indexToExpectedValue) { Assert.NotNull(collection); Assert.False(collection.IsSynchronized); Assert.Same(dictionary.SyncRoot, collection.SyncRoot); Assert.Throws <ArgumentNullException>("array", () => collection.CopyTo(null, 0)); Assert.Throws <ArgumentException>(() => collection.CopyTo(new int[collection.Count, 1], 0)); Assert.Throws <ArgumentException>(() => collection.CopyTo(Array.CreateInstance(typeof(int), lengths: new[] { collection.Count }, lowerBounds: new[] { 1 }), 0)); Assert.Throws <ArgumentOutOfRangeException>("index", () => collection.CopyTo(new int[collection.Count], -1)); Assert.Throws <ArgumentOutOfRangeException>("index", () => collection.CopyTo(new int[collection.Count], collection.Count + 1)); Assert.Throws <ArgumentException>(() => collection.CopyTo(new int[collection.Count], 1)); var elements = new T[collection.Count]; collection.CopyTo(elements, 0); Assert.Equal(elements, collection); var objects = new object[collection.Count]; collection.CopyTo(objects, 0); Assert.Equal(objects, collection); Assert.Throws <ArgumentException>(() => collection.CopyTo(new string[collection.Count], 0)); Assert.Throws <ArgumentException>(() => collection.CopyTo(new byte[collection.Count], 0)); IEnumerator collectionEnumerator = collection.GetEnumerator(); Assert.NotNull(collectionEnumerator); Assert.True(collectionEnumerator.MoveNext()); Assert.Equal(indexToExpectedValue(0), collectionEnumerator.Current); Assert.True(collectionEnumerator.MoveNext()); Assert.Equal(indexToExpectedValue(1), collectionEnumerator.Current); collectionEnumerator.Reset(); Assert.True(collectionEnumerator.MoveNext()); Assert.Equal(indexToExpectedValue(0), collectionEnumerator.Current); } }