Hello!
I’ve been trying to solve the circular buffer, my code works locally and in other online editors. The provided error is:
An error occurred while running your tests. This might mean that there was an issue in our infrastructure, or it might mean that you have something in your code that’s causing our systems to break.
Please check your code, and if nothing seems to be wrong, try running the tests again.
so I’ve tried to scale it down to find the source of the bug, and it seems like everytime I try to mutate a value inside of a mutating func it breaks the CI. Example Code:
struct CircularBuffer {
var capacity: Int
var current: Int = 0
var arr: [Int] = []
var readIndex: Int = 0
var writeIndex: Int = 0
init(capacity: Int) {
self.capacity = capacity
arr.append(contentsOf: Array(0..<capacity))
}
func clear() {}
func read() throws -> Int {
guard current > 0 else { throw CircularBufferError.bufferEmpty }
return 0
}
mutating func write(_ value: Int) throws {
guard current < capacity else {
throw CircularBufferError.bufferEmpty
}
current = current + 1
}
func overwrite(_ value: Int) {}
}
enum CircularBufferError: Error {
case bufferEmpty
case bufferFull
}
so in the code above if you comment line 25(current = current + 1, I also tried with current += 1 and adding self. beforehand) suddently it works, it doesn’t pass the tests but the original error stops appearing.
My previous solution was:
import Foundation
enum CircularBufferError: Error {
case bufferFull
case bufferEmpty
}
struct CircularBuffer {
var size: Int
var count: Int = 0
var arr: Array<Int>
var removeIndex: Int = 0
var insertIndex: Int = 0
init(capacity: Int){
self.size = capacity
self.arr = Array(0..<size)
}
mutating func clear() {
self.count = 0
self.removeIndex = 0
self.insertIndex = 0
self.arr = Array(repeating: 0, count: capacity)
}
mutating func read() throws -> Int {
guard count > 0 else{ throw CircularBufferError.bufferEmpty
}
let val = arr[removeIndex]
removeIndex = increment(removeIndex)
count -= 1
return val
}
mutating func write(_ value: Int) throws {
guard count < size else {
throw CircularBufferError.bufferFull
}
self.count += 1
self.arr[insertIndex] = value
self.insertIndex = increment(insertIndex)
}
mutating func overwrite(_ value: Int) {
self.arr[insertIndex] = value
self.insertIndex = increment(insertIndex)
self.count += 1
}
private func increment(_ head: Int) -> Int {
return ((head + 1) >= size) ? 0 : (head + 1)
}
}