#1 2019-02-06 08:16:09

zed
Member
From: Belarus
Registered: 2015-02-26
Posts: 105

TDynArray.Sort bug

This test case fails on version 1.18.5015:

uses
  mORMot,
  SynCommons;

procedure Test_ArraySorted;
var
  VCount: Integer;
  VArr, VIntArr: TIntegerDynArray;
  VDynArr: TDynArray;
begin
  VDynArr.InitSpecific(TypeInfo(TIntegerDynArray), VIntArr, djInteger, @VCount);

  SetLength(VArr, 1);
  VArr[0] := 1;

  VDynArr.AddArray(VArr);
  VDynArr.Sort;

  Assert(VDynArr.Sorted, 'TDynArray.Sort failed!');
end;

Possible fix (SynCommons.pas):

procedure TDynArray.Sort(aCompare: TDynArraySortCompare);
begin
  if Count <= 1 then
    fSorted := true // array with 0 or 1 element is always sorted
  else
    SortRange(0,Count-1,aCompare);  
end;

Offline

#2 2019-02-06 16:05:27

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,660
Website

Re: TDynArray.Sort bug

Offline

#3 2019-02-07 06:43:44

zed
Member
From: Belarus
Registered: 2015-02-26
Posts: 105

Re: TDynArray.Sort bug

Thank you.

Offline

Board footer

Powered by FluxBB