Exemple #1
0
        public async Task <int> GetNextNumberAsync(int currentNumber)
        {
            var fibonacciNumber = await _repository.GetFibonacciNumberAsync(currentNumber);

            if (fibonacciNumber != null)
            {
                return(fibonacciNumber.Next);
            }

            fibonacciNumber = await _repository.GetClosestNumberAsync(currentNumber) ?? new FibonacciNumber
            {
                Value = 1, Previous = 1, Next = 2
            };

            while (fibonacciNumber.Value <= currentNumber)
            {
                var newValue    = fibonacciNumber.Next;
                var newPrevious = fibonacciNumber.Value;
                var newNext     = fibonacciNumber.Next + fibonacciNumber.Value;

                var fn = new FibonacciNumber
                {
                    Value    = newValue,
                    Next     = newNext,
                    Previous = newPrevious
                };

                await _repository.AddAsync(fn);

                fibonacciNumber = fn;
            }

            return(fibonacciNumber.Value);
        }
        public void Fib(int n, int expected)
        {
            var sut    = new FibonacciNumber();
            var actual = sut.Fib(n);

            Assert.AreEqual(expected, actual);
        }
        public void TestMethod3()
        {
            var fibonaccinumber = new FibonacciNumber();
            int expected        = 144;
            int output          = FibonacciNumber.Fibonacci(12);

            Assert.AreEqual(expected, output);
        }
Exemple #4
0
        public FibonacciNumber ComputingNextNumber(FibonacciNumber number)
        {
            logger.LogInformation("Start local computing next number");
            var nextNumber = numberProvider.GetNext(number);

            logger.LogInformation("End local computing next number - {nextNumberValue}", nextNumber.Value);
            return(nextNumber);
        }
Exemple #5
0
        public void FibonacciNumberTest3()
        {
            var fibonacciNumber = new FibonacciNumber();

            var actual   = fibonacciNumber.FibBottomUp(100);
            var expected = 3736710778780434371;

            Assert.That(actual, Is.EqualTo(expected));
        }
Exemple #6
0
        public void CalculationNaiveTest()
        {
            var n      = 9;
            var result = FibonacciNumber.Naive(n);

            const int expectedResult = 34;

            Assert.IsTrue(result == expectedResult, "The result is incorrect");
        }
Exemple #7
0
        public void CalculationMemoizationTest()
        {
            var n      = 93;
            var result = FibonacciNumber.Memoization(n);

            const ulong expectedResult = 12200160415121876738;

            Assert.IsTrue(result == expectedResult, "The result is incorrect");
        }
        public IActionResult Post(Guid sequenceId, [FromBody] FibonacciNumber number)
        {
            numberValidator.EnsureValid(number);

            backgroundJobDirector.Enqueue(() => numberService.PublishNextAsync(sequenceId, number));
            logger.LogInformation("Background calculation of the next number for sequence {sequenceId} is started", sequenceId);

            return(Accepted());
        }
Exemple #9
0
        public void GetSequenceTest()
        {
            List <int> expected = new List <int> {
                0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55
            };
            List <int> result = FibonacciNumber.GetSequence(10).ToList();

            CollectionAssert.AreEqual(expected, result);
        }
Exemple #10
0
        public void FibonacciNumberTest1()
        {
            var fibonacciNumber = new FibonacciNumber();

            var actual   = fibonacciNumber.Fib(5);
            var expected = 5;

            Assert.That(actual, Is.EqualTo(expected));
        }
        static void Main(string[] args)
        {
            FibonacciNumber f = new FibonacciNumber(25);

            foreach (long number in f)
            {
                Console.WriteLine(number);
            }
            Console.ReadLine();
        }
Exemple #12
0
        public void Baseline()
        {
            var sut     = new FibonacciNumber();
            var numbers = new[] { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765 };

            for (var i = 0; i < numbers.Length; i++)
            {
                Assert.Equal(numbers[i], sut.GetNumber(i));
            }
        }
        public FibonacciNumber GetNext(FibonacciNumber number)
        {
            numberValidator.EnsureValid(number);

            return(new FibonacciNumber
                   (
                       index: number.Index + 1,
                       value: fibonacciAlgorithm.Get(number.Index - 1) + number.Value
                   ));
        }
Exemple #14
0
        public async Task PublishNextAsync(Guid sequenceId, FibonacciNumber number)
        {
            var nextNumber = numberProvider.GetNext(number);

            logger.LogInformation("Next number for sequence {sequenceId} is {value}", sequenceId, nextNumber.Value);

            var message = messageBuilder.Build(nextNumber);
            await messagePublisher.PublishAsync(topic : sequenceId.ToString(), message).ConfigureAwait(false);

            logger.LogInformation("Message {messageId} with nex number for sequence {sequenceId} is published", message.Id, sequenceId);
        }
        public void TestMethod2(int N, int expected)
        {
            // Arrange
            FibonacciNumber question = new FibonacciNumber();

            // Act
            int actual = question.FibIterative(N);

            // Assert
            Assert.AreEqual(expected, actual);
        }
        public void GetValueTest()
        {
            var numbers = TestData.Numbers;

            for (var i = 0; i < numbers.Length; i++)
            {
                var expected = numbers.Span[i];
                var actual   = FibonacciNumber.GetValue(i);
                Assert.Equal(expected, actual);
            }
        }
Exemple #17
0
 public FibonacciNumberMessage Build(FibonacciNumber number)
 {
     return(new FibonacciNumberMessage
            (
                id: guidManager.GetNew(),
                createdAt: dateTimeManager.GetNow(),
                data: new FibonacciNumber
                (
                    index: number.Index,
                    value: number.Value
                )
            ));
 }
 public void EnsureValid(FibonacciNumber number)
 {
     if (number is null)
     {
         throw new ArgumentNullException(nameof(number));
     }
     if (number.Value < 1)
     {
         throw new ArgumentException("Value must be greater than zero.", nameof(number.Value));
     }
     if (number.Index < 1)
     {
         throw new ArgumentException("Value must be greater than zero.", nameof(number.Index));
     }
 }
Exemple #19
0
        public async Task AddAsync(FibonacciNumber fibonacciNumber)
        {
            try
            {
                fibonacciNumber.Id = Guid.NewGuid().ToString().Replace("-", string.Empty);

                _context.FibonacciNumbers.Add(fibonacciNumber);
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                //If we get a db exception, it is most likely a duplicate entry, so its safe to ignore.
                return;
            }
        }
        public static void LoadCache()
        {
            var fib = new FibonacciNumber(0, 1, 0);

            _fibonacciCache.Add(0, fib);
            fib = new FibonacciNumber(1, fib.Value, 0);
            _fibonacciCache.Add(1, fib);
            fib = new FibonacciNumber(2, fib.Value, fib.First);
            _fibonacciCache.Add(2, fib);
            fib = new FibonacciNumber(3, fib.Value, fib.First);
            _fibonacciCache.Add(3, fib);
            fib = new FibonacciNumber(4, fib.Value, fib.First);
            _fibonacciCache.Add(4, fib);
            fib = new FibonacciNumber(5, fib.Value, fib.First);
            _fibonacciCache.Add(5, fib);
        }
        public void PublishNextAsync_ValidCase(Guid sequenceId, FibonacciNumber number, string expectedTopic, FibonacciNumberMessage expectedMessage)
        {
            string actualTopic = null;
            FibonacciNumberMessage actualMessage = null;

            publisherMoq
            .Setup(x => x.PublishAsync(It.IsAny <string>(), It.IsAny <FibonacciNumberMessage>()))
            .Callback <string, FibonacciNumberMessage>((topic, message) =>
            {
                actualTopic   = topic;
                actualMessage = message;
            });

            service.PublishNextAsync(sequenceId, number).GetAwaiter().GetResult();

            using (new AssertionScope())
            {
                actualTopic.Should().Be(expectedTopic);
                actualMessage.Should().BeEquivalentTo(expectedMessage);
            }
        }
Exemple #22
0
        public void ZeroNumber()
        {
            var sut = new FibonacciNumber();

            Assert.Equal(0, sut.GetNumber(0));
        }
Exemple #23
0
        public void SecondNumber()
        {
            var sut = new FibonacciNumber();

            Assert.Equal(1, sut.GetNumber(2));
        }
Exemple #24
0
        public void FirstNumber()
        {
            var sut = new FibonacciNumber();

            Assert.Equal(1, sut.GetNumber(1));
        }
Exemple #25
0
 public void GetFibonacciNumberTestCase9()
 {
     FibonacciNumber.Fib(8).Should().Be(21);
 }
Exemple #26
0
 public void GetFibonacciNumberTestCase8()
 {
     FibonacciNumber.Fib(7).Should().Be(13);
 }
Exemple #27
0
 public void GetFibonacciNumberTestCase7()
 {
     FibonacciNumber.Fib(6).Should().Be(8);
 }
Exemple #28
0
 public void GetFibonacciNumberTestCase6()
 {
     FibonacciNumber.Fib(5).Should().Be(5);
 }
Exemple #29
0
 public void GetFibonacciNumberTestCase5()
 {
     FibonacciNumber.Fib(4).Should().Be(3);
 }
Exemple #30
0
 public void GetFibonacciNumberTestCase4()
 {
     FibonacciNumber.Fib(3).Should().Be(2);
 }