Sure, here are a couple of xUnit test cases written in C# with Shouldly assertions to verify that the algorithm meets the functional requirements you described:
using System;
using Xunit;
using Shouldly;
public class FibonacciTests
{
[Theory]
[InlineData(-1, 0, 1)] // Input is negative
[InlineData(0, 0, 1)] // Input is zero
[InlineData(1, 1, 1)] // Input is one
[InlineData(3, 3, 5)] // Input is a Fibonacci number
[InlineData(4, 3, 5)] // Input is between two Fibonacci numbers
[InlineData(10, 8, 13)] // Input is greater than the last Fibonacci number
public void ClosestFibonacci_ReturnsExpectedTuple(int input, int expectedFib1, int expectedFib2)
{
// Arrange
var fib = new Fibonacci();
// Act
var result = fib.ClosestFibonacci(input);
// Assert
result.Item1.ShouldBe(expectedFib1);
result.Item2.ShouldBe(expectedFib2);
}
}
In this example, we are using a parameterized test to pass in various inputs to the ClosestFibonacci method and verify that the expected tuple of Fibonacci numbers is returned.
Note that we are also covering edge cases, such as negative inputs and inputs that are greater than the last Fibonacci number. We expect that the algorithm should return the closest Fibonacci numbers in these cases as well.
And here's the implementation of the Fibonacci class:
public class Fibonacci
{
public (int, int) ClosestFibonacci(int num)
{
if (num <= 0)
{
return (0, 1);
}
int fib1 = 0;
int fib2 = 1;
int fib = 1;
while (fib < num)
{
fib1 = fib2;
fib2 = fib;
fib = fib1 + fib2;
}
if (fib - num < num - fib2)
{
return (fib, fib + fib2);
}
else
{
return (fib2, fib);
}
}
}
The ClosestFibonacci method implements the algorithm to find the closest Fibonacci numbers to the input. It first checks if the input is less than or equal to zero and returns (0, 1) in that case, since 0 and 1 are the first two Fibonacci numbers.
Otherwise, it initializes three variables: fib1, fib2, and fib, where fib is the current Fibonacci number and fib1 and fib2 are the two previous Fibonacci numbers. It then uses a while loop to iterate through the Fibonacci sequence until it reaches a number greater than or equal to the input.
Once the loop has finished, it compares the differences between the input and the two closest Fibonacci numbers, fib and fib2, and returns the tuple with the closest numbers.