Пример #1
0
		public void AddAdjacentElementAfterMerges()
		{
			// Arrange
			var sut = new OrderedRangedSet<int>();
			int v1 = 123;
			int v2 = v1 + 1;

			// Act
			sut.Add(v1);
			sut.Add(v2);

			// Assert
			Assert.That(sut.IsEmpty, Is.False);
			Assert.That(sut.Count, Is.EqualTo(2));
			Assert.That(sut, Is.EquivalentTo(new [] { v1, v2 }));
			Assert.That(sut.Contains(v1), Is.True);
			Assert.That(sut.Contains(v2), Is.True);
			Assert.That(sut.GetRanges(), Is.EquivalentTo(new []{ new Interval<int>(v1, v2 + 1) }));
		}
Пример #2
0
		public void AddManyUnrelatedElements()
		{
			// Arrange
			var sut = new OrderedRangedSet<int>();
			int[] arr = new [] { 1234, 123, 456, 5678, 789 };

			// Act
			foreach(int v in arr)
				sut.Add(v);

			// Assert
			Assert.That(sut.IsEmpty, Is.False);
			Assert.That(sut.Count, Is.EqualTo(arr.Length));
			Assert.That(sut, Is.EquivalentTo(arr.OrderBy(i => i)));
			foreach(int v in arr)
				Assert.That(sut.Contains(v), Is.True);
			Assert.That(sut.GetRanges(), Is.EquivalentTo(arr.Select(i => new Interval<int>(i, i + 1))));
		}
Пример #3
0
		public void AddOneElement()
		{
			// Arrange
			var sut = new OrderedRangedSet<int>();
			int v = 123;

			// Act
			sut.Add(v);

			// Assert
			Assert.That(sut.IsEmpty, Is.False);
			Assert.That(sut.Count, Is.EqualTo(1));
			Assert.That(sut, Is.EquivalentTo(new [] { v }));
			Assert.That(sut.Contains(v), Is.True);
			Assert.That(sut.Contains(v - 1), Is.False);
			Assert.That(sut.Contains(v + 1), Is.False);
			Assert.That(sut.GetRanges(), Is.EquivalentTo(new []{ new Interval<int>(v, v + 1) }));
		}
Пример #4
0
		public void RemoveElementInMiddleOfRange()
		{
			// Arrange
			var sut = new OrderedRangedSet<int>();
			int v1 = 123;
			int v2 = v1 + 1;
			int v3 = v2 + 1;
			sut.Add(v1);
			sut.Add(v2);
			sut.Add(v3);

			// Act
			sut.Remove(v2);

			// Assert
			Assert.That(sut.IsEmpty, Is.False);
			Assert.That(sut.Count, Is.EqualTo(2));
			Assert.That(sut, Is.EquivalentTo(new [] { v1, v3 }));
			Assert.That(sut.Contains(v1), Is.True);
			Assert.That(sut.Contains(v2), Is.False);
			Assert.That(sut.Contains(v3), Is.True);
			Assert.That(sut.GetRanges(), Is.EquivalentTo(new []{ new Interval<int>(v1, v1 + 1), new Interval<int>(v3, v3 + 1) }));
		}
Пример #5
0
		public void RemoveStandAloneElement()
		{
			// Arrange
			var sut = new OrderedRangedSet<int>();
			int[] arr = new [] { 1234, 123, 456, 5678, 789 };
			foreach(int v in arr) sut.Add(v);

			// Act
			sut.Remove(arr[2]);

			// Assert
			Assert.That(sut.IsEmpty, Is.False);
			Assert.That(sut.Count, Is.EqualTo(arr.Length - 1));
			Assert.That(sut, Is.EquivalentTo(arr.Where(i => i != arr[2]).OrderBy(i => i)));
			foreach(int v in arr.Where(i => i != arr[2]))
				Assert.That(sut.Contains(v), Is.True);
			Assert.That(sut.GetRanges(), Is.EquivalentTo(arr.Where(i => i != arr[2]).Select(i => new Interval<int>(i, i + 1))));
		}
		/// <summary>
		/// Prints the list of character and label ranges.
		/// </summary>
		/// <param name="characters">The collection of characters; or <see langword="null"/>.</param>
		/// <param name="labels">The collection of labels; or <see langword="null"/>.</param>
		/// <returns>A list of terms.</returns>
		private IListTerm PrintCharacterRangesAndLabels(IEnumerable<CodePoint> characters, IEnumerable<LabelRef> labels)
		{
			#region Contract
			Contract.Ensures(Contract.Result<IListTerm>() != null);
			#endregion

			// Put all characters and labels in one set (that tracks ranges).
			var set = new OrderedRangedSet<int>();
			if (characters != null)
			{
				var codePoints = characters as CodePoint[] ?? characters.ToArray();
				set.AddAll(codePoints.Where(c => !c.IsEof).Select(c => unchecked((int)c.Value)));

				// If the range contains the 16-bit Eof, add the 8-bit Eof.
				if (codePoints.Any(c => c.IsEof))
					set.Add(256);

			}
			if (labels != null)
				set.AddAll(labels.Select(l => l.Index + SpoofaxParseTableFormat.LabelBase));

			// Turn each range into a term.
			List<ITerm> result = new List<ITerm>();
			foreach(var range in set.GetRanges())
			{
				if (range.IsEmpty)
					// ReSharper disable once RedundantJumpStatement
					continue;
				else if (range.Start == range.End - 1)
					result.Add(factory.Int(range.Start));
				else
					result.Add(factory.Cons("range", factory.Int(range.Start), factory.Int(range.End - 1)));
			}
			return factory.List(result);
		}