v2 / vlib / v / tests / casts / cast_to_byte_test.v
41 lines · 35 sloc · 847 bytes · 6488041a749df9762348d019c4223908c476f2e2
Raw
1// see: https://discordapp.com/channels/592103645835821068/592114487759470596/762270244566728704
2enum WireType {
3 varint = 0
4 _64bit = 1
5 length_prefixed = 2
6 _32bit = 5
7}
8
9fn pack_wire_type(w WireType) u8 {
10 return u8(w)
11}
12
13fn test_casting_an_enum_to_u8() {
14 x := WireType.length_prefixed
15 y := pack_wire_type(x)
16 assert 'x: ${x}' == 'x: length_prefixed'
17 assert 'y: ${y.hex()}' == 'y: 02'
18}
19
20//
21
22fn test_casting_a_float_to_u8() {
23 x := 1.23
24 b := u8(x)
25 assert 'x: ${x} | b: ${b.hex()}' == 'x: 1.23 | b: 01'
26}
27
28fn test_casting_an_int_to_u8() {
29 x := 12
30 b := u8(x)
31 assert 'x: ${x} | b: ${b.hex()}' == 'x: 12 | b: 0c'
32}
33
34fn test_casting_a_bool_to_u8() {
35 x := true
36 b1 := u8(x)
37 assert 'x: ${x} | b: ${b1.hex()}' == 'x: true | b: 01'
38 y := false
39 b2 := u8(y)
40 assert 'y: ${y} | b: ${b2.hex()}' == 'y: false | b: 00'
41}
42