public static void TestBugFix669376() { var cd = new LurchTable <string, int>(new OrdinalStringComparer()); cd["test"] = 10; Assert.True(cd.ContainsKey("TEST"), "Customized comparer didn't work"); }
public void TestLimitorByAccess() { //multiple of prime will produce hash collision, thus testing removal of non-first elements const int prime = 1103; var test = new LurchTable <int, string>(LurchTableOrder.Access, 3, prime, 10, 10, EqualityComparer <int> .Default); test[1 * prime] = "a"; test[2 * prime] = "b"; test[3 * prime] = "c"; Assert.AreEqual(3, test.Count); Assert.AreEqual("b", test[2 * prime]); //access moves to front.. test[4] = "d"; test[5] = "e"; Assert.AreEqual(3, test.Count); // still 3 items Assert.IsFalse(test.ContainsKey(1 * prime)); Assert.IsTrue(test.ContainsKey(2 * prime)); //recently access is still there Assert.IsFalse(test.ContainsKey(3 * prime)); }
public void Dictionary_Generic_ContainsValue_DefaultValuePresent(int count) { LurchTable <TKey, TValue> dictionary = (LurchTable <TKey, TValue>)GenericIDictionaryFactory(count); int seed = 4315; TKey notPresent = CreateTKey(seed++); while (dictionary.ContainsKey(notPresent)) { notPresent = CreateTKey(seed++); } dictionary.Add(notPresent, default(TValue)); Assert.True(dictionary.ContainsValue(default(TValue))); }
public void Dictionary_Generic_RemoveKey_DefaultKeyNotContainedInDictionary(int count) { LurchTable <TKey, TValue> dictionary = (LurchTable <TKey, TValue>)GenericIDictionaryFactory(count); TValue outValue; if (DefaultValueAllowed) { TKey missingKey = default(TKey); while (dictionary.ContainsKey(missingKey)) { dictionary.Remove(missingKey); } Assert.False(dictionary.Remove(missingKey, out outValue)); Assert.Equal(default(TValue), outValue); } else { TValue initValue = CreateTValue(count); outValue = initValue; Assert.Throws <ArgumentNullException>(() => dictionary.Remove(default(TKey), out outValue)); Assert.Equal(initValue, outValue); } }
public bool ContainsKey(TKey key) { return(cache.ContainsKey(key)); }
public static void TestExceptions() { var dictionary = new LurchTable <string, int>(); Assert.Null(Record.Exception( () => dictionary.TryAdd(null, 0))); // "TestExceptions: FAILED. TryAdd threw ANE when null key is passed"); Assert.Null(Record.Exception( () => dictionary.ContainsKey(null))); // "TestExceptions: FAILED. Contains threw ANE when null key is passed"); int item; Assert.Null(Record.Exception( () => dictionary.TryRemove(null, out item))); // "TestExceptions: FAILED. TryRemove threw ANE when null key is passed"); Assert.Null(Record.Exception( () => dictionary.TryGetValue(null, out item))); // "TestExceptions: FAILED. TryGetValue threw ANE when null key is passed"); Assert.Throws <KeyNotFoundException>( () => { var x = dictionary[null]; }); // "TestExceptions: FAILED. this[] threw ANE when null key is passed"); (it should throw KeyNotFoundException instead) Assert.Throws <KeyNotFoundException>( () => { var x = dictionary["1"]; }); // "TestExceptions: FAILED. this[] didn't throw KeyNotFoundException!"); Assert.Null(Record.Exception( () => dictionary[null] = 1)); // "TestExceptions: FAILED. this[] threw ANE when null key is passed"); //Assert.Throws<ArgumentNullException>( // () => dictionary.GetOrAdd(null, (k, m) => 0, 0)); //// "TestExceptions: FAILED. GetOrAdd didn't throw ANE when null key is passed"); //Assert.Throws<ArgumentNullException>( // () => dictionary.GetOrAdd("1", null, 0)); //// "TestExceptions: FAILED. GetOrAdd didn't throw ANE when null valueFactory is passed"); Assert.Null(Record.Exception( () => dictionary.GetOrAdd(null, (k) => 0))); // "TestExceptions: FAILED. GetOrAdd threw ANE when null key is passed"); Assert.Throws <ArgumentNullException>( () => dictionary.GetOrAdd("1", null)); // "TestExceptions: FAILED. GetOrAdd didn't throw ANE when null valueFactory is passed"); Assert.Null(Record.Exception( () => dictionary.GetOrAdd(null, 0))); // "TestExceptions: FAILED. GetOrAdd threw ANE when null key is passed"); //Assert.Throws<ArgumentNullException>( // () => dictionary.AddOrUpdate(null, (k, m) => 0, (k, v, m) => 0, 42)); //// "TestExceptions: FAILED. AddOrUpdate didn't throw ANE when null key is passed"); //Assert.Throws<ArgumentNullException>( // () => dictionary.AddOrUpdate("1", (k, m) => 0, null, 42)); //// "TestExceptions: FAILED. AddOrUpdate didn't throw ANE when null updateFactory is passed"); //Assert.Throws<ArgumentNullException>( // () => dictionary.AddOrUpdate("1", null, (k, v, m) => 0, 42)); //// "TestExceptions: FAILED. AddOrUpdate didn't throw ANE when null addFactory is passed"); Assert.Null(Record.Exception( () => dictionary.AddOrUpdate(null, (k) => 0, (k, v) => 0))); // "TestExceptions: FAILED. AddOrUpdate threw ANE when null key is passed"); Assert.Throws <ArgumentNullException>( () => dictionary.AddOrUpdate("1", null, (k, v) => 0)); // "TestExceptions: FAILED. AddOrUpdate didn't throw ANE when null updateFactory is passed"); Assert.Throws <ArgumentNullException>( () => dictionary.AddOrUpdate(null, (k) => 0, null)); // "TestExceptions: FAILED. AddOrUpdate didn't throw ANE when null addFactory is passed"); // Duplicate key. dictionary.TryAdd("1", 1); AssertExtensions.Throws <ArgumentException>(null, () => ((IDictionary <string, int>)dictionary).Add("1", 2)); }