Download Now

    Smart Records

    This page contains testing results for Maciej's smart record management operators. The compiler for the following tests were build from this svn source:
    svn co -r 33384 http://svn.freepascal.org/svn/fpc/branches/maciej/smart_pointers
    user@linux:~/Development/Pascal/tests$ ./smart 
    
    Global pre-initialize: passed
    Global initializing: passed
    Stack initialize test: passed
    Nested test: passed
    Nested finalized: passed
    Sub local exit test: passed
    Array length test: passed
    Array test: passed
    Tests finalized: passed
    
    Balance: 1
    Total tests failed: 0
    Total tests passed: 9
    Compilation yielded the following messages:
    user@linux:~/Development/Pascal/tests$ fpc smart.pas
    
    Free Pascal Compiler version 3.1.1 [2016/03/30] for x86_64
    Copyright (c) 1993-2015 by Florian Klaempfl and others
    Target OS: Linux for x86-64
    Compiling smart.pas
    smart.pas(49,24) Warning: Local variable "Foo" of a managed type does not seem to be initialized
    smart.pas(64,33) Warning: Local variable "Foo" of a managed type does not seem to be initialized
    smart.pas(76,31) Warning: Variable "Foo" of a managed type does not seem to be initialized
    Linking smart
    83 lines compiled, 0.1 sec
    3 warning(s) issued
    This is the test source code.
    program smart;
    
    {$mode delphi}
    
    var
      FailCount: Integer;
      PassCount: Integer;
      Balance: Integer;
      Created: Boolean;
    
    procedure Check(const Message: string; Valid: Boolean);
    const
      Passed: array[Boolean] of string = (': failed', ': passed');
    begin
      WriteLn(Message, Passed[Valid]);
      if Valid then Inc(PassCount) else Inc(FailCount);
    end;
    
    type
      TFoo = record
      private
        class operator Initialize(var Foo: TFoo);
        class operator Finalize(var Foo: TFoo);
        public
        IntValue: Integer;
      end;
    
      TFooArray = array of TFoo;
    
    class operator TFoo.Initialize(var Foo: TFoo);
    begin
      Inc(Balance);
      Foo.IntValue := Balance;
      Created := True;
    end;
    
    class operator TFoo.Finalize(var Foo: TFoo);
    begin
      Dec(Balance);
      Created := False;
    end;
    
    procedure Tests;
      
      procedure NestedTest;
      var
        Foo: TFoo;
      begin
        Check('Nested test', Foo.IntValue = 3);
      end;
    
      procedure ArrayTest;
      var
        Foos: TFooArray;
      begin
        SetLength(Foos, 5);
        Check('Array length test', Balance = 7);
        Check('Array test', Foos[0].IntValue = 3);
      end;
    
    var
      Foo: TFoo;
    begin
      Check('Stack initialize test', Foo.IntValue = 2);
      NestedTest;
      Check('Nested finalized', not Created);
      Check('Sub local exit test', Balance = 2);
      Created := True;
      ArrayTest;
    end;
    
    var
      Foo: TFoo;
    begin
      Check('Global pre-initialize', Created);
      Check('Global initializing', Foo.IntValue = 1);
      Tests;
      Check('Tests finalized', not Created);
      WriteLn;
      WriteLn('Balance: ', Balance);
      WriteLn('Total tests failed: ', FailCount);
      WriteLn('Total tests passed: ', PassCount);
    end.