-
-
Notifications
You must be signed in to change notification settings - Fork 354
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Property based testing #2279
Comments
Hello. Thanks for opening an issue on Exercism 🙂 At Exercism we use our Community Forum, not GitHub issues, as the primary place for discussion. That allows maintainers and contributors from across Exercism's ecosystem to discuss your problems/ideas/suggestions without them having to subscribe to hundreds of repositories. This issue will be automatically closed. Please use this link to copy your GitHub Issue into a new topic on the forum, where we look forward to chatting with you! If you're interested in learning more about this auto-responder, please read this blog post. |
We have two such exercises:
|
Do you think these tests will improve the Circular Buffer exercise? I personally feel more confident about my implementation after running them, but I'm uncertain whether they cover more scenarios than the example-based tests. If you believe they add value, I can create a pull request to include them. [Property]
public void WriteAndRead_ShouldWorkCorrectly(int capacity, int[] values)
{
if (capacity <= 0 || values == null) return;
var buffer = new CircularBuffer<int>(capacity);
foreach (var value in values)
{
buffer.Write(value);
Assert.Equal(value, buffer.Read());
}
}
[Property]
public void WriteBeyondCapacity_ShouldThrowException(int capacity, int[] values)
{
if (capacity <= 0 || values == null) return;
var buffer = new CircularBuffer<int>(capacity);
foreach (var value in values)
{
if (buffer.Size < capacity)
{
buffer.Write(value);
}
else
{
Assert.Throws<InvalidOperationException>(() => buffer.Write(value));
}
}
}
[Property]
public void ReadFromEmptyBuffer_ShouldThrowException(int capacity)
{
if (capacity <= 0) return;
var buffer = new CircularBuffer<int>(capacity);
Assert.Throws<InvalidOperationException>(() => buffer.Read());
}
[Property]
public void Overwrite_ShouldReplaceOldestValue(int capacity, int[] values)
{
if (capacity <= 0 || values == null) return;
var buffer = new CircularBuffer<int>(capacity);
foreach (var value in values)
{
buffer.Overwrite(value);
}
for (int i = Math.Max(0, values.Length - capacity); i < values.Length; i++)
{
Assert.Equal(values[i], buffer.Read());
}
}
[Property]
public void Clear_ShouldResetBuffer(int capacity, int[] values)
{
if (capacity <= 0 || values == null) return;
var buffer = new CircularBuffer<int>(capacity);
foreach (var value in values)
{
if (buffer.Size < capacity)
{
buffer.Write(value);
}
}
buffer.Clear();
Assert.Throws<InvalidOperationException>(() => buffer.Read());
} |
Personally, I really like property-based tests, but in this case I think the existing test cases cover these scenarios well enough. |
I believe it would be beneficial to add property-based tests to some of the exercises. This would ensure that students provide correct answers even for unusual edge cases. Have you considered this approach? I've already added property-based tests to the circular buffer exercise but am unsure how to contribute further.
The text was updated successfully, but these errors were encountered: