v2 / vlib / v / tests / casts / cast_to_array_test.v
47 lines · 38 sloc · 743 bytes · 064be5213df771af92af36f5268b77e798640f03
Raw
1module main
2
3type Bytes = [3]u8
4type Str = []u8
5type NumOrText = int | string
6
7fn test_cast_to_fixed_array() {
8 mut x := Bytes{}
9 x[0] = 1
10 x[1] = 2
11 x[2] = 3
12 dump(x)
13
14 y := [3]u8(x)
15 dump(y)
16 assert y.str() == '[1, 2, 3]'
17
18 z := Bytes(y)
19 dump(z)
20 assert z.str() == 'Bytes([1, 2, 3])'
21}
22
23fn test_cast_to_array() {
24 mut x := Str{len: 3}
25 x[0] = 10
26 x[1] = 20
27 x[2] = 30
28 dump(x)
29
30 y := []u8(x)
31 dump(y)
32 assert y.str() == '[10, 20, 30]'
33
34 z := Str(y)
35 dump(z)
36 assert z.str() == 'Str([10, 20, 30])'
37}
38
39fn test_cast_array_literal_to_array_type() {
40 values := []u32([10, 5, 10])
41 assert values == [u32(10), 5, 10]
42}
43
44fn test_cast_array_literal_to_sumtype_array() {
45 values := []NumOrText([1, 'x', 2])
46 assert values == [NumOrText(1), 'x', 2]
47}
48