nimdeque/tests/linked.nim

134 lines
4.4 KiB
Nim

# Copyright 2022 Mattia Giambirtone
#
# 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 random
import strformat
import ../src/nimdeque
when isMainModule:
const size = 1000
var deque = newLinkedDeque[int]()
echo &"Generating {size} values"
for i in countup(0, size - 1, 1):
deque.add(i)
echo "\t- Checking length"
doAssert deque.len() == size
echo "Checking iteration"
echo "\t- Checking indeces"
for i in countup(0, size - 1, 1):
doAssert deque[i] == i
echo "\t- Checking pairs"
for i, e in deque:
assert i == e
echo "\t- Checking reversed iteration"
var j = 0
for e in deque.reversed():
assert size - j - 1 == e
inc(j)
echo "Checking contains"
for i in countup(0, size - 1, 1):
doAssert i in deque
echo "Popping off the head"
doAssert deque.pop() == 0
echo "\t- Checking length"
doAssert deque.len() == size - 1
echo "\t- Checking new head"
doAssert deque[0] == 1
echo "Popping off the tail"
doAssert deque.pop(deque.high()) == size - 1
echo "\t- Checking length"
doAssert deque.len() == size - 2
echo "\t- Checking new tail"
doAssert deque[deque.high()] == size - 2
echo "Re-checking values"
for i in countup(0, size - 3, 1):
doAssert deque[i] == i + 1
echo "Checking addLeft"
deque.addLeft(0)
echo "\t- Checking length"
doAssert deque.len() == size - 1
echo "\t- Re-checking head"
doAssert deque[0] == 0
echo "Re-checking values"
for i in countup(0, size - 2, 1):
doAssert deque[i] == i
echo "Checking insert(3)"
var oldLen = deque.len()
deque.insert(3, 69420)
echo "\t- Checking length"
doAssert oldLen + 1 == deque.len()
echo "\t- Checking inserted value"
doAssert deque.pop(3) == 69420
echo "\t- Checking length"
doAssert deque.len() == oldLen
echo &"Checking insert({size - 2})"
oldLen = deque.len()
deque.insert(size - 2, 0x42362)
echo "\t- Checking length"
doAssert oldLen + 1 == deque.len()
echo "\t- Checking inserted value"
doAssert deque.pop(size - 1) == 0x42362
echo "\t- Checking length"
doAssert deque.len() == oldLen
echo &"Checking insert({size div 2})"
oldLen = deque.len()
deque.insert(size div 2, 0xf7102)
echo "\t- Checking length"
doAssert oldLen + 1 == deque.len()
echo "\t- Checking inserted value"
doAssert deque.pop(size div 2) == 0xf7102
echo "\t- Checking length"
doAssert deque.len() == oldLen
randomize()
let idx = rand(size - 1)
echo &"Checking insert({idx})"
oldLen = deque.len()
deque.insert(size - 2, idx)
echo "\t- Checking length"
doAssert oldLen + 1 == deque.len()
echo "\t- Checking inserted value"
doAssert deque.pop(size - 1) == idx
echo "\t- Checking length"
doAssert deque.len() == oldLen
echo "Checking backwards indeces"
for i in countdown(deque.high(), 1):
doAssert deque[^i] == deque[deque.len() - i]
deque.add(deque.pop(^1))
doAssert deque[deque.high()] == deque[^1]
echo &"Checking maxSize ({size div 2})"
var queue = newLinkedDeque[int](size div 2)
echo &"\t- Generating {size div 2} values"
for i in countup(0, (size div 2) - 1):
queue.add(i)
echo "\t- Checking length"
doAssert queue.len() == size div 2
var temp = queue[0]
echo "\t- Testing append at the end"
queue.add((size div 2) + 1)
echo "\t- Checking length"
doAssert queue.len() == size div 2
echo "\t- Checking item"
doAssert queue[^1] == (size div 2) + 1
echo "\t- Checking displacement"
doAssert queue[0] == temp + 1
echo "Testing prepend at the beginning"
queue.addLeft(0)
echo "\t- Checking length"
doAssert queue.len() == size div 2
echo "\t- Checking item"
doAssert queue[0] == 0
echo "\t- Checking displacement"
doAssert queue[^1] == (size div 2) - 1
echo "All tests passed!"