I have the functional requirement to build an algorithm that takes any real number and…
- User
I have the functional requirement to build an algorithm that takes any real number and gives back a tuple of the closest number in the Fibonacci sequence and the next one.
Please write me a couple of unit tests for this requirement using xunit and shouldly.
- Assistant
Sure, here are a couple of unit tests for the given functional requirement using xUnit and Shouldly in C#:
c#using System; using Xunit; using Shouldly; public class FibonacciTests { [Fact] public void TestClosestFibonacciNumbersForNegativeNumber() { // Arrange double number = -10.5; // Act (int closest, int next) = Fibonacci.GetClosestFibonacciNumbers(number); // Assert closest.ShouldBe(0); next.ShouldBe(1); } [Fact] public void TestClosestFibonacciNumbersForLargeNumber() { // Arrange double number = 1000000; // Act (int closest, int next) = Fibonacci.GetClosestFibonacciNumbers(number); // Assert closest.ShouldBe(832040); next.ShouldBe(1346269); } } - User
Write an implementation of
GetClosestFibonacciNumberswhich satisfies the initial description as well as the tests above. - Assistant
public static class Fibonacci { public static (int closest, int next) GetClosestFibonacciNumbers(double number) { if (number < 0) { return (0, 1); }
codeint previous = 0; int current = 1; while (true) { int nextFibonacci = previous + current; if (nextFibonacci > number) { return (current, nextFibonacci); } previous = current; current = nextFibonacci; } }}
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)