datatypes: make Direction pub and fix and add tests for push_many (#19983)

This commit is contained in:
Swastik Baranwal 2023-11-24 19:40:00 +05:30 committed by GitHub
parent 43709e9c58
commit 67fabddcaf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 2 deletions

View File

@ -1,6 +1,6 @@
module datatypes
enum Direction {
pub enum Direction {
front
back
}
@ -88,7 +88,8 @@ pub fn (mut list DoublyLinkedList[T]) push_front(item T) {
pub fn (mut list DoublyLinkedList[T]) push_many(elements []T, direction Direction) {
match direction {
.front {
for v in elements {
for i := elements.len - 1; i >= 0; i-- {
v := elements[i]
list.push_front(v)
}
}

View File

@ -230,3 +230,18 @@ fn test_back_iterator() {
}
assert res == [3, 2, 1]
}
fn test_push_many() {
mut list := DoublyLinkedList[int]{}
list.push_back(1)
list.push_back(2)
list.push_back(3)
list.push_many([4, 5, 6], .front)
list.push_many([7, 8, 9], .back)
mut res := []int{}
for x in list {
res << x
}
assert res == [4, 5, 6, 1, 2, 3, 7, 8, 9]
}