| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- unit IdTestGlobal;
- interface
- uses
- IdTest,
- IdObjs,
- IdSys,
- IdException,
- IdGlobal;
- type
- TIdTestGlobal = class(TIdTest)
- published
- procedure TestToBytes;
- procedure TestBytesToChar;
- procedure TestReadStringFromStream;
- procedure TestReadTIdBytesFromStream;
- procedure TestBytesToString;
- procedure TestPosIdx;
- end;
- implementation
- procedure TIdTestGlobal.TestReadStringFromStream;
- var
- TempStream: TIdMemoryStream;
- begin
- TempStream := TIdMemoryStream.Create;
- try
- Assert(ReadStringFromStream(TempStream) = '');
- finally
- Sys.FreeAndNil(TempStream);
- end;
- end;
- procedure TIdTestGlobal.TestBytesToChar;
- var
- aBytes:TIdBytes;
- aChar:char;
- begin
- //test normal behaviour
- SetLength(aBytes,1);
- aBytes[0]:=$31;
- aChar:=BytesToChar(aBytes);
- Assert(aChar='1');
- //should fail trying to read from empty buffer
- SetLength(aBytes,0);
- try
- BytesToChar(aBytes);
- Assert(False);
- except
- //expect to be here
- end;
- //should fail trying to read from nil buffer
- aBytes:=nil;
- try
- BytesToChar(aBytes);
- Assert(False);
- except
- //expect to be here
- end;
- end;
- procedure TIdTestGlobal.TestToBytes;
- const
- cTestString1 = 'UA';
- cTestChar = 'U';
- var
- aBytes : TIdBytes;
- begin
- //test string
- aBytes := ToBytes(cTestString1);
- Assert(Length(aBytes)=2);
- Assert(aBytes[0] = 85);
- Assert(aBytes[1] = 65);
- //test char
- aBytes := ToBytes(cTestChar);
- Assert(Length(aBytes)=1);
- Assert(aBytes[0] = 85);
- //todo, test other types
- end;
- procedure TIdTestGlobal.TestReadTIdBytesFromStream;
- var
- aStream:TIdMemoryStream;
- aBytes:TIdBytes;
- aStr:string;
- const
- cStr='123';
- begin
- aStream:=TIdMemoryStream.Create;
- try
- WriteStringToStream(aStream,cStr);
- aStream.Position:=0;
- ReadTIdBytesFromStream(aStream,aBytes,-1);
- aStr:=BytesToString(aBytes);
- Assert(aStr=cStr);
- finally
- Sys.FreeAndNil(aStream);
- end;
- end;
- procedure TIdTestGlobal.TestBytesToString;
- var
- aBytes:TIdBytes;
- aStr:string;
- aExpected:Boolean;
- const
- cStr='12345';
- begin
- aBytes := ToBytes(cStr);
- aStr:=BytesToString(aBytes);
- Assert(aStr=cStr);
- aStr:=BytesToString(aBytes,0,0);
- Assert(aStr='');
- aStr:=BytesToString(aBytes,0,1);
- Assert(aStr='1');
- aStr:=BytesToString(aBytes,1,1);
- Assert(aStr='2');
- aStr:=BytesToString(aBytes,4,1);
- Assert(aStr='5');
- aStr:=BytesToString(aBytes,0,5);
- Assert(aStr=cStr);
- //start past end of buffer
- aExpected:=False;
- try
- aStr:=BytesToString(aBytes,5,1);
- Assert(aStr='5');
- except
- on e:Exception do
- begin
- aExpected:=e is EIdRangeException;
- end;
- end;
- Assert(aExpected);
- //read past end of buffer
- aStr:=BytesToString(aBytes,0,10);
- Assert(aStr=cStr);
- end;
- procedure TIdTestGlobal.TestPosIdx;
- var
- i:Integer;
- begin
- i:=PosIdx('1234','12345',4);
- Assert(i=0);
- i:=PosIdx('23','12345',0);
- Assert(i=2);
- i:=PosIdx('23','12345',2);
- Assert(i=2);
- i:=PosIdx('a','12345',2);
- Assert(i=0);
- i:=PosIdx('123','1');
- Assert(i=0);
- end;
- initialization
- TIdTest.RegisterTest(TIdTestGlobal);
- end.
|