| 1 | module main |
| 2 | |
| 3 | type Bytes = [3]u8 |
| 4 | type Str = []u8 |
| 5 | type NumOrText = int | string |
| 6 | |
| 7 | fn 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 | |
| 23 | fn 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 | |
| 39 | fn test_cast_array_literal_to_array_type() { |
| 40 | values := []u32([10, 5, 10]) |
| 41 | assert values == [u32(10), 5, 10] |
| 42 | } |
| 43 | |
| 44 | fn test_cast_array_literal_to_sumtype_array() { |
| 45 | values := []NumOrText([1, 'x', 2]) |
| 46 | assert values == [NumOrText(1), 'x', 2] |
| 47 | } |
| 48 | |