Skip to content

Commit 79c3f97

Browse files
committed
Corrected '{' convention. Implemented all resolved suggestions on pull request
1 parent 38a41ca commit 79c3f97

File tree

2 files changed

+98
-37
lines changed

2 files changed

+98
-37
lines changed
Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
using System;
22

3-
namespace DataStructures.Lists {
4-
public class CircularBuffer<T> {
3+
namespace DataStructures.Lists
4+
{
5+
public class CircularBuffer<T>
6+
{
57
/// <summary>
68
/// Initializes a circular buffer with initial length of 10
79
/// </summary>
8-
public CircularBuffer(bool canOverride=true) : this(10,canOverride) {
10+
public CircularBuffer(bool canOverride=true) : this(_defaultBufferLength, canOverride)
11+
{
912
}
1013

1114
/// <summary>
1215
/// Initializes a circular buffer with given length
1316
/// </summary>
1417
/// <param name="length">The length of the buffer</param>
15-
public CircularBuffer(int length, bool canOverride=true) {
16-
if (length < 1) {
18+
public CircularBuffer(int length, bool canOverride=true)
19+
{
20+
if (length < 1)
21+
{
1722
throw new ArgumentOutOfRangeException("length can not be zero or negative");
1823
}
1924
_circularBuffer = new T[length + 1];
@@ -26,62 +31,68 @@ public CircularBuffer(int length, bool canOverride=true) {
2631
/// Writes value to the back of the buffer
2732
/// </summary>
2833
/// <param name="value">value to be added to the buffer</param>
29-
public void Write(T value) {
30-
if (CanOverride) {
34+
public void Write(T value)
35+
{
36+
if (CanOverride)
37+
{
3138
InsertData(value);
3239
}
33-
else {
40+
else
41+
{
3442
DontOverrides(value);
3543
}
3644
}
3745

38-
/// <summary>
39-
/// Inserts data into the buffer when it is not filled up
40-
/// </summary>
41-
/// <param name="value"></param>
42-
private void DontOverrides(T value) {
43-
if (IsFilledUp) {
44-
return;
46+
// Inserts data into the buffer when it is not filled up
47+
private void DontOverrides(T value)
48+
{
49+
if (IsFilledUp)
50+
{
51+
throw new CircularBufferFullException($"Circular Buffer is filled up. {value} can not be inserted");
4552
}
4653
InsertData(value);
4754
}
4855

49-
/// <summary>
50-
/// Inserts data into the buffer without checking if it is full
51-
/// </summary>
52-
/// <param name="value"></param>
53-
private void InsertData(T value) {
56+
// Inserts data into the buffer without checking if it is full
57+
private void InsertData(T value)
58+
{
5459
_circularBuffer[_end] = value;
5560
_end = (_end + 1) % _circularBuffer.Length;
56-
if (_end == _start) {
61+
if (_end == _start)
62+
{
5763
_start = (_start + 1) % _circularBuffer.Length;
5864
}
5965
}
6066

6167
/// <summary>
6268
/// Reads value from the front of the buffer
63-
/// <summay>
64-
/// Use the IsEmpty property to know when to read appropriately.
65-
/// </summay>
6669
/// </summary>
67-
/// <returns></returns>
68-
public T Read() {
70+
public T Read()
71+
{
6972
var result = _circularBuffer[_start];
7073
_circularBuffer[_start] = _circularBuffer[_end];
7174
_start = (_start + 1) % _circularBuffer.Length;
7275
return result;
7376
}
7477

75-
public bool IsEmpty { get => _end == _start; }
76-
public int Length { get => _circularBuffer.Length - 1; }
77-
public bool IsFilledUp { get => ((_end + 1) % _circularBuffer.Length == _start) && !_circularBuffer[_start].Equals(_circularBuffer[_end]); }
7878
/// <summary>
7979
/// Controls whether data should be overridden when it is continously inserted without reading
8080
/// </summary>
8181
public bool CanOverride { get; }
82+
public bool IsEmpty { get => _end == _start; }
83+
public int Length { get => _circularBuffer.Length - 1; }
84+
public bool IsFilledUp { get => ((_end + 1) % _circularBuffer.Length == _start) && !_circularBuffer[_start].Equals(_circularBuffer[_end]); }
8285

8386
private T[] _circularBuffer;
8487
private int _end;
8588
private int _start;
89+
private static readonly int _defaultBufferLength = 10;
90+
}
91+
92+
public class CircularBufferFullException: Exception
93+
{
94+
public CircularBufferFullException(string message) : base(message)
95+
{
96+
}
8697
}
8798
}

UnitTest/DataStructuresTests/CircularBufferTest.cs

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,75 @@
11
using DataStructures.Lists;
2+
using System;
23
using Xunit;
34

4-
namespace UnitTest.DataStructuresTests {
5-
public class CircularBufferTest {
5+
namespace UnitTest.DataStructuresTests
6+
{
7+
public class CircularBufferTest
8+
{
69

710
[Fact]
8-
public static void SetsFixedLength() {
11+
public static void SetsFixedLength()
12+
{
913
var circularBuffer = new CircularBuffer<byte>(3);
1014
var length = circularBuffer.Length;
11-
1215
Assert.Equal(3, length);
1316
}
1417

1518
[Fact]
16-
public static void InitializesWithDefaultLengthOf10() {
19+
public static void ChecksIsEmptyProperty()
20+
{
21+
var circularBuffer = new CircularBuffer<byte>(4);
22+
Assert.True(circularBuffer.IsEmpty);
23+
}
24+
25+
[Fact]
26+
public static void ChecksIsFilledProperty()
27+
{
28+
var circularBuffer = new CircularBuffer<byte>(3, false);
29+
circularBuffer.Write(1);
30+
circularBuffer.Write(2);
31+
circularBuffer.Write(3);
32+
33+
Assert.True(circularBuffer.IsFilledUp);
34+
}
35+
36+
[Fact]
37+
public static void InitializesWithDefaultLengthOf10()
38+
{
1739
var circularBuffer = new CircularBuffer<byte>();
1840
var length = circularBuffer.Length;
1941

2042
Assert.Equal(10, length);
2143
}
44+
[Fact]
45+
public static void ThrowsArguementOutOfRangeExceptionForLengthLessThanOne()
46+
{
47+
Assert.Throws<ArgumentOutOfRangeException>(() =>
48+
{
49+
var circularBuffer = new CircularBuffer<byte>(0);
50+
});
51+
Assert.Throws<ArgumentOutOfRangeException>(() =>
52+
{
53+
var circularBuffer = new CircularBuffer<byte>(-2);
54+
});
55+
}
56+
57+
[Fact]
58+
public static void ThrowsCircularBufferFullExceptionWhenInsertingInFullBuffer()
59+
{
60+
Assert.Throws<CircularBufferFullException>(() =>
61+
{
62+
var circularBuffer = new CircularBuffer<byte>(3, false);
63+
circularBuffer.Write(1);
64+
circularBuffer.Write(2);
65+
circularBuffer.Write(3);
66+
circularBuffer.Write(4);
67+
});
68+
}
2269

2370
[Fact]
24-
public static void WritesAndReadsValue() {
71+
public static void WritesAndReadsValue()
72+
{
2573
var circularBuffer = new CircularBuffer<byte>(4);
2674
circularBuffer.Write(13);
2775
circularBuffer.Write(43);
@@ -48,7 +96,8 @@ public static void WritesAndReadsValue() {
4896
}
4997

5098
[Fact]
51-
public static void TestingCantOverrideFunctionality() {
99+
public static void TestingCantOverrideFunctionality()
100+
{
52101
var circularBuffer = new CircularBuffer<byte>(3, false);
53102
circularBuffer.Write(3);
54103
circularBuffer.Write(34);
@@ -69,7 +118,8 @@ public static void TestingCantOverrideFunctionality() {
69118
}
70119

71120
[Fact]
72-
public static void TestingWritingAndReadingSimultenouslyWithoutOverriding() {
121+
public static void TestingWritingAndReadingSimultenouslyWithoutOverriding()
122+
{
73123
var circularBuffer = new CircularBuffer<byte>(3, false);
74124
circularBuffer.Write(3);
75125
circularBuffer.Write(34);

0 commit comments

Comments
 (0)