Free cookie consent management tool by TermsFeed Policy Generator

source: branches/MathNetNumerics-Exploration-2789/HeuristicLab.Algorithms.DataAnalysis.Experimental/csharp/tests/test_x.cs @ 15311

Last change on this file since 15311 was 15311, checked in by gkronber, 7 years ago

#2789 exploration of alglib solver for non-linear optimization with non-linear constraints

File size: 48.1 KB
Line 
1#pragma warning disable 219
2#pragma warning disable 162
3using System;
4public class MemoryLeaksTest : System.Runtime.ConstrainedExecution.CriticalFinalizerObject
5{
6    public int dummy;
7    public MemoryLeaksTest()
8    {
9        dummy = 0;
10    }
11    ~MemoryLeaksTest()
12    {
13        long cnt = alglib.alloc_counter();
14        System.Console.WriteLine("Allocation counter checked... "+(cnt==0 ? "OK" : "FAILED"));
15        if( cnt!=0 )
16            System.Environment.ExitCode = 1;
17    }
18}
19public class XTest
20{
21    public static void Main(string[] args)
22    {
23        bool _TotalResult = true;
24        bool _TestResult;
25        System.Console.WriteLine("x-tests. Please wait...");
26        alglib.alloc_counter_activate();
27        System.Console.WriteLine("Allocation counter activated...");
28        try
29        {
30            const int max1d = 70;
31            const int max2d = 40;
32           
33            System.Console.WriteLine("Basic tests:");
34            {
35                // deallocateimmediately()
36                alglib.minlbfgsstate s;
37                double[] x = new double[100];
38                long cnt0, cnt1;
39                cnt0 = alglib.alloc_counter();
40                alglib.minlbfgscreate(x.Length, 10, x, out s);
41                alglib.deallocateimmediately(ref s);
42                cnt1 = alglib.alloc_counter();
43                _TestResult = cnt1<=cnt0;
44                System.Console.WriteLine("* deallocateimmediately()    "+(_TestResult ? " OK" : " FAILED"));
45                _TotalResult = _TotalResult && _TestResult;
46            }
47            {
48                // boolean 1D arrays (this test checks both interface and ref/out conventions used by ALGLIB)
49                int n, i, cnt;
50                _TestResult = true;
51                for(n=0; n<=max1d; n++)
52                {
53                    bool[] arr0 = new bool[n];
54                    bool[] arr1 = new bool[n];
55                    bool[] arr2 = new bool[n];
56                    bool[] arr3 = null;
57                    cnt = 0;
58                    for(i=0; i<n; i++)
59                    {
60                        arr0[i] = alglib.math.randomreal()>0.5;
61                        arr1[i] = arr0[i];
62                        arr2[i] = arr0[i];
63                        if( arr0[i] )
64                            cnt++;
65                    }
66                    _TestResult = _TestResult && (alglib.xdebugb1count(arr0)==cnt);
67                    alglib.xdebugb1not(ref arr1);
68                    if( alglib.ap.len(arr1)==n )
69                    {
70                        for(i=0; i<n; i++)
71                            _TestResult = _TestResult && (arr1[i]==!arr0[i]);
72                    }
73                    else
74                        _TestResult = false;
75                    alglib.xdebugb1appendcopy(ref arr2);
76                    if( alglib.ap.len(arr2)==2*n )
77                    {
78                        for(i=0; i<2*n; i++)
79                            _TestResult = _TestResult && (arr2[i]==arr0[i%n]);
80                    }
81                    else
82                        _TestResult = false;
83                    alglib.xdebugb1outeven(n, out arr3);
84                    if( alglib.ap.len(arr3)==n )
85                    {
86                        for(i=0; i<n; i++)
87                            _TestResult = _TestResult && (arr3[i]==(i%2==0));
88                    }
89                    else
90                        _TestResult = false;
91                }
92                System.Console.WriteLine("* boolean 1D arrays          "+(_TestResult ? " OK" : " FAILED"));
93                _TotalResult = _TotalResult && _TestResult;
94            }
95            {
96                // integer 1D arrays (this test checks both interface and ref/out conventions used by ALGLIB)
97                int n, i, sum;
98                _TestResult = true;
99                for(n=0; n<=max1d; n++)
100                {
101                    int[] arr0 = new int[n];
102                    int[] arr1 = new int[n];
103                    int[] arr2 = new int[n];
104                    int[] arr3 = null;
105                    sum = 0;
106                    for(i=0; i<n; i++)
107                    {
108                        arr0[i] = alglib.math.randominteger(10);
109                        arr1[i] = arr0[i];
110                        arr2[i] = arr0[i];
111                        sum+=arr0[i];
112                    }
113                    _TestResult = _TestResult && (alglib.xdebugi1sum(arr0)==sum);
114                    alglib.xdebugi1neg(ref arr1);
115                    if( alglib.ap.len(arr1)==n )
116                    {
117                        for(i=0; i<n; i++)
118                            _TestResult = _TestResult && (arr1[i]==-arr0[i]);
119                    }
120                    else
121                        _TestResult = false;
122                    alglib.xdebugi1appendcopy(ref arr2);
123                    if( alglib.ap.len(arr2)==2*n )
124                    {
125                        for(i=0; i<2*n; i++)
126                            _TestResult = _TestResult && (arr2[i]==arr0[i%n]);
127                    }
128                    else
129                        _TestResult = false;
130                    alglib.xdebugi1outeven(n,out arr3);
131                    if( alglib.ap.len(arr3)==n )
132                    {
133                        for(i=0; i<n; i++)
134                            if( i%2==0 )
135                                _TestResult = _TestResult && (arr3[i]==i);
136                            else
137                                _TestResult = _TestResult && (arr3[i]==0);
138                    }
139                    else
140                        _TestResult = false;
141                }
142                System.Console.WriteLine("* integer 1D arrays          "+(_TestResult ? " OK" : " FAILED"));
143                _TotalResult = _TotalResult && _TestResult;
144            }
145            {
146                // real 1D arrays (this test checks both interface and ref/out conventions used by ALGLIB)
147                int n, i;
148                double sum;
149                _TestResult = true;
150                for(n=0; n<=max1d; n++)
151                {
152                    double[] arr0 = new double[n];
153                    double[] arr1 = new double[n];
154                    double[] arr2 = new double[n];
155                    double[] arr3 = null;
156                    sum = 0;
157                    for(i=0; i<n; i++)
158                    {
159                        arr0[i] = alglib.math.randomreal()-0.5;
160                        arr1[i] = arr0[i];
161                        arr2[i] = arr0[i];
162                        sum+=arr0[i];
163                    }
164                    _TestResult = _TestResult && (Math.Abs(alglib.xdebugr1sum(arr0)-sum)<1.0E-10);
165                    alglib.xdebugr1neg(ref arr1);
166                    if( alglib.ap.len(arr1)==n )
167                    {
168                        for(i=0; i<n; i++)
169                            _TestResult = _TestResult && (Math.Abs(arr1[i]+arr0[i])<1.0E-10);
170                    }
171                    else
172                        _TestResult = false;
173                    alglib.xdebugr1appendcopy(ref arr2);
174                    if( alglib.ap.len(arr2)==2*n )
175                    {
176                        for(i=0; i<2*n; i++)
177                            _TestResult = _TestResult && (arr2[i]==arr0[i%n]);
178                    }
179                    else
180                        _TestResult = false;
181                    alglib.xdebugr1outeven(n,out arr3);
182                    if( alglib.ap.len(arr3)==n )
183                    {
184                        for(i=0; i<n; i++)
185                            if( i%2==0 )
186                                _TestResult = _TestResult && (arr3[i]==i*0.25);
187                            else
188                                _TestResult = _TestResult && (arr3[i]==0);
189                    }
190                    else
191                        _TestResult = false;
192                }
193                System.Console.WriteLine("* real 1D arrays             "+(_TestResult ? " OK" : " FAILED"));
194                _TotalResult = _TotalResult && _TestResult;
195            }
196            {
197                // complex 1D arrays (this test checks both interface and ref/out conventions used by ALGLIB)
198                int n, i;
199                alglib.complex sum;
200                _TestResult = true;
201                for(n=0; n<=max1d; n++)
202                {
203                    alglib.complex[] arr0 = new alglib.complex[n];
204                    alglib.complex[] arr1 = new alglib.complex[n];
205                    alglib.complex[] arr2 = new alglib.complex[n];
206                    alglib.complex[] arr3 = null;
207                    sum = 0;
208                    for(i=0; i<n; i++)
209                    {
210                        arr0[i].x = alglib.math.randomreal()-0.5;
211                        arr0[i].y = alglib.math.randomreal()-0.5;
212                        arr1[i] = arr0[i];
213                        arr2[i] = arr0[i];
214                        sum+=arr0[i];
215                    }
216                    _TestResult = _TestResult && (alglib.math.abscomplex(alglib.xdebugc1sum(arr0)-sum)<1.0E-10);
217                    alglib.xdebugc1neg(ref arr1);
218                    if( alglib.ap.len(arr1)==n )
219                    {
220                        for(i=0; i<n; i++)
221                            _TestResult = _TestResult && (alglib.math.abscomplex(arr1[i]+arr0[i])<1.0E-10);
222                    }
223                    else
224                        _TestResult = false;
225                    alglib.xdebugc1appendcopy(ref arr2);
226                    if( alglib.ap.len(arr2)==2*n )
227                    {
228                        for(i=0; i<2*n; i++)
229                            _TestResult = _TestResult && (arr2[i]==arr0[i%n]);
230                    }
231                    else
232                        _TestResult = false;
233                    alglib.xdebugc1outeven(n,out arr3);
234                    if( alglib.ap.len(arr3)==n )
235                    {
236                        for(i=0; i<n; i++)
237                            if( i%2==0 )
238                            {
239                                _TestResult = _TestResult && (arr3[i].x==i*0.250);
240                                _TestResult = _TestResult && (arr3[i].y==i*0.125);
241                            }
242                            else
243                                _TestResult = _TestResult && (arr3[i]==0);
244                    }
245                    else
246                        _TestResult = false;
247                }
248                System.Console.WriteLine("* complex 1D arrays          "+(_TestResult ? " OK" : " FAILED"));
249                _TotalResult = _TotalResult && _TestResult;
250            }
251            {
252                // boolean 2D arrays (this test checks both interface and ref/out conventions used by ALGLIB)
253                int m, n, i, j, cnt;
254                _TestResult = true;
255                for(n=0; n<=max2d; n++)
256                    for(m=0; m<=max2d; m++)
257                    {
258                        // skip situations when n*m==0, but n!=0 or m!=0
259                        if( n*m==0 && (n!=0 || m!=0) )
260                            continue;
261                       
262                        // proceed to testing
263                        bool[,] arr0 = new bool[m,n];
264                        bool[,] arr1 = new bool[m,n];
265                        bool[,] arr2 = new bool[m,n];
266                        bool[,] arr3 = null;
267                        cnt = 0;
268                        for(i=0; i<m; i++)
269                            for(j=0; j<n; j++)
270                            {
271                                arr0[i,j] = alglib.math.randomreal()>0.5;
272                                arr1[i,j] = arr0[i,j];
273                                arr2[i,j] = arr0[i,j];
274                                if( arr0[i,j] )
275                                    cnt++;
276                            }
277                        _TestResult = _TestResult && (alglib.xdebugb2count(arr0)==cnt);
278                        alglib.xdebugb2not(ref arr1);
279                        if( alglib.ap.rows(arr1)==m && alglib.ap.cols(arr1)==n )
280                        {
281                            for(i=0; i<m; i++)
282                                for(j=0; j<n; j++)
283                                    _TestResult = _TestResult && (arr1[i,j]==!arr0[i,j]);
284                        }
285                        else
286                            _TestResult = false;
287                        alglib.xdebugb2transpose(ref arr2);
288                        if( alglib.ap.rows(arr2)==n && alglib.ap.cols(arr2)==m )
289                        {
290                            for(i=0; i<m; i++)
291                                for(j=0; j<n; j++)
292                                    _TestResult = _TestResult && (arr2[j,i]==arr0[i,j]);
293                        }
294                        else
295                            _TestResult = false;
296                        alglib.xdebugb2outsin(m, n, out arr3);
297                        if( alglib.ap.rows(arr3)==m && alglib.ap.cols(arr3)==n )
298                        {
299                            for(i=0; i<m; i++)
300                                for(j=0; j<n; j++)
301                                    _TestResult = _TestResult && (arr3[i,j]==(Math.Sin(3*i+5*j)>0));
302                        }
303                        else
304                            _TestResult = false;
305                    }
306                System.Console.WriteLine("* boolean 2D arrays          "+(_TestResult ? " OK" : " FAILED"));
307                _TotalResult = _TotalResult && _TestResult;
308            }
309            {
310                // integer 2D arrays (this test checks both interface and ref/out conventions used by ALGLIB)
311                int m, n, i, j;
312                int sum;
313                _TestResult = true;
314                for(n=0; n<=max2d; n++)
315                    for(m=0; m<=max2d; m++)
316                    {
317                        // skip situations when n*m==0, but n!=0 or m!=0
318                        if( n*m==0 && (n!=0 || m!=0) )
319                            continue;
320                       
321                        // proceed to testing
322                        int[,] arr0 = new int[m,n];
323                        int[,] arr1 = new int[m,n];
324                        int[,] arr2 = new int[m,n];
325                        int[,] arr3 = null;
326                        sum = 0;
327                        for(i=0; i<m; i++)
328                            for(j=0; j<n; j++)
329                            {
330                                arr0[i,j] = alglib.math.randominteger(10);
331                                arr1[i,j] = arr0[i,j];
332                                arr2[i,j] = arr0[i,j];
333                                sum += arr0[i,j];
334                            }
335                        _TestResult = _TestResult && (alglib.xdebugi2sum(arr0)==sum);
336                        alglib.xdebugi2neg(ref arr1);
337                        if( alglib.ap.rows(arr1)==m && alglib.ap.cols(arr1)==n )
338                        {
339                            for(i=0; i<m; i++)
340                                for(j=0; j<n; j++)
341                                    _TestResult = _TestResult && (arr1[i,j]==-arr0[i,j]);
342                        }
343                        else
344                            _TestResult = false;
345                        alglib.xdebugi2transpose(ref arr2);
346                        if( alglib.ap.rows(arr2)==n && alglib.ap.cols(arr2)==m )
347                        {
348                            for(i=0; i<m; i++)
349                                for(j=0; j<n; j++)
350                                    _TestResult = _TestResult && (arr2[j,i]==arr0[i,j]);
351                        }
352                        else
353                            _TestResult = false;
354                        alglib.xdebugi2outsin(m, n, out arr3);
355                        if( alglib.ap.rows(arr3)==m && alglib.ap.cols(arr3)==n )
356                        {
357                            for(i=0; i<m; i++)
358                                for(j=0; j<n; j++)
359                                    _TestResult = _TestResult && (arr3[i,j]==System.Math.Sign(Math.Sin(3*i+5*j)));
360                        }
361                        else
362                            _TestResult = false;
363                    }
364                System.Console.WriteLine("* integer 2D arrays          "+(_TestResult ? " OK" : " FAILED"));
365                _TotalResult = _TotalResult && _TestResult;
366            }
367            {
368                // real 2D arrays (this test checks both interface and ref/out conventions used by ALGLIB)
369                int m, n, i, j;
370                double sum;
371                _TestResult = true;
372                for(n=0; n<=max2d; n++)
373                    for(m=0; m<=max2d; m++)
374                    {
375                        // skip situations when n*m==0, but n!=0 or m!=0
376                        if( n*m==0 && (n!=0 || m!=0) )
377                            continue;
378                       
379                        // proceed to testing
380                        double[,] arr0 = new double[m,n];
381                        double[,] arr1 = new double[m,n];
382                        double[,] arr2 = new double[m,n];
383                        double[,] arr3 = null;
384                        sum = 0;
385                        for(i=0; i<m; i++)
386                            for(j=0; j<n; j++)
387                            {
388                                arr0[i,j] = alglib.math.randomreal()-0.5;
389                                arr1[i,j] = arr0[i,j];
390                                arr2[i,j] = arr0[i,j];
391                                sum += arr0[i,j];
392                            }
393                        _TestResult = _TestResult && (System.Math.Abs(alglib.xdebugr2sum(arr0)-sum)<1.0E-10);
394                        alglib.xdebugr2neg(ref arr1);
395                        if( alglib.ap.rows(arr1)==m && alglib.ap.cols(arr1)==n )
396                        {
397                            for(i=0; i<m; i++)
398                                for(j=0; j<n; j++)
399                                    _TestResult = _TestResult && (arr1[i,j]==-arr0[i,j]);
400                        }
401                        else
402                            _TestResult = false;
403                        alglib.xdebugr2transpose(ref arr2);
404                        if( alglib.ap.rows(arr2)==n && alglib.ap.cols(arr2)==m )
405                        {
406                            for(i=0; i<m; i++)
407                                for(j=0; j<n; j++)
408                                    _TestResult = _TestResult && (arr2[j,i]==arr0[i,j]);
409                        }
410                        else
411                            _TestResult = false;
412                        alglib.xdebugr2outsin(m, n, out arr3);
413                        if( alglib.ap.rows(arr3)==m && alglib.ap.cols(arr3)==n )
414                        {
415                            for(i=0; i<m; i++)
416                                for(j=0; j<n; j++)
417                                    _TestResult = _TestResult && (System.Math.Abs(arr3[i,j]-Math.Sin(3*i+5*j))<1E-10);
418                        }
419                        else
420                            _TestResult = false;
421                    }
422                System.Console.WriteLine("* real 2D arrays             "+(_TestResult ? " OK" : " FAILED"));
423                _TotalResult = _TotalResult && _TestResult;
424            }
425            {
426                // real 2D arrays (this test checks both interface and ref/out conventions used by ALGLIB)
427                int m, n, i, j;
428                alglib.complex sum;
429                _TestResult = true;
430                for(n=0; n<=max2d; n++)
431                    for(m=0; m<=max2d; m++)
432                    {
433                        // skip situations when n*m==0, but n!=0 or m!=0
434                        if( n*m==0 && (n!=0 || m!=0) )
435                            continue;
436                       
437                        // proceed to testing
438                        alglib.complex[,] arr0 = new alglib.complex[m,n];
439                        alglib.complex[,] arr1 = new alglib.complex[m,n];
440                        alglib.complex[,] arr2 = new alglib.complex[m,n];
441                        alglib.complex[,] arr3 = null;
442                        sum = 0;
443                        for(i=0; i<m; i++)
444                            for(j=0; j<n; j++)
445                            {
446                                arr0[i,j].x = alglib.math.randomreal()-0.5;
447                                arr0[i,j].y = alglib.math.randomreal()-0.5;
448                                arr1[i,j] = arr0[i,j];
449                                arr2[i,j] = arr0[i,j];
450                                sum += arr0[i,j];
451                            }
452                        _TestResult = _TestResult && (alglib.math.abscomplex(alglib.xdebugc2sum(arr0)-sum)<1.0E-10);
453                        alglib.xdebugc2neg(ref arr1);
454                        if( alglib.ap.rows(arr1)==m && alglib.ap.cols(arr1)==n )
455                        {
456                            for(i=0; i<m; i++)
457                                for(j=0; j<n; j++)
458                                    _TestResult = _TestResult && (arr1[i,j]==-arr0[i,j]);
459                        }
460                        else
461                            _TestResult = false;
462                        alglib.xdebugc2transpose(ref arr2);
463                        if( alglib.ap.rows(arr2)==n && alglib.ap.cols(arr2)==m )
464                        {
465                            for(i=0; i<m; i++)
466                                for(j=0; j<n; j++)
467                                    _TestResult = _TestResult && (arr2[j,i]==arr0[i,j]);
468                        }
469                        else
470                            _TestResult = false;
471                        alglib.xdebugc2outsincos(m, n, out arr3);
472                        if( alglib.ap.rows(arr3)==m && alglib.ap.cols(arr3)==n )
473                        {
474                            for(i=0; i<m; i++)
475                                for(j=0; j<n; j++)
476                                {
477                                    _TestResult = _TestResult && (System.Math.Abs(arr3[i,j].x-Math.Sin(3*i+5*j))<1E-10);
478                                    _TestResult = _TestResult && (System.Math.Abs(arr3[i,j].y-Math.Cos(3*i+5*j))<1E-10);
479                                }
480                        }
481                        else
482                            _TestResult = false;
483                    }
484                System.Console.WriteLine("* complex 2D arrays          "+(_TestResult ? " OK" : " FAILED"));
485                _TotalResult = _TotalResult && _TestResult;
486            }
487            {
488                // "biased product / sum" test
489                int m, n, i, j;
490                double sum;
491                _TestResult = true;
492                for(n=1; n<=max2d; n++)
493                    for(m=1; m<=max2d; m++)
494                    {
495                        // proceed to testing
496                        double[,] a = new double[m,n];
497                        double[,] b = new double[m,n];
498                        bool[,]   c = new bool[m,n];
499                        sum = 0;
500                        for(i=0; i<m; i++)
501                            for(j=0; j<n; j++)
502                            {
503                                a[i,j] = alglib.math.randomreal()-0.5;
504                                b[i,j] = alglib.math.randomreal()-0.5;
505                                c[i,j] = alglib.math.randomreal()>0.5;
506                                if( c[i,j] )
507                                    sum += a[i,j]*(1+b[i,j]);
508                            }
509                        _TestResult = _TestResult && (Math.Abs(alglib.xdebugmaskedbiasedproductsum(m,n,a,b,c)-sum)<1.0E-10);
510                    }
511                System.Console.WriteLine("* multiple arrays            "+(_TestResult ? " OK" : " FAILED"));
512                _TotalResult = _TotalResult && _TestResult;
513            }
514           
515           
516            //////////////////////////////////
517            // Advanced tests
518            //////
519            System.Console.WriteLine("Advances tests:");
520
521            //
522            // Testing CSV functionality
523            //
524            {
525                string csv_name = "alglib-tst-35252-ndg4sf.csv";
526                _TestResult = true;
527                try
528                {
529                    // CSV_DEFAULT must be zero
530                    _TestResult = _TestResult && alglib.CSV_DEFAULT==0;
531                   
532                    // absent file - must fail
533                    try
534                    {
535                        double[,] arr;
536                        alglib.read_csv("nonexistent123foralgtestinglib", '\t', alglib.CSV_DEFAULT, out arr);
537                        _TestResult = false;
538                    }
539                    catch
540                    { }
541                   
542                    // non-rectangular file - must fail
543                    try
544                    {
545                        double[,] arr;
546                        System.IO.File.WriteAllText(csv_name, "a,b,c\r\n1,2");
547                        alglib.read_csv(csv_name, ',', alglib.CSV_SKIP_HEADERS, out arr);
548                        System.IO.File.Delete(csv_name);
549                        _TestResult = false;
550                    }
551                    catch
552                    { }
553                    try
554                    {
555                        double[,] arr;
556                        System.IO.File.WriteAllText(csv_name, "a,b,c\r\n1,2,3,4");
557                        alglib.read_csv(csv_name, ',', alglib.CSV_SKIP_HEADERS, out arr);
558                        System.IO.File.Delete(csv_name);
559                        _TestResult = false;
560                    }
561                    catch
562                    { }
563                    try
564                    {
565                        double[,] arr;
566                        System.IO.File.WriteAllText(csv_name, "1,2,3,4\n1,2,3\n1,2,3");
567                        alglib.read_csv(csv_name, ',', alglib.CSV_DEFAULT, out arr);
568                        System.IO.File.Delete(csv_name);
569                        _TestResult = false;
570                    }
571                    catch
572                    { }
573                   
574                    // empty file
575                    try
576                    {
577                        double[,] arr;
578                        System.IO.File.WriteAllText(csv_name, "");
579                        alglib.read_csv(csv_name, '\t', alglib.CSV_DEFAULT, out arr);
580                        System.IO.File.Delete(csv_name);
581                        _TestResult = _TestResult && arr.GetLength(0)==0 && arr.GetLength(1)==0;
582                    }
583                    catch
584                    { _TestResult = false; }
585                   
586                    // one row with header, tab separator
587                    try
588                    {
589                        double[,] arr;
590                        System.IO.File.WriteAllText(csv_name, "a\tb\tc\n");
591                        alglib.read_csv(csv_name, '\t', alglib.CSV_SKIP_HEADERS, out arr);
592                        System.IO.File.Delete(csv_name);
593                        _TestResult = _TestResult && arr.GetLength(0)==0 && arr.GetLength(1)==0;
594                    }
595                    catch
596                    { _TestResult = false; }
597                   
598                    // no header, comma-separated, full stop as decimal point
599                    try
600                    {
601                        double[,] arr;
602                        System.IO.File.WriteAllText(csv_name, "1.5,2,3.25\n4,5,6");
603                        alglib.read_csv(csv_name, ',', alglib.CSV_DEFAULT, out arr);
604                        System.IO.File.Delete(csv_name);
605                        _TestResult = _TestResult && alglib.ap.format(arr,2)=="{{1.50,2.00,3.25},{4.00,5.00,6.00}}";
606                    }
607                    catch
608                    { _TestResult = false; }
609                   
610                    // header, tab-separated, mixed use of comma and full stop as decimal points
611                    try
612                    {
613                        double[,] arr;
614                        System.IO.File.WriteAllText(csv_name, "a\tb\tc\n1.5\t2\t3,25\n4\t5.25\t6,1\n");
615                        alglib.read_csv(csv_name, '\t', alglib.CSV_SKIP_HEADERS, out arr);
616                        System.IO.File.Delete(csv_name);
617                        _TestResult = _TestResult && alglib.ap.format(arr,2)=="{{1.50,2.00,3.25},{4.00,5.25,6.10}}";
618                    }
619                    catch
620                    { _TestResult = false; }
621                   
622                    // header, tab-separated, fixed/exponential, spaces, mixed use of comma and full stop as decimal points
623                    try
624                    {
625                        double[,] arr;
626                        System.IO.File.WriteAllText(csv_name, " a\t b \tc\n1,1\t 2.9\t -3.5  \n  1.1E1  \t 2.0E-1 \t-3E+1 \n+1  \t -2\t 3.    \n.1\t-.2\t+.3\n");
627                        alglib.read_csv(csv_name, '\t', alglib.CSV_SKIP_HEADERS, out arr);
628                        System.IO.File.Delete(csv_name);
629                        _TestResult = _TestResult && alglib.ap.format(arr,2)=="{{1.10,2.90,-3.50},{11.00,0.20,-30.00},{1.00,-2.00,3.00},{0.10,-0.20,0.30}}";
630                    }
631                    catch
632                    { _TestResult = false; }
633                }
634                catch
635                {
636                    _TestResult = false;
637                }
638               
639                //
640                // Report
641                //
642                System.Console.WriteLine("* CSV support                "+(_TestResult ? " OK" : " FAILED"));
643                _TotalResult = _TotalResult && _TestResult;
644            }
645
646            //
647            // Testing serialization functionality (using kd-trees as playground)
648            //
649            {
650               
651                _TestResult = true;
652                try
653                {
654                    // prepare data
655                    alglib.hqrndstate rs;
656                    alglib.kdtree tree0;
657                    double[,] xy, rxy0 = new double[0,0], rxy1 = new double[0,0];
658                    double[]  qx;
659                    const int npts = 50;
660                    const int nx = 2;
661                    const int ny = 1;
662                    int cnt0, cnt1;
663                    alglib.hqrndrandomize(out rs);
664                    xy = new double[npts,nx+ny];
665                    for(int i=0; i<npts; i++)
666                        for(int j=0; j<nx+ny; j++)
667                            xy[i,j] = alglib.hqrndnormal(rs);
668                    alglib.kdtreebuild(xy, npts, nx, ny, 2, out tree0);
669                    qx = new double[nx];
670                   
671                    try
672                    {
673                        // test string serialization/unserialization
674                        alglib.kdtree tree1;
675                        string s;
676                        alglib.kdtreeserialize(tree0, out s);
677                        alglib.kdtreeunserialize(s, out tree1);
678                        for(int i=0; i<100; i++)
679                        {
680                            for(int j=0; j<nx; j++)
681                                qx[j] = alglib.hqrndnormal(rs);
682                            cnt0 = alglib.kdtreequeryknn(tree0, qx, 1, true);
683                            cnt1 = alglib.kdtreequeryknn(tree1, qx, 1, true);
684                            if( (cnt0!=1) || (cnt1!=1) )
685                            {
686                                _TestResult = false;
687                                break;
688                            }
689                            alglib.kdtreequeryresultsxy(tree0, ref rxy0);
690                            alglib.kdtreequeryresultsxy(tree1, ref rxy1);
691                            for(int j=0; j<nx+ny; j++)
692                                _TestResult = _TestResult && (rxy0[0,j]==rxy1[0,j]);
693                        }
694                    }
695                    catch
696                    { _TestResult = false; }
697                   
698                    try
699                    {
700                        // test stream serialization/unserialization
701                        //
702                        // NOTE: we add a few symbols at the beginning and after the end of the data
703                        //       in order to test algorithm ability to work in the middle of the stream
704                        alglib.kdtree tree1;
705                        System.IO.MemoryStream s = new System.IO.MemoryStream();
706                        s.WriteByte((byte)'b');
707                        s.WriteByte((byte)' ');
708                        s.WriteByte((byte)'e');
709                        s.WriteByte((byte)'g');
710                        alglib.kdtreeserialize(tree0, s);
711                        s.WriteByte((byte)'@');
712                        s.WriteByte((byte)' ');
713                        s.WriteByte((byte)'n');
714                        s.WriteByte((byte)'d');
715                        s.Seek(0, System.IO.SeekOrigin.Begin);
716                        _TestResult = _TestResult && (s.ReadByte()==(byte)'b');
717                        _TestResult = _TestResult && (s.ReadByte()==(byte)' ');
718                        _TestResult = _TestResult && (s.ReadByte()==(byte)'e');
719                        _TestResult = _TestResult && (s.ReadByte()==(byte)'g');
720                        alglib.kdtreeunserialize(s, out tree1);
721                        _TestResult = _TestResult && (s.ReadByte()==(byte)'@');
722                        _TestResult = _TestResult && (s.ReadByte()==(byte)' ');
723                        _TestResult = _TestResult && (s.ReadByte()==(byte)'n');
724                        _TestResult = _TestResult && (s.ReadByte()==(byte)'d');
725                        for(int i=0; i<100; i++)
726                        {
727                            for(int j=0; j<nx; j++)
728                                qx[j] = alglib.hqrndnormal(rs);
729                            cnt0 = alglib.kdtreequeryknn(tree0, qx, 1, true);
730                            cnt1 = alglib.kdtreequeryknn(tree1, qx, 1, true);
731                            if( (cnt0!=1) || (cnt1!=1) )
732                            {
733                                _TestResult = false;
734                                break;
735                            }
736                            alglib.kdtreequeryresultsxy(tree0, ref rxy0);
737                            alglib.kdtreequeryresultsxy(tree1, ref rxy1);
738                            for(int j=0; j<nx+ny; j++)
739                                _TestResult = _TestResult && (rxy0[0,j]==rxy1[0,j]);
740                        }
741                    }
742                    catch
743                    { _TestResult = false; }
744                   
745                    try
746                    {
747                        // test string-to-stream serialization/unserialization
748                        alglib.kdtree tree1;
749                        string s0;
750                        alglib.kdtreeserialize(tree0, out s0);
751                        System.IO.MemoryStream s1 = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(s0));
752                        alglib.kdtreeunserialize(s1, out tree1);
753                        for(int i=0; i<100; i++)
754                        {
755                            for(int j=0; j<nx; j++)
756                                qx[j] = alglib.hqrndnormal(rs);
757                            cnt0 = alglib.kdtreequeryknn(tree0, qx, 1, true);
758                            cnt1 = alglib.kdtreequeryknn(tree1, qx, 1, true);
759                            if( (cnt0!=1) || (cnt1!=1) )
760                            {
761                                _TestResult = false;
762                                break;
763                            }
764                            alglib.kdtreequeryresultsxy(tree0, ref rxy0);
765                            alglib.kdtreequeryresultsxy(tree1, ref rxy1);
766                            for(int j=0; j<nx+ny; j++)
767                                _TestResult = _TestResult && (rxy0[0,j]==rxy1[0,j]);
768                        }
769                    }
770                    catch
771                    { _TestResult = false; }
772                   
773                    try
774                    {
775                        // test stream-to-string serialization/unserialization
776                        alglib.kdtree tree1;
777                        System.IO.MemoryStream s0 = new System.IO.MemoryStream();
778                        alglib.kdtreeserialize(tree0, s0);
779                        s0.Seek(0, System.IO.SeekOrigin.Begin);
780                        string s1 = System.Text.Encoding.UTF8.GetString(s0.ToArray());
781                        alglib.kdtreeunserialize(s1, out tree1);
782                        for(int i=0; i<100; i++)
783                        {
784                            for(int j=0; j<nx; j++)
785                                qx[j] = alglib.hqrndnormal(rs);
786                            cnt0 = alglib.kdtreequeryknn(tree0, qx, 1, true);
787                            cnt1 = alglib.kdtreequeryknn(tree1, qx, 1, true);
788                            if( (cnt0!=1) || (cnt1!=1) )
789                            {
790                                _TestResult = false;
791                                break;
792                            }
793                            alglib.kdtreequeryresultsxy(tree0, ref rxy0);
794                            alglib.kdtreequeryresultsxy(tree1, ref rxy1);
795                            for(int j=0; j<nx+ny; j++)
796                                _TestResult = _TestResult && (rxy0[0,j]==rxy1[0,j]);
797                        }
798                    }
799                    catch
800                    { _TestResult = false; }
801
802                }
803                catch
804                {
805                    _TestResult = false;
806                }
807               
808                //
809                // Report
810                //
811                System.Console.WriteLine("* Serialization (kd-tree)    "+(_TestResult ? " OK" : " FAILED"));
812                _TotalResult = _TotalResult && _TestResult;
813            }
814           
815            //////////////////////////////////
816            // Test issues from Mantis
817            //////
818            System.Console.WriteLine("Testing issies from Mantis:");
819               
820           
821            //
822            // Task #594 (http://bugs.alglib.net/view.php?id=594) - additional
823            // test for correctness of copying of objects. When we copy ALGLIB
824            // object, indenendent new copy is created.
825            //
826            {
827                //
828                // First, test copying of alglib.multilayerperceptron, which
829                // is an "opaque object".
830                //
831                // Test copy constructors:
832                // * copy object with make_copy()
833                // * process vector with original network
834                // * randomize original network
835                // * process vector with copied networks and compare
836                //
837                alglib.multilayerperceptron net0, net1;
838                double[] x  = new double[]{1,2};
839                double[] y0 = new double[]{0,0};
840                double[] y1 = new double[]{0,0};
841                double[] y2 = new double[]{0,0};
842                _TestResult = true;
843                alglib.mlpcreate0(2, 2, out net0);
844                alglib.mlpprocess(net0, x, ref y0);
845                net1 = (alglib.multilayerperceptron)net0.make_copy();
846                alglib.mlprandomize(net0);
847                alglib.mlpprocess(net1, x, ref y1);
848                _TestResult = _TestResult && (Math.Abs(y0[0]-y1[0])<1.0E-9) && (Math.Abs(y0[1]-y1[1])<1.0E-9);
849               
850                //
851                // Then, test correctness of copying "records", i.e.
852                // objects with publicly visible fields.
853                //
854                alglib.xdebugrecord1 r0, r1;
855                alglib.xdebuginitrecord1(out r0);
856                r1 = (alglib.xdebugrecord1)r0.make_copy();
857                _TestResult = _TestResult && (r1.i==r0.i);
858                _TestResult = _TestResult && (r1.c==r0.c);
859               
860                _TestResult = _TestResult && (r1.a.Length==2);
861                _TestResult = _TestResult && (r0.a.Length==2);
862                _TestResult = _TestResult && (r1.a!=r0.a);
863                _TestResult = _TestResult && (r1.a[0]==r0.a[0]);
864                _TestResult = _TestResult && (r1.a[1]==r0.a[1]);
865               
866                //
867                // Test result
868                //
869                System.Console.WriteLine("* issue 594                  "+(_TestResult ? " OK" : " FAILED"));
870                _TotalResult = _TotalResult && _TestResult;
871            }
872           
873        }
874        catch
875        {
876            System.Console.WriteLine("Unhandled exception was raised!");
877            System.Environment.ExitCode = 1;
878            return;
879        }
880       
881           
882        //////////////////////////////////
883        // Backward compatibility tests
884        //////
885        System.Console.WriteLine("Backward compatibility tests:");
886
887        //
888        // Testing RBF storage format
889        //
890        {
891            double eps = 0.0000000001;
892            double[] ref_val = new double[]{
893                -0.042560546916643,
894                 0.942523544654062,
895                 0.875197036560778,
896                 0.0656948997826632,
897                -0.743065973803404,
898                -0.8903682039297,
899                -0.26994815318748,
900                 0.602248517290195,
901                 0.980011992233124,
902                 0.436594293214176
903                };
904            string _ss = @"50000000000 00000000000 20000000000 10000000000 A0000000000
90530000000000 20000000000 00000000000 A0000000000 30000000000
90600000000000 20000000000 A0000000000 60000000000 00000000000
90700000000000 00000000000 00000000000 00000000000 00000000000
90800000000m_3 00000000000 00000000000 00000000m_3 00000000000
90900000000000 00000000004 00000000000 00000000000 00000000004
91000000000000 00000000000 00000000804 00000000000 00000000000
91100000000804 00000000000 00000000000 00000000G04 00000000000
91200000000000 00000000G04 00000000000 00000000000 00000000O04
91300000000000 00000000000 00000000O04 00000000000 00000000000
91400000000S04 00000000000 00000000000 00000000S04 00000000000
91500000000000 00000000W04 00000000000 00000000000 00000000W04
91600000000000 00000000000 00000000Y04 00000000000 00000000000
91700000000Y04 00000000000 00000000000 00000000K04 00000000000
91800000000000 00000000K04 00000000000 00000000000 A0000000000
91900000000000 10000000000 20000000000 30000000000 40000000000
92060000000000 70000000000 80000000000 90000000000 50000000000
92130000000000 00000000000 00000000000 00000000000 30000000000
92200000000Y04 00000000000 00000000000 u1000000000 00000000000
92300000000000 00000000000 60000000000 80000000000 00000000000
92450000000000 00000000000 50000000000 50000000000 00000000000
92500000000000 00000000000 00000000000 00000000000 00000000000
92600000000000 00000000000 00000000000 00000000000 00000000000
92700000000000 00000000000 00000000000 00000000000 00000000000
92800000000000 00000000000 00000000000 00000000000 00000000000
92900000000000 00000000000 00000000000 00000000000 00000000000
93000000000000 00000000000 00000000000 00000000000 00000000000
93100000000000 00000000000 00000000000 00000000000 00000000000
93200000000000 00000000000 00000000000 00000000000 00000000000
93300000000000 00000000000 00000000000 00000000000 00000000000
93400000000000 00000000000 00000000000 00000000000 00000000000
93500000000000 00000000000 00000000000 00000000000 00000000000
93600000000000 00000000000 00000000000 00000000000 00000000000
93700000000000 00000000000 00000000000 00000000000 00000000000
93800000000000 00000000000 00000000000 00000000000 00000000000
93900000000000 00000000000 00000000000 00000000000 00000000000
94000000000000 00000000000 00000000000 00000000000 00000000000
94100000000000 00000000000 00000000000 00000000000 00000000000
94200000000000 00000000000 00000000000 00000000000 00000000000
94300000000000 00000000000 00000000000 00000000000 00000000000
94400000000000 00000000000 00000000000 00000000000 00000000000
94500000000000 00000000000 00000000000 00000000000 00000000000
94600000000000 00000000000 00000000000 00000000000 K0000000000
94700000000I04 00000000000 00000000000 00000000000 00000000000
94800000000000 00000000000 00000000000 00000000000 00000000000
94900000000000 00000000000 00000000000 00000000000 00000000000
95000000000000 00000000000 00000000000 00000000000 00000000000
951A0000000000 30000000000 00000000000 00000000000 00000000000
95200000000m_3 00000000000 00000000000 00000000004 00000000000
95300000000000 00000000804 00000000000 00000000000 00000000G04
95400000000000 00000000000 00000000K04 00000000000 00000000000
95500000000O04 00000000000 00000000000 00000000S04 00000000000
95600000000000 00000000W04 00000000000 00000000000 00000000Y04
95700000000000 00000000000 A0000000000 40000000000 00000000q04
958-pAGQnQBI14 UqUWierJ91C esm8ag6G61C 00000000q04 4wcFMyCtu04
959oPDvwHqst04 CExQXp8Ct04 00000000q04 litzPFhRb0C oKJvjcct314
9605-fT-X8w614 00000000q04 3HSOsPVH11C vZWf4dgfv04 GbZg4MTJn04
96100000000q04 iv7rMhuR71C hRtixp15r_3 EvCEDtLu-0C 00000000q04
96241CXzA_q71C umRYLK2yp0C 1zzY3Zqd91C 00000000q04 JvxJzDeI21C
963TVbyd7Ygz0C JLywRdR1n0C 00000000q04 KmFarhc4g0C 1ehrn2tUt0C
964AECfwTIX814 00000000q04 Big__6hwt04 nSPzmAQrh_B 2H3o-KftH14
96500000000q04 n1b9361vI14 mhJhviUE114 54a_qyBrH1C 00000000q04
96610000000000 40000000000 StLCgor39-3 00000000000 00000000000
9676qTG7Ae-1_3
968";
969
970            // test string unserialization without trailing dot symbol (end-of-stream marker); must work
971            try
972            {
973                string s = _ss;
974                alglib.rbfmodel model;
975                alglib.rbfunserialize(s, out model);
976                _TestResult = true;
977                for(int i=0; i<ref_val.Length; i++)
978                    _TestResult = _TestResult && (Math.Abs(alglib.rbfcalc2(model,i,0)-ref_val[i])<eps);
979            }
980            catch
981            { _TestResult = false; }
982
983            // test string unserialization with trailing dot symbol (end-of-stream marker); must work
984            try
985            {
986                string s = _ss+".";
987                alglib.rbfmodel model;
988                alglib.rbfunserialize(s, out model);
989                _TestResult = true;
990                for(int i=0; i<ref_val.Length; i++)
991                    _TestResult = _TestResult && (Math.Abs(alglib.rbfcalc2(model,i,0)-ref_val[i])<eps);
992            }
993            catch
994            { _TestResult = false; }
995
996            // test stream unserialization with trailing dot symbol (end-of-stream marker); must work
997            try
998            {
999                System.IO.Stream s = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(_ss+"."));
1000                alglib.rbfmodel model;
1001                alglib.rbfunserialize(s, out model);
1002                _TestResult = true;
1003                for(int i=0; i<ref_val.Length; i++)
1004                    _TestResult = _TestResult && (Math.Abs(alglib.rbfcalc2(model,i,0)-ref_val[i])<eps);
1005            }
1006            catch
1007            { _TestResult = false; }
1008
1009            // test stream unserialization with trailing dot symbol (end-of-stream marker); MUST FAIL
1010            try
1011            {
1012                System.IO.Stream s = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(_ss));
1013                alglib.rbfmodel model;
1014                alglib.rbfunserialize(s, out model);
1015                _TestResult = false; // must FAIL!
1016            }
1017            catch
1018            {  }
1019           
1020            //
1021            // Report
1022            //
1023            System.Console.WriteLine("* RBF bwd compatibility      "+(_TestResult ? " OK" : " FAILED"));
1024            _TotalResult = _TotalResult && _TestResult;
1025        }
1026       
1027       
1028        //
1029        // Test below creates instance of MemoryLeaksTest object.
1030        //
1031        // This object is descendant of CriticalFinalizerObject class,
1032        // which guarantees that it will be finalized AFTER all other
1033        // ALGLIB objects which hold pointers to unmanaged memory.
1034        //
1035        // Tests for memory leaks are done within object's destructor.
1036        //
1037        MemoryLeaksTest _test_object = new MemoryLeaksTest();
1038        if( !_TotalResult )
1039            System.Environment.ExitCode = 1;
1040    }
1041}
Note: See TracBrowser for help on using the repository browser.