packages feed

zeolite-lang-0.24.0.0: lib/container/test/vector.0rt

/* -----------------------------------------------------------------------------
Copyright 2021,2023 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 TestChecker
}

unittest createSize {
  Vector<Int> values <- Vector:createSize<Int>(10)
  \ values.size() `Matches:with` CheckValue:equals(10)

  scoped {
    Int i <- 0
  } in while (i < values.size()) {
    \ values.readAt(i) `Matches:with` CheckValue:equals(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)
    \ values.size() `Matches:with` CheckValue:equals(i+1)
  } update {
    i <- i+1
  }
  \ values.size() `Matches:with` CheckValue:equals(10)

  scoped {
    Int i <- 0
  } in while (i < values.size()) {
    \ values.readAt(i) `Matches:with` CheckValue:equals(i)
  } update {
    i <- i+1
  }
}

unittest pushAndPop {
  Vector<Int> values <- Vector<Int>.new()

  scoped {
    Int i <- 0
  } in while (i < 10) {
    \ values.push(i)
    \ values.size() `Matches:with` CheckValue:equals(i+1)
  } update {
    i <- i+1
  }
  \ values.size() `Matches:with` CheckValue:equals(10)

  scoped {
    Int i <- 0
  } in while (i < 10) {
    \ values.pop() `Matches:with` CheckValue:equals(10-i-1)
    \ values.size() `Matches:with` CheckValue:equals(10-i-1)
  } update {
    i <- i+1
  }
  \ values.size() `Matches:with` CheckValue:equals(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()) {
    \ values.readAt(i) `Matches:with` CheckValue:equals(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) {
    \ i `Matches:with` CheckValue:equals(vector.readAt(index))
    index <- index+1
  }

  \ index `Matches:with` CheckValue:equals(6)
}

unittest duplicate {
  Vector<Int> vector <- Vector<Int>.default().push(1).push(2)

  Vector<Int> copy <- vector.duplicate()
  \ copy.size() `Matches:with` CheckValue:equals(2)
  \ copy.readAt(0) `Matches:with` CheckValue:equals(1)
  \ copy.readAt(1) `Matches:with` CheckValue:equals(2)

  \ copy.writeAt(1, 3)
  \ copy.pop()
  \ copy.size() `Matches:with` CheckValue:equals(1)
  \ copy.readAt(0) `Matches:with` CheckValue:equals(1)
  \ vector.readAt(1) `Matches:with` CheckValue:equals(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 TestChecker
}

unittest test {
  Vector<Type> values <- Vector:createSize<Type>(10)
  \ values.readAt(3).set(7)

  \ values.readAt(3) `Matches:with` CheckValue:equals(Type.create(7))

  scoped {
    Int i <- 0
  } in while (i < values.size()) {
    if (i != 3) {
      \ values.readAt(i) `Matches:with` CheckValue:equals(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" {
  failure
}

unittest test {
  Vector<Int> values <- Vector<Int>.new()
  \ values.readAt(-1)
}


testcase "Vector index out of bounds" {
  failure
}

unittest test {
  Vector<Int> values <- Vector<Int>.new()
  \ values.readAt(1)
}