Commit 78b060e6 authored by David Owens II's avatar David Owens II

Removed the RingBuffer class; not needed.

Minor updates to the test infra.
Updates to the ScannerTests.
parent 4229cfc5
Pipeline #664 passed with stage
// Copyright (c) 2016 Anarchy Tools Contributors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import Foundation
/**
* A fixed size, continuous buffer.
*/
class RingBuffer<ElementType> {
let capacity: Int
var buffer: [ElementType?]
let overwrite: Bool
private var start: Int! = nil
private var end: Int! = nil
init(capacity: Int, overwrite: Bool = true) {
precondition(capacity > 1, "The capacity of the buffer must be greater than one.")
self.overwrite = overwrite
self.capacity = capacity
buffer = [ElementType?](count: capacity, repeatedValue: nil)
}
/**
* Inserts an element into the buffer. When the capacity has been met,
* the oldest item is overwritten if `overwrite` was set to `true`.
*/
func insert(element: ElementType) {
if start == nil {
buffer[0] = element
start = 0
end = 1
}
else {
if overwrite {
buffer[end] = element
end = (end + 1) % capacity
if end == start {
start = start + 1
}
}
else {
if start != end {
buffer[end] = element
end = (end + 1) % capacity
}
}
}
}
/**
* Removes the oldest item in the buffer.
*/
func remove() -> ElementType? {
if start == nil || end == nil { return nil }
let element = buffer[start]
buffer[start] = nil
start = (start + 1) % capacity
return element
}
/**
* Retrieves an item from the buffer at the given index.
*
* @param index The index within the buffer.
*/
subscript(index: Int) -> ElementType? {
precondition(index >= 0, "The index must be greater than or equal to 0.")
precondition(index < capacity, "The index must be less than the capacity.")
return buffer[index]
}
}
......@@ -39,7 +39,7 @@ enum Token {
}
}
static func isEqualTo(lhs: Token?, _ rhs: Token?) -> Bool {
static func isEqual(lhs: Token?, to rhs: Token?) -> Bool {
if lhs == nil && rhs == nil { return true }
guard let lhs = lhs else { return false }
......
......@@ -30,48 +30,48 @@ class LexerTests: Test {
let scanner = Scanner(content: content)
let lexer = Lexer(scanner: scanner)
try test.assert(Token.isEqualTo(lexer.next(), Token.Comment(" This is the most basic of sample files.", line: 0, column: 0)))
try test.assert(Token.isEqualTo(lexer.next(), Token.Terminal(line: 1, column: 0)))
try test.assert(Token.isEqualTo(lexer.next(), Token.OpenParen(line: 2, column: 0)))
try test.assert(Token.isEqualTo(lexer.next(), Token.Identifier("project", line: 2, column: 1)))
try test.assert(Token.isEqualTo(lexer.next(), Token.Terminal(line: 2, column: 8)))
try test.assert(Token.isEqualTo(lexer.next(), Token.Colon(line: 3, column: 2)))
try test.assert(Token.isEqualTo(lexer.next(), Token.Identifier("name", line: 3, column: 3)))
try test.assert(Token.isEqualTo(lexer.next(), Token.StringLiteral("\"basic\"", line: 3, column: 8)))
try test.assert(Token.isEqualTo(lexer.next(), Token.Terminal(line: 3, column: 15)))
try test.assert(Token.isEqualTo(lexer.next(), Token.Colon(line: 4, column: 2)))
try test.assert(Token.isEqualTo(lexer.next(), Token.Identifier("version", line: 4, column: 3)))
try test.assert(Token.isEqualTo(lexer.next(), Token.StringLiteral("\"0.1.0-dev\"", line: 4, column: 11)))
try test.assert(Token.isEqualTo(lexer.next(), Token.Terminal(line: 4, column: 22)))
try test.assert(Token.isEqualTo(lexer.next(), Token.Terminal(line: 5, column: 2)))
try test.assert(Token.isEqualTo(lexer.next(), Token.Colon(line: 6, column: 2)))
try test.assert(Token.isEqualTo(lexer.next(), Token.Identifier("tasks", line: 6, column: 3)))
try test.assert(Token.isEqualTo(lexer.next(), Token.OpenBrace(line: 6, column: 9)))
try test.assert(Token.isEqualTo(lexer.next(), Token.Colon(line: 6, column: 10)))
try test.assert(Token.isEqualTo(lexer.next(), Token.Identifier("build", line: 6, column: 11)))
try test.assert(Token.isEqualTo(lexer.next(), Token.OpenBrace(line: 6, column: 17)))
try test.assert(Token.isEqualTo(lexer.next(), Token.Colon(line: 6, column: 18)))
try test.assert(Token.isEqualTo(lexer.next(), Token.Identifier("tool", line: 6, column: 19)))
try test.assert(Token.isEqualTo(lexer.next(), Token.StringLiteral("\"lldb-build\"", line: 6, column: 24)))
try test.assert(Token.isEqualTo(lexer.next(), Token.Terminal(line: 6, column: 36)))
try test.assert(Token.isEqualTo(lexer.next(), Token.Colon(line: 7, column: 18)))
try test.assert(Token.isEqualTo(lexer.next(), Token.Identifier("name", line: 7, column: 19)))
try test.assert(Token.isEqualTo(lexer.next(), Token.StringLiteral("\"json-swift\"", line: 7, column: 24)))
try test.assert(Token.isEqualTo(lexer.next(), Token.Terminal(line: 7, column: 36)))
try test.assert(Token.isEqualTo(lexer.next(), Token.Colon(line: 8, column: 18)))
try test.assert(Token.isEqualTo(lexer.next(), Token.Identifier("output-type", line: 8, column: 19)))
try test.assert(Token.isEqualTo(lexer.next(), Token.StringLiteral("\"lib\"", line: 8, column: 31)))
try test.assert(Token.isEqualTo(lexer.next(), Token.Terminal(line: 8, column: 37)))
try test.assert(Token.isEqualTo(lexer.next(), Token.Colon(line: 9, column: 18)))
try test.assert(Token.isEqualTo(lexer.next(), Token.Identifier("source", line: 9, column: 19)))
try test.assert(Token.isEqualTo(lexer.next(), Token.OpenBracket(line: 9, column: 26)))
try test.assert(Token.isEqualTo(lexer.next(), Token.StringLiteral("\"src/**.swift\"", line: 9, column: 28)))
try test.assert(Token.isEqualTo(lexer.next(), Token.CloseBracket(line: 9, column: 43)))
try test.assert(Token.isEqualTo(lexer.next(), Token.CloseBrace(line: 9, column: 44)))
try test.assert(Token.isEqualTo(lexer.next(), Token.CloseBrace(line: 9, column: 45)))
try test.assert(Token.isEqualTo(lexer.next(), Token.Terminal(line: 9, column: 46)))
try test.assert(Token.isEqualTo(lexer.next(), Token.CloseParen(line: 10, column: 0)))
try test.assert(Token.isEqualTo(lexer.next(), Token.Terminal(line: 10, column: 1)))
try test.assert(Token.isEqualTo(lexer.next(), Token.EOF))
try test.assert(Token.isEqual(lexer.next(), to: Token.Comment(" This is the most basic of sample files.", line: 0, column: 0)))
try test.assert(Token.isEqual(lexer.next(), to: Token.Terminal(line: 1, column: 0)))
try test.assert(Token.isEqual(lexer.next(), to: Token.OpenParen(line: 2, column: 0)))
try test.assert(Token.isEqual(lexer.next(), to: Token.Identifier("project", line: 2, column: 1)))
try test.assert(Token.isEqual(lexer.next(), to: Token.Terminal(line: 2, column: 8)))
try test.assert(Token.isEqual(lexer.next(), to: Token.Colon(line: 3, column: 2)))
try test.assert(Token.isEqual(lexer.next(), to: Token.Identifier("name", line: 3, column: 3)))
try test.assert(Token.isEqual(lexer.next(), to: Token.StringLiteral("\"basic\"", line: 3, column: 8)))
try test.assert(Token.isEqual(lexer.next(), to: Token.Terminal(line: 3, column: 15)))
try test.assert(Token.isEqual(lexer.next(), to: Token.Colon(line: 4, column: 2)))
try test.assert(Token.isEqual(lexer.next(), to: Token.Identifier("version", line: 4, column: 3)))
try test.assert(Token.isEqual(lexer.next(), to: Token.StringLiteral("\"0.1.0-dev\"", line: 4, column: 11)))
try test.assert(Token.isEqual(lexer.next(), to: Token.Terminal(line: 4, column: 22)))
try test.assert(Token.isEqual(lexer.next(), to: Token.Terminal(line: 5, column: 2)))
try test.assert(Token.isEqual(lexer.next(), to: Token.Colon(line: 6, column: 2)))
try test.assert(Token.isEqual(lexer.next(), to: Token.Identifier("tasks", line: 6, column: 3)))
try test.assert(Token.isEqual(lexer.next(), to: Token.OpenBrace(line: 6, column: 9)))
try test.assert(Token.isEqual(lexer.next(), to: Token.Colon(line: 6, column: 10)))
try test.assert(Token.isEqual(lexer.next(), to: Token.Identifier("build", line: 6, column: 11)))
try test.assert(Token.isEqual(lexer.next(), to: Token.OpenBrace(line: 6, column: 17)))
try test.assert(Token.isEqual(lexer.next(), to: Token.Colon(line: 6, column: 18)))
try test.assert(Token.isEqual(lexer.next(), to: Token.Identifier("tool", line: 6, column: 19)))
try test.assert(Token.isEqual(lexer.next(), to: Token.StringLiteral("\"lldb-build\"", line: 6, column: 24)))
try test.assert(Token.isEqual(lexer.next(), to: Token.Terminal(line: 6, column: 36)))
try test.assert(Token.isEqual(lexer.next(), to: Token.Colon(line: 7, column: 18)))
try test.assert(Token.isEqual(lexer.next(), to: Token.Identifier("name", line: 7, column: 19)))
try test.assert(Token.isEqual(lexer.next(), to: Token.StringLiteral("\"json-swift\"", line: 7, column: 24)))
try test.assert(Token.isEqual(lexer.next(), to: Token.Terminal(line: 7, column: 36)))
try test.assert(Token.isEqual(lexer.next(), to: Token.Colon(line: 8, column: 18)))
try test.assert(Token.isEqual(lexer.next(), to: Token.Identifier("output-type", line: 8, column: 19)))
try test.assert(Token.isEqual(lexer.next(), to: Token.StringLiteral("\"lib\"", line: 8, column: 31)))
try test.assert(Token.isEqual(lexer.next(), to: Token.Terminal(line: 8, column: 37)))
try test.assert(Token.isEqual(lexer.next(), to: Token.Colon(line: 9, column: 18)))
try test.assert(Token.isEqual(lexer.next(), to: Token.Identifier("source", line: 9, column: 19)))
try test.assert(Token.isEqual(lexer.next(), to: Token.OpenBracket(line: 9, column: 26)))
try test.assert(Token.isEqual(lexer.next(), to: Token.StringLiteral("\"src/**.swift\"", line: 9, column: 28)))
try test.assert(Token.isEqual(lexer.next(), to: Token.CloseBracket(line: 9, column: 43)))
try test.assert(Token.isEqual(lexer.next(), to: Token.CloseBrace(line: 9, column: 44)))
try test.assert(Token.isEqual(lexer.next(), to: Token.CloseBrace(line: 9, column: 45)))
try test.assert(Token.isEqual(lexer.next(), to: Token.Terminal(line: 9, column: 46)))
try test.assert(Token.isEqual(lexer.next(), to: Token.CloseParen(line: 10, column: 0)))
try test.assert(Token.isEqual(lexer.next(), to: Token.Terminal(line: 10, column: 1)))
try test.assert(Token.isEqual(lexer.next(), to: Token.EOF))
}
}
\ No newline at end of file
// Copyright (c) 2016 Anarchy Tools Contributors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
class RingBufferTests: Test {
required init() {}
let tests = [
RingBufferTests.testBasicInit,
RingBufferTests.testInsertion,
RingBufferTests.testRemove,
RingBufferTests.testNoOverwrite
]
let filename: String = __FILE__
static func testBasicInit() throws {
let buffer = RingBuffer<Int>(capacity: 10)
try test.assert(buffer.capacity == 10)
}
static func testInsertion() throws {
let buffer = RingBuffer<Int>(capacity: 3)
try test.assert(buffer.capacity == 3)
buffer.insert(100)
try test.assert(buffer[0] == 100)
buffer.insert(101)
try test.assert(buffer[1] == 101)
buffer.insert(102)
try test.assert(buffer[2] == 102)
buffer.insert(103)
try test.assert(buffer[0] == 103)
}
static func testRemove() throws {
let buffer = RingBuffer<Int>(capacity: 3)
try test.assert(buffer.capacity == 3)
buffer.insert(100)
try test.assert(buffer[0] == 100)
try test.assert(buffer.remove() == 100)
try test.assert(buffer[0] == nil)
try test.assert(buffer[1] == nil)
try test.assert(buffer[2] == nil)
buffer.insert(100)
buffer.insert(101)
try test.assert(buffer[0] == nil)
try test.assert(buffer[1] == 100)
try test.assert(buffer[2] == 101)
try test.assert(buffer.remove() == 100)
try test.assert(buffer.remove() == 101)
try test.assert(buffer.remove() == nil)
try test.assert(buffer.remove() == nil)
buffer.insert(100)
buffer.insert(101)
try test.assert(buffer[0] == 100)
try test.assert(buffer[1] == 101)
try test.assert(buffer[2] == nil)
}
static func testNoOverwrite() throws {
let buffer = RingBuffer<Int>(capacity: 3, overwrite: false)
try test.assert(buffer.capacity == 3)
buffer.insert(100)
buffer.insert(101)
buffer.insert(102)
buffer.insert(103)
try test.assert(buffer[0] == 100)
try test.assert(buffer[1] == 101)
try test.assert(buffer[2] == 102)
try test.assert(buffer.remove() == 100)
try test.assert(buffer[0] == nil)
try test.assert(buffer[1] == 101)
try test.assert(buffer[2] == 102)
buffer.insert(103)
try test.assert(buffer[0] == 103)
try test.assert(buffer[1] == 101)
try test.assert(buffer[2] == 102)
}
}
\ No newline at end of file
This diff is collapsed.
......@@ -17,9 +17,11 @@
extension String : ErrorType {}
enum test {
static func assert(condition: Bool, functionName: String = __FUNCTION__) throws {
print(" \(functionName): \(condition ? "PASSED" : "**FAILED**")")
if !condition { throw "clj.tests.failed" }
static func assert(condition: Bool, file: String = __FILE__, functionName: String = __FUNCTION__, line: Int = __LINE__) throws {
if !condition {
print(" \(file):\(line) \(functionName) **FAILED**")
throw "clj.tests.failed"
}
}
}
......@@ -45,7 +47,6 @@ print()
let tests: [Test] = [
// NOTE: Add your test classes here...
RingBufferTests(),
ScannerTests(),
LexerTests()
]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment