v2 / vlib / v / tests / structs / struct_address_of_field_comparison_test.v
27 lines · 25 sloc · 654 bytes · 1c34ac6eda831af9cea273ff6c6a2844ef3fcf96
Raw
1// Regression test for https://github.com/vlang/v/issues/27089
2// Comparing a stored reference with `&expr` should compare addresses,
3// not perform a deep struct equality check.
4struct Data {
5mut:
6 a f32
7 b int
8 c string
9}
10
11struct Holder {
12mut:
13 buf Data
14 ref &Data = unsafe { nil }
15}
16
17fn test_address_of_field_compares_addresses() {
18 mut t := Holder{}
19 // `t.ref` is nil, so address comparison must be true (nil != &t.buf).
20 assert t.ref != &t.buf
21 t.ref = &t.buf
22 // Now both refer to the same address.
23 assert t.ref == &t.buf
24 other := Data{}
25 // Different addresses with structurally equal contents must still be unequal.
26 assert &other != &t.buf
27}
28