/// <summary> /// Updates the value for an existing key in the dictionary, if that key has a specific value. /// </summary> /// <param name="key">Key</param> /// <param name="newValue">New value</param> /// <param name="comparisonValue">Old value</param> /// <returns>True if the value with key was equal to comparisonValue and was replaced with newValue; otherwise, false.</returns> public bool TryUpdate(TKey key, TValue newValue, TValue comparisonValue) { var currentMachine = Runtime.GetCurrentMachine(); Runtime.SendEvent(DictionaryMachine, SharedDictionaryEvent.TryUpdateEvent(key, newValue, comparisonValue, currentMachine.Id)); var e = currentMachine.Receive(typeof(SharedDictionaryResponseEvent <bool>)).Result as SharedDictionaryResponseEvent <bool>; return(e.Value); }
/// <summary> /// Adds a new key to the dictionary, if it doesn’t already exist in the dictionary. /// </summary> /// <param name="key">Key</param> /// <param name="value">Value</param> /// <returns>True or false depending on whether the new key/value pair was added.</returns> public bool TryAdd(TKey key, TValue value) { var currentMachine = this.Runtime.GetCurrentMachine(); this.Runtime.SendEvent(this.DictionaryMachine, SharedDictionaryEvent.TryAddEvent(key, value, currentMachine.Id)); var e = currentMachine.Receive(typeof(SharedDictionaryResponseEvent <bool>)).Result as SharedDictionaryResponseEvent <bool>; return(e.Value); }
/// <summary> /// Attempts to get the value associated with the specified key. /// </summary> /// <param name="key">Key</param> /// <param name="value">Value associated with the key, or the default value if the key does not exist</param> /// <returns>True if the key was found; otherwise, false.</returns> public bool TryGetValue(TKey key, out TValue value) { var currentMachine = Runtime.GetCurrentMachine(); Runtime.SendEvent(DictionaryMachine, SharedDictionaryEvent.TryGetEvent(key, currentMachine.Id)); var e = currentMachine.Receive(typeof(SharedDictionaryResponseEvent <Tuple <bool, TValue> >)).Result as SharedDictionaryResponseEvent <Tuple <bool, TValue> >; value = e.Value.Item2; return(e.Value.Item1); }
/// <summary> /// Removes the specified key from the dictionary. /// </summary> public bool TryRemove(TKey key, out TValue value) { var currentMachine = this.Runtime.GetExecutingMachine <Machine>(); this.Runtime.SendEvent(this.DictionaryMachine, SharedDictionaryEvent.TryRemoveEvent(key, currentMachine.Id)); var e = currentMachine.Receive(typeof(SharedDictionaryResponseEvent <Tuple <bool, TValue> >)).Result as SharedDictionaryResponseEvent <Tuple <bool, TValue> >; value = e.Value.Item2; return(e.Value.Item1); }
/// <summary> /// Initializes the shared dictionary. /// </summary> /// <param name="comparer">Comparre for keys</param> /// <param name="Runtime">BugFindingRuntime</param> public MockSharedDictionary(IEqualityComparer <TKey> comparer, BugFindingRuntime Runtime) { this.Runtime = Runtime; if (comparer != null) { DictionaryMachine = Runtime.CreateMachine(typeof(SharedDictionaryMachine <TKey, TValue>), SharedDictionaryEvent.InitEvent(comparer)); } else { DictionaryMachine = Runtime.CreateMachine(typeof(SharedDictionaryMachine <TKey, TValue>)); } }
/// <summary> /// Gets or sets the value associated with the specified key. /// </summary> /// <param name="key">Key</param> /// <returns>Value</returns> public TValue this[TKey key] { get { var currentMachine = Runtime.GetCurrentMachine(); Runtime.SendEvent(DictionaryMachine, SharedDictionaryEvent.GetEvent(key, currentMachine.Id)); var e = currentMachine.Receive(typeof(SharedDictionaryResponseEvent <TValue>)).Result as SharedDictionaryResponseEvent <TValue>; return(e.Value); } set { Runtime.SendEvent(DictionaryMachine, SharedDictionaryEvent.SetEvent(key, value)); } }
/// <summary> /// Initializes a new instance of the <see cref="MockSharedDictionary{TKey, TValue}"/> class. /// </summary> public MockSharedDictionary(IEqualityComparer <TKey> comparer, SystematicTestingRuntime runtime) { this.Runtime = runtime; if (comparer != null) { this.DictionaryMachine = this.Runtime.CreateMachine( typeof(SharedDictionaryMachine <TKey, TValue>), SharedDictionaryEvent.InitEvent(comparer)); } else { this.DictionaryMachine = this.Runtime.CreateMachine(typeof(SharedDictionaryMachine <TKey, TValue>)); } }