Repository: JuliaCollections/DataStructures.jl (julia)

Issue:
CircularBuffer push! increases size even if value can't be converted
Reproducible with the following steps:
```
julia> using DataStructures
julia> cb = CircularBuffer{Int}(5);
julia> size(cb)
(0,)
julia> push!(cb, 1.5)
ERROR: InexactError: Int64(1.5)
[...]
julia> size(cb)
(1,)
pkg> status
[...]
  [864edb3b] DataStructures v0.18.9
[...]
```

This seems strongly like unexpected behavior to me and should probably be fixed.

Pull request description:
closes #754

Patch:
diff --git a/src/circular_buffer.jl b/src/circular_buffer.jl
index ab56dfa8f..e1a6a88d7 100644
--- a/src/circular_buffer.jl
+++ b/src/circular_buffer.jl
@@ -101,14 +101,18 @@ end
 
 Add an element to the back and overwrite front if full.
 """
-@inline function Base.push!(cb::CircularBuffer, data)
+@inline function Base.push!(cb::CircularBuffer{T}, data) where T
+
+    # As per the behaviour of Base.push!
+    data_converted = convert(T, data)
+
     # if full, increment and overwrite, otherwise push
     if cb.length == cb.capacity
         cb.first = (cb.first == cb.capacity ? 1 : cb.first + 1)
     else
         cb.length += 1
     end
-    @inbounds cb.buffer[_buffer_index(cb, cb.length)] = data
+    @inbounds cb.buffer[_buffer_index(cb, cb.length)] = data_converted
     return cb
 end
 


Test patch:
diff --git a/test/test_circular_buffer.jl b/test/test_circular_buffer.jl
index 5c6af105d..24b90edd5 100644
--- a/test/test_circular_buffer.jl
+++ b/test/test_circular_buffer.jl
@@ -165,6 +165,15 @@
         end
     end
 
+    @testset "Issue 754" begin
+        cb = CircularBuffer{Int}(5)
+        @test size(cb) == (0,)
+        @test_throws InexactError push!(cb, 1.5)
+        @test size(cb) == (0,)
+        push!(cb, 1.0)
+        @test size(cb) == (1,)
+    end
+
     @testset "resize!" begin
         @testset "resize an empty buffer" begin
             cb = CircularBuffer{Int}(3)