delphi2007中Test单元少文件的解决!
2007-04-06 10:38
417 查看
delphi2007中Test单元少文件,一个叫“FastMMMemLeakMonitor.pas”的单元没有,而不能编译,现在找的了这个单元文件特发上来给大家分享!
{#(@)$Id: FastMMMemLeakMonitor.pas,v 1.1 2006/07/19 02:55:29 judc Exp $ }
{ DUnit: An XTreme testing framework for Delphi programs. }
(*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is DUnit.
*
* The Initial Developers of the Original Code are Kent Beck, Erich Gamma,
* and Juancarlo Aez.
* Portions created The Initial Developers are Copyright (C) 1999-2000.
* Portions created by The DUnit Group are Copyright (C) 2000-2004.
* All rights reserved.
*
* Contributor(s):
* Kent Beck <kentbeck@csi.com>
* Erich Gamma <Erich_Gamma@oti.com>
* Juanco Aez <juanco@users.sourceforge.net>
* Chris Morris <chrismo@users.sourceforge.net>
* Jeff Moore <JeffMoore@users.sourceforge.net>
* Uberto Barbini <uberto@usa.net>
* Brett Shearer <BrettShearer@users.sourceforge.net>
* Kris Golko <neuromancer@users.sourceforge.net>
* The DUnit group at SourceForge <http://dunit.sourceforge.net>
* Peter McNab <>
*
*******************************************************************************
* Code to provide Memory Leak Detection at the test case level.
* This code makes use of FastMM4.pas available from
* http://fastmm.sourceforge.net
*
* FastMM is a fast replacement memory manager for Borland Delphi Win32
* applications that scales well under multi-threaded usage, is not prone to
* memory fragmentation, and supports shared memory without the use of
* external .DLL files.
* To use FastMM in DUnit for memory leak detection it is necessary to download
* the latest stable release of FastMM from "fastmm.sourceforge.net"
* Then add the path of the folder containing the FastMM4 source code to the
* DUnit project's search path.
*
* Generally it should not be necessary to change FastMMOptions.inc settings.
* However read the notes in FastMMOptions.inc carefully to understand the
* effect of each option and set accordingly to best meet your testing
* environment.
*
* Finally, select
* "Project, Option, Directories/Conditionals, Conditional Defines"
* in the Delphi IDE and add the conditional define
* FASTMM (prefix with an extra ; if there are other defines)
* to allow DUnit to use the FASTMM specific code.
*
*)
unit FastMMMemLeakMonitor;
interface
uses
{$IFDEF VER180}
SysUtils,
{$ELSE}
{$IFDEF FASTMM}
FastMM4,
{$ENDIF}
{$ENDIF}
TestFrameWork;
type
TMemLeakMonitor = class(TInterfacedObject, IMemLeakMonitor)
protected
FMS1: TMemoryManagerState;
FMS2: TMemoryManagerState;
function MemLeakDetected(out LeakSize: Integer): boolean; overload;
public
constructor Create;
end;
TDUnitMemLeakMonitor = class(TMemLeakMonitor, IDUnitMemLeakMonitor)
procedure MarkMemInUse;
function MemLeakDetected(const AllowedLeakSize: Integer;
const FailOnMemoryRecovery: boolean;
out LeakSize: Integer): boolean; overload;
function MemLeakDetected(const AllowedValuesGetter: TListIterator;
const FailOnMemoryRecovery: Boolean;
out LeakIndex: integer;
out LeakSize: Integer): Boolean; overload;
function GetMemoryUseMsg(const FailOnMemoryRecovery: boolean;
const TestProcChangedMem: Integer;
out ErrorMsg: string): boolean; overload;
function GetMemoryUseMsg(const FailOnMemoryRecovery: boolean;
const TestSetupChangedMem: Integer;
const TestProcChangedMem: Integer;
const TestTearDownChangedMem: Integer;
const TestCaseChangedMem: Integer;
out ErrorMsg: string): boolean; overload;
end;
implementation
{$IFNDEF VER180}
uses
SysUtils;
{$ENDIF}
{ TMemLeakMonitor }
constructor TMemLeakMonitor.Create;
begin
inherited;
GetMemoryManagerState(FMS1);
end;
function TMemLeakMonitor.MemLeakDetected(out LeakSize: Integer): boolean;
var
I: Integer;
SMBSize1,
SMBSize2: Int64;
begin
LeakSize := 0;
SMBSize1 := 0;
SMBSize2 := 0;
GetMemoryManagerState(FMS2);
for I := 0 to NumSmallBlockTypes - 1 do // Iterate through the blocks
begin
Inc(SMBSize1, (FMS1.SmallBlockTypeStates[i].InternalBlockSize *
FMS1.SmallBlockTypeStates[i].AllocatedBlockCount));
Inc(SMBSize2, (FMS2.SmallBlockTypeStates[i].InternalBlockSize *
FMS2.SmallBlockTypeStates[i].AllocatedBlockCount));
end;
LeakSize := (SMBSize2 - SMBSize1);
LeakSize := LeakSize +
(Int64(FMS2.TotalAllocatedMediumBlockSize) - Int64(FMS1.TotalAllocatedMediumBlockSize)) +
(Int64(FMS2.TotalAllocatedLargeBlockSize) - Int64(FMS1.TotalAllocatedLargeBlockSize));
Result := LeakSize <> 0;
end;
// May be called after detecting memory use change at Test Procedure level
function TDUnitMemLeakMonitor.GetMemoryUseMsg(const FailOnMemoryRecovery: boolean;
const TestProcChangedMem: Integer;
out ErrorMsg: string): boolean;
begin
ErrorMsg := '';
if (TestProcChangedMem > 0) then
ErrorMsg := IntToStr(TestProcChangedMem) +
' Bytes Memory Leak in Test Procedure'
else
if (TestProcChangedMem < 0) and (FailOnMemoryRecovery) then
ErrorMsg := IntToStr(Abs(TestProcChangedMem)) +
' Bytes Memory Recovered in Test Procedure';
Result := (Length(ErrorMsg) = 0);
end;
function TDUnitMemLeakMonitor.MemLeakDetected(const AllowedLeakSize: Integer;
const FailOnMemoryRecovery: boolean;
out LeakSize: Integer): boolean;
begin
LeakSize := 0;
inherited MemLeakDetected(LeakSize);
Result := ((LeakSize > 0) and (LeakSize <> AllowedLeakSize)) or
((LeakSize < 0) and (FailOnMemoryRecovery) and (LeakSize <> AllowedLeakSize));
end;
procedure TDUnitMemLeakMonitor.MarkMemInUse;
begin
GetMemoryManagerState(FMS1);
end;
function TDUnitMemLeakMonitor.MemLeakDetected(const AllowedValuesGetter: TListIterator;
const FailOnMemoryRecovery: Boolean;
out LeakIndex: integer;
out LeakSize: Integer): Boolean;
var
AllowedLeakSize: Integer;
begin
LeakIndex := 0;
LeakSize := 0;
Result := False;
inherited MemLeakDetected(LeakSize);
if (LeakSize = 0) then
exit;
// Next line access value stored via SetAllowedLeakSize, if any
if LeakSize = AllowedValuesGetter then
Exit;
repeat // loop over values stored via SetAllowedLeakArray
inc(LeakIndex);
AllowedLeakSize := AllowedValuesGetter;
if (LeakSize = AllowedLeakSize) then
Exit;
until (AllowedLeakSize = 0);
Result := (LeakSize > 0) or ((LeakSize < 0) and FailOnMemoryRecovery);
end;
// Expanded message generation for detected leak isolation
// Use additional knowledge of when Setup and or TearDown have nor run.
function TDUnitMemLeakMonitor.GetMemoryUseMsg(const FailOnMemoryRecovery: boolean;
const TestSetupChangedMem: integer;
const TestProcChangedMem: Integer;
const TestTearDownChangedMem: integer;
const TestCaseChangedMem: Integer;
out ErrorMsg: string): boolean;
var
Location: string;
begin
Result := False;
ErrorMsg := '';
if (TestSetupChangedMem = 0) and (TestProcChangedMem = 0) and
(TestTearDownChangedMem = 0) and (TestCaseChangedMem <> 0) then
begin
ErrorMsg :=
'Error in TestFrameWork. No leaks in Setup, TestProc or Teardown but '+
IntToStr(TestCaseChangedMem) +
' Bytes Memory Leak reported across TestCase';
Exit;
end;
if (TestSetupChangedMem + TestProcChangedMem + TestTearDownChangedMem) <>
TestCaseChangedMem then
begin
ErrorMsg :=
'Error in TestFrameWork. Sum of Setup, TestProc and Teardown leaks <> '+
IntToStr(TestCaseChangedMem) +
' Bytes Memory Leak reported across TestCase';
Exit;
end;
Result := True;
if TestCaseChangedMem = 0 then
Exit; // Dont waste further time here
if (TestCaseChangedMem < 0) and not FailOnMemoryRecovery then
Exit; // Dont waste further time here
// We get to here because there is a memory use imbalance to report.
if (TestCaseChangedMem > 0) then
ErrorMsg := IntToStr(TestCaseChangedMem) + ' Bytes memory leak ('
else
ErrorMsg := IntToStr(TestCaseChangedMem) + ' Bytes memory recovered (';
Location := '';
if (TestSetupChangedMem <> 0) then
Location := 'Setup= ' + IntToStr(TestSetupChangedMem) + ' ';
if (TestProcChangedMem <> 0) then
Location := Location + 'TestProc= ' + IntToStr(TestProcChangedMem) + ' ';
if (TestTearDownChangedMem <> 0) then
Location := Location + 'TearDown= ' + IntToStr(TestTearDownChangedMem) + ' ';
ErrorMsg := ErrorMsg + Location + ')';
Result := (Length(ErrorMsg) = 0);
end;
end.
{#(@)$Id: FastMMMemLeakMonitor.pas,v 1.1 2006/07/19 02:55:29 judc Exp $ }
{ DUnit: An XTreme testing framework for Delphi programs. }
(*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is DUnit.
*
* The Initial Developers of the Original Code are Kent Beck, Erich Gamma,
* and Juancarlo Aez.
* Portions created The Initial Developers are Copyright (C) 1999-2000.
* Portions created by The DUnit Group are Copyright (C) 2000-2004.
* All rights reserved.
*
* Contributor(s):
* Kent Beck <kentbeck@csi.com>
* Erich Gamma <Erich_Gamma@oti.com>
* Juanco Aez <juanco@users.sourceforge.net>
* Chris Morris <chrismo@users.sourceforge.net>
* Jeff Moore <JeffMoore@users.sourceforge.net>
* Uberto Barbini <uberto@usa.net>
* Brett Shearer <BrettShearer@users.sourceforge.net>
* Kris Golko <neuromancer@users.sourceforge.net>
* The DUnit group at SourceForge <http://dunit.sourceforge.net>
* Peter McNab <>
*
*******************************************************************************
* Code to provide Memory Leak Detection at the test case level.
* This code makes use of FastMM4.pas available from
* http://fastmm.sourceforge.net
*
* FastMM is a fast replacement memory manager for Borland Delphi Win32
* applications that scales well under multi-threaded usage, is not prone to
* memory fragmentation, and supports shared memory without the use of
* external .DLL files.
* To use FastMM in DUnit for memory leak detection it is necessary to download
* the latest stable release of FastMM from "fastmm.sourceforge.net"
* Then add the path of the folder containing the FastMM4 source code to the
* DUnit project's search path.
*
* Generally it should not be necessary to change FastMMOptions.inc settings.
* However read the notes in FastMMOptions.inc carefully to understand the
* effect of each option and set accordingly to best meet your testing
* environment.
*
* Finally, select
* "Project, Option, Directories/Conditionals, Conditional Defines"
* in the Delphi IDE and add the conditional define
* FASTMM (prefix with an extra ; if there are other defines)
* to allow DUnit to use the FASTMM specific code.
*
*)
unit FastMMMemLeakMonitor;
interface
uses
{$IFDEF VER180}
SysUtils,
{$ELSE}
{$IFDEF FASTMM}
FastMM4,
{$ENDIF}
{$ENDIF}
TestFrameWork;
type
TMemLeakMonitor = class(TInterfacedObject, IMemLeakMonitor)
protected
FMS1: TMemoryManagerState;
FMS2: TMemoryManagerState;
function MemLeakDetected(out LeakSize: Integer): boolean; overload;
public
constructor Create;
end;
TDUnitMemLeakMonitor = class(TMemLeakMonitor, IDUnitMemLeakMonitor)
procedure MarkMemInUse;
function MemLeakDetected(const AllowedLeakSize: Integer;
const FailOnMemoryRecovery: boolean;
out LeakSize: Integer): boolean; overload;
function MemLeakDetected(const AllowedValuesGetter: TListIterator;
const FailOnMemoryRecovery: Boolean;
out LeakIndex: integer;
out LeakSize: Integer): Boolean; overload;
function GetMemoryUseMsg(const FailOnMemoryRecovery: boolean;
const TestProcChangedMem: Integer;
out ErrorMsg: string): boolean; overload;
function GetMemoryUseMsg(const FailOnMemoryRecovery: boolean;
const TestSetupChangedMem: Integer;
const TestProcChangedMem: Integer;
const TestTearDownChangedMem: Integer;
const TestCaseChangedMem: Integer;
out ErrorMsg: string): boolean; overload;
end;
implementation
{$IFNDEF VER180}
uses
SysUtils;
{$ENDIF}
{ TMemLeakMonitor }
constructor TMemLeakMonitor.Create;
begin
inherited;
GetMemoryManagerState(FMS1);
end;
function TMemLeakMonitor.MemLeakDetected(out LeakSize: Integer): boolean;
var
I: Integer;
SMBSize1,
SMBSize2: Int64;
begin
LeakSize := 0;
SMBSize1 := 0;
SMBSize2 := 0;
GetMemoryManagerState(FMS2);
for I := 0 to NumSmallBlockTypes - 1 do // Iterate through the blocks
begin
Inc(SMBSize1, (FMS1.SmallBlockTypeStates[i].InternalBlockSize *
FMS1.SmallBlockTypeStates[i].AllocatedBlockCount));
Inc(SMBSize2, (FMS2.SmallBlockTypeStates[i].InternalBlockSize *
FMS2.SmallBlockTypeStates[i].AllocatedBlockCount));
end;
LeakSize := (SMBSize2 - SMBSize1);
LeakSize := LeakSize +
(Int64(FMS2.TotalAllocatedMediumBlockSize) - Int64(FMS1.TotalAllocatedMediumBlockSize)) +
(Int64(FMS2.TotalAllocatedLargeBlockSize) - Int64(FMS1.TotalAllocatedLargeBlockSize));
Result := LeakSize <> 0;
end;
// May be called after detecting memory use change at Test Procedure level
function TDUnitMemLeakMonitor.GetMemoryUseMsg(const FailOnMemoryRecovery: boolean;
const TestProcChangedMem: Integer;
out ErrorMsg: string): boolean;
begin
ErrorMsg := '';
if (TestProcChangedMem > 0) then
ErrorMsg := IntToStr(TestProcChangedMem) +
' Bytes Memory Leak in Test Procedure'
else
if (TestProcChangedMem < 0) and (FailOnMemoryRecovery) then
ErrorMsg := IntToStr(Abs(TestProcChangedMem)) +
' Bytes Memory Recovered in Test Procedure';
Result := (Length(ErrorMsg) = 0);
end;
function TDUnitMemLeakMonitor.MemLeakDetected(const AllowedLeakSize: Integer;
const FailOnMemoryRecovery: boolean;
out LeakSize: Integer): boolean;
begin
LeakSize := 0;
inherited MemLeakDetected(LeakSize);
Result := ((LeakSize > 0) and (LeakSize <> AllowedLeakSize)) or
((LeakSize < 0) and (FailOnMemoryRecovery) and (LeakSize <> AllowedLeakSize));
end;
procedure TDUnitMemLeakMonitor.MarkMemInUse;
begin
GetMemoryManagerState(FMS1);
end;
function TDUnitMemLeakMonitor.MemLeakDetected(const AllowedValuesGetter: TListIterator;
const FailOnMemoryRecovery: Boolean;
out LeakIndex: integer;
out LeakSize: Integer): Boolean;
var
AllowedLeakSize: Integer;
begin
LeakIndex := 0;
LeakSize := 0;
Result := False;
inherited MemLeakDetected(LeakSize);
if (LeakSize = 0) then
exit;
// Next line access value stored via SetAllowedLeakSize, if any
if LeakSize = AllowedValuesGetter then
Exit;
repeat // loop over values stored via SetAllowedLeakArray
inc(LeakIndex);
AllowedLeakSize := AllowedValuesGetter;
if (LeakSize = AllowedLeakSize) then
Exit;
until (AllowedLeakSize = 0);
Result := (LeakSize > 0) or ((LeakSize < 0) and FailOnMemoryRecovery);
end;
// Expanded message generation for detected leak isolation
// Use additional knowledge of when Setup and or TearDown have nor run.
function TDUnitMemLeakMonitor.GetMemoryUseMsg(const FailOnMemoryRecovery: boolean;
const TestSetupChangedMem: integer;
const TestProcChangedMem: Integer;
const TestTearDownChangedMem: integer;
const TestCaseChangedMem: Integer;
out ErrorMsg: string): boolean;
var
Location: string;
begin
Result := False;
ErrorMsg := '';
if (TestSetupChangedMem = 0) and (TestProcChangedMem = 0) and
(TestTearDownChangedMem = 0) and (TestCaseChangedMem <> 0) then
begin
ErrorMsg :=
'Error in TestFrameWork. No leaks in Setup, TestProc or Teardown but '+
IntToStr(TestCaseChangedMem) +
' Bytes Memory Leak reported across TestCase';
Exit;
end;
if (TestSetupChangedMem + TestProcChangedMem + TestTearDownChangedMem) <>
TestCaseChangedMem then
begin
ErrorMsg :=
'Error in TestFrameWork. Sum of Setup, TestProc and Teardown leaks <> '+
IntToStr(TestCaseChangedMem) +
' Bytes Memory Leak reported across TestCase';
Exit;
end;
Result := True;
if TestCaseChangedMem = 0 then
Exit; // Dont waste further time here
if (TestCaseChangedMem < 0) and not FailOnMemoryRecovery then
Exit; // Dont waste further time here
// We get to here because there is a memory use imbalance to report.
if (TestCaseChangedMem > 0) then
ErrorMsg := IntToStr(TestCaseChangedMem) + ' Bytes memory leak ('
else
ErrorMsg := IntToStr(TestCaseChangedMem) + ' Bytes memory recovered (';
Location := '';
if (TestSetupChangedMem <> 0) then
Location := 'Setup= ' + IntToStr(TestSetupChangedMem) + ' ';
if (TestProcChangedMem <> 0) then
Location := Location + 'TestProc= ' + IntToStr(TestProcChangedMem) + ' ';
if (TestTearDownChangedMem <> 0) then
Location := Location + 'TearDown= ' + IntToStr(TestTearDownChangedMem) + ' ';
ErrorMsg := ErrorMsg + Location + ')';
Result := (Length(ErrorMsg) = 0);
end;
end.
相关文章推荐
- 【学习笔记】Firefox等浏览器无法正常打开dojo的test文件解决办法
- 在Windows下如何解决Cocos2d-x中实例代码TestCpp找不到lib库文件的问题
- JMeter学习-027-JMeter参数文件(脚本分发)路径问题:jmeter.threads.JMeterThread: Test failed! java.lang.IllegalArgumentException: File distributed.csv must exist and be readable解决方法
- 在Windows如何解决下Cocos2d-x示例代码TestCpp我找不到lib问题库文件
- 在sql2005中附加数据库时出现无法打开物理文件 "F:\ajax\test.mdf"。操作系统错误 5:"5(拒绝访问。)"解决办法
- 极光推送NotificationServiceTest.appex文件变红不存在丢失解决办法
- 解决服务器代码执行mvn test后在classes和test-classes下找不到Spring的bean.xml配置文件问题
- JDBC及配置文件的使用。测试类(@Test和@Before依旧没解决)。DBCP。事务
- python:解决pycharm运行py文件时只有unittest选项的方法
- py+selenium+unittest遇到HTMLTestRunner_cn文件报错:ZeroDivisionError: float division by zero【已解决】
- OpenFOAM用wmake编译applications/test里的文件时缺少libmpi.so.1的解决办法
- 编译时.test文件报错无法解决的方法,关闭test编译
- VB的32位程序在64位系统中出现文件和注册表自动转向的解决方法
- 解决Windows 7删除执行过的 EXE、Bat文件有延迟的问题
- eclipse 中查看文件时,文件后面的 版本号 、 文件的状态图标 等等都不见问题解决
- Win7 64 MTK编译Modis出现fatal error C1083: 无法打开包括文件:“stdarg.h”: No such file or directory 解决方法
- “在Web 窗体设计器中未能加载该文件”的解决方法
- 解决配置文件不提示
- 解决python中路径中包含中文无法找到文件的问题
- 如何解决下载的CHM文件无法显示网页问题