PageRenderTime 23ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/Server/SWXPHP/2.00/php/tests/SwxAssemblerTest.php

http://swx-format.googlecode.com/
PHP | 222 lines | 90 code | 46 blank | 86 comment | 0 complexity | c865fe4af1538102d8a9f06ff3792c8c MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0
  1. <?php
  2. require_once('../swx_config.php');
  3. require_once('../core/swx/SwxAS2Assembler.php');
  4. class SwxAssemblerTest extends UnitTestCase
  5. {
  6. var $swxAssembler;
  7. function setUp()
  8. {
  9. $this->swxAssembler = new SwxAS2Assembler();
  10. }
  11. /*
  12. function testPush ()
  13. {
  14. //
  15. // Strings
  16. //
  17. // The string 'hello'
  18. $this->assertPushIsCorrect('hello', '960D000064617461000068656C6C6F00');
  19. // Empty string
  20. $this->assertPushIsCorrect('', '9608000064617461000000');
  21. // String longer than 255 characters (multi-byte length)
  22. // Note: Max length of a string is 65535 bytes.
  23. $data = '0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345';
  24. $this->assertPushIsCorrect($data, '960801006461746100003031323334353637383930313233343536373839303132333435363738393031323334353637383930313233343536373839303132333435363738393031323334353637383930313233343536373839303132333435363738393031323334353637383930313233343536373839303132333435363738393031323334353637383930313233343536373839303132333435363738393031323334353637383930313233343536373839303132333435363738393031323334353637383930313233343536373839303132333435363738393031323334353637383930313233343536373839303132333435363738393031323334353637383930313233343500');
  25. // Array [1,2,3]
  26. $data = array(1, 2, 3);
  27. $this->assertPushIsCorrect($data, '961A00006461746100070300000007020000000701000000070300000042');
  28. // Boolean
  29. $data = true;
  30. $this->assertPushIsCorrect($data, '9608000064617461000501');
  31. }
  32. */
  33. function testCreateSwf()
  34. {
  35. // Correct SWF bytecode that contains a single frame with the action data="hello" in it
  36. /*
  37. $correctSwfBytecode = '4657530631000000300A00A0000101004302FFFFFF3F0312000000960D000064617461000068656C6C6F001D0040000000';
  38. $testSwfBytecode = $this->swxAssembler->createSwf('hello');
  39. $this->assertEqual($correctSwfBytecode, $testSwfBytecode);
  40. */
  41. // TODO: Add an actual test for this that passes an array.
  42. }
  43. // Test generic data to bytecode
  44. function testDataToBytecode()
  45. {
  46. // 'hello'
  47. $correctBytecode = '0068656C6C6F00';
  48. $str = 'hello';
  49. $this->assertEqual($correctBytecode, $this->swxAssembler->dataToBytecode($str));
  50. // [1, 2, 3]
  51. $correctBytecode = '961400070300000007020000000701000000070300000042';
  52. $testBytecode = $this->swxAssembler->dataToBytecode(array(1, 2, 3));
  53. $this->assertEqual($correctBytecode, $testBytecode);
  54. // null
  55. $this->assertEqual('02', $this->swxAssembler->dataToBytecode(null));
  56. // Double float (3.14)
  57. // $correctBytecode = '06 B8 1E 09 40 20 85 EB 51'
  58. // TODO: Add tests for object, etc.
  59. }
  60. // Test string to bytecode conversion
  61. function testStringToBytecode()
  62. {
  63. // 'hello'
  64. $correctBytecode = '0068656C6C6F00';
  65. $str = 'hello';
  66. $this->assertEqual($correctBytecode, $this->swxAssembler->stringToBytecode($str));
  67. // Empty string
  68. $testBytecode = $this->swxAssembler->stringToBytecode('');
  69. $this->assertEqual('0000', $testBytecode);
  70. $this->assertEqual(4, strlen($testBytecode));
  71. }
  72. // Test boolean to bytecode conversion
  73. function testBooleanToBytecode()
  74. {
  75. // True test
  76. $this->assertEqual('0501', $this->swxAssembler->booleanToBytecode(true));
  77. // False test
  78. $this->assertEqual('0500', $this->swxAssembler->booleanToBytecode(false));
  79. }
  80. // Test array to bytecode conversion
  81. function testArrayToBytecode()
  82. {
  83. // [1, 2, 3]
  84. $correctBytecode = '961400070300000007020000000701000000070300000042';
  85. $testBytecode = $this->swxAssembler->arrayToBytecode(array(1, 2, 3));
  86. $this->assertEqual($correctBytecode, $testBytecode);
  87. // error_log('SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS');
  88. /*
  89. // data = [{x: 1, y: 2, z: 3}, {a: 4, b: 5, c: [true, false]}, [6, 7, {d: 8, e: 9}]];
  90. // $correctBytecode = '961B0000646174610000640007080000000065000709000000070200000043960F0007070000000706000000070300000042961C000061000704000000006200070500000000630005000501070200000042960500070300000043961D0000780007010000000079000702000000007A000703000000070300000043960500070300000042';
  91. $correctBytecode = '00640007080000000065000709000000070200000043960F0007070000000706000000070300000042961C000061000704000000006200070500000000630005000501070200000042960500070300000043961D0000780007010000000079000702000000007A000703000000070300000043960500070300000042';
  92. $testData = array(array('x' => 1, 'y' => 2, 'z' => 3), array('a' => 4, 'b' => 5, 'c' => array(true, false)), array(6, 7, array('d' => 8, 'e' => 9)));
  93. $this->assertEqual($correctBytecode, $this->swxAssembler->arrayToBytecode($testData));
  94. */
  95. //error_log('EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE');
  96. // [{x: 1, y: 2, z: 3}]
  97. /*
  98. $correctBytecode = '962500006461746100050100780007010000000079000702000000007A000703000000070300000043960500070200000042';
  99. $testBytecode = $this->swxAssembler->arrayToBytecode(array(array('x'=>1, 'y'=>2, 'z'=>3)));
  100. $this->assertEqual($correctBytecode, $testBytecode);
  101. */
  102. }
  103. // Test integer to bytecode conversion
  104. function testIntegerToBytecode()
  105. {
  106. $intToTest = 16711680;
  107. $correctBytecode = '070000FF00';
  108. $this->assertEqual($correctBytecode, $this->swxAssembler->integerToBytecode($intToTest));
  109. }
  110. // Test double to bytecode conversion
  111. // Note: Not all float values generated by PHP and Flash will be
  112. // entirely identical but, in testing, the slight difference in the
  113. // hex representation isn't large enough to change the actual value.
  114. // (This may be proven wrong with additional testing but I'm assuming
  115. // that it's a fact of life with floats.)
  116. function testDoubleToBytecode()
  117. {
  118. $correctBytecode = '06DD9ABFBF5F633937';
  119. $floatToTest = -0.123456789;
  120. $this->assertEqual($correctBytecode, $this->swxAssembler->doubleToBytecode($floatToTest));
  121. }
  122. function testGetIntAsHex()
  123. {
  124. $intToTest = 16711680;
  125. $numBytes = 8;
  126. $correctString = '0000FF0000000000';
  127. $this->assertEqual($correctString, $this->swxAssembler->getIntAsHex($intToTest, $numBytes));
  128. }
  129. function testGetStringLengthInBytesHex()
  130. {
  131. // Single-byte length
  132. $lengthInBytesHex = $this->swxAssembler->getStringLengthInBytesHex('68656c6c6f', 2);
  133. $this->assertEqual('0500', $lengthInBytesHex);
  134. // Double-byte length
  135. $lengthInBytesHex = $this->swxAssembler->getStringLengthInBytesHex('006461746100003031323334353637383930313233343536373839303132333435363738393031323334353637383930313233343536373839303132333435363738393031323334353637383930313233343536373839303132333435363738393031323334353637383930313233343536373839303132333435363738393031323334353637383930313233343536373839303132333435363738393031323334353637383930313233343536373839303132333435363738393031323334353637383930313233343536373839303132333435363738393031323334353637383930313233343536373839303132333435363738393031323334353637383930313233343500', 2);
  136. $this->assertEqual('0801', $lengthInBytesHex);
  137. }
  138. //
  139. // Helper method tests
  140. //
  141. function testMakeLittleEndian()
  142. {
  143. // Single byte
  144. $this->assertEqual('AB', $this->swxAssembler->makeLittleEndian('AB'));
  145. // Two bytes
  146. $this->assertEqual('BBAA', $this->swxAssembler->makeLittleEndian('AABB'));
  147. }
  148. function testStrhex()
  149. {
  150. $this->assertEqual('68656c6c6f', $this->swxAssembler->strhex('hello'));
  151. }
  152. function testHexStr()
  153. {
  154. $this->assertEqual('hello', $this->swxAssembler->hexstr('68656c6c6f'));
  155. }
  156. //
  157. // Helper methods
  158. //
  159. function assertPushIsCorrect($data, $correctBytecode)
  160. {
  161. // Create the bytecode for the push statement
  162. // based on the passed $data.
  163. $result = $this->swxAssembler->push('data', $data);
  164. $this->assertEqual(strlen($result), strlen($correctBytecode));
  165. $this->assertEqual($result, $correctBytecode);
  166. }
  167. }
  168. ?>