Skip to content

Commit

Permalink
datatypes: make Direction pub and fix and add tests for push_many (
Browse files Browse the repository at this point in the history
  • Loading branch information
Delta456 committed Nov 24, 2023
1 parent 43709e9 commit 67fabdd
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
5 changes: 3 additions & 2 deletions vlib/datatypes/doubly_linked_list.v
@@ -1,6 +1,6 @@
module datatypes

enum Direction {
pub enum Direction {
front
back
}
Expand Down Expand Up @@ -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)
}
}
Expand Down
15 changes: 15 additions & 0 deletions vlib/datatypes/doubly_linked_list_test.v
Expand Up @@ -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]
}

0 comments on commit 67fabdd

Please sign in to comment.