v2 / vlib / v / tests / prefix_expr_test.v
46 lines · 37 sloc · 717 bytes · 16d14c660bd5bb79b6de3c2d202081aa83e90cf1
Raw
1import strconv
2
3fn value(n int) int {
4 return n
5}
6
7struct Foo {
8 n int
9}
10
11fn (foo Foo) value() int {
12 return foo.n
13}
14
15fn test_negative() {
16 one := 1
17 negative_one := -1
18 assert -one == negative_one
19 assert one == -negative_one
20
21 assert -value(1) == -1
22
23 // issue #9643
24 foo := Foo{1}
25 assert -foo.value() == -1
26 assert -(foo.value()) == -1
27
28 arr := [1, 2, 3]
29 assert -arr[0] == -1
30 assert -arr[1] == -2
31}
32
33fn test_positive() {
34 two := 2
35 assert +two == 2
36 assert +(two - 1) == 1
37}
38
39fn test_prefix_expr_in_assert_comparison() ! {
40 i := 2
41 assert strconv.atoi('1')! == 1
42 assert strconv.atoi('-2')! == -2
43 assert strconv.atoi('+2')! == i - 1 + 1
44 assert strconv.atoi('+2')! == -(-2)
45 assert strconv.atoi('+2')! == +2
46}
47