public void MapStringInsertion() { VersioningDictionary <string, int> stringMap = VersioningDictionary <string, int> .Create(CompareStrings); stringMap = stringMap.Set("Fred", 1); stringMap = stringMap.Set("Wilma", 2); stringMap = stringMap.Set("Betty", 3); stringMap = stringMap.Set("Barney", 4); Assert.IsTrue(stringMap is VersioningDictionary <string, int> .TreeDictionary, "Dictionary type"); Assert.AreEqual(stringMap.Get("Fred"), 1, "Key value"); Assert.AreEqual(stringMap.Get("Barney"), 4, "Key value"); Assert.AreEqual(stringMap.Get("Betty"), 3, "Key value"); Assert.AreEqual(stringMap.Get("Wilma"), 2, "Key value"); for (int i = 1; i < 1000; i++) { stringMap = stringMap.Set("Fred" + i, i + 1); stringMap = stringMap.Set("Wilma" + i, i + 2); stringMap = stringMap.Set("Betty" + i, i + 3); stringMap = stringMap.Set("Barney" + i, i + 4); } Assert.IsTrue(stringMap is VersioningDictionary <string, int> .HashTreeDictionary, "Dictionary type"); Assert.AreEqual(stringMap.Get("Fred"), 1, "Key value"); Assert.AreEqual(stringMap.Get("Barney"), 4, "Key value"); Assert.AreEqual(stringMap.Get("Betty"), 3, "Key value"); Assert.AreEqual(stringMap.Get("Wilma"), 2, "Key value"); for (int i = 1; i < 1000; i++) { Assert.AreEqual(stringMap.Get("Fred" + i), i + 1, "Key value"); Assert.AreEqual(stringMap.Get("Wilma" + i), i + 2, "Key value"); Assert.AreEqual(stringMap.Get("Betty" + i), i + 3, "Key value"); Assert.AreEqual(stringMap.Get("Barney" + i), i + 4, "Key value"); } }
private void SetAnnotationValue(IEdmElement element, string namespaceName, string localName, object value, ref VersioningDictionary <IEdmElement, object> annotationsDictionary) { object transientAnnotations = EdmDirectValueAnnotationsManager.GetTransientAnnotations(element, annotationsDictionary); object obj = transientAnnotations; EdmDirectValueAnnotationsManager.SetAnnotation(this.GetAttachedAnnotations(element), ref transientAnnotations, namespaceName, localName, value); if (transientAnnotations != obj) { annotationsDictionary = annotationsDictionary.Set(element, transientAnnotations); } }
private void SetAnnotationValue(IEdmElement element, string namespaceName, string localName, object value, ref VersioningDictionary <IEdmElement, object> annotationsDictionary) { object transientAnnotations = GetTransientAnnotations(element, annotationsDictionary); object transientAnnotationsBeforeSet = transientAnnotations; SetAnnotation(this.GetAttachedAnnotations(element), ref transientAnnotations, namespaceName, localName, value); // There is at least one case (removing an annotation that was not present to begin with) where the transient annotations are not changed, // so test to see if updating the dictionary is necessary. if (transientAnnotations != transientAnnotationsBeforeSet) { annotationsDictionary = annotationsDictionary.Set(element, transientAnnotations); } }
public static void SetNamespaceAlias(this IEdmModel model, string namespaceName, string alias) { VersioningDictionary <string, string> annotationValue = model.GetAnnotationValue <VersioningDictionary <string, string> >(model, "http://schemas.microsoft.com/ado/2011/04/edm/internal", "NamespaceAlias"); if (annotationValue == null) { annotationValue = VersioningDictionary <string, string> .Create(new Func <string, string, int>(string.CompareOrdinal)); } if (alias != null) { annotationValue = annotationValue.Set(namespaceName, alias); } else { annotationValue = annotationValue.Remove(namespaceName); } model.SetAnnotationValue(model, "http://schemas.microsoft.com/ado/2011/04/edm/internal", "NamespaceAlias", annotationValue); }
/// <summary> /// Sets the serialization alias for a given namespace /// </summary> /// <param name="model">Model that will be serialized.</param> /// <param name="namespaceName">The namespace to set the alias for.</param> /// <param name="alias">The alias for that namespace.</param> public static void SetNamespaceAlias(this IEdmModel model, string namespaceName, string alias) { VersioningDictionary <string, string> mappings = model.GetAnnotationValue <VersioningDictionary <string, string> >(model, EdmConstants.InternalUri, CsdlConstants.NamespaceAliasAnnotation); if (mappings == null) { mappings = VersioningDictionary <string, string> .Create(string.CompareOrdinal); } if (alias == null) { mappings = mappings.Remove(namespaceName); } else { mappings = mappings.Set(namespaceName, alias); } model.SetAnnotationValue(model, EdmConstants.InternalUri, CsdlConstants.NamespaceAliasAnnotation, mappings); }
/// <summary> /// Sets the serialization alias for a given namespace(including current model's schemas namespace-alias, and referenced models' schemas namespace-alias) /// TODO: REF make sure no duplicated alias. /// </summary> /// <param name="model">Model that will be serialized.</param> /// <param name="namespaceName">The namespace to set the alias for.</param> /// <param name="alias">The alias for that namespace.</param> public static void SetNamespaceAlias(this IEdmModel model, string namespaceName, string alias) { VersioningDictionary <string, string> mappings = model.GetAnnotationValue <VersioningDictionary <string, string> >(model, EdmConstants.InternalUri, CsdlConstants.NamespaceAliasAnnotation); if (mappings == null) { mappings = VersioningDictionary <string, string> .Create(string.CompareOrdinal); } if (EdmUtil.IsNullOrWhiteSpaceInternal(alias)) { string val; if (mappings.TryGetValue(namespaceName, out val)) { mappings = mappings.Remove(namespaceName); } } else { mappings = mappings.Set(namespaceName, alias); } model.SetAnnotationValue(model, EdmConstants.InternalUri, CsdlConstants.NamespaceAliasAnnotation, mappings); var list = model.GetAnnotationValue <VersioningList <string> >(model, EdmConstants.InternalUri, CsdlConstants.UsedNamespacesAnnotation); if (list == null) { list = VersioningList <string> .Create(); } if (!string.IsNullOrEmpty(namespaceName) && !list.Contains(namespaceName)) { list = list.Add(namespaceName); } model.SetAnnotationValue(model, EdmConstants.InternalUri, CsdlConstants.UsedNamespacesAnnotation, list); }
private void SetAnnotationValue(IEdmElement element, string namespaceName, string localName, object value, ref VersioningDictionary<IEdmElement, object> annotationsDictionary) { object transientAnnotations = GetTransientAnnotations(element, annotationsDictionary); object transientAnnotationsBeforeSet = transientAnnotations; SetAnnotation(this.GetAttachedAnnotations(element), ref transientAnnotations, namespaceName, localName, value); // There is at least one case (removing an annotation that was not present to begin with) where the transient annotations are not changed, // so test to see if updating the dictionary is necessary. if (transientAnnotations != transientAnnotationsBeforeSet) { annotationsDictionary = annotationsDictionary.Set(element, transientAnnotations); } }
private void SetAnnotationValue(IEdmElement element, string namespaceName, string localName, object value, ref VersioningDictionary<IEdmElement, object> annotationsDictionary) { object transientAnnotations = EdmDirectValueAnnotationsManager.GetTransientAnnotations(element, annotationsDictionary); object obj = transientAnnotations; EdmDirectValueAnnotationsManager.SetAnnotation(this.GetAttachedAnnotations(element), ref transientAnnotations, namespaceName, localName, value); if (transientAnnotations != obj) { annotationsDictionary = annotationsDictionary.Set(element, transientAnnotations); } }
public void MapIntegerInsertionAndDeletion() { VersioningDictionary <int, int> map = VersioningDictionary <int, int> .Create(CompareIntegers); int value; Assert.IsTrue(map is VersioningDictionary <int, int> .EmptyVersioningDictionary, "Dictionary type"); Assert.IsFalse(map.TryGetValue(0, out value), "Zero key"); // Test conversions back and forth between EmptyDictionary, OneKeyDictionary, and TwoKeyDictionary. map = map.Set(1, 10); Assert.IsTrue(map is VersioningDictionary <int, int> .OneKeyDictionary, "Dictionary type"); Assert.AreEqual(map.Get(1), 10, "Key value"); Assert.IsFalse(map.TryGetValue(0, out value), "Zero key"); map = map.Set(1, 100); Assert.IsTrue(map is VersioningDictionary <int, int> .OneKeyDictionary, "Dictionary type"); Assert.AreEqual(map.Get(1), 100, "Key value"); Assert.IsFalse(map.TryGetValue(0, out value), "Zero key"); map = map.Remove(1); Assert.IsTrue(map is VersioningDictionary <int, int> .EmptyVersioningDictionary, "Dictionary type"); map = map.Set(1, 10); map = map.Set(2, 20); Assert.IsTrue(map is VersioningDictionary <int, int> .TwoKeyDictionary, "Dictionary type"); Assert.AreEqual(map.Get(1), 10, "Key value"); Assert.AreEqual(map.Get(2), 20, "Key value"); Assert.IsFalse(map.TryGetValue(0, out value), "Zero key"); map = map.Set(1, 100); Assert.IsTrue(map is VersioningDictionary <int, int> .TwoKeyDictionary, "Dictionary type"); Assert.AreEqual(map.Get(1), 100, "Key value"); Assert.AreEqual(map.Get(2), 20, "Key value"); map = map.Set(2, 200); Assert.IsTrue(map is VersioningDictionary <int, int> .TwoKeyDictionary, "Dictionary type"); Assert.AreEqual(map.Get(1), 100, "Key value"); Assert.AreEqual(map.Get(2), 200, "Key value"); VersioningDictionary <int, int> oneMap = map.Remove(1); Assert.IsTrue(oneMap is VersioningDictionary <int, int> .OneKeyDictionary, "Dictionary type"); Assert.AreEqual(oneMap.Get(2), 200, "Key value"); Assert.IsTrue(map is VersioningDictionary <int, int> .TwoKeyDictionary, "Dictionary type"); Assert.AreEqual(map.Get(1), 100, "Key value"); Assert.AreEqual(map.Get(2), 200, "Key value"); Assert.IsFalse(map.TryGetValue(0, out value), "Zero key"); oneMap = map.Remove(2); Assert.IsTrue(oneMap is VersioningDictionary <int, int> .OneKeyDictionary, "Dictionary type"); Assert.AreEqual(oneMap.Get(1), 100, "Key value"); Assert.IsTrue(map is VersioningDictionary <int, int> .TwoKeyDictionary, "Dictionary type"); Assert.AreEqual(map.Get(1), 100, "Key value"); Assert.AreEqual(map.Get(2), 200, "Key value"); // Test conversion to a TreeDictionary. map = map.Set(3, 30); Assert.IsTrue(map is VersioningDictionary <int, int> .TreeDictionary, "Dictionary type"); Assert.AreEqual(map.Get(1), 100, "Key value"); Assert.AreEqual(map.Get(2), 200, "Key value"); Assert.AreEqual(map.Get(3), 30, "Key value"); Assert.IsFalse(map.TryGetValue(0, out value), "Zero key"); map = map.Set(3, 300); Assert.IsTrue(map is VersioningDictionary <int, int> .TreeDictionary, "Dictionary type"); Assert.AreEqual(map.Get(1), 100, "Key value"); Assert.AreEqual(map.Get(2), 200, "Key value"); Assert.AreEqual(map.Get(3), 300, "Key value"); // Test that a significant number of keys does not force conversion to a HashKeyDictionary. for (int i = 1; i <= 100; i++) { map = map.Set(i, i * 10); } Assert.IsTrue(map is VersioningDictionary <int, int> .TreeDictionary, "Dictionary type"); for (int i = 1; i <= 100; i++) { Assert.AreEqual(i * 10, map.Get(i), "Key value"); } Assert.IsFalse(map.TryGetValue(0, out value), "Zero key"); // Test removing half the keys. for (int i = 1; i < 100; i += 2) { map = map.Remove(i); } Assert.IsTrue(map is VersioningDictionary <int, int> .TreeDictionary, "Dictionary type"); for (int i = 1; i < 100; i += 2) { Assert.IsFalse(map.TryGetValue(i, out value), "Odd key"); Assert.AreEqual((i + 1) * 10, map.Get(i + 1), "Even key value"); } // Test conversion to a HashTreeDictionary. for (int i = 1; i <= 10000; i++) { map = map.Set(i, i * 2); } Assert.IsTrue(map is VersioningDictionary <int, int> .HashTreeDictionary, "Dictionary type"); for (int i = 1; i <= 10000; i++) { Assert.AreEqual(i * 2, map.Get(i), "Key value"); } Assert.IsFalse(map.TryGetValue(0, out value), "Zero key"); // Test removing half the keys. for (int i = 1; i < 10000; i += 2) { map = map.Remove(i); } Assert.IsTrue(map is VersioningDictionary <int, int> .HashTreeDictionary, "Dictionary type"); for (int i = 1; i < 10000; i += 2) { Assert.IsFalse(map.TryGetValue(i, out value), "Odd key"); Assert.AreEqual((i + 1) * 2, map.Get(i + 1), "Even key value"); } }