Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Visualization.Test/3.2/LineChartTests.cs @ 1802

Last change on this file since 1802 was 1607, checked in by cbahner, 16 years ago

#519 test cases + code review

File size: 16.2 KB
Line 
1using System;
2using System.Drawing;
3using System.Threading;
4using NUnit.Framework;
5
6namespace HeuristicLab.Visualization.Test {
7  [TestFixture]
8  public class LineChartTests {
9    private ChartDataRowsModel model;
10
11    [SetUp]
12    public void SetUp() {
13      model = new ChartDataRowsModel();
14    }
15
16    [Test]
17    public void TestLineChart() {
18      LineChartTestForm f = new LineChartTestForm(model);
19
20      IDataRow row1 = new DataRow();
21      IDataRow row2 = new DataRow();
22      IDataRow row3 = new DataRow();
23
24      row1.Color = Color.Red;
25      row2.Color = Color.Green;
26      row3.Color = Color.Blue;
27
28      row1.Thickness = 3;
29      row2.Thickness = 4;
30      row3.Thickness = 5;
31
32      row1.Label = "Simon";
33      row2.Label = "Gertschi";
34      row3.Label = "Maxi";
35
36      row1.Style = DrawingStyle.Solid;
37      row2.Style = DrawingStyle.Solid;
38      row3.Style = DrawingStyle.Dashed;
39
40
41      model.AddDataRow(row1);
42      model.AddDataRow(row2);
43      model.AddDataRow(row3);
44
45      row1.AddValue(10);
46      row1.AddValue(5);
47      row1.AddValue(7);
48      row1.AddValue(3);
49      row1.AddValue(10);
50      row1.AddValue(2);
51
52      row2.AddValue(5);
53      row2.AddValue(6);
54      row2.AddValue(5);
55
56      row3.AddValue(2);
57      row3.AddValue(2);
58      row3.AddValue(2);
59      row3.AddValue(2);
60      row3.AddValue(2);
61
62      Random rand = new Random();
63
64      for (int i = 0; i < 10000; i++) {
65        row1.AddValue(rand.NextDouble() * 10);
66        row2.AddValue(rand.NextDouble() * 10);
67        row3.AddValue(rand.NextDouble() * 10);
68      }
69
70      f.ShowDialog();
71    }
72
73    [Test]
74    public void TestAxes() {
75      LineChartTestForm f = new LineChartTestForm(model);
76
77      IDataRow row1 = new DataRow();
78      IDataRow row2 = new DataRow();
79      IDataRow row3 = new DataRow();
80
81      YAxisDescriptor yaxis1 = model.DefaultYAxis;
82      YAxisDescriptor yaxis2 = new YAxisDescriptor();
83
84      model.XAxisLabel = "X Axis";
85      model.ShowXAxisLabel = true;
86
87      yaxis1.Label = "Y-Axis 1";
88      yaxis1.ShowYAxisLabel = true;
89      yaxis1.Position = AxisPosition.Left;
90      yaxis1.ShowGrid = true;
91      yaxis1.GridColor = Color.Gray;
92
93      yaxis2.Label = "Y-Axis 2";
94      yaxis2.ShowYAxisLabel = true;
95      yaxis2.Position = AxisPosition.Right;
96      yaxis2.ShowGrid = false;
97
98      row1.Color = Color.Red;
99      row1.Thickness = 3;
100      row1.Style = DrawingStyle.Solid;
101      row1.Label = "Die Rote";
102
103      row2.Color = Color.Green;
104      row2.Thickness = 3;
105      row2.Style = DrawingStyle.Solid;
106      row2.Label = "Die Grüne";
107
108      row3.Color = Color.Blue;
109      row3.Thickness = 3;
110      row3.Style = DrawingStyle.Solid;
111      row3.Label = "Die Blaue";
112      row3.YAxis = yaxis2;
113
114      model.AddDataRow(row1);
115      model.AddDataRow(row2);
116      model.AddDataRow(row3);
117
118      Random rand = new Random(42);
119
120      for (int i = 0; i < 10; i++) {
121        row1.AddValue(rand.NextDouble() * 10);
122        row2.AddValue(rand.NextDouble() * 10);
123        row3.AddValue(rand.NextDouble() * 1);
124      }
125
126      f.AddValue += delegate {
127        row1.AddValue(rand.NextDouble() * 10);
128        row2.AddValue(rand.NextDouble() * 10);
129        row3.AddValue(rand.NextDouble() * 1);
130      };
131
132      f.ShowDialog();
133    }
134
135    [Test]
136    public void SimpleTestAggregator() {
137      LineChartTestForm f = new LineChartTestForm(model);
138
139      IDataRow row1 = new DataRow { Label = "row", Color = Color.Red, Thickness = 3, Style = DrawingStyle.Solid };
140
141      model.AddDataRow(row1);
142
143
144      MaxAggregator aggregator = new MaxAggregator {
145        Label = "MinAggregator",
146        Color = Color.Pink,
147        Thickness = 5,
148        Style = DrawingStyle.Solid,
149        LineType = DataRowType.SingleValue
150      };
151      aggregator.AddWatch(row1);
152
153      model.AddDataRow(aggregator);
154
155      row1.AddValue(10);
156      row1.AddValue(5);
157      row1.AddValue(7);
158      row1.AddValue(3);
159      row1.AddValue(10);
160      row1.AddValue(2);
161
162      f.ShowDialog();
163    }
164
165
166    public class Worker {
167      // This method will be called when the thread is started.
168      private readonly ChartDataRowsModel model;
169
170      public Worker(ChartDataRowsModel model) {
171        this.model = model;
172      }
173
174      public void DoWorkMultiLine() {
175        IDataRow row1 = new DataRow { Color = Color.Red, Thickness = 2, Label = "Sinus", Style = DrawingStyle.Solid, ShowMarkers = false };
176        model.AddDataRow(row1);
177
178        IDataRow row2 = new DataRow { Color = Color.Red, Thickness = 3, Label = "Growing", Style = DrawingStyle.Solid, ShowMarkers = false };
179        model.AddDataRow(row2);
180
181        AvgAggregator multiAvgAggregator = new AvgAggregator {
182          Label = "MultiAvgAggregator",
183          Color = Color.DarkOliveGreen,
184          Thickness = 3,
185          Style = DrawingStyle.Solid,
186          LineType = DataRowType.SingleValue,
187          ShowMarkers = false
188        };
189        multiAvgAggregator.AddWatch(row1);
190        multiAvgAggregator.AddWatch(row2);
191        model.AddDataRow(multiAvgAggregator);
192
193        MaxAggregator multiMaxAggregator = new MaxAggregator {
194          Label = "MultiMaxAggregator",
195          Color = Color.DarkKhaki,
196          Thickness = 3,
197          Style = DrawingStyle.Solid,
198          LineType = DataRowType.SingleValue,
199          ShowMarkers = false
200        };
201        multiMaxAggregator.AddWatch(row1);
202        multiMaxAggregator.AddWatch(row2);
203        model.AddDataRow(multiMaxAggregator);
204
205        MinAggregator multiMinAggregator = new MinAggregator {
206          Label = "MultiMinAggregator",
207          Color = Color.DarkRed,
208          Thickness = 3,
209          Style = DrawingStyle.Solid,
210          LineType = DataRowType.SingleValue,
211          ShowMarkers = false
212        };
213        multiMinAggregator.AddWatch(row1);
214        multiMinAggregator.AddWatch(row2);
215        model.AddDataRow(multiMinAggregator);
216
217        //        AvgLineAggregator multiLineAvgAggregator = new AvgLineAggregator {
218        //                                                                           Label = "MultiLineAvgAggregator",
219        //                                                                           Color = Color.Red,
220        //                                                                           Thickness = 4,
221        //                                                                           Style = DrawingStyle.Solid,
222        //                                                                           LineType = DataRowType.Normal,
223        //                                                                           ShowMarkers = false
224        //                                                                         };
225        //        multiLineAvgAggregator.AddWatch(row1);
226        //        multiLineAvgAggregator.AddWatch(row2);
227        //        multiLineAvgAggregator.AddValue(0);
228        //        model.AddDataRow(multiLineAvgAggregator);
229
230        double i = 0;
231
232
233        while (!_shouldStop && i <= 24) {
234          i += 0.2;
235          double newY = Math.Sin(i);
236          Console.WriteLine("working");
237          //row1.AddValue(rand.NextDouble() * 10);
238          row1.AddValue(newY * 10);
239          row2.AddValue(i * 2 - 15);
240          Thread.Sleep(100);
241        }
242        Console.WriteLine("worker thread: terminating gracefully.");
243      }
244
245      public void DoWorkSingleLine() {
246        IDataRow row1 = new DataRow {
247          Color = Color.Red,
248          Thickness = 2,
249          Label = "Sinus",
250          Style = DrawingStyle.Solid,
251          ShowMarkers = false
252        };
253        model.AddDataRow(row1);
254
255        IDataRow row2 = new DataRow {
256          Color = Color.Red,
257          Thickness = 3,
258          Label = "Growing",
259          Style = DrawingStyle.Solid,
260          ShowMarkers = false
261        };
262        model.AddDataRow(row2);
263
264        MinAggregator aggregator = new MinAggregator {
265          Label = "MinAggregator",
266          Color = Color.Pink,
267          Thickness = 3,
268          Style = DrawingStyle.Solid,
269          LineType = DataRowType.SingleValue
270        };
271        aggregator.AddWatch(row1);
272        model.AddDataRow(aggregator);
273
274        MaxAggregator maxAggregator = new MaxAggregator {
275          Label = "MaxAggregator",
276          Color = Color.DeepSkyBlue,
277          Thickness = 3,
278          Style = DrawingStyle.Solid,
279          LineType = DataRowType.SingleValue
280        };
281        maxAggregator.AddWatch(row1);
282        model.AddDataRow(maxAggregator);
283
284        AvgAggregator avgAggregator = new AvgAggregator {
285          Label = "AvgAggregator",
286          Color = Color.Violet,
287          Thickness = 3,
288          Style = DrawingStyle.Solid,
289          LineType = DataRowType.SingleValue
290        };
291        avgAggregator.AddWatch(row1);
292        model.AddDataRow(avgAggregator);
293
294        double i = 0;
295
296
297        while (!_shouldStop && i <= 240) {
298          i += 0.2;
299          double newY = Math.Sin(i);
300          Console.WriteLine("working");
301          //row1.AddValue(rand.NextDouble() * 10);
302          row1.AddValue(newY * 10);
303          row2.AddValue(i * 2 - 15);
304          //System.Threading.Thread.Sleep(100);
305        }
306        Console.WriteLine("worker thread: terminating gracefully.");
307      }
308
309      public void DoWorkAvgLine() {
310        IDataRow row1 = new DataRow {
311          Color = Color.Red,
312          Thickness = 2,
313          Label = "Sinus",
314          Style = DrawingStyle.Solid,
315          ShowMarkers = false
316        };
317        model.AddDataRow(row1);
318
319        IDataRow row2 = new DataRow {
320          Color = Color.Red,
321          Thickness = 3,
322          Label = "Growing",
323          Style = DrawingStyle.Solid,
324          ShowMarkers = false
325        };
326        model.AddDataRow(row2);
327
328        AvgLineAggregator avgLineAggregator = new AvgLineAggregator {
329          Label = "AvgLineAggregator",
330          Color = Color.Violet,
331          Thickness = 3,
332          Style = DrawingStyle.Solid,
333          LineType = DataRowType.Normal,
334          ShowMarkers = false
335        };
336        avgLineAggregator.AddWatch(row1);
337        avgLineAggregator.AddWatch(row2);
338        model.AddDataRow(avgLineAggregator);
339
340        double i = 0;
341
342        while (!_shouldStop && i <= 240) {
343          i += 0.2;
344          double newY = Math.Sin(i);
345          Console.WriteLine("working");
346          //row1.AddValue(rand.NextDouble() * 10);
347          row1.AddValue(newY * 10);
348          row2.AddValue(i * 2 - 15);
349          //Thread.Sleep(100);
350        }
351        Console.WriteLine("worker thread: terminating gracefully.");
352      }
353
354      public void DoWorkFloatingAvg() {
355        IDataRow row1 = new DataRow {
356          Color = Color.Red,
357          Thickness = 2,
358          Label = "SinusHacked",
359          Style = DrawingStyle.Solid,
360          ShowMarkers = false
361        };
362        model.AddDataRow(row1);
363
364        IDataRow row2 = new DataRow {
365          Color = Color.Red,
366          Thickness = 3,
367          Label = "GrowingHacked",
368          Style = DrawingStyle.Solid,
369          ShowMarkers = false
370        };
371        model.AddDataRow(row2);
372
373        // insert 2 floating avg line aggregators (for each hacked line)
374
375        // test floating avg aggregator without visible watcher line
376
377        double i = 0;
378        Random rnd = new Random();
379
380        while (!_shouldStop && i <= 240) {
381          i += 0.2;
382          double newY = Math.Sin(i);
383
384          double hack = rnd.NextDouble() * i / 10;
385          row1.AddValue(newY * 10 + hack);
386
387          hack = rnd.NextDouble() * i / 10;
388          row2.AddValue(i * 2 - 15 + hack);
389          //Thread.Sleep(100);
390        }
391        Console.WriteLine("worker thread: terminating gracefully.");
392      }
393
394      public void RequestStop() {
395        _shouldStop = true;
396      }
397
398      // Volatile is used as hint to the compiler that this data
399      // member will be accessed by multiple threads.
400      private volatile bool _shouldStop;
401    }
402
403
404    [Test]
405    public void TestAggregatorMultiLine() {
406      LineChartTestForm f = new LineChartTestForm(model);
407
408      // Create the thread object. This does not start the thread.
409      Worker workerObject = new Worker(model);
410      Thread workerThread = new Thread(workerObject.DoWorkMultiLine);
411
412      // Start the worker thread.
413      workerThread.Start();
414
415      f.ShowDialog();
416      workerObject.RequestStop();
417    }
418
419    [Test]
420    public void TestAggregatorSingleLine() {
421      LineChartTestForm f = new LineChartTestForm(model);
422      model.Title = "SingleLineAggregator Tests";
423
424      // Create the thread object. This does not start the thread.
425      Worker workerObject = new Worker(model);
426      Thread workerThread = new Thread(workerObject.DoWorkSingleLine);
427
428      // Start the worker thread.
429      workerThread.Start();
430
431      f.ShowDialog();
432      workerObject.RequestStop();
433    }
434
435    [Test]
436    public void TestAggregatorAvgLine() {
437      LineChartTestForm f = new LineChartTestForm(model);
438      model.Title = "AvgLineTest";
439
440      // Create the thread object. This does not start the thread.
441      Worker workerObject = new Worker(model);
442      Thread workerThread = new Thread(workerObject.DoWorkAvgLine);
443
444      // Start the worker thread.
445      workerThread.Start();
446
447      f.ShowDialog();
448      workerObject.RequestStop();
449    }
450
451    [Test]
452    public void TestFloatingAvg() {
453      LineChartTestForm f = new LineChartTestForm(model);
454      model.Title = "FloatingAvg Test";
455      model.ViewSettings.LegendPosition = Legend.LegendPosition.Top;
456
457      // Create the thread object. This does not start the thread.
458      Worker workerObject = new Worker(model);
459      Thread workerThread = new Thread(workerObject.DoWorkFloatingAvg);
460
461      // Start the worker thread.
462      workerThread.Start();
463
464      f.ShowDialog();
465      workerObject.RequestStop();
466    }
467
468
469    [Test]
470    public void TestAutoZoomInConstructor() {
471      IDataRow row1 = new DataRow { Color = Color.Red, Thickness = 3, Style = DrawingStyle.Solid };
472
473      model.AddDataRow(row1);
474
475      row1.AddValue(10);
476      row1.AddValue(5);
477      row1.AddValue(7);
478      row1.AddValue(3);
479      row1.AddValue(10);
480      row1.AddValue(2);
481
482      LineChartTestForm f = new LineChartTestForm(model);
483      f.ShowDialog();
484    }
485
486
487    [Test]
488    public void TestSingleValueDataRows() {
489      LineChartTestForm f = new LineChartTestForm(model);
490
491      IDataRow row1 = new DataRow();
492      IDataRow row2 = new DataRow();
493      IDataRow row3 = new DataRow();
494
495      IDataRow row4 = new DataRow();
496      IDataRow row5 = new DataRow();
497      IDataRow row6 = new DataRow();
498
499      row1.Color = Color.Red;
500      row2.Color = Color.Green;
501      row3.Color = Color.Blue;
502
503      row4.Color = Color.DeepPink;
504      row5.Color = Color.Firebrick;
505      row6.Color = Color.DarkSlateGray;
506
507      row1.Thickness = 3;
508      row2.Thickness = 4;
509      row3.Thickness = 5;
510
511      row4.Thickness = 3;
512      row5.Thickness = 4;
513      row6.Thickness = 5;
514
515      row1.Label = "SingleValue";
516      row2.Label = "Gertschi";
517      row3.Label = "Maxi";
518
519      row4.Label = "Simon";
520      row5.Label = "klausmuellerwesternhagenunddierasperies";
521      row6.Label = "anyways";
522
523      row1.Style = DrawingStyle.Solid;
524      row2.Style = DrawingStyle.Solid;
525      row3.Style = DrawingStyle.Dashed;
526
527      row4.Style = DrawingStyle.Solid;
528      row5.Style = DrawingStyle.Solid;
529      row6.Style = DrawingStyle.Dashed;
530
531      row1.LineType = DataRowType.SingleValue;
532      row2.LineType = DataRowType.SingleValue;
533      row1.AddValue(12);
534
535      row2.AddValue(5);
536
537
538      row3.AddValue(2);
539      row3.AddValue(5);
540      row3.AddValue(9);
541      row3.AddValue(1);
542      row3.AddValue(3);
543
544
545      row4.AddValue(10);
546      row5.AddValue(11);
547      row6.AddValue(11);
548
549      model.AddDataRow(row1);
550      model.AddDataRow(row2);
551      model.AddDataRow(row3);
552      model.AddDataRow(row4);
553      model.AddDataRow(row5);
554      model.AddDataRow(row6);
555
556      f.ShowDialog();
557    }
558
559    [Test]
560    public void TestMainForm() {
561      MainForm f = new MainForm();
562      f.ShowDialog();
563    }
564  }
565}
Note: See TracBrowser for help on using the repository browser.