Exemplo n.º 1
0
        public void CountSubarrays_ReverseNaturalNumbers_TheSame()
        {
            var numbers = Enumerable.Range(1, 1_000_000).Reverse().ToArray();

            ContiguousSubarray.CountSubarrays(numbers)
            .Should().Equal(numbers);
        }
Exemplo n.º 2
0
        public void TwoElementTest()
        {
            var array = new ContiguousSubarray();

            Assert.Equal(2, array.FindMaxLength(new [] { 0, 1 }));
            Assert.Equal(0, array.FindMaxLength(new[] { 0, 0 }));
        }
Exemplo n.º 3
0
        public void ManyElementTest()
        {
            var array = new ContiguousSubarray();

            Assert.Equal(8, array.FindMaxLength(new[] { 0, 1, 1, 0, 1, 1, 0, 0 }));
            Assert.Equal(6, array.FindMaxLength(new[] { 0, 1, 1, 1, 0, 0 }));
            Assert.Equal(4, array.FindMaxLength(new[] { 0, 0, 1, 0, 0, 1 }));
        }
Exemplo n.º 4
0
        public void CountSubarrays_Random_CompareTheTwoSolutions()
        {
            var numbers = Enumerable.Range(1, 10_000).ToArray();

            Shuffle(numbers);

            var watchFast = Stopwatch.StartNew();
            var fast      = ContiguousSubarray.CountSubarrays(numbers);

            watchFast.Stop();

            var watchSlow = Stopwatch.StartNew();
            var slow      = ContiguousSubarray1.CountSubarrays(numbers);

            watchSlow.Stop();

            slow.Should().BeEquivalentTo(fast);
            watchSlow.ElapsedMilliseconds.Should().BeLessOrEqualTo(watchFast.ElapsedMilliseconds);
        }
Exemplo n.º 5
0
 public void CountSubarrays_Sample1_ExpectedResult()
 {
     ContiguousSubarray.CountSubarrays(new[] { 3, 4, 1, 6, 2 })
     .Should().Equal(new [] { 1, 3, 1, 5, 1 });
 }