TestIO.hx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. o.bigEndian = endian;
  23. eq(o.bigEndian,endian);
  24. o.prepare(4);
  25. o.writeByte(0x00);
  26. o.writeByte(0x01);
  27. o.writeByte(0x02);
  28. o.writeByte(0x03);
  29. o.write(b);
  30. o.writeByte(55);
  31. o.writeBytes(b,3,5);
  32. excv(function() o.writeBytes(b,-1,5),OutsideBounds);
  33. excv(function() o.writeBytes(b,3,-1),OutsideBounds);
  34. excv(function() o.writeBytes(b,3,20),OutsideBounds);
  35. o.writeByte(98);
  36. o.writeDouble(1.23);
  37. o.writeDouble(-1.23);
  38. o.writeDouble(0.0);
  39. o.writeDouble(-0.0);
  40. o.writeFloat(1.2e10);
  41. o.writeFloat(-1.2e10);
  42. o.writeFloat(0.0);
  43. o.writeFloat(-0.0);
  44. o.writeByte(99);
  45. var str = "Héllo World !";
  46. o.writeString(str);
  47. o.writeInt16(-12345);
  48. excv(function() o.writeInt16(1 << 15),Overflow);
  49. excv(function() o.writeInt16(-((1 << 15)+1)),Overflow);
  50. o.writeInt24(-1234567);
  51. excv(function() o.writeInt16(1 << 24),Overflow);
  52. excv(function() o.writeInt16(-((1 << 24)+1)),Overflow);
  53. o.writeInt32(-123456789);
  54. o.writeInt8(-5);
  55. excv(function() o.writeInt8(128),Overflow);
  56. excv(function() o.writeInt8(-129),Overflow);
  57. o.writeUInt16(0xFF55);
  58. excv(function() o.writeUInt16(1 << 16),Overflow);
  59. excv(function() o.writeUInt16(-1),Overflow);
  60. o.writeUInt24(0xFF00EE);
  61. excv(function() o.writeUInt24(1 << 24),Overflow);
  62. excv(function() o.writeUInt24(-1),Overflow);
  63. o.writeInt32(0x3FAABBCC);
  64. o.writeInt32(0xA0FFEEDD);
  65. o.writeInt32(0xC0FFEEDD);
  66. unspec(function() o.writeByte(-1));
  67. unspec(function() o.writeByte(257));
  68. var i = new haxe.io.BytesInput(o.getBytes());
  69. i.bigEndian = endian;
  70. eq( i.readInt32(), endian ? 0x00010203 : 0x03020100 );
  71. eq( i.read(b.length).compare(b) , 0 );
  72. eq( i.readByte(), 55 );
  73. eq( i.read(5).compare(b.sub(3,5)), 0 );
  74. eq( i.readByte(), 98 );
  75. eq( i.readDouble(), 1.23 );
  76. eq( i.readDouble(), -1.23 );
  77. eq( i.readDouble(), 0.0 );
  78. eq( i.readDouble(), -0.0 );
  79. eq( i.readFloat(), 1.2e10 );
  80. eq( i.readFloat(), -1.2e10 );
  81. eq( i.readFloat(), 0.0 );
  82. eq( i.readFloat(), -0.0 );
  83. eq( i.readByte(), 99 );
  84. eq( i.readString(haxe.io.Bytes.ofString(str).length), str );
  85. eq( i.readInt16(), -12345 );
  86. eq( i.readInt24(), -1234567 );
  87. eq( i.readInt32(), -123456789 );
  88. eq( i.readInt8(), -5 );
  89. eq( i.readUInt16(), 0xFF55 );
  90. eq( i.readUInt24(), 0xFF00EE );
  91. eq( i.readInt32(), 0x3FAABBCC );
  92. eq( i.readInt32() , 0xA0FFEEDD );
  93. eq( i.readInt32() , 0xC0FFEEDD );
  94. }
  95. function testBytesBounds() {
  96. var b = haxe.io.Bytes.ofString("ABCDEFGHIJ");
  97. var tmp = haxe.io.Bytes.alloc(7);
  98. var i = new haxe.io.BytesInput(b);
  99. excv( function() i.readBytes(tmp,1,7), OutsideBounds );
  100. excv( function() i.readBytes(tmp,-1,7), OutsideBounds );
  101. excv( function() i.readBytes(tmp,8,1), OutsideBounds );
  102. eq( i.readBytes(tmp,0,7), 7 );
  103. eq( tmp.get(0), "A".code );
  104. eq( tmp.get(6), "G".code );
  105. eq( i.readBytes(tmp,0,7), 3 );
  106. eq( tmp.get(0), "H".code );
  107. eq( tmp.get(2), "J".code );
  108. eq( tmp.get(3), "D".code );
  109. exc( function() i.readBytes(tmp,0,7) );
  110. }
  111. }