File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ #include < assert.h>
2+
3+ enum Color { RED , GREEN , BLUE };
4+
5+ enum Option {
6+ OPT_NONE = 0 ,
7+ OPT_A = 10 ,
8+ OPT_B = 20 ,
9+ OPT_C = 30 ,
10+ };
11+
12+ typedef enum {
13+ TAG_ZERO = 0 ,
14+ TAG_ONE = 1 ,
15+ TAG_TWO = 2 ,
16+ } Tag;
17+
18+ int as_int (Color c) { return c; }
19+
20+ int classify_option (int option) {
21+ switch (option) {
22+ case OPT_NONE :
23+ return -1 ;
24+ case OPT_A :
25+ return 1 ;
26+ case OPT_B :
27+ return 2 ;
28+ case OPT_C :
29+ return 3 ;
30+ default :
31+ return 0 ;
32+ }
33+ }
34+
35+ Color make_color (int n) { return (Color)n; }
36+
37+ int main () {
38+ Color c = RED ;
39+
40+ assert (c == RED );
41+ assert (c == 0 );
42+ assert (c != 1 );
43+
44+ if (c == GREEN ) {
45+ return 1 ;
46+ }
47+
48+ switch (c) {
49+ case 0 :
50+ break ;
51+ case 1 :
52+ return 1 ;
53+ case 2 :
54+ return 2 ;
55+ default :
56+ return 99 ;
57+ }
58+
59+ int x = c;
60+ assert (x == 0 );
61+
62+ int y = c + 1 ;
63+ assert (y == 1 );
64+
65+ c = (Color)2 ;
66+ assert (c == BLUE );
67+ assert (c == 2 );
68+
69+ c = make_color (1 );
70+ assert (c == GREEN );
71+
72+ Color cmp = (Color)(c + 1 );
73+ assert (cmp == BLUE );
74+
75+ Option o = OPT_A ;
76+ assert (o == OPT_A );
77+ assert (o == 10 );
78+
79+ int oi = o;
80+ assert (oi == 10 );
81+
82+ o = (Option)20 ;
83+ assert (o == OPT_B );
84+
85+ int rc = classify_option (o);
86+ assert (rc == 2 );
87+
88+ rc = classify_option (20 );
89+ assert (rc == 2 );
90+
91+ rc = classify_option (OPT_C );
92+ assert (rc == 3 );
93+
94+ Tag t = TAG_ONE ;
95+ assert (t == 1 );
96+ assert (t == TAG_ONE );
97+
98+ int ti = t;
99+ assert (ti == 1 );
100+
101+ t = (Tag)2 ;
102+ assert (t == TAG_TWO );
103+
104+ switch (t) {
105+ case TAG_ZERO :
106+ return 90 ;
107+ case 1 :
108+ return 91 ;
109+ case 2 :
110+ break ;
111+ }
112+
113+ int extra = (int )RED + (int )GREEN + (int )BLUE ;
114+ assert (extra == 0 + 1 + 2 );
115+
116+ return 0 ;
117+ }
Original file line number Diff line number Diff line change 1+ #include <assert.h>
2+
3+ enum Color { RED , GREEN , BLUE };
4+
5+ enum Option {
6+ OPT_NONE = 0 ,
7+ OPT_A = 10 ,
8+ OPT_B = 20 ,
9+ OPT_C = 30 ,
10+ };
11+
12+ typedef enum {
13+ TAG_ZERO = 0 ,
14+ TAG_ONE = 1 ,
15+ TAG_TWO = 2 ,
16+ } Tag ;
17+
18+ int as_int (enum Color c ) { return c ; }
19+
20+ int classify_option (int option ) {
21+ switch (option ) {
22+ case OPT_NONE :
23+ return -1 ;
24+ case OPT_A :
25+ return 1 ;
26+ case OPT_B :
27+ return 2 ;
28+ case OPT_C :
29+ return 3 ;
30+ default :
31+ return 0 ;
32+ }
33+ }
34+
35+ enum Color make_color (int n ) { return (enum Color )n ; }
36+
37+ int main () {
38+ enum Color c = RED ;
39+
40+ assert (c == RED );
41+ assert (c == 0 );
42+ assert (c != 1 );
43+
44+ if (c == GREEN ) {
45+ return 1 ;
46+ }
47+
48+ switch (c ) {
49+ case 0 :
50+ break ;
51+ case 1 :
52+ return 1 ;
53+ case 2 :
54+ return 2 ;
55+ default :
56+ return 99 ;
57+ }
58+
59+ int x = c ;
60+ assert (x == 0 );
61+
62+ int y = c + 1 ;
63+ assert (y == 1 );
64+
65+ c = (enum Color )2 ;
66+ assert (c == BLUE );
67+ assert (c == 2 );
68+
69+ c = make_color (1 );
70+ assert (c == GREEN );
71+
72+ enum Color cmp = (enum Color )(c + 1 );
73+ assert (cmp == BLUE );
74+
75+ enum Option o = OPT_A ;
76+ assert (o == OPT_A );
77+ assert (o == 10 );
78+
79+ int oi = o ;
80+ assert (oi == 10 );
81+
82+ o = (enum Option )20 ;
83+ assert (o == OPT_B );
84+
85+ int rc = classify_option (o );
86+ assert (rc == 2 );
87+
88+ rc = classify_option (20 );
89+ assert (rc == 2 );
90+
91+ rc = classify_option (OPT_C );
92+ assert (rc == 3 );
93+
94+ Tag t = TAG_ONE ;
95+ assert (t == 1 );
96+ assert (t == TAG_ONE );
97+
98+ int ti = t ;
99+ assert (ti == 1 );
100+
101+ t = (Tag )2 ;
102+ assert (t == TAG_TWO );
103+
104+ switch (t ) {
105+ case TAG_ZERO :
106+ return 90 ;
107+ case 1 :
108+ return 91 ;
109+ case 2 :
110+ break ;
111+ }
112+
113+ int extra = (int )RED + (int )GREEN + (int )BLUE ;
114+ assert (extra == 0 + 1 + 2 );
115+
116+ return 0 ;
117+ }
You can’t perform that action at this time.
0 commit comments