예제 #1
0
        public void TestInitialise()
        {
            HashCollection <int> hashCollection = new HashCollection <int> {
                1
            };

            Assert.IsTrue(hashCollection.Contains(1));
            Assert.IsFalse(hashCollection.Contains(2));
            Assert.IsFalse(hashCollection.Contains(3));

            hashCollection = new HashCollection <int>(new Modulo2Comparer())
            {
                1
            };
            Assert.IsTrue(hashCollection.Contains(1));
            Assert.IsFalse(hashCollection.Contains(2));
            Assert.IsTrue(hashCollection.Contains(3));

            hashCollection = new HashCollection <int>(new[] { 1 }, new Modulo2Comparer());
            Assert.IsTrue(hashCollection.Contains(1));
            Assert.IsFalse(hashCollection.Contains(2));
            Assert.IsTrue(hashCollection.Contains(3));
        }
예제 #2
0
        private void BeginValidation()
        {
            ThreadStart beginValidation = delegate
            {
                while (true)
                {
                    // Wait if there is no work to do
                    this.queueResetEvent.WaitOne();

                    ISupportValidation obj = null;
                    lock (this.validationQueue)
                    {
                        if (this.validationQueue.Count == 0)
                        {
                            this.Status = ValidationStatus.Ready;
                            this.queueResetEvent.Reset();
                            continue;
                        }
                        else
                        {
                            obj = this.validationQueue[0];
                            this.validationQueue.RemoveAt(0);
                        }
                    }

                    lock (this.validationResults)
                    {
                        // Try to get the validation results (they may have been removed by a different
                        // thread calling RemoveObject)
                        HashCollection <ValidationResult> oldValidationResults;
                        if (this.validationResults.TryGetValue(obj, out oldValidationResults))
                        {
                            HashCollection <ValidationResult> newValidationResults = new HashCollection <ValidationResult> ();
                            foreach (ValidationResult validationResult in obj.Validate())
                            {
                                if (!newValidationResults.Contains(validationResult))
                                {
                                    newValidationResults.Add(validationResult);
                                }
                            }

                            foreach (ValidationResult validationResult in new List <ValidationResult> (oldValidationResults))
                            {
                                if (!newValidationResults.Contains(validationResult))
                                {
                                    oldValidationResults.Remove(validationResult);
                                    ValidationResult   currentValidationResult = validationResult;
                                    SendOrPostCallback raiseRemovedEvent       = delegate
                                    {
                                        this.RaiseValidationResultRemovedEvent(currentValidationResult);
                                    };
                                    this.asyncOperation.Post(raiseRemovedEvent, null);
                                }
                            }

                            foreach (ValidationResult validationResult in newValidationResults)
                            {
                                if (!oldValidationResults.Contains(validationResult))
                                {
                                    oldValidationResults.Add(validationResult);
                                    ValidationResult   currentValidationResult = validationResult;
                                    SendOrPostCallback raiseAddedEvent         = delegate
                                    {
                                        this.RaiseValidationResultAddedEvent(currentValidationResult);
                                    };
                                    this.asyncOperation.Post(raiseAddedEvent, null);
                                }
                            }
                        }
                    }
                }
            };

            Thread thread = new Thread(beginValidation);

            thread.Name         = "Validation Manager";
            thread.IsBackground = true;
            thread.Priority     = ThreadPriority.BelowNormal;
            thread.Start();
        }
예제 #3
0
        public void TestISetMethods()
        {
            HashCollection <int> hashCollection = new HashCollection <int>();
            ISet set = hashCollection;

            set.Add(1);
            Assert.IsTrue(hashCollection.Contains(1));
            Assert.IsFalse(hashCollection.Contains(2));

            set.UnionWith(new[] { 2 });
            Assert.IsTrue(hashCollection.Contains(1));
            Assert.IsTrue(hashCollection.Contains(2));

            set.IntersectWith(new[] { 2 });
            Assert.IsFalse(hashCollection.Contains(1));
            Assert.IsTrue(hashCollection.Contains(2));

            hashCollection.Add(1);
            set.SymmetricExceptWith(new[] { 2, 3 });
            Assert.IsTrue(hashCollection.Contains(1));
            Assert.IsFalse(hashCollection.Contains(2));
            Assert.IsTrue(hashCollection.Contains(3));

            set.ExceptWith(new[] { 2, 3 });
            Assert.IsTrue(hashCollection.Contains(1));
            Assert.IsFalse(hashCollection.Contains(2));
            Assert.IsFalse(hashCollection.Contains(3));

            Assert.IsTrue(set.IsSupersetOf(Array <int> .Empty));
            Assert.IsFalse(set.IsSupersetOf(new[] { 2 }));
            Assert.IsTrue(set.IsSubsetOf(new[] { 1, 2 }));
            Assert.IsFalse(set.IsSubsetOf(new[] { 2 }));

            Assert.IsTrue(set.IsProperSupersetOf(Array <int> .Empty));
            Assert.IsFalse(set.IsProperSupersetOf(set));
            Assert.IsTrue(set.IsProperSubsetOf(new[] { 1, 2 }));
            Assert.IsFalse(set.IsProperSubsetOf(set));

            Assert.IsTrue(set.Overlaps(new[] { 1, 2 }));
            Assert.IsFalse(set.Overlaps(new[] { 3, 4 }));
        }