Tutorial 15 - Memory leak testing

// tutorial15.ot
group "tutorial_15";

test("Leaking test") leakcheck;
{
  char* str = new char[10];
  verify(1 == 1);
}

test("Leaking test that will also fail") leakcheck;
{
  char* str = new char[10];
  verify(2 == 3);
}

test("A test that is not leaking") leakcheck;
{
  char* str = new char[10];
  verify(4 == 4);
  delete [] str;
}

test("A failing test that is leaking") leakcheck;
{
  char* str = new char[10];
  verify(5 == 6);
  delete [] str;
}

test("A failing test that is not leaking using finally") leakcheck;
{
  char* str = new char[10];
  verify(5 == 6);
}
finally
{
  delete [] str;
}

With leakcheck, a test will report memory leaks. Memory allocated while running the test, which is not deallocated, will be reported as leaked. Selftest is using the operating system's leak reporting tool, so the output will differ between Windows and Linux. A test that is failing and all allocated memory hasn't been deallocated, will be reported as leaking memory.

If you are using finally, you can add code that always will run, even if the test fail. If you are using leackcheck, it will be practical to use finally, to avoid getting memory leak reports when a test fail.

=========================================================================
Opera Testsuite
=========================================================================

=========================================================================
E:/src/operaclean/opera-windows7/modules/selftest/documentation/tutorial/tutorial15.ot(2):  tutorial_15 
=========================================================================
  Leaking test .................................................. Passed
0 bytes in 0 Free Blocks.
10 bytes in 1 Normal Blocks.
0 bytes in 0 CRT Blocks.
0 bytes in 0 Ignore Blocks.
0 bytes in 0 Client Blocks.
Largest number used: 0 bytes.
Total allocations: 2010 bytes.
Dumping objects ->
{136310} normal block at 0x0277C030, 10 bytes long.
 Data: <様様様様様> CD CD CD CD CD CD CD CD CD CD 
Object dump complete.
  Leaking test that will also fail .............................. FAILED
E:/src/operaclean/opera-windows7/modules/selftest/documentation/tutorial/tutorial15.ot(13): '2' should be 3. The value is 2
0 bytes in 0 Free Blocks.
10 bytes in 1 Normal Blocks.
0 bytes in 0 CRT Blocks.
0 bytes in 0 Ignore Blocks.
0 bytes in 0 Client Blocks.
Largest number used: 0 bytes.
Total allocations: 5010 bytes.
Dumping objects ->
{136314} normal block at 0x0277C9F0, 10 bytes long.
 Data: <様様様様様> CD CD CD CD CD CD CD CD CD CD 
Object dump complete.
  A test that is not leaking .................................... Passed
  A failing test that is leaking ................................ FAILED
E:/src/operaclean/opera-windows7/modules/selftest/documentation/tutorial/tutorial15.ot(26): '5' should be 6. The value is 5
0 bytes in 0 Free Blocks.
10 bytes in 1 Normal Blocks.
0 bytes in 0 CRT Blocks.
0 bytes in 0 Ignore Blocks.
0 bytes in 0 Client Blocks.
Largest number used: 0 bytes.
Total allocations: 5010 bytes.
Dumping objects ->
{136325} normal block at 0x0277C9B0, 10 bytes long.
 Data: <様様様様様> CD CD CD CD CD CD CD CD CD CD 
Object dump complete.
  A failing test that is not leaking using finally .............. FAILED
E:/src/operaclean/opera-windows7/modules/selftest/documentation/tutorial/tutorial15.ot(33): '5' should be 6. The value is 5
=========================================================================
5 tests run, 3 tests failed, 0 tests skipped, 2 tests OK
Place a breakpoint in testsuite_break_here() to debug the failed tests
=========================================================================