11module MxTests {
22
3+ // class without equality-comparer
4+ class SimpleClass {
5+ } ;
36
7+
8+ // class overriding '__hash__' and '__equals__' methods.
9+ class SimpleClassWithComparer {
10+
11+ constructor ( val : number ) {
12+ this . value = val ;
13+ this . name = val . toString ( ) ;
14+ }
15+
16+ public value : number ;
17+ public name : string ;
18+
19+ __hash__ ( ) : number {
20+ return mx . hash ( this . value , this . name ) ;
21+ }
22+
23+ __equals__ ( obj : any ) {
24+ return obj instanceof SimpleClassWithComparer && obj . value === this . value && obj . name === this . name ;
25+ }
26+ } ;
27+
28+
29+
30+
31+ QUnit . module ( "Runtime" ) ;
32+
33+
34+ QUnit . test ( "hash" , function ( assert ) {
35+
36+ assert . ok ( mx . hash ( null ) === 0 , "hash null!" ) ;
37+ assert . ok ( mx . hash ( undefined ) === 0 , "hash undefined!" ) ;
38+ assert . ok ( mx . hash ( 10 ) === mx . hash ( 10 ) , "hash integer number!" ) ;
39+ assert . ok ( mx . hash ( 10.5 ) === mx . hash ( 10.5 ) , "hash float number!" ) ;
40+ assert . ok ( mx . hash ( "string" ) === mx . hash ( "string" ) , "hash string!" ) ;
41+ assert . ok ( mx . hash ( true ) === mx . hash ( true ) , "hash boolean!" ) ;
42+ assert . ok ( mx . hash ( new Date ( 2015 , 0 , 1 ) ) === mx . hash ( new Date ( 2015 , 0 , 1 ) ) , "hash date!" ) ;
43+ assert . ok ( mx . hash ( { name : "A" } ) === mx . hash ( { name : "A" } ) , "hash object literal!" ) ;
44+ assert . ok ( mx . hash ( new SimpleClass ( ) ) !== mx . hash ( new SimpleClass ( ) ) , "hash class instance!" ) ;
45+ assert . ok ( mx . hash ( new SimpleClassWithComparer ( 10 ) ) === mx . hash ( new SimpleClassWithComparer ( 10 ) ) , "hash class instance overriding __hash__ method!" ) ;
46+ assert . ok ( mx . hash ( 10 , 10.5 , "string" , new Date ( 2015 , 0 , 1 ) ) === mx . hash ( 10 , 10.5 , "string" , new Date ( 2015 , 0 , 1 ) ) , "combine hash codes!" ) ;
47+ } ) ;
48+
49+
50+ QUnit . test ( "equals" , function ( assert ) {
51+
52+ assert . ok ( mx . equals ( null , null ) === true , "equals null!" ) ;
53+ assert . ok ( mx . equals ( undefined , undefined ) === true , "equals undefined!" ) ;
54+ assert . ok ( mx . equals ( 10 , 10 ) , "equals integer number!" ) ;
55+ assert . ok ( mx . equals ( 10.5 , 10.5 ) , "equals float number!" ) ;
56+ assert . ok ( mx . equals ( "string" , "string" ) , "equals string!" ) ;
57+ assert . ok ( mx . equals ( true , true ) , "equals boolean!" ) ;
58+ assert . ok ( mx . equals ( new Date ( 2015 , 0 , 1 ) , new Date ( 2015 , 0 , 1 ) ) , "equals date!" ) ;
59+ assert . ok ( mx . equals ( { name : "A" } , { name : "A" } ) , "equals object literal!" ) ;
60+ assert . ok ( mx . equals ( new SimpleClass ( ) , new SimpleClass ( ) ) === false , "equals class instance!" ) ;
61+ assert . ok ( mx . equals ( new SimpleClass ( ) , new SimpleClass ( ) , EqualityComparer . create (
62+ function ( ) { return 0 ; } ,
63+ function ( ) { return true ; }
64+ ) ) , "equals class instance using comparer!" ) ;
65+ assert . ok ( mx . equals ( new SimpleClassWithComparer ( 10 ) , new SimpleClassWithComparer ( 10 ) ) , "equals class instance overriding __equals__ method!" ) ;
66+ } ) ;
67+
68+
69+ QUnit . test ( "compare" , function ( assert ) {
70+
71+ assert . ok ( mx . runtime . compare ( 1 , null ) === 1 && mx . runtime . compare ( null , 1 ) === - 1 && mx . runtime . compare ( null , null ) === 0 , "compare null!" ) ;
72+ assert . ok ( mx . runtime . compare ( 1 , 0 ) === 1 && mx . runtime . compare ( 0 , 1 ) === - 1 && mx . runtime . compare ( 1 , 1 ) === 0 , "compare numbers!" ) ;
73+ assert . ok ( mx . runtime . compare ( "B" , "A" ) === 1 && mx . runtime . compare ( "A" , "B" ) === - 1 && mx . runtime . compare ( "A" , "A" ) === 0 , "compare string!" ) ;
74+ assert . ok ( mx . runtime . compare ( true , false ) === 1 && mx . runtime . compare ( false , true ) === - 1 && mx . runtime . compare ( true , true ) === 0 , "compare bolean!" ) ;
75+ assert . ok ( mx . runtime . compare ( new Date ( 2015 , 0 , 2 ) , new Date ( 2015 , 0 , 1 ) ) === 1 && mx . runtime . compare ( new Date ( 2015 , 0 , 1 ) , new Date ( 2015 , 0 , 2 ) ) === - 1 && mx . runtime . compare ( new Date ( 2015 , 0 , 1 ) , new Date ( 2015 , 0 , 1 ) ) === 0 , "compare date!" ) ;
76+ assert . ok ( mx . runtime . compare ( { name : "A" } , { name : "B" } ) === 0 , "compare objects!" ) ;
77+ } ) ;
78+
79+
80+ QUnit . test ( "lambda" , function ( assert ) {
81+
82+ var _f1 = mx . runtime . lambda < number , number > ( "t => t * t" ) ,
83+ _f2 = mx . runtime . lambda < number , number , number > ( "(t, u) => t + u" ) ,
84+ _f3 = mx . runtime . lambda < number > ( "(t, u, r) => t + u + r" ) ,
85+ _f4 = mx . runtime . lambda < number , string , { id : number ; name : string } > ( "(t, u) => {id:t, name:u}" ) ;
86+
87+ assert . ok ( _f1 ( 2 ) === 4 , "square root lambda!" ) ;
88+ assert . ok ( _f2 ( 1 , 2 ) === 3 , "sum of 2 numbers lambda!" ) ;
89+ assert . ok ( _f3 ( 1 , 2 , 3 ) === 6 , "sum of 3 numbers lambda!" ) ;
90+ assert . ok ( _f4 ( 1 , "A" ) . id === 1 && _f4 ( 1 , "A" ) . name === "A" , "object literal lambda!" ) ;
91+ } ) ;
492}
0 commit comments