소스 검색

added Std.int + Math operations

Nicolas Cannasse 16 년 전
부모
커밋
ac3de39939
2개의 변경된 파일46개의 추가작업 그리고 0개의 파일을 삭제
  1. 5 0
      tests/unit/Test.hx
  2. 41 0
      tests/unit/TestBasetypes.hx

+ 5 - 0
tests/unit/Test.hx

@@ -10,6 +10,11 @@ class Test #if swf_mark implements mt.Protect #end {
 		if( v != v2 ) report(v+" should be "+v2,pos);
 	}
 
+	function feq( v : Float, v2 : Float, ?pos ) {
+		count++;
+		if( Math.abs(v - v2) > 1e-18 ) report(v+" should be "+v2,pos);
+	}
+
 	function t( v, ?pos ) {
 		eq(v,true,pos);
 	}

+ 41 - 0
tests/unit/TestBasetypes.hx

@@ -28,4 +28,45 @@ class TestBasetypes extends Test {
 		eq( "x" + null, "xnull" );
 	}
 
+	function testMath() {
+		eq( Std.int(-1.7), -1 );
+		eq( Std.int(-1.2), -1 );
+		eq( Std.int(1.7), 1 );
+		eq( Std.int(1.2), 1 );
+		eq( Std.int(-0.7), 0 );
+		eq( Std.int(-0.2), 0 );
+		eq( Std.int(0.7), 0 );
+		eq( Std.int(0.2), 0 );
+
+		eq( Math.floor(-1.7), -2 );
+		eq( Math.floor(-1.5), -2 );
+		eq( Math.floor(-1.2), -2 );
+		eq( Math.floor(1.7), 1 );
+		eq( Math.floor(1.5), 1 );
+		eq( Math.floor(1.2), 1 );
+		eq( Math.ceil(-1.7), -1 );
+		eq( Math.ceil(-1.5), -1 );
+		eq( Math.ceil(-1.2), -1 );
+		eq( Math.ceil(1.7), 2 );
+		eq( Math.ceil(1.5), 2 );
+		eq( Math.ceil(1.2), 2 );
+		eq( Math.round(-1.7), -2 );
+		eq( Math.round(-1.5), -1 );
+		eq( Math.round(-1.2), -1 );
+		eq( Math.round(1.7), 2 );
+		eq( Math.round(1.5), 2 );
+		eq( Math.round(1.2), 1 );
+
+		// overflows might occurs depending on the platform
+		unspec(function() Std.int(-10000000000.7));
+		unspec( function() Math.floor(-10000000000.7) );
+		unspec( function() Math.ceil(-10000000000.7) );
+		unspec( function() Math.round(-10000000000.7) );
+		// should still give a proper result for lower bits
+		eq( Std.int(-10000000000.7) & 0xFFFFFF, 15997952 );
+		eq( Math.floor(-10000000000.7) & 0xFFFFFF, 15997951 );
+		eq( Math.ceil(-10000000000.7) & 0xFFFFFF, 15997952 );
+		eq( Math.round(-10000000000.7) & 0xFFFFFF, 15997951 );
+	}
+
 }