ソースを参照

[php] finally fixed a test for #7533

Alexander Kuzmenko 6 年 前
コミット
12c84cea9f
3 ファイル変更28 行追加3 行削除
  1. 1 1
      std/php/Boot.hx
  2. 10 0
      std/php/Global.hx
  3. 17 2
      tests/unit/src/unit/issues/Issue7533.hx

+ 1 - 1
std/php/Boot.hx

@@ -505,7 +505,7 @@ class Boot {
 		if (right == 0) {
 			return left;
 		} else if (left >= 0) {
-			return ((left >> right) & ~(1 << ( 8*Const.PHP_INT_SIZE-1 ) >> (right-1)));
+			return (left >> right) & ~( (1 << (8 * Const.PHP_INT_SIZE - 1)) >> (right - 1) );
 		} else {
 			return (left >> right) & (0x7fffffff >> (right - 1));
 		}

+ 10 - 0
std/php/Global.hx

@@ -872,6 +872,16 @@ extern class Global {
 	**/
 	static function hexdec( hex_string:String ) : Int;
 
+	/**
+		@see http://php.net/manual/en/function.decbin.php
+	**/
+	static function decbin( number:Int ) : String;
+
+	/**
+		@see http://php.net/manual/en/function.bindec.php
+	**/
+	static function bindec( binary_string:String ) : Float;
+
 	/**
 		@see http://php.net/manual/en/function.bin2hex.php
 	**/

+ 17 - 2
tests/unit/src/unit/issues/Issue7533.hx

@@ -1,8 +1,23 @@
 package unit.issues;
 
+#if php
+import php.*;
+#end
+
 class Issue7533 extends unit.Test {
+	#if php
 	function test() {
-      var bint:Int = 0xB425B745;
-      eq( (bint>>>20), 0xB42);
+		var signBit = Const.PHP_INT_SIZE * 8 - 1; //31 or 63
+
+		var bint:Int = Syntax.code('bindec(decbin({0}))', 1 << signBit);
+		/**
+		 * `bindec(decbin())` is to convert `1 << signBit` to a positive float instead of a negative int.
+		 * This is the only way for it to fall into `else if(left >= 0)` branch of `php.Boot.shiftRightUnsigned()`.
+		 * And then on a bitwise operation it will be converted back to a negative integer by PHP interpreter.
+		 * And that causes this bug.
+		 */
+
+		eq( 1 << (signBit - 2), bint >>> 2 );
 	}
+	#end
 }