Skip to content

Commit

Permalink
tests: add checker and passing test for generic receiver indexing (#2…
Browse files Browse the repository at this point in the history
  • Loading branch information
Delta456 committed Nov 29, 2023
1 parent 782bf86 commit fed3552
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
7 changes: 7 additions & 0 deletions vlib/v/checker/tests/generic_mut_struct_index_err.out
@@ -0,0 +1,7 @@
vlib/v/checker/tests/generic_mut_struct_index_err.vv:7:3: error: type `mut Heap` does not support slicing
5 |
6 | fn (mut h Heap[T]) insert(mut element T, pos int) {
7 | h[pos] = element
| ~~~~~
8 | }
9 |
17 changes: 17 additions & 0 deletions vlib/v/checker/tests/generic_mut_struct_index_err.vv
@@ -0,0 +1,17 @@
pub struct Heap[T] {
mut:
arr []&T
}

fn (mut h Heap[T]) insert(mut element T, pos int) {
h[pos] = element
}

fn main() {
mut h := &Heap[int]{
arr: []&int{cap: 10}
}
mut a := 3
h.insert(mut &a, 0)
dump(h)
}
15 changes: 15 additions & 0 deletions vlib/v/tests/generic_with_ptr_as_param_test.v
@@ -0,0 +1,15 @@
struct Number[T] {
value T
}

fn f[T](numbers &Number[T], idx int) T {
return unsafe { numbers[idx].value }
}

fn test_indexing_a_pointer_to_generic_instances() {
numbers := [10]Number[int]{init: Number[int]{
value: index * 10
}}
assert f(&numbers[0], 3) == 30
assert f(&numbers[0], 9) == 90
}

0 comments on commit fed3552

Please sign in to comment.