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