[Pas2js] Testing generics

warleyalex warleyalex at yahoo.com.br
Fri Oct 18 23:12:04 CEST 2019


Test I: test OK
====

type
  { TFakeClass }
  generic TFakeClass<_GT> = class
    class function gmax(a,b: _GT):_GT;
  end;

  TFakeClassInt = specialize TFakeClass<integer>;
  TFakeClassDouble = specialize TFakeClass<double>;


{ An example of how to use generics to write a function gmax() that takes
the
  maximum of two not-yet-typed variables. Note that while the functions here
  are namespaced by the classname, FPC versions from 3.1.1 onwards also
support
  fully free-standing generic methods.
}
procedure Fake_Class;
begin
  // show max of two integers
  console.log( 'Integer GMax:', TFakeClassInt.gmax( 23, 56 ) );
  // show max of two doubles
  console.log( 'Double GMax:', TFakeClassDouble.gmax( 23.89, 56.5) );
end;

// console output:
Integer GMax: 56
Double GMax: 56.5
---------------------------------------------------------
Test II --> this test fail :(
=====
  {$mode delphi}

uses
  JS, Classes, SysUtils, Web, Types, Math,
  generics.collections, generics.defaults, generics.strings;

type
  TDoubleIntegerArray = array of TIntegerDynArray;

procedure PrintMatrix(A: TDoubleIntegerArray);
var
  i, j: Integer;
begin
  for i := Low(A) to High(A) do
  begin
    for j := Low(A[0]) to High(A[0]) do
      Write(A[i, j]: 3, ' ');
    Writeln;
  end;
  Writeln; Writeln;
end;

function CustomCompare_1(constref Left, Right: TIntegerDynArray): Integer;
begin
  Result := TCompare.Integer(Right[0], Left[0]);
end;

function CustomCompare_2(constref Left, Right: TIntegerDynArray): Integer;
var
  i: Integer;
begin
  i := 0;
  repeat
    Result := TCompare.Integer(Right[i], Left[i]);
    Inc(i);
  until ((Result <> 0) or (i = Length(Left)));
end;

function CustomCompare1(const Left, Right: TIntegerDynArray): Integer;
begin
  Result := Right[0] - Left[0];
end;

function CustomCompare2(const Left, Right: TIntegerDynArray): Integer;
var
  i: Integer;
begin
  i := 0;
  repeat
    Result := Right[i] - Left[i]; 
    Inc(i);
  until ((Result <> 0) or (i = Length(Left)));
end;


var
  A: TDoubleIntegerArray;
  FoundIndex: Integer;
  i, j: Integer;
begin
  WriteLn('Working with TArray - a two-dimensional integer array');
  WriteLn;

  // Fill integer array with random numbers [1 .. 50]
  SetLength(A, 4, 7);
  Random;
//  Randomize;
  for i := Low(A) to High(A) do
    for j := Low(A[0]) to High(A[0]) do
      A[i, j] := Math.RandomRange(1, 50);

  // Equate some of the elements for further "cascade" sorting
  A[1, 0] := A[0, 0];
  A[2, 0] := A[0, 0];
  A[1, 1] := A[0, 1];

  // Print out what happened
  Writeln('The original array:');
  PrintMatrix(A);

  // ! pas2js don't support anonymous methods yet

  // Sort descending 1st column, with cutom comparer_1
  TArrayHelper<TIntegerDynArray>.Sort(A,
TComparer<TIntegerDynArray>.Construct(@CustomCompare1));
  Writeln('Descending in column 1:');
  PrintMatrix(A);

  // Sort descending 1st column "cascade" -
  // If the line items are equal, compare neighboring
  TArrayHelper<TIntegerDynArray>.Sort(A,
TComparer<TIntegerDynArray>.Construct(@CustomCompare2));
  Writeln('Cascade sorting, starting from the 1st column:');
  PrintMatrix(A);       

//--------------------------------------------------------------------
// This pas2js project compiles but we have this output error:

rtl.js:688 
Uncaught TypeError: Cannot read property '_Release' of null
    at Object.free (rtl.js:688)
    at Object.$mod.$main (projGenerics.lpr:493)
    at doRun (rtl.js:141)
    at run (rtl.js:156)


// example expected results:
==================
Working with TArray - a two-dimensional integer array

The original array:
  9  43  14   1  48  40  33
  9  43  30  21  10  36  45
  9  32  45  45  42  33  42
 39  35  26  38  45  48  15


Descending in column 1:
 39  35  26  38  45  48  15
  9  32  45  45  42  33  42
  9  43  14   1  48  40  33
  9  43  30  21  10  36  45


Cascade sorting, starting from the 1st column:
 39  35  26  38  45  48  15
  9  43  30  21  10  36  45
  9  43  14   1  48  40  33
  9  32  45  45  42  33  42
 
 
-------------------------------------------------------- 
Working with TArray - a two-dimensional integer array

The original array:
  6  31   7  17  47  21  16
  6  31  20  12   9  28   4
  6  44  27  40  29  45  44
 39  12  18  37  10   3  29


Descending in column 1:
 39  12  18  37  10   3  29
  6  44  27  40  29  45  44
  6  31   7  17  47  21  16
  6  31  20  12   9  28   4


Cascade sorting, starting from the 1st column:
 39  12  18  37  10   3  29
  6  44  27  40  29  45  44
  6  31  20  12   9  28   4
  6  31   7  17  47  21  16



--
Sent from: http://pas2js.38893.n8.nabble.com/


More information about the Pas2js mailing list