summary refs log tree commit diff
path: root/src/test/run-pass/ternary.rs
blob: 6adfada2c90e99aa275c378d6ec9eb80dc5f1e94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
fn test_simple() { let x = true ? 10 : 11; assert (x == 10); }

fn test_precedence() {
    let x;

    x = true || true ? 10 : 11;
    assert (x == 10);

    x = true == false ? 10 : 11;
    assert (x == 11);

    x = true ? false ? 10 : 11 : 12;
    assert (x == 11);

    let y = true ? 0xF0 : 0x0 | 0x0F;
    assert (y == 0xF0);

    y = true ? 0xF0 | 0x0F : 0x0;
    assert (y == 0xFF);
}

fn test_associativity() {
    // Ternary is right-associative
    let x = false ? 10 : false ? 11 : 12;
    assert (x == 12);
}

fn test_lval() {
    let box1: @mutable int = @mutable 10;
    let box2: @mutable int = @mutable 10;
    *(true ? box1 : box2) = 100;
    assert (*box1 == 100);
}

fn test_as_stmt() { let s; true ? s = 10 : s = 12; assert (s == 10); }

fn main() {
    test_simple();
    test_precedence();
    test_associativity();
    test_lval();
    test_as_stmt();
}