示例#1
0
            public void ShouldNotAddTheLowElement_WhenTheElementIsAlreadyPresent()
            {
                // Arrange
                var set    = new CodePointSet(new[] { new CodePoint(42), new CodePoint(1000) });
                var result = new CodePointSet(set);

                // Act
                result.Add(new CodePoint(42));

                // Assert
                Assert.Equivalent(new[] { new CodePoint(42), new CodePoint(1000) }, result);
            }
示例#2
0
            public void ShouldNotAddEof_WhenEofIsAlreadyPresent()
            {
                // Arrange
                var set    = new CodePointSet(new[] { new CodePoint(42), new CodePoint(1000), CodePoint.Eof });
                var result = new CodePointSet(set);

                // Act
                result.Add(CodePoint.Eof);

                // Assert
                Assert.Equivalent(new[] { new CodePoint(42), new CodePoint(1000), CodePoint.Eof }, result);
            }
        /// <summary>
        /// Determines whether the current code point set overlaps with the specified collection.
        /// </summary>
        /// <param name="other">The collection to compare to the current set.</param>
        /// <returns><see langword="true"/> if the current code point set and the specified collection
        /// share at least one common element; otherwise, <see langword="false"/>.</returns>
        public bool Overlaps(CodePointSet other)
        {
            #region Contract
            if (other == null)
            {
                throw new ArgumentNullException(nameof(other));
            }
            #endregion

            return((this.hasEof && other.hasEof) ||
                   this.bits.ZipEqual(other.bits, (x, y) => new { x, y }).Aggregate(false, (v, p) => v || (p.x & p.y) != 0) ||
                   Fold(this.intervals, other.intervals, false, (_, __, x, y, v) => v || x && y));
        }
示例#4
0
            public void ShouldReturnFalseAndTheDefaultCodePoint_WhenTheSetIsEmpty()
            {
                // Arrange
                var set  = new CodePointSet();
                var test = new CodePoint(42);

                // Act
                var present = set.TryGetValue(test, out CodePoint result);

                // Assert
                Assert.False(present);
                Assert.Equal(default(CodePoint), result);
            }
            public void ShouldReturnTheNumberOfCodePoints_WhenTheSetHasCodePoints()
            {
                // Arrange
                var set = new CodePointSet(new[]
                {
                    new CodePoint(42), new CodePoint(43), new CodePoint(44), new CodePoint(66),
                    new CodePoint(1000), new CodePoint(1001), new CodePoint(1002), new CodePoint(1010)
                });

                // Act
                var result = set.Count;

                // Assert
                Assert.Equal(8, result);
            }
            public void ShouldReturnFalse_WhenTheHighElementIsNotInTheSet()
            {
                // Arrange
                var set = new CodePointSet(new[]
                {
                    new CodePoint(42), new CodePoint(43), new CodePoint(44), new CodePoint(66),
                    new CodePoint(1000), new CodePoint(1001), new CodePoint(1002), new CodePoint(1010)
                });

                // Act
                var result = set.Contains(new CodePoint(1011));

                // Assert
                Assert.False(result);
            }
            public void ShouldReturnTrue_WhenTheHighElementIsInTheSet_2()
            {
                // Arrange
                var set = new CodePointSet(new[]
                {
                    new CodePoint(42), new CodePoint(43), new CodePoint(44), new CodePoint(66),
                    new CodePoint(1000), new CodePoint(1001), new CodePoint(1002), new CodePoint(1010), CodePoint.Eof
                });

                // Act
                var result = set.Contains(new CodePoint(1001));

                // Assert
                Assert.True(result);
            }
            public void ShouldReturnTrue_WhenEofIsInTheSet()
            {
                // Arrange
                var set = new CodePointSet(new[]
                {
                    CodePoint.Eof,
                    new CodePoint(42), new CodePoint(43), new CodePoint(44), new CodePoint(66),
                    new CodePoint(1000), new CodePoint(1001), new CodePoint(1002), new CodePoint(1010)
                });

                // Act
                var result = set.Contains(CodePoint.Eof);

                // Assert
                Assert.True(result);
            }
示例#9
0
            public void ShouldReturnTrueAndTheGivenCodePoint_WhenTheHighElementIsInTheSet_1()
            {
                // Arrange
                var set = new CodePointSet(new[]
                {
                    new CodePoint(42), new CodePoint(43), new CodePoint(44), new CodePoint(66),
                    new CodePoint(1000), new CodePoint(1001), new CodePoint(1002), new CodePoint(1010)
                });
                var test = new CodePoint(1000);

                // Act
                var present = set.TryGetValue(test, out CodePoint result);

                // Assert
                Assert.True(present);
                Assert.Equal(test, result);
            }
示例#10
0
            public void ShouldReturnFalseAndTheDefaultCodePoint_WhenTheElementIsNotInTheSet()
            {
                // Arrange
                var set = new CodePointSet(new[]
                {
                    new CodePoint(42), new CodePoint(43), new CodePoint(44), new CodePoint(66),
                    new CodePoint(1000), new CodePoint(1001), new CodePoint(1002), new CodePoint(1010)
                });
                var test = new CodePoint(100);

                // Act
                var present = set.TryGetValue(test, out CodePoint result);

                // Assert
                Assert.False(present);
                Assert.Equal(default(CodePoint), result);
            }
        public void SymmetricExceptWith(CodePointSet other)
        {
            #region Contract
            if (other == null)
            {
                throw new ArgumentNullException(nameof(other));
            }
            #endregion

            var newEof       = this.hasEof ^ other.hasEof;
            var newBits      = this.bits.ZipEqual(other.bits, (a, b) => a ^ b).ToArray();
            var newIntervals = Filter(this.intervals, other.intervals, (s, e, a, b) => a ^ b).ToArray();

            this.hasEof    = newEof;
            this.bits      = newBits;
            this.intervals = newIntervals;
            this.Count     = CountCharacters(newEof, newBits, newIntervals);
        }
示例#12
0
        /// <summary>
        /// Compares this character set to the specified character set.
        /// </summary>
        /// <param name="other">The other character set.</param>
        /// <returns>The comparison result.</returns>
        private ComparisonResult CompareTo(CodePointSet other)
        {
            ComparisonResult result = ComparisonResult.Equal;

            if (this.hasEof && !other.hasEof) result &= ~ComparisonResult.Subset;
            if (!this.hasEof && other.hasEof) result &= ~ComparisonResult.Superset;

            result = this.bits.ZipEqual(other.bits, (x, y) => new { x, y }).Aggregate(result, (v, p) =>
            {
                if ((p.x & (~p.y)) != 0) v &= ~ComparisonResult.Subset;
                if (((~p.x) & p.y) != 0) v &= ~ComparisonResult.Superset;
                return v;
            });

            result = Fold(this.intervals, other.intervals, result, (_, __, x, y, v) =>
            {
                if (x && !y) v &= ~ComparisonResult.Subset;
                if (!x && y) v &= ~ComparisonResult.Superset;
                return v;
            });

            return result;
        }