zeolite-lang-0.22.0.0: lib/container/test/vector.0rt
/* -----------------------------------------------------------------------------
Copyright 2021 Kevin P. Barry
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.
----------------------------------------------------------------------------- */
// Author: Kevin P. Barry [ta0kira@gmail.com]
testcase "Vector tests" {
success
}
unittest createSize {
Vector<Int> values <- Vector:createSize<Int>(10)
\ Testing.checkEquals(values.size(),10)
scoped {
Int i <- 0
} in while (i < values.size()) {
\ Testing.checkEquals(values.readAt(i),Int.default())
} update {
i <- i+1
}
}
unittest append {
Vector<Int> values <- Vector<Int>.new()
scoped {
Int i <- 0
} in while (i < 10) {
\ values.append(i)
\ Testing.checkEquals(values.size(),i+1)
} update {
i <- i+1
}
\ Testing.checkEquals(values.size(),10)
scoped {
Int i <- 0
} in while (i < values.size()) {
\ Testing.checkEquals(values.readAt(i),i)
} update {
i <- i+1
}
}
unittest pushAndPop {
Vector<Int> values <- Vector<Int>.new()
scoped {
Int i <- 0
} in while (i < 10) {
\ values.push(i)
\ Testing.checkEquals(values.size(),i+1)
} update {
i <- i+1
}
\ Testing.checkEquals(values.size(),10)
scoped {
Int i <- 0
} in while (i < 10) {
\ Testing.checkEquals(values.pop(),10-i-1)
\ Testing.checkEquals(values.size(),10-i-1)
} update {
i <- i+1
}
\ Testing.checkEquals(values.size(),0)
}
unittest writeAt {
Vector<Int> values <- Vector:createSize<Int>(10)
scoped {
Int i <- 0
} in while (i < values.size()) {
\ values.writeAt(i,2*i)
} update {
i <- i+1
}
scoped {
Int i <- 0
} in while (i < values.size()) {
\ Testing.checkEquals(values.readAt(i),2*i)
} update {
i <- i+1
}
}
unittest traverseVector {
Int index <- 0
Vector<Int> vector <- Vector<Int>.new()
\ vector.push(2)
\ vector.push(6)
\ vector.push(3)
\ vector.push(5)
\ vector.push(4)
\ vector.push(1)
traverse (vector.defaultOrder() -> Int i) {
\ Testing.checkEquals(i,vector.readAt(index))
index <- index+1
}
\ Testing.checkEquals(index,6)
}
unittest duplicate {
Vector<Int> vector <- Vector<Int>.default().push(1).push(2)
Vector<Int> copy <- vector.duplicate()
\ Testing.checkEquals(copy.size(),2)
\ Testing.checkEquals(copy.readAt(0),1)
\ Testing.checkEquals(copy.readAt(1),2)
\ copy.writeAt(1,3)
\ copy.pop()
\ Testing.checkEquals(copy.size(),1)
\ Testing.checkEquals(copy.readAt(0),1)
\ Testing.checkEquals(vector.readAt(1),2)
}
concrete Value {
refines Duplicate
@type new (Int) -> (#self)
@value set (Int) -> ()
@value get () -> (Int)
}
define Value {
@value Int value
new (value) { return Value{ value } }
set (value2) { value <- value2 }
get () { return value }
duplicate () { return new(value) }
}
testcase "distinct default values in pre-sized Vector" {
success
}
unittest test {
Vector<Type> values <- Vector:createSize<Type>(10)
\ values.readAt(3).set(7)
\ Testing.checkEquals(values.readAt(3),Type.create(7))
scoped {
Int i <- 0
} in while (i < values.size()) {
if (i != 3) {
\ Testing.checkEquals(values.readAt(i),Type.create(0))
}
} update {
i <- i+1
}
}
concrete Type {
refines Formatted
defines Default
defines Equals<Type>
@type create (Int) -> (Type)
@value set (Int) -> ()
}
define Type {
@value Int value
default () {
return create(0)
}
create (v) {
return Type{ v }
}
formatted () {
return value.formatted()
}
equals (x,y) {
return x.get() == y.get()
}
set (v) {
value <- v
}
@value get () -> (Int)
get () {
return value
}
}
testcase "negative Vector index" {
crash
}
unittest test {
Vector<Int> values <- Vector<Int>.new()
\ values.readAt(-1)
}
testcase "Vector index out of bounds" {
crash
}
unittest test {
Vector<Int> values <- Vector<Int>.new()
\ values.readAt(1)
}