2021-12-26 17:01:36 +03:00
|
|
|
module datatypes
|
2021-12-23 19:23:04 +03:00
|
|
|
|
|
|
|
fn test_is_empty() {
|
2022-11-26 19:23:26 +03:00
|
|
|
mut list := LinkedList[int]{}
|
2021-12-23 19:23:04 +03:00
|
|
|
assert list.is_empty() == true
|
|
|
|
list.push(1)
|
|
|
|
assert list.is_empty() == false
|
|
|
|
}
|
|
|
|
|
2022-11-28 11:24:47 +03:00
|
|
|
fn test_len() {
|
2022-11-26 19:23:26 +03:00
|
|
|
mut list := LinkedList[int]{}
|
2021-12-23 19:23:04 +03:00
|
|
|
assert list.len() == 0
|
|
|
|
list.push(1)
|
|
|
|
assert list.len() == 1
|
2022-11-28 11:24:47 +03:00
|
|
|
list.pop()!
|
2021-12-23 19:23:04 +03:00
|
|
|
assert list.len() == 0
|
|
|
|
}
|
|
|
|
|
2022-11-28 11:24:47 +03:00
|
|
|
fn test_first() {
|
2022-11-26 19:23:26 +03:00
|
|
|
mut list := LinkedList[int]{}
|
2021-12-23 19:23:04 +03:00
|
|
|
list.push(1)
|
2022-11-28 11:24:47 +03:00
|
|
|
assert list.first()! == 1
|
2021-12-23 19:23:04 +03:00
|
|
|
list.push(2)
|
2022-11-28 11:24:47 +03:00
|
|
|
assert list.first()! == 1
|
2022-11-26 19:23:26 +03:00
|
|
|
list = LinkedList[int]{}
|
2021-12-23 19:23:04 +03:00
|
|
|
list.first() or { return }
|
|
|
|
assert false
|
|
|
|
}
|
|
|
|
|
2022-11-28 11:24:47 +03:00
|
|
|
fn test_last() {
|
2022-11-26 19:23:26 +03:00
|
|
|
mut list := LinkedList[int]{}
|
2021-12-23 19:23:04 +03:00
|
|
|
list.push(1)
|
2022-11-28 11:24:47 +03:00
|
|
|
assert list.last()! == 1
|
2021-12-23 19:23:04 +03:00
|
|
|
list.push(2)
|
2022-11-28 11:24:47 +03:00
|
|
|
assert list.last()! == 2
|
2022-11-26 19:23:26 +03:00
|
|
|
list = LinkedList[int]{}
|
2021-12-23 19:23:04 +03:00
|
|
|
list.last() or { return }
|
|
|
|
assert false
|
|
|
|
}
|
|
|
|
|
2022-11-28 11:24:47 +03:00
|
|
|
fn test_index() {
|
2022-11-26 19:23:26 +03:00
|
|
|
mut list := LinkedList[int]{}
|
2022-01-16 20:11:10 +03:00
|
|
|
list.push(1)
|
2022-11-28 11:24:47 +03:00
|
|
|
assert list.index(0)! == 1
|
2022-01-16 20:11:10 +03:00
|
|
|
list.push(2)
|
2022-11-28 11:24:47 +03:00
|
|
|
assert list.index(1)! == 2
|
|
|
|
list.pop()!
|
2022-01-16 20:11:10 +03:00
|
|
|
list.index(1) or { return }
|
|
|
|
assert false
|
|
|
|
}
|
|
|
|
|
2022-11-28 11:24:47 +03:00
|
|
|
fn test_push() {
|
2022-11-26 19:23:26 +03:00
|
|
|
mut list := LinkedList[int]{}
|
2021-12-23 19:23:04 +03:00
|
|
|
list.push(1)
|
2022-11-28 11:24:47 +03:00
|
|
|
assert list.last()! == 1
|
2021-12-23 19:23:04 +03:00
|
|
|
list.push(2)
|
2022-11-28 11:24:47 +03:00
|
|
|
assert list.last()! == 2
|
2021-12-23 19:23:04 +03:00
|
|
|
list.push(3)
|
2022-11-28 11:24:47 +03:00
|
|
|
assert list.last()! == 3
|
2021-12-23 19:23:04 +03:00
|
|
|
}
|
|
|
|
|
2022-11-28 11:24:47 +03:00
|
|
|
fn test_pop() {
|
2022-11-26 19:23:26 +03:00
|
|
|
mut list := LinkedList[int]{}
|
2021-12-23 19:23:04 +03:00
|
|
|
list.push(1)
|
|
|
|
list.push(2)
|
|
|
|
list.push(3)
|
2022-11-28 11:24:47 +03:00
|
|
|
assert list.pop()! == 3
|
2021-12-23 19:23:04 +03:00
|
|
|
list.push(4)
|
2022-11-28 11:24:47 +03:00
|
|
|
assert list.pop()! == 4
|
|
|
|
assert list.pop()! == 2
|
2022-11-26 19:23:26 +03:00
|
|
|
list = LinkedList[int]{}
|
2021-12-23 19:23:04 +03:00
|
|
|
list.pop() or { return }
|
|
|
|
assert false
|
|
|
|
}
|
|
|
|
|
2022-11-28 11:24:47 +03:00
|
|
|
fn test_shift() {
|
2022-11-26 19:23:26 +03:00
|
|
|
mut list := LinkedList[int]{}
|
2021-12-23 19:23:04 +03:00
|
|
|
list.push(1)
|
|
|
|
list.push(2)
|
|
|
|
list.push(3)
|
2022-11-28 11:24:47 +03:00
|
|
|
assert list.shift()! == 1
|
2021-12-23 19:23:04 +03:00
|
|
|
list.push(4)
|
2022-11-28 11:24:47 +03:00
|
|
|
assert list.shift()! == 2
|
|
|
|
assert list.shift()! == 3
|
2022-11-26 19:23:26 +03:00
|
|
|
list = LinkedList[int]{}
|
2021-12-23 19:23:04 +03:00
|
|
|
list.shift() or { return }
|
|
|
|
assert false
|
|
|
|
}
|
|
|
|
|
2022-11-28 11:24:47 +03:00
|
|
|
fn test_insert() {
|
2022-11-26 19:23:26 +03:00
|
|
|
mut list := LinkedList[int]{}
|
2021-12-23 19:23:04 +03:00
|
|
|
list.push(1)
|
|
|
|
list.push(2)
|
|
|
|
list.push(3)
|
|
|
|
list.insert(1, 111) or { return }
|
|
|
|
assert true
|
|
|
|
}
|
|
|
|
|
2022-11-28 11:24:47 +03:00
|
|
|
fn test_prepend() {
|
2022-11-26 19:23:26 +03:00
|
|
|
mut list := LinkedList[int]{}
|
2021-12-23 19:23:04 +03:00
|
|
|
list.push(1)
|
|
|
|
list.push(2)
|
|
|
|
list.push(3)
|
|
|
|
list.prepend(111)
|
2022-11-28 11:24:47 +03:00
|
|
|
assert list.first()! == 111
|
2021-12-23 19:23:04 +03:00
|
|
|
}
|
|
|
|
|
2022-11-28 11:24:47 +03:00
|
|
|
fn test_str() {
|
2022-11-26 19:23:26 +03:00
|
|
|
mut list := LinkedList[int]{}
|
2021-12-23 19:23:04 +03:00
|
|
|
list.push(1)
|
|
|
|
list.push(2)
|
|
|
|
list.push(3)
|
|
|
|
assert list.str() == '[1, 2, 3]'
|
|
|
|
}
|
2022-08-25 14:12:39 +03:00
|
|
|
|
2022-11-28 11:24:47 +03:00
|
|
|
fn test_array() {
|
2022-11-26 19:23:26 +03:00
|
|
|
mut list := LinkedList[int]{}
|
2022-08-25 14:12:39 +03:00
|
|
|
list.push(1)
|
|
|
|
list.push(2)
|
|
|
|
list.push(3)
|
|
|
|
assert list.array() == [1, 2, 3]
|
|
|
|
}
|
2022-09-23 22:29:13 +03:00
|
|
|
|
2022-11-28 11:24:47 +03:00
|
|
|
fn test_linked_list_iterating_with_for() {
|
2022-11-26 19:23:26 +03:00
|
|
|
mut list := LinkedList[int]{}
|
2022-09-23 22:29:13 +03:00
|
|
|
list.push(1)
|
|
|
|
list.push(2)
|
|
|
|
list.push(3)
|
|
|
|
mut res := []int{}
|
|
|
|
for x in list {
|
|
|
|
res << x
|
|
|
|
}
|
|
|
|
assert res == [1, 2, 3]
|
|
|
|
}
|
|
|
|
|
2022-11-28 11:24:47 +03:00
|
|
|
fn test_linked_list_separate_iterators() {
|
2022-11-26 19:23:26 +03:00
|
|
|
mut list := LinkedList[int]{}
|
2022-09-23 22:29:13 +03:00
|
|
|
list.push(1)
|
|
|
|
list.push(2)
|
|
|
|
list.push(3)
|
|
|
|
mut it1 := list.iterator()
|
|
|
|
mut it2 := list.iterator()
|
|
|
|
mut it3 := list.iterator()
|
2022-11-28 11:24:47 +03:00
|
|
|
assert it1.next()! == 1
|
|
|
|
assert it1.next()! == 2
|
|
|
|
assert it1.next()! == 3
|
|
|
|
assert it2.next()! == 1
|
2022-09-23 22:29:13 +03:00
|
|
|
if _ := it1.next() {
|
|
|
|
assert false
|
|
|
|
} else {
|
|
|
|
assert true
|
|
|
|
}
|
|
|
|
if _ := it1.next() {
|
|
|
|
assert false
|
|
|
|
} else {
|
|
|
|
assert true
|
|
|
|
}
|
2022-11-28 11:24:47 +03:00
|
|
|
assert it2.next()! == 2
|
|
|
|
assert it2.next()! == 3
|
2022-09-23 22:29:13 +03:00
|
|
|
if _ := it2.next() {
|
|
|
|
assert false
|
|
|
|
} else {
|
|
|
|
assert true
|
|
|
|
}
|
|
|
|
mut res := []int{}
|
|
|
|
for x in it3 {
|
|
|
|
res << x
|
|
|
|
}
|
|
|
|
assert res == [1, 2, 3]
|
|
|
|
}
|