v2 / vlib / v / tests / consts / constant_array_size_test.v
41 lines · 36 sloc · 1.09 KB · 6488041a749df9762348d019c4223908c476f2e2
Raw
1const c_a_s = 1
2const c_b_s = 1 + 1
3const c_c_s = c_b_s + 1
4
5fn test_consant_array_size() {
6 mut a := [c_a_s]int{}
7 a = [1]!
8 mut b := [c_b_s]int{}
9 b = [1, 2]!
10}
11
12// for 19593
13// test const was declared below struct fixed array fields declaration
14struct Foo {
15 arr [width][2][width + 1]f64
16}
17
18fn test_const_below_at_struct_fixed_array_fields() {
19 foo := Foo{}
20 assert foo.arr.len == 2
21 assert foo.arr[0].len == 2
22 assert foo.arr[0][0].len == 3
23 assert foo.arr == [[[0.0, 0.0, 0.0]!, [0.0, 0.0, 0.0]!]!,
24 [[0.0, 0.0, 0.0]!, [0.0, 0.0, 0.0]!]!]!
25}
26
27// for issue 20311
28// when using a const variable to define a fixed array size,
29// if the const variable is defined below or in another module, the size value will not be calculated correctly.
30fn test_const_below_at_fixed_array() {
31 arr := [width][2][width + 1]f64{}
32 assert arr.len == 2
33 assert arr[0].len == 2
34 assert arr[0][0].len == 3
35 assert arr == [[[0.0, 0.0, 0.0]!, [0.0, 0.0, 0.0]!]!, [[0.0, 0.0, 0.0]!,
36 [0.0, 0.0, 0.0]!]!]!
37}
38
39// do not move this definition,
40// it must be below `struct Foo {...}` and `fn test_const_below_at_fixed_array()`.
41const width = 2
42