2
0

TestIO.hx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package unit;
  2. import haxe.io.Error;
  3. class TestIO extends Test {
  4. public function test() {
  5. check(false);
  6. check(true);
  7. }
  8. function excv<T>( f, e : T, ?pos ) {
  9. try {
  10. f();
  11. eq(null,e,pos);
  12. } catch( e2 : Dynamic ) {
  13. eq(e2,e,pos);
  14. }
  15. }
  16. function check(endian:Bool) {
  17. infos("endian = "+endian);
  18. var b = haxe.io.Bytes.ofString("ABCééé\r\n\t");
  19. eq( b.length, 12 );
  20. b.set(1,0);
  21. var o = new haxe.io.BytesOutput();
  22. eq(o.length, 0);
  23. o.bigEndian = endian;
  24. eq(o.bigEndian,endian);
  25. o.prepare(4);
  26. o.writeByte(0x00);
  27. o.writeByte(0x01);
  28. o.writeByte(0x02);
  29. o.writeByte(0x03);
  30. eq(o.length, 4);
  31. o.write(b);
  32. o.writeByte(55);
  33. o.writeBytes(b,3,5);
  34. excv(function() o.writeBytes(b,-1,5),OutsideBounds);
  35. excv(function() o.writeBytes(b,3,-1),OutsideBounds);
  36. excv(function() o.writeBytes(b,3,20),OutsideBounds);
  37. o.writeByte(98);
  38. o.writeDouble(1.23);
  39. o.writeDouble(-1.23);
  40. o.writeDouble(0.0);
  41. o.writeDouble(-0.0);
  42. o.writeFloat(1.2e10);
  43. o.writeFloat(-1.2e10);
  44. o.writeFloat(0.0);
  45. o.writeFloat(-0.0);
  46. o.writeByte(99);
  47. var str = "Héllo World !";
  48. o.writeString(str);
  49. eq(o.length, 86);
  50. o.writeInt16(-12345);
  51. excv(function() o.writeInt16(1 << 15),Overflow);
  52. excv(function() o.writeInt16(-((1 << 15)+1)),Overflow);
  53. o.writeInt24(-1234567);
  54. excv(function() o.writeInt16(1 << 24),Overflow);
  55. excv(function() o.writeInt16(-((1 << 24)+1)),Overflow);
  56. o.writeInt32(-123456789);
  57. o.writeInt8(-5);
  58. excv(function() o.writeInt8(128),Overflow);
  59. excv(function() o.writeInt8(-129),Overflow);
  60. o.writeUInt16(0xFF55);
  61. excv(function() o.writeUInt16(1 << 16),Overflow);
  62. excv(function() o.writeUInt16(-1),Overflow);
  63. o.writeUInt24(0xFF00EE);
  64. excv(function() o.writeUInt24(1 << 24),Overflow);
  65. excv(function() o.writeUInt24(-1),Overflow);
  66. o.writeInt32(0x3FAABBCC);
  67. o.writeInt32(0xA0FFEEDD);
  68. o.writeInt32(0xC0FFEEDD);
  69. var i = new haxe.io.BytesInput(o.getBytes());
  70. i.bigEndian = endian;
  71. eq( i.position, 0 );
  72. eq( i.length, 113 );
  73. eq( i.readInt32(), endian ? 0x00010203 : 0x03020100 );
  74. eq( i.read(b.length).compare(b) , 0 );
  75. eq( i.readByte(), 55 );
  76. eq( i.read(5).compare(b.sub(3,5)), 0 );
  77. eq( i.readByte(), 98 );
  78. eq( i.readDouble(), 1.23 );
  79. eq( i.readDouble(), -1.23 );
  80. eq( i.readDouble(), 0.0 );
  81. eq( i.readDouble(), -0.0 );
  82. eq( i.readFloat(), 1.2e10 );
  83. eq( i.readFloat(), -1.2e10 );
  84. eq( i.readFloat(), 0.0 );
  85. eq( i.readFloat(), -0.0 );
  86. eq( i.readByte(), 99 );
  87. eq( i.readString(haxe.io.Bytes.ofString(str).length), str );
  88. eq( i.readInt16(), -12345 );
  89. eq( i.readInt24(), -1234567 );
  90. eq( i.readInt32(), -123456789 );
  91. eq( i.readInt8(), -5 );
  92. eq( i.readUInt16(), 0xFF55 );
  93. eq( i.readUInt24(), 0xFF00EE );
  94. eq( i.readInt32(), 0x3FAABBCC );
  95. eq( i.readInt32() , 0xA0FFEEDD );
  96. eq( i.readInt32() , 0xC0FFEEDD );
  97. eq( i.position, i.length );
  98. }
  99. function testBytesBounds() {
  100. var b = haxe.io.Bytes.ofString("ABCDEFGHIJ");
  101. var tmp = haxe.io.Bytes.alloc(7);
  102. var i = new haxe.io.BytesInput(b);
  103. excv( function() i.readBytes(tmp,1,7), OutsideBounds );
  104. excv( function() i.readBytes(tmp,-1,7), OutsideBounds );
  105. excv( function() i.readBytes(tmp,8,1), OutsideBounds );
  106. eq( i.readBytes(tmp,0,7), 7 );
  107. eq( tmp.get(0), "A".code );
  108. eq( tmp.get(6), "G".code );
  109. eq( i.readBytes(tmp,0,7), 3 );
  110. eq( tmp.get(0), "H".code );
  111. eq( tmp.get(2), "J".code );
  112. eq( tmp.get(3), "D".code );
  113. exc( function() i.readBytes(tmp,0,7) );
  114. }
  115. function testBytesInputSeek() {
  116. var b = haxe.io.Bytes.ofString("0123456789abcdef");
  117. var i = new haxe.io.BytesInput( b );
  118. i.position = 15;
  119. eq( i.readByte(), "f".code );
  120. exc( i.readByte );
  121. i.position = 1;
  122. eq( i.readByte(), "1".code );
  123. eq( i.readByte(), "2".code );
  124. var tmp = haxe.io.Bytes.alloc(14);
  125. eq( i.readBytes(tmp,0,13), 13 );
  126. exc( i.readByte );
  127. i.position = -10;
  128. eq( i.position, 0 );
  129. eq( i.readByte(), "0".code );
  130. i.position = 999;
  131. eq( i.position, i.length );
  132. exc( i.readByte );
  133. }
  134. }