added additional tests
[swftools.git] / lib / as3 / ok / precedence.as
1 package {
2     
3     import flash.display.MovieClip
4
5     class C {
6         function f() {return new Array(1,2,3);}
7     }
8     public class Main extends flash.display.MovieClip {
9         var count:int = 1;
10         var num:int = 27;
11         function assert(b) {
12             if(b) {
13                 trace("ok "+count+"/"+num);
14             } else {
15                 trace("error "+count+"/"+num);
16             }
17             count = count + 1
18         }
19         function compare(x,y) {
20             assert(x==y);
21         }
22             
23         var a:Array = new Array(1,2,3);
24         var b:int=10;
25         function f() {return a;}
26         function g() {return b;}
27         var i=0;
28
29         function Main() {
30
31             // test that ., [], (), @, ::, .. {x:y}, new all have the same precedence
32             // TODO: @, ::, ..
33
34             compare(this.f()[2] , (((this).f)())[2])
35             //compare(new C.f()[1],2); //breaks
36             compare(new C().f()[1],2);
37
38             // test that ., [], () have higher precedence than ++, --, -, ~, !, delete, typeof
39             // TODO: @, delete, typeof
40             var i:int=5,j:int;
41             this.i=0;
42             this.i++;compare(this.i,1);compare(i,5);
43             ++(this.a)[2]; compare(this.a[2],4);
44             delete this.f()[0];compare(String(this.a[0]), "undefined");
45
46             // test that ++ has higher precedence than unary -,~,!
47             compare(-i++,-5);
48             compare(!i++,false);
49             compare(~i++,~7);
50
51             // test that * / % have the same precedence
52             compare(3*10/15%4, 2)
53             
54             // test that ++,--,~ have higher precedence than * / %
55             i = 0;j = 1;compare(++i*j++, 1)
56             i = 0;j = 1;compare(++i/j++, 1)
57             i = 2;j = 2;compare(++i%j++, 1)
58             compare( (~1*2) & 1, 0);
59
60             // test that +,- have lower precedence than * / %
61             compare(1+1*2, 3)
62             compare(5-4/2, 3)
63             compare(3-4%2, 3)
64             compare(2+1%2, 3)
65
66             // test that +, - have higher precedence than >>,<<,>>>
67             compare(4>>1+1, 1)
68             compare(4>>1-1, 4)
69             compare(4>>>1+1, 1)
70             compare(4>>>1-1, 4)
71             compare(1<<1+1, 4)
72             compare(4<<1-1, 4)
73             
74             // test that >>,<< have higher precedence than <,>,<=,>=,==
75             compare(3 < 1<<2, true)
76             compare(4 <= 1<<2, true)
77             compare(2 > 8>>3, true)
78             compare(1 >= 8>>3, true)
79             compare(1 == 1<<0, true)
80             compare(1 == 1>>0, true)
81             
82             // test that <,>,<=,>= have higher precedence than as, in
83             compare(1<2 as Boolean, true)
84             //compare(1<2 in [true,true,true], true)
85
86             // test that >,<,>=,<= have higher precedence than ==, !=, ===, !==
87             compare(true == 3<4, true)
88             compare(true != 3>4, true)
89             compare(true === 3<=4, true)
90             compare(true !== 3>=4, true)
91             
92             // test that ==,!= have higher precedence than &
93             compare(3&4==4, 1)
94             compare(3&0!=4, 1)
95
96             // test that & has higher precedence than ^
97             compare(1^1&4, 1)
98             
99             // test that ^ has higher precedence than |
100             compare(5|4^4, 5)
101             
102             // test that | has higher precedence than &&
103             compare(false && 0|5, false)
104             
105             // test that && has higher precedence than ||
106             compare(false && true || true, true)
107             
108             // test that || has higher precedence than ?:
109             compare(true || false?11:0, 11)
110             
111             // test that ?: and = have same precedence 
112             var y = 0;
113             var x = false ? y=3 : y=4;
114             compare(y,4)
115             false ? y=3 : y=5;
116             compare(y,5)
117             
118             // test that = is right-associative
119             x = y = 3;
120             compare(x,3)
121             compare(y,3)
122             
123             // test that = has higher precedence than +=,-=,*=,/=,%=
124             x=4;y=10;
125             x += y = 4;
126             compare(x,8);
127
128             // test that +=,-=,*=,/=,%=,>>=,<<=,>>>= all have the same associativity
129             // TODO: %=,/=,-=,>>=,<<=,>>>=
130             x=2;y=3;
131             x *= y += 4;
132             assert(x==14 && y==7);
133
134             // test that , has lower precedence than +=
135             x=0;
136             var v=0;
137             v += 3,4;
138             compare(v,3)
139
140         }
141
142     }
143 }
144