Last active
December 20, 2015 00:59
-
-
Save jsbattig/6045902 to your computer and use it in GitHub Desktop.
Unit tests for uWinVMMemoryStream unit ( https://gist.github.com/jsbattig/6041478#file-uwinvmmemorystream-pas )
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
unit TestuWinVMMemoryStream; | |
{ | |
Delphi DUnit Test Case | |
---------------------- | |
This unit contains a skeleton test case class generated by the Test Case Wizard. | |
Modify the generated code to correctly setup and call the methods from the unit | |
being tested. | |
} | |
interface | |
uses | |
TestFramework, Classes, uWinVMMemoryStream; | |
type | |
// Test methods for class TWinVMMemoryStream | |
TestTWinVMMemoryStream = class(TTestCase) | |
private | |
FWinVMMemoryStream: TWinVMMemoryStream; | |
public | |
procedure SetUp; override; | |
procedure TearDown; override; | |
published | |
procedure TestClear; | |
procedure TestSetSize; | |
procedure TestWriteAndRead; | |
end; | |
implementation | |
uses | |
Windows, SysUtils; | |
procedure TestTWinVMMemoryStream.SetUp; | |
begin | |
FWinVMMemoryStream := TWinVMMemoryStream.Create(16 *1024, PAGE_EXECUTE_READWRITE); | |
end; | |
procedure TestTWinVMMemoryStream.TearDown; | |
begin | |
FWinVMMemoryStream.Free; | |
FWinVMMemoryStream := nil; | |
end; | |
procedure TestTWinVMMemoryStream.TestClear; | |
begin | |
FWinVMMemoryStream.Clear; | |
Check(FWinVMMemoryStream.Memory = nil, 'Memory pointer should be nil'); | |
CheckEquals(0, FWinVMMemoryStream.Size, 'Size should be zero'); | |
CheckEquals(0, FWinVMMemoryStream.Capacity, 'Capacity should be zero'); | |
end; | |
procedure TestTWinVMMemoryStream.TestSetSize; | |
var | |
NewSize: System.Integer; | |
begin | |
NewSize := FWinVMMemoryStream.Capacity * 2; | |
FWinVMMemoryStream.SetSize(NewSize); | |
CheckEquals(NewSize, FWinVMMemoryStream.Capacity, 'Capacity should be equals to the initial capacity * 2'); | |
end; | |
procedure TestTWinVMMemoryStream.TestWriteAndRead; | |
var | |
ReturnValue: System.Integer; | |
Count: System.Integer; | |
Buffer: PAnsiChar; | |
i : integer; | |
begin | |
Count := FWinVMMemoryStream.Capacity * 2; | |
GetMem(Buffer, Count); | |
try | |
for i := 0 to Count - 1 do | |
Buffer[i] := AnsiChar(i mod 256); | |
ReturnValue := FWinVMMemoryStream.Write(Buffer^, Count); | |
CheckEquals(Count, ReturnValue, 'Data written should be equals to Capacity * 2'); | |
FillChar(Buffer^, Count, 0); | |
for i := 0 to Count - 1 do | |
CheckEquals(0, byte(Buffer[i]), 'Data of buffer should be zeroed out'); | |
FWinVMMemoryStream.Position := 0; | |
ReturnValue := FWinVMMemoryStream.Read(Buffer^, Count); | |
CheckEquals(Count, ReturnValue, 'Data read should be equals to Capacity * 2'); | |
for i := 0 to Count - 1 do | |
CheckEquals(i mod 256, byte(Buffer[i]), Format('Data element %d of buffer should be %d', [i, i mod 256])); | |
finally | |
FreeMem(Buffer); | |
end; | |
end; | |
initialization | |
// Register any test cases with the test runner | |
RegisterTest(TestTWinVMMemoryStream.Suite); | |
end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment