summaryrefslogtreecommitdiffstats
path: root/src/test/ui/structs-enums/tag-variant-disr-val.rs
blob: 297d85c588679fdc0fd3adaa08f10645930493d6 (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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// run-pass
#![allow(non_camel_case_types)]

use color::{red, green, blue, black, white, imaginary, purple, orange};

#[derive(Copy, Clone)]
enum color {
    red = 0xff0000,
    green = 0x00ff00,
    blue = 0x0000ff,
    black = 0x000000,
    white = 0xFFFFFF,
    imaginary = -1,
    purple = 1 << 1,
    orange = 8 >> 1
}

impl PartialEq for color {
    fn eq(&self, other: &color) -> bool {
        ((*self) as usize) == ((*other) as usize)
    }
    fn ne(&self, other: &color) -> bool { !(*self).eq(other) }
}

pub fn main() {
    test_color(red, 0xff0000, "red".to_string());
    test_color(green, 0x00ff00, "green".to_string());
    test_color(blue, 0x0000ff, "blue".to_string());
    test_color(black, 0x000000, "black".to_string());
    test_color(white, 0xFFFFFF, "white".to_string());
    test_color(imaginary, -1, "imaginary".to_string());
    test_color(purple, 2, "purple".to_string());
    test_color(orange, 4, "orange".to_string());
}

fn test_color(color: color, val: isize, name: String) {
    //assert_eq!(unsafe::transmute(color), val);
    assert_eq!(color as isize, val);
    assert_eq!(get_color_alt(color), name);
    assert_eq!(get_color_if(color), name);
}

fn get_color_alt(color: color) -> String {
    match color {
      red => {"red".to_string()}
      green => {"green".to_string()}
      blue => {"blue".to_string()}
      black => {"black".to_string()}
      white => {"white".to_string()}
      imaginary => {"imaginary".to_string()}
      purple => {"purple".to_string()}
      orange => {"orange".to_string()}
    }
}

fn get_color_if(color: color) -> String {
    if color == red {"red".to_string()}
    else if color == green {"green".to_string()}
    else if color == blue {"blue".to_string()}
    else if color == black {"black".to_string()}
    else if color == white {"white".to_string()}
    else if color == imaginary {"imaginary".to_string()}
    else if color == purple {"purple".to_string()}
    else if color == orange {"orange".to_string()}
    else {"unknown".to_string()}
}