~ubuntu-branches/ubuntu/saucy/lazarus/saucy

« back to all changes in this revision

Viewing changes to components/tachart/test/SourcesTest.pas

  • Committer: Package Import Robot
  • Author(s): Paul Gevers, Abou Al Montacir, Bart Martens, Paul Gevers
  • Date: 2013-06-08 14:12:17 UTC
  • mfrom: (1.1.9)
  • Revision ID: package-import@ubuntu.com-20130608141217-7k0cy9id8ifcnutc
Tags: 1.0.8+dfsg-1
[ Abou Al Montacir ]
* New upstream major release and multiple maintenace release offering many
  fixes and new features marking a new milestone for the Lazarus development
  and its stability level.
  - The detailed list of changes can be found here:
    http://wiki.lazarus.freepascal.org/Lazarus_1.0_release_notes
    http://wiki.lazarus.freepascal.org/Lazarus_1.0_fixes_branch
* LCL changes:
  - LCL is now a normal package.
      + Platform independent parts of the LCL are now in the package LCLBase
      + LCL is automatically recompiled when switching the target platform,
        unless pre-compiled binaries for this target are already installed.
      + No impact on existing projects.
      + Linker options needed by LCL are no more added to projects that do
        not use the LCL package.
  - Minor changes in LCL basic classes behaviour
      + TCustomForm.Create raises an exception if a form resource is not
        found.
      + TNotebook and TPage: a new implementation of these classes was added.
      + TDBNavigator: It is now possible to have focusable buttons by setting
        Options = [navFocusableButtons] and TabStop = True, useful for
        accessibility and for devices with neither mouse nor touch screen.
      + Names of TControlBorderSpacing.GetSideSpace and GetSpace were swapped
        and are now consistent. GetSideSpace = Around + GetSpace.
      + TForm.WindowState=wsFullscreen was added
      + TCanvas.TextFitInfo was added to calculate how many characters will
        fit into a specified Width. Useful for word-wrapping calculations.
      + TControl.GetColorResolvingParent and
        TControl.GetRGBColorResolvingParent were added, simplifying the work
        to obtain the final color of the control while resolving clDefault
        and the ParentColor.
      + LCLIntf.GetTextExtentExPoint now has a good default implementation
        which works in any platform not providing a specific implementation.
        However, Widgetset specific implementation is better, when available.
      + TTabControl was reorganized. Now it has the correct class hierarchy
        and inherits from TCustomTabControl as it should.
  - New unit in the LCL:
      + lazdialogs.pas: adds non-native versions of various native dialogs,
        for example TLazOpenDialog, TLazSaveDialog, TLazSelectDirectoryDialog.
        It is used by widgetsets which either do not have a native dialog, or
        do not wish to use it because it is limited. These dialogs can also be
        used by user applications directly.
      + lazdeviceapis.pas: offers an interface to more hardware devices such
        as the accelerometer, GPS, etc. See LazDeviceAPIs
      + lazcanvas.pas: provides a TFPImageCanvas descendent implementing
        drawing in a LCL-compatible way, but 100% in Pascal.
      + lazregions.pas. LazRegions is a wholly Pascal implementation of
        regions for canvas clipping, event clipping, finding in which control
        of a region tree one an event should reach, for drawing polygons, etc.
      + customdrawncontrols.pas, customdrawndrawers.pas,
        customdrawn_common.pas, customdrawn_android.pas and
        customdrawn_winxp.pas: are the Lazarus Custom Drawn Controls -controls
        which imitate the standard LCL ones, but with the difference that they
        are non-native and support skinning.
  - New APIs added to the LCL to improve support of accessibility software
    such as screen readers.
* IDE changes:
  - Many improvments.
  - The detailed list of changes can be found here:
    http://wiki.lazarus.freepascal.org/New_IDE_features_since#v1.0_.282012-08-29.29
    http://wiki.lazarus.freepascal.org/Lazarus_1.0_release_notes#IDE_Changes
* Debugger / Editor changes:
  - Added pascal sources and breakpoints to the disassembler
  - Added threads dialog.
* Components changes:
  - TAChart: many fixes and new features
  - CodeTool: support Delphi style generics and new syntax extensions.
  - AggPas: removed to honor free licencing. (Closes: Bug#708695)
[Bart Martens]
* New debian/watch file fixing issues with upstream RC release.
[Abou Al Montacir]
* Avoid changing files in .pc hidden directory, these are used by quilt for
  internal purpose and could lead to surprises during build.
[Paul Gevers]
* Updated get-orig-source target and it compinion script orig-tar.sh so that they
  repack the source file, allowing bug 708695 to be fixed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
interface
22
22
 
23
23
uses
24
 
  Classes, SysUtils, FPCUnit, TestRegistry, TASources;
 
24
  Classes, SysUtils, FPCUnit, TestRegistry,
 
25
  TAChartUtils, TACustomSource, TAIntervalSources, TASources;
25
26
 
26
27
type
27
28
 
30
31
  TListSourceTest = class(TTestCase)
31
32
  private
32
33
    FSource: TListChartSource;
 
34
 
 
35
    procedure AssertItemEquals(
 
36
      const AItem: TChartDataItem; AX, AY: Double; AText: String = '';
 
37
      AColor: TChartColor = clTAColor);
33
38
  protected
34
39
    procedure SetUp; override;
35
40
    procedure TearDown; override;
36
41
  published
37
42
    procedure Basic;
38
43
    procedure DataPoint;
 
44
    procedure DataPointSeparator;
 
45
    procedure Enum;
39
46
    procedure Extent;
40
47
    procedure Multi;
41
48
  end;
58
65
    procedure TearDown; override;
59
66
  published
60
67
    procedure Accumulate;
 
68
    procedure Derivative;
61
69
    procedure Percentage;
62
70
    procedure Reorder;
63
71
  end;
64
72
 
 
73
  TIntervalSourceTest = class(TTestCase)
 
74
  private
 
75
    procedure AssertValueEquals(
 
76
      const AExpected: array of Double; const AActual: TChartValueTextArray);
 
77
  published
 
78
    procedure IntervalSource;
 
79
    procedure ListSource;
 
80
  end;
 
81
 
65
82
implementation
66
83
 
67
84
uses
68
 
  Math, TAChartUtils;
 
85
  Math, TAMath;
 
86
 
 
87
type
 
88
  TDummyTransform = object
 
89
  public
 
90
    function IdentityDouble(AX: Double): Double;
 
91
    function IdentityInteger(AX: Integer): Integer;
 
92
    function PrepareValuesInRangeParams: TValuesInRangeParams;
 
93
    function Round(AX: Double): Integer;
 
94
  end;
 
95
 
 
96
var
 
97
  VDummyTransform: TDummyTransform;
 
98
 
 
99
{ TDummyTransform }
 
100
 
 
101
function TDummyTransform.IdentityDouble(AX: Double): Double;
 
102
begin
 
103
  Result := AX;
 
104
end;
 
105
 
 
106
function TDummyTransform.IdentityInteger(AX: Integer): Integer;
 
107
begin
 
108
  Result := AX;
 
109
end;
 
110
 
 
111
function TDummyTransform.PrepareValuesInRangeParams: TValuesInRangeParams;
 
112
begin
 
113
  with Result do begin
 
114
    FAxisToGraph := @IdentityDouble;
 
115
    FGraphToAxis := @IdentityDouble;
 
116
    FFormat := '';
 
117
    FGraphToImage := @Round;
 
118
    FMin := 30;
 
119
    FMax := 69;
 
120
    FMinStep := 0;
 
121
    FScale := @IdentityInteger;
 
122
    FUseY := false;
 
123
    FIntervals := TChartAxisIntervalParams.Create(nil);
 
124
  end;
 
125
end;
 
126
 
 
127
function TDummyTransform.Round(AX: Double): Integer;
 
128
begin
 
129
  Result := System.Round(AX);
 
130
end;
69
131
 
70
132
{ TCalculatedSourceTest }
71
133
 
82
144
  AssertEquals(2, FSource[1]^.X);
83
145
  AssertEquals(102 + 202, FSource[1]^.Y);
84
146
  AssertEquals(202 + 302, FSource[2]^.Y);
 
147
  FSource.AccumulationDirection := cadForward;
 
148
  AssertEquals(202 + 302, FSource[1]^.Y);
 
149
  AssertEquals(302 + 402, FSource[2]^.Y);
 
150
  FSource.AccumulationDirection := cadBackward;
85
151
  FSource.AccumulationMethod := camAverage;
86
152
  AssertEquals((2002 + 2102) / 2, FSource[20]^.Y);
87
153
  AssertEquals(1, FSource[0]^.X);
88
154
  AssertEquals(102, FSource[0]^.Y);
89
155
  AssertEquals((102 + 202) / 2, FSource[1]^.Y);
90
156
  AssertEquals(102, FSource[0]^.Y);
 
157
  FSource.AccumulationDirection := cadCenter;
 
158
  AssertEquals((1102 + 1202 + 1302) / 3, FSource[11]^.Y);
 
159
  FSource.AccumulationDirection := cadBackward;
91
160
 
92
161
  FSource.AccumulationRange := 5;
93
162
  rng := TMWCRandomGenerator.Create;
110
179
  end;
111
180
end;
112
181
 
 
182
procedure TCalculatedSourceTest.Derivative;
 
183
begin
 
184
  FSource.AccumulationMethod := camDerivative;
 
185
  FSource.AccumulationRange := 2;
 
186
  FOrigin.SetYValue(1, 202);
 
187
  AssertTrue(IsNan(FSource[0]^.Y));
 
188
  AssertEquals(100, FSource[1]^.Y);
 
189
  FSource.AccumulationDirection := cadCenter;
 
190
  AssertEquals(100, FSource[0]^.Y);
 
191
end;
 
192
 
113
193
procedure TCalculatedSourceTest.Percentage;
114
194
begin
115
195
  FSource.Percentage := true;
120
200
 
121
201
procedure TCalculatedSourceTest.Reorder;
122
202
var
123
 
  i, j: Integer;
 
203
  i: Integer;
124
204
begin
125
205
  AssertEquals(3, FSource.YCount);
126
206
  FSource.ReorderYList := '2';
 
207
  AssertEquals(1, FSource.YCount);
 
208
  AssertEquals(104, FSource[0]^.Y);
 
209
  AssertEquals(204, FSource[1]^.Y);
 
210
  FSource.ReorderYList := '0,2';
127
211
  AssertEquals(2, FSource.YCount);
 
212
  AssertEquals(102, FSource[0]^.Y);
128
213
  AssertEquals(104, FSource[0]^.YList[0]);
 
214
  AssertEquals(202, FSource[1]^.Y);
129
215
  AssertEquals(204, FSource[1]^.YList[0]);
130
 
  FSource.ReorderYList := '0,0,0';
131
 
  AssertEquals(4, FSource.YCount);
 
216
  FSource.ReorderYList := '1,1,1';
 
217
  AssertEquals(3, FSource.YCount);
 
218
  AssertEquals(103, FSource[0]^.Y);
132
219
  AssertEquals(103, FSource[0]^.YList[0]);
133
220
  AssertEquals(103, FSource[0]^.YList[1]);
134
 
  AssertEquals(103, FSource[0]^.YList[2]);
135
221
  FSource.ReorderYList := '';
136
222
  for i := 0 to FSource.Count - 1 do begin
137
223
    AssertEquals(FOrigin[i]^.Y, FSource[i]^.Y);
138
 
    for j := 0 to FSource.YCount - 2 do
139
 
      AssertEquals(FOrigin[i]^.YList[j], FSource[i]^.YList[j]);
 
224
    AssertEquals(FOrigin[i]^.YList[0], FSource[i]^.YList[0]);
 
225
    AssertEquals(FOrigin[i]^.YList[1], FSource[i]^.YList[1]);
140
226
  end;
141
227
end;
142
228
 
162
248
 
163
249
{ TListSourceTest }
164
250
 
 
251
procedure TListSourceTest.AssertItemEquals(
 
252
  const AItem: TChartDataItem; AX, AY: Double; AText: String;
 
253
  AColor: TChartColor);
 
254
begin
 
255
  AssertEquals('X', AX, AItem.X);
 
256
  AssertEquals('Y', AY, AItem.Y);
 
257
  AssertEquals('Text', AText, AItem.Text);
 
258
  AssertEquals('Color', AColor, AItem.Color);
 
259
end;
 
260
 
165
261
procedure TListSourceTest.Basic;
 
262
var
 
263
  i: Integer;
 
264
  srcDest: TListChartSource;
166
265
begin
167
266
  FSource.Clear;
168
267
  AssertEquals(0, FSource.Count);
170
269
  AssertEquals(1, FSource.Count);
171
270
  FSource.Delete(0);
172
271
  AssertEquals(0, FSource.Count);
 
272
  for i := 1 to 10 do
 
273
    FSource.Add(i, i * 2, IntToStr(i));
 
274
  srcDest := TListChartSource.Create(nil);
 
275
  try
 
276
    srcDest.CopyFrom(FSource);
 
277
    AssertEquals(FSource.Count, srcDest.Count);
 
278
    for i := 0 to FSource.Count - 1 do
 
279
      with FSource[i]^ do
 
280
        AssertItemEquals(srcDest[i]^, X, Y, Text, Color);
 
281
  finally
 
282
    srcDest.Free;
 
283
  end;
173
284
end;
174
285
 
175
286
procedure TListSourceTest.DataPoint;
178
289
  FSource.DataPoints.Add('3|4|?|text1');
179
290
  FSource.DataPoints.Add('5|6|$FF0000|');
180
291
  AssertEquals(2, FSource.Count);
181
 
  AssertEquals(3, FSource[0]^.X);
182
 
  AssertEquals(4, FSource[0]^.Y);
183
 
  AssertEquals('text1', FSource[0]^.Text);
184
 
  AssertEquals(clTAColor, FSource[0]^.Color);
185
 
  AssertEquals(5, FSource[1]^.X);
186
 
  AssertEquals(6, FSource[1]^.Y);
187
 
  AssertEquals('', FSource[1]^.Text);
188
 
  AssertEquals($FF0000, FSource[1]^.Color);
 
292
  AssertItemEquals(FSource[0]^, 3, 4, 'text1');
 
293
  AssertItemEquals(FSource[1]^, 5, 6, '', $FF0000);
189
294
  FSource[0]^.Color := 0;
190
295
  AssertEquals('3|4|$000000|text1', FSource.DataPoints[0]);
191
296
  FSource.DataPoints.Add('7|8|0|two words');
192
297
  AssertEquals('two words', FSource[2]^.Text);
193
298
end;
194
299
 
 
300
procedure TListSourceTest.DataPointSeparator;
 
301
var
 
302
  oldSeparator: Char;
 
303
begin
 
304
  FSource.Clear;
 
305
  oldSeparator := DefaultFormatSettings.DecimalSeparator;
 
306
  try
 
307
    DefaultFormatSettings.DecimalSeparator := ':';
 
308
    FSource.DataPoints.Add('3:5');
 
309
    AssertEquals(3.5, FSource[0]^.X);
 
310
    FSource.DataPoints[0] := '4.5';
 
311
    AssertEquals(4.5, FSource[0]^.X);
 
312
  finally
 
313
    DefaultFormatSettings.DecimalSeparator := oldSeparator;
 
314
  end;
 
315
end;
 
316
 
 
317
procedure TListSourceTest.Enum;
 
318
var
 
319
  it: PChartDataItem;
 
320
  s: Double = 0;
 
321
begin
 
322
  FSource.Clear;
 
323
  for it in FSource do
 
324
    s += 1;
 
325
  AssertEquals(0, s);
 
326
  FSource.Add(10, 1);
 
327
  FSource.Add(20, 7);
 
328
  for it in FSource do
 
329
    s += it^.X + it^.Y;
 
330
  AssertEquals(38, s);
 
331
end;
 
332
 
195
333
procedure TListSourceTest.Extent;
196
334
 
197
335
  procedure AssertExtent(AX1, AY1, AX2, AY2: Double);
209
347
  Assert(IsInfinite(FSource.Extent.a.X) and IsInfinite(FSource.Extent.a.Y));
210
348
  Assert(IsInfinite(FSource.Extent.b.X) and IsInfinite(FSource.Extent.b.Y));
211
349
 
212
 
  FSource.Add(1, 2, '', 0);
 
350
  FSource.Add(1, 2);
213
351
  AssertExtent(1, 2, 1, 2);
214
352
 
215
 
  FSource.Add(3, 4, '', 0);
 
353
  FSource.Add(3, 4);
216
354
  AssertExtent(1, 2, 3, 4);
217
355
 
218
356
  FSource.SetXValue(0, -1);
221
359
  FSource.SetXValue(1, -2);
222
360
  AssertExtent(-2, 2, -1, 4);
223
361
 
 
362
  FSource.SetXValue(1, SafeNaN);
 
363
  AssertExtent(-1, 2, -1, 4);
 
364
  FSource.SetXValue(1, -2);
 
365
 
224
366
  FSource.SetYValue(0, 5);
225
367
  AssertExtent(-2, 4, -1, 5);
226
368
 
227
369
  FSource.SetYValue(0, 4.5);
228
370
  AssertExtent(-2, 4, -1, 4.5);
 
371
 
 
372
  FSource.SetYValue(1, SafeNaN);
 
373
  AssertExtent(-2, 4.5, -1, 4.5);
 
374
 
 
375
  FSource.Delete(1);
 
376
  AssertExtent(-1, 4.5, -1, 4.5);
 
377
 
 
378
  FSource.Clear;
 
379
  FSource.Add(1, 1);
 
380
  FSource.Add(2, 2);
 
381
  FSource.Add(3, 3);
 
382
  FSource.Add(4, 4);
 
383
  FSource.Delete(0);
 
384
  FSource.Delete(1);
 
385
  AssertExtent(2, 2, 4, 4);
229
386
end;
230
387
 
231
388
procedure TListSourceTest.Multi;
280
437
  end;
281
438
end;
282
439
 
 
440
{ TIntervalSourceTest }
 
441
 
 
442
procedure TIntervalSourceTest.AssertValueEquals(
 
443
  const AExpected: array of Double; const AActual: TChartValueTextArray);
 
444
var
 
445
  i: Integer;
 
446
begin
 
447
  AssertEquals(Length(AExpected), Length(AActual));
 
448
  for i := 0 to High(AExpected) do
 
449
    AssertEquals(AExpected[i], AActual[i].FValue);
 
450
end;
 
451
 
 
452
procedure TIntervalSourceTest.IntervalSource;
 
453
var
 
454
  p: TValuesInRangeParams;
 
455
  src: TIntervalChartSource;
 
456
  r: TChartValueTextArray = nil;
 
457
begin
 
458
  p := VDummyTransform.PrepareValuesInRangeParams;
 
459
  src := TIntervalChartSource.Create(nil);
 
460
  try
 
461
    src.Params.MaxLength := 15;
 
462
    src.ValuesInRange(p, r);
 
463
    AssertValueEquals([20, 30, 40, 50, 60, 70], r);
 
464
    src.Params.Options := [aipUseCount];
 
465
    src.Params.Count := 7;
 
466
    src.Params.Tolerance := 1;
 
467
    src.ValuesInRange(p, r);
 
468
    AssertValueEquals([24, 30, 36, 41, 47, 52, 58, 63, 69, 75], r);
 
469
  finally
 
470
    p.FIntervals.Free;
 
471
    src.Free;
 
472
  end;
 
473
end;
 
474
 
 
475
procedure TIntervalSourceTest.ListSource;
 
476
var
 
477
  i: Integer;
 
478
  p: TValuesInRangeParams;
 
479
  r: TChartValueTextArray = nil;
 
480
  src: TListChartSource;
 
481
 
 
482
  procedure Check(const AExpected: array of Double);
 
483
  begin
 
484
    r := nil;
 
485
    src.ValuesInRange(p, r);
 
486
    AssertValueEquals(AExpected, r);
 
487
  end;
 
488
 
 
489
begin
 
490
  p := VDummyTransform.PrepareValuesInRangeParams;
 
491
  src := TListChartSource.Create(nil);
 
492
  for i := 1 to 10 do
 
493
    src.Add(10 * i, i);
 
494
  try
 
495
    Check([20, 30, 40, 50, 60, 70]);
 
496
    p.FIntervals.MinLength := 20;
 
497
    Check([20, 30, 50, 70]);
 
498
    p.FMin := 81;
 
499
    p.FMax := 82;
 
500
    Check([80, 90]);
 
501
    p.FMin := 9;
 
502
    p.FMax := 11;
 
503
    Check([10, 20]);
 
504
    src.Add(8, 11);
 
505
    Check([8, 10, 20]);
 
506
    p.FMin := 1;
 
507
    p.FMax := 20;
 
508
    p.FIntervals.Options := p.FIntervals.Options - [aipUseMinLength];
 
509
    Check([8, 10, 20, 30]);
 
510
    p.FIntervals.Options := p.FIntervals.Options + [aipUseMinLength];
 
511
    p.FMax := 50;
 
512
    Check([8, 30, 50, 60]);
 
513
  finally
 
514
    p.FIntervals.Free;
 
515
    src.Free;
 
516
  end;
 
517
end;
 
518
 
283
519
initialization
284
520
 
285
 
  RegisterTests([TListSourceTest, TRandomSourceTest, TCalculatedSourceTest]);
 
521
  RegisterTests([
 
522
    TListSourceTest, TRandomSourceTest, TCalculatedSourceTest,
 
523
    TIntervalSourceTest]);
286
524
 
287
525
end.
288
526