IdTestGlobal.pas 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. unit IdTestGlobal;
  2. interface
  3. uses
  4. IdTest,
  5. IdObjs,
  6. IdSys,
  7. IdException,
  8. IdGlobal;
  9. type
  10. TIdTestGlobal = class(TIdTest)
  11. published
  12. procedure TestToBytes;
  13. procedure TestBytesToChar;
  14. procedure TestReadStringFromStream;
  15. procedure TestReadTIdBytesFromStream;
  16. procedure TestBytesToString;
  17. procedure TestPosIdx;
  18. end;
  19. implementation
  20. procedure TIdTestGlobal.TestReadStringFromStream;
  21. var
  22. TempStream: TIdMemoryStream;
  23. begin
  24. TempStream := TIdMemoryStream.Create;
  25. try
  26. Assert(ReadStringFromStream(TempStream) = '');
  27. finally
  28. Sys.FreeAndNil(TempStream);
  29. end;
  30. end;
  31. procedure TIdTestGlobal.TestBytesToChar;
  32. var
  33. aBytes:TIdBytes;
  34. aChar:char;
  35. begin
  36. //test normal behaviour
  37. SetLength(aBytes,1);
  38. aBytes[0]:=$31;
  39. aChar:=BytesToChar(aBytes);
  40. Assert(aChar='1');
  41. //should fail trying to read from empty buffer
  42. SetLength(aBytes,0);
  43. try
  44. BytesToChar(aBytes);
  45. Assert(False);
  46. except
  47. //expect to be here
  48. end;
  49. //should fail trying to read from nil buffer
  50. aBytes:=nil;
  51. try
  52. BytesToChar(aBytes);
  53. Assert(False);
  54. except
  55. //expect to be here
  56. end;
  57. end;
  58. procedure TIdTestGlobal.TestToBytes;
  59. const
  60. cTestString1 = 'UA';
  61. cTestChar = 'U';
  62. var
  63. aBytes : TIdBytes;
  64. begin
  65. //test string
  66. aBytes := ToBytes(cTestString1);
  67. Assert(Length(aBytes)=2);
  68. Assert(aBytes[0] = 85);
  69. Assert(aBytes[1] = 65);
  70. //test char
  71. aBytes := ToBytes(cTestChar);
  72. Assert(Length(aBytes)=1);
  73. Assert(aBytes[0] = 85);
  74. //todo, test other types
  75. end;
  76. procedure TIdTestGlobal.TestReadTIdBytesFromStream;
  77. var
  78. aStream:TIdMemoryStream;
  79. aBytes:TIdBytes;
  80. aStr:string;
  81. const
  82. cStr='123';
  83. begin
  84. aStream:=TIdMemoryStream.Create;
  85. try
  86. WriteStringToStream(aStream,cStr);
  87. aStream.Position:=0;
  88. ReadTIdBytesFromStream(aStream,aBytes,-1);
  89. aStr:=BytesToString(aBytes);
  90. Assert(aStr=cStr);
  91. finally
  92. Sys.FreeAndNil(aStream);
  93. end;
  94. end;
  95. procedure TIdTestGlobal.TestBytesToString;
  96. var
  97. aBytes:TIdBytes;
  98. aStr:string;
  99. aExpected:Boolean;
  100. const
  101. cStr='12345';
  102. begin
  103. aBytes := ToBytes(cStr);
  104. aStr:=BytesToString(aBytes);
  105. Assert(aStr=cStr);
  106. aStr:=BytesToString(aBytes,0,0);
  107. Assert(aStr='');
  108. aStr:=BytesToString(aBytes,0,1);
  109. Assert(aStr='1');
  110. aStr:=BytesToString(aBytes,1,1);
  111. Assert(aStr='2');
  112. aStr:=BytesToString(aBytes,4,1);
  113. Assert(aStr='5');
  114. aStr:=BytesToString(aBytes,0,5);
  115. Assert(aStr=cStr);
  116. //start past end of buffer
  117. aExpected:=False;
  118. try
  119. aStr:=BytesToString(aBytes,5,1);
  120. Assert(aStr='5');
  121. except
  122. on e:Exception do
  123. begin
  124. aExpected:=e is EIdRangeException;
  125. end;
  126. end;
  127. Assert(aExpected);
  128. //read past end of buffer
  129. aStr:=BytesToString(aBytes,0,10);
  130. Assert(aStr=cStr);
  131. end;
  132. procedure TIdTestGlobal.TestPosIdx;
  133. var
  134. i:Integer;
  135. begin
  136. i:=PosIdx('1234','12345',4);
  137. Assert(i=0);
  138. i:=PosIdx('23','12345',0);
  139. Assert(i=2);
  140. i:=PosIdx('23','12345',2);
  141. Assert(i=2);
  142. i:=PosIdx('a','12345',2);
  143. Assert(i=0);
  144. i:=PosIdx('123','1');
  145. Assert(i=0);
  146. end;
  147. initialization
  148. TIdTest.RegisterTest(TIdTestGlobal);
  149. end.