Skip to content

Commit

Permalink
circular-buffer: sync
Browse files Browse the repository at this point in the history
  • Loading branch information
senekor committed Apr 2, 2024
1 parent e318d66 commit 76c7b5b
Show file tree
Hide file tree
Showing 4 changed files with 212 additions and 92 deletions.
34 changes: 34 additions & 0 deletions exercises/practice/circular-buffer/.meta/additional-tests.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[
{
"uuid": "8a9c5921-a266-4c11-b392-3b2fbb53517c",
"description": "char buffer",
"property": "generic",
"input": {
"capacity": 1,
"operations": [
{
"operation": "write",
"item": "'A'",
"should_succeed": true
}
]
},
"expected": {}
},
{
"uuid": "8a9c5921-a266-4c11-b392-3b2fbb53517c",
"description": "string buffer",
"property": "generic",
"input": {
"capacity": 1,
"operations": [
{
"operation": "write",
"item": "\"Testing\".to_string()",
"should_succeed": true
}
]
},
"expected": {}
}
]
61 changes: 61 additions & 0 deletions exercises/practice/circular-buffer/.meta/test_template.tera
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use circular_buffer::*;
use std::rc::Rc;

{% for test in cases %}
#[test]
#[ignore]
fn {{ test.description | snake_case }}() {
let mut buffer = CircularBuffer{% if loop.index == 1 %}::<i32>{% endif %}::new({{ test.input.capacity }});
{%- for op in test.input.operations %}
{%- if op.operation == "read" %}
{%- if op.should_succeed %}
assert_eq!(buffer.read(), Ok({{ op.expected }}));
{%- else %}
assert_eq!(buffer.read(), Err(Error::EmptyBuffer));
{% endif -%}
{%- elif op.operation == "write" %}
{%- if op.should_succeed %}
assert!(buffer.write({{ op.item }}).is_ok());
{%- else %}
assert_eq!(buffer.write({{ op.item }}), Err(Error::FullBuffer));
{% endif -%}
{%- elif op.operation == "overwrite" %}
buffer.overwrite({{ op.item }});
{%- elif op.operation == "clear" %}
buffer.clear();
{%- else %}
panic!("error in test template: unknown operation");
{% endif -%}
{% endfor %}
}
{% endfor %}

{#-
We usually do not add additional tests directly in the template.
In this case however, the structure of the tests is different from the
regular ones. Accommodating that in the template would be complicated
and unnecessary.
#}

#[test]
#[ignore]
fn clear_actually_frees_up_its_elements() {
let mut buffer = CircularBuffer::new(1);
let element = Rc::new(());
assert!(buffer.write(Rc::clone(&element)).is_ok());
assert_eq!(Rc::strong_count(&element), 2);
buffer.clear();
assert_eq!(Rc::strong_count(&element), 1);
}

#[test]
#[ignore]
fn dropping_the_buffer_drops_its_elements() {
let element = Rc::new(());
{
let mut buffer = CircularBuffer::new(1);
assert!(buffer.write(Rc::clone(&element)).is_ok());
assert_eq!(Rc::strong_count(&element), 2);
}
assert_eq!(Rc::strong_count(&element), 1);
}
28 changes: 25 additions & 3 deletions exercises/practice/circular-buffer/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
# This is an auto-generated file. Regular comments will be removed when this
# file is regenerated. Regenerating will not touch any manually added keys,
# so comments can be added in a "comment" key.
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[28268ed4-4ff3-45f3-820e-895b44d53dfa]
description = "reading empty buffer should fail"

[2e6db04a-58a1-425d-ade8-ac30b5f318f3]
description = "can read an item just written"

[90741fe8-a448-45ce-be2b-de009a24c144]
description = "each item may only be read once"
Expand All @@ -11,6 +24,9 @@ description = "items are read in the order they are written"
[2af22046-3e44-4235-bfe6-05ba60439d38]
description = "full buffer can't be written to"

[547d192c-bbf0-4369-b8fa-fc37e71f2393]
description = "a read frees up capacity for another write"

[04a56659-3a81-4113-816b-6ecb659b4471]
description = "read position is maintained even across multiple writes"

Expand All @@ -23,8 +39,14 @@ description = "clear frees up capacity for another write"
[e1ac5170-a026-4725-bfbe-0cf332eddecd]
description = "clear does nothing on empty buffer"

[9c2d4f26-3ec7-453f-a895-7e7ff8ae7b5b]
description = "overwrite acts like write on non-full buffer"

[880f916b-5039-475c-bd5c-83463c36a147]
description = "overwrite replaces the oldest item on full buffer"

[bfecab5b-aca1-4fab-a2b0-cd4af2b053c3]
description = "overwrite replaces the oldest item remaining in buffer following a read"

[9cebe63a-c405-437b-8b62-e3fdc1ecec5a]
description = "initial clear does not affect wrapping around"
Loading

0 comments on commit 76c7b5b

Please sign in to comment.