home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 60 / IOPROG_60.ISO / soft / c++ / gsl-1.1.1-setup.exe / {app} / src / matrix / test_source.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-05  |  12.2 KB  |  469 lines

  1. /* matrix/test_source.c
  2.  * 
  3.  * Copyright (C) 1996, 1997, 1998, 1999, 2000 Gerard Jungman, Brian Gough
  4.  * 
  5.  * This program is free software; you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation; either version 2 of the License, or (at
  8.  * your option) any later version.
  9.  * 
  10.  * This program is distributed in the hope that it will be useful, but
  11.  * WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.  * General Public License for more details.
  14.  * 
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program; if not, write to the Free Software
  17.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  */
  19.  
  20. void FUNCTION (test, func) (void);
  21. void FUNCTION (test, trap) (void);
  22. void FUNCTION (test, text) (void);
  23. void FUNCTION (test, binary) (void);
  24.  
  25.  
  26. void
  27. FUNCTION (test, func) (void)
  28. {
  29.   TYPE (gsl_vector) * v;
  30.   size_t i, j;
  31.   size_t k = 0;
  32.  
  33.   TYPE (gsl_matrix) * m = FUNCTION (gsl_matrix, alloc) (M, N);
  34.  
  35.   gsl_test (m->data == 0, NAME (gsl_matrix) "_alloc returns valid pointer");
  36.   gsl_test (m->size1 != M, NAME (gsl_matrix) "_alloc returns valid size1");
  37.   gsl_test (m->size2 != N, NAME (gsl_matrix) "_alloc returns valid size2");
  38.   gsl_test (m->tda != N, NAME (gsl_matrix) "_alloc returns valid tda");
  39.  
  40.   for (i = 0; i < M; i++)
  41.     {
  42.       for (j = 0; j < N; j++)
  43.     {
  44.       k++;
  45.       FUNCTION (gsl_matrix, set) (m, i, j, (BASE) k);
  46.     }
  47.     }
  48.  
  49.   {
  50.     status = 0;
  51.     k = 0;
  52.     for (i = 0; i < M; i++)
  53.       {
  54.     for (j = 0; j < N; j++)
  55.       {
  56.         k++;
  57.         if (m->data[i * N + j] != (BASE) k)
  58.           status = 1;
  59.       };
  60.       };
  61.  
  62.     gsl_test (status, NAME (gsl_matrix) "_set writes into array correctly");
  63.   }
  64.  
  65.   {
  66.     status = 0;
  67.     k = 0;
  68.     for (i = 0; i < M; i++)
  69.       {
  70.     for (j = 0; j < N; j++)
  71.       {
  72.         k++;
  73.         if (FUNCTION (gsl_matrix, get) (m, i, j) != (BASE) k)
  74.           status = 1;
  75.       };
  76.       };
  77.     gsl_test (status, NAME (gsl_matrix) "_get reads from array correctly");
  78.   }
  79.  
  80.  
  81.   FUNCTION (gsl_matrix, free) (m);    /* free whatever is in m */
  82.  
  83.   m = FUNCTION (gsl_matrix, calloc) (M, N);
  84.   v = FUNCTION (gsl_vector, calloc) (N);
  85.  
  86.   k = 0;
  87.   for (i = 0; i < M; i++)
  88.     {
  89.       for (j = 0; j < N; j++)
  90.     {
  91.       k++;
  92.       FUNCTION (gsl_matrix, set) (m, i, j, (BASE) k);
  93.     }
  94.     }
  95.  
  96.   {
  97.     status = 0;
  98.     k = 0;
  99.     for (i = 0; i < M; i++)
  100.       {
  101.     FUNCTION (gsl_matrix, get_row) (v, m, i);
  102.  
  103.     for (j = 0; j < N; j++)
  104.       {
  105.         k++;
  106.         if (v->data[j] != (BASE) k)
  107.           status = 1;
  108.       }
  109.       }
  110.  
  111.     gsl_test (status, NAME (gsl_matrix) "_get_row extracts row correctly");
  112.   }
  113.  
  114.   {
  115.     BASE exp_max = FUNCTION(gsl_matrix, get) (m, 0, 0);
  116.     BASE exp_min = FUNCTION(gsl_matrix, get) (m, 0, 0);
  117.     size_t exp_imax = 0, exp_jmax = 0, exp_imin = 0, exp_jmin = 0;
  118.  
  119.     for (i = 0; i < M; i++)
  120.       {
  121.         for (j = 0; j < N; j++)
  122.           {
  123.             BASE k = FUNCTION(gsl_matrix, get) (m, i, j);
  124.             if (k > exp_max) {
  125.               exp_max =  FUNCTION(gsl_matrix, get) (m, i, j);
  126.               exp_imax = i;
  127.               exp_jmax = j;
  128.             }
  129.             if (k < exp_min) {
  130.               exp_min =  FUNCTION(gsl_matrix, get) (m, i, j);
  131.               exp_imin = i;
  132.               exp_jmin = j;
  133.             }
  134.           }
  135.       }
  136.  
  137.     {
  138.       BASE max = FUNCTION(gsl_matrix, max) (m) ;
  139.  
  140.       gsl_test (max != exp_max, NAME(gsl_matrix) "_max returns correct maximum value");
  141.     }
  142.  
  143.     {
  144.       BASE min = FUNCTION(gsl_matrix, min) (m) ;
  145.       
  146.       gsl_test (min != exp_min, NAME(gsl_matrix) "_min returns correct minimum value");
  147.     }
  148.  
  149.     {
  150.       BASE min, max;
  151.       FUNCTION(gsl_matrix, minmax) (m, &min, &max);
  152.  
  153.       gsl_test (max != exp_max, NAME(gsl_matrix) "_minmax returns correct maximum value");
  154.       gsl_test (min != exp_min, NAME(gsl_matrix) "_minmax returns correct minimum value");
  155.     }
  156.  
  157.  
  158.     {
  159.       size_t imax, jmax;
  160.       FUNCTION(gsl_matrix, max_index) (m, &imax, &jmax) ;
  161.  
  162.       gsl_test (imax != exp_imax, NAME(gsl_matrix) "_max_index returns correct maximum i");
  163.       gsl_test (jmax != exp_jmax, NAME(gsl_matrix) "_max_index returns correct maximum j");
  164.     }
  165.  
  166.     {
  167.       size_t imin, jmin;
  168.       FUNCTION(gsl_matrix, min_index) (m, &imin, &jmin) ;
  169.  
  170.       gsl_test (imin != exp_imin, NAME(gsl_matrix) "_min_index returns correct minimum i");
  171.       gsl_test (jmin != exp_jmin, NAME(gsl_matrix) "_min_index returns correct minimum j");
  172.     }
  173.  
  174.     {
  175.       size_t imin, jmin, imax, jmax;
  176.  
  177.       FUNCTION(gsl_matrix, minmax_index) (m,  &imin, &jmin, &imax, &jmax);
  178.  
  179.       gsl_test (imax != exp_imax, NAME(gsl_matrix) "_minmax_index returns correct maximum i");
  180.       gsl_test (jmax != exp_jmax, NAME(gsl_matrix) "_minmax_index returns correct maximum j");
  181.  
  182.       gsl_test (imin != exp_imin, NAME(gsl_matrix) "_minmax_index returns correct minimum i");
  183.       gsl_test (jmin != exp_jmin, NAME(gsl_matrix) "_minmax_index returns correct minimum j");
  184.     }
  185.   }
  186.  
  187.   {
  188.     TYPE (gsl_matrix) * a = FUNCTION (gsl_matrix, calloc) (M, N);
  189.     TYPE (gsl_matrix) * b = FUNCTION (gsl_matrix, calloc) (M, N);
  190.     
  191.     for (i = 0; i < M; i++)
  192.       {
  193.         for (j = 0; j < N; j++)
  194.           {
  195.             FUNCTION (gsl_matrix, set) (a, i, j, (BASE)(3 + i +  5 * j));
  196.             FUNCTION (gsl_matrix, set) (b, i, j, (BASE)(3 + 2 * i + 4 * j));
  197.           }
  198.       }
  199.     
  200.     FUNCTION(gsl_matrix, memcpy) (m, a);
  201.     FUNCTION(gsl_matrix, add) (m, b);
  202.     
  203.     {
  204.       int status = 0;
  205.       
  206.       for (i = 0; i < M; i++)
  207.         {
  208.           for (j = 0; j < N; j++)
  209.             {
  210.               BASE r = FUNCTION(gsl_matrix,get) (m,i,j);
  211.               BASE x = FUNCTION(gsl_matrix,get) (a,i,j);
  212.               BASE y = FUNCTION(gsl_matrix,get) (b,i,j);
  213.               BASE z = x + y;
  214.               if (r != z)
  215.                 status = 1;
  216.             }
  217.         }
  218.       gsl_test (status, NAME (gsl_matrix) "_add adds correctly");
  219.     }
  220.  
  221.  
  222.     FUNCTION(gsl_matrix, memcpy) (m, a);
  223.     FUNCTION(gsl_matrix, sub) (m, b);
  224.     
  225.     {
  226.       int status = 0;
  227.       
  228.       for (i = 0; i < M; i++)
  229.         {
  230.           for (j = 0; j < N; j++)
  231.             {
  232.               BASE r = FUNCTION(gsl_matrix,get) (m,i,j);
  233.               BASE x = FUNCTION(gsl_matrix,get) (a,i,j);
  234.               BASE y = FUNCTION(gsl_matrix,get) (b,i,j);
  235.               BASE z = x - y;
  236.               if (r != z)
  237.                 status = 1;
  238.             }
  239.         }
  240.       gsl_test (status, NAME (gsl_matrix) "_sub subtracts correctly");
  241.     }
  242.  
  243.     FUNCTION(gsl_matrix, memcpy) (m, a);
  244.     FUNCTION(gsl_matrix, mul_elements) (m, b);
  245.     
  246.     {
  247.       int status = 0;
  248.       
  249.       for (i = 0; i < M; i++)
  250.         {
  251.           for (j = 0; j < N; j++)
  252.             {
  253.               BASE r = FUNCTION(gsl_matrix,get) (m,i,j);
  254.               BASE x = FUNCTION(gsl_matrix,get) (a,i,j);
  255.               BASE y = FUNCTION(gsl_matrix,get) (b,i,j);
  256.               BASE z = x * y;
  257.               if (r != z)
  258.                 status = 1;
  259.             }
  260.         }
  261.       gsl_test (status, NAME (gsl_matrix) "_mul_elements multiplies correctly");
  262.     }
  263.  
  264.     FUNCTION(gsl_matrix, memcpy) (m, a);
  265.     FUNCTION(gsl_matrix, div_elements) (m, b);
  266.     
  267.     {
  268.       int status = 0;
  269.       
  270.       for (i = 0; i < M; i++)
  271.         {
  272.           for (j = 0; j < N; j++)
  273.             {
  274.               BASE r = FUNCTION(gsl_matrix,get) (m,i,j);
  275.               BASE x = FUNCTION(gsl_matrix,get) (a,i,j);
  276.               BASE y = FUNCTION(gsl_matrix,get) (b,i,j);
  277.               BASE z = x / y;
  278.               if (fabs(r - z) > 2 * GSL_FLT_EPSILON * fabs(z))
  279.                 status = 1;
  280.             }
  281.         }
  282.       gsl_test (status, NAME (gsl_matrix) "_div_elements divides correctly");
  283.     }
  284.  
  285.  
  286.     FUNCTION(gsl_matrix, free) (a);
  287.     FUNCTION(gsl_matrix, free) (b);
  288.   }
  289.  
  290.  
  291.  FUNCTION (gsl_matrix, free) (m);
  292.  FUNCTION (gsl_vector, free) (v);
  293. }
  294.  
  295. #if !(defined(USES_LONGDOUBLE) && !defined(HAVE_PRINTF_LONGDOUBLE))
  296. void
  297. FUNCTION (test, text) (void)
  298. {
  299.   TYPE (gsl_matrix) * m = FUNCTION (gsl_matrix, alloc) (M, N);
  300.  
  301.   size_t i, j;
  302.   int k = 0;
  303.  
  304.   {
  305.     FILE *f = fopen ("test.txt", "w");
  306.  
  307.     for (i = 0; i < M; i++)
  308.       {
  309.     for (j = 0; j < N; j++)
  310.       {
  311.         k++;
  312.         FUNCTION (gsl_matrix, set) (m, i, j, (BASE) k);
  313.       }
  314.       }
  315.  
  316.     FUNCTION (gsl_matrix, fprintf) (f, m, OUT_FORMAT);
  317.     fclose (f);
  318.   }
  319.  
  320.   {
  321.     FILE *f = fopen ("test.txt", "r");
  322.     TYPE (gsl_matrix) * mm = FUNCTION (gsl_matrix, alloc) (M, N);
  323.     status = 0;
  324.  
  325.     FUNCTION (gsl_matrix, fscanf) (f, mm);
  326.     k = 0;
  327.     for (i = 0; i < M; i++)
  328.       {
  329.     for (j = 0; j < N; j++)
  330.       {
  331.         k++;
  332.         if (mm->data[i * N + j] != (BASE) k)
  333.           status = 1;
  334.       }
  335.       }
  336.  
  337.     gsl_test (status, NAME (gsl_matrix) "_fprintf and fscanf work correctly");
  338.  
  339.     fclose (f);
  340.     FUNCTION (gsl_matrix, free) (mm);
  341.   }
  342.  
  343.   FUNCTION (gsl_matrix, free) (m);
  344. }
  345. #endif
  346.  
  347. void
  348. FUNCTION (test, binary) (void)
  349. {
  350.   TYPE (gsl_matrix) * m = FUNCTION (gsl_matrix, calloc) (M, N);
  351.  
  352.   size_t i, j;
  353.   size_t k = 0;
  354.  
  355.   {
  356.     FILE *f = fopen ("test.dat", "wb");
  357.     k = 0;
  358.     for (i = 0; i < M; i++)
  359.       {
  360.     for (j = 0; j < N; j++)
  361.       {
  362.         k++;
  363.         FUNCTION (gsl_matrix, set) (m, i, j, (BASE) k);
  364.       }
  365.       }
  366.  
  367.     FUNCTION (gsl_matrix, fwrite) (f, m);
  368.     fclose (f);
  369.   }
  370.  
  371.   {
  372.     FILE *f = fopen ("test.dat", "rb");
  373.     TYPE (gsl_matrix) * mm = FUNCTION (gsl_matrix, alloc) (M, N);
  374.     status = 0;
  375.  
  376.     FUNCTION (gsl_matrix, fread) (f, mm);
  377.     k = 0;
  378.     for (i = 0; i < M; i++)
  379.       {
  380.     for (j = 0; j < N; j++)
  381.       {
  382.         k++;
  383.         if (mm->data[i * N + j] != (BASE) k)
  384.           status = 1;
  385.       }
  386.       }
  387.  
  388.     gsl_test (status, NAME (gsl_matrix) "_write and read work correctly");
  389.  
  390.     fclose (f);
  391.     FUNCTION (gsl_matrix, free) (mm);
  392.   }
  393.  
  394.   FUNCTION (gsl_matrix, free) (m);
  395. }
  396.  
  397. void
  398. FUNCTION (test, trap) (void)
  399. {
  400.   TYPE (gsl_matrix) * m = FUNCTION (gsl_matrix, alloc) (M, N);
  401.  
  402.   size_t i = 0, j = 0;
  403.   double x;
  404.  
  405.   status = 0;
  406.   FUNCTION (gsl_matrix, set) (m, M + 1, 0, (BASE) 1.2);
  407.   gsl_test (!status,
  408.         NAME (gsl_matrix) "_set traps 1st index above upper bound");
  409.  
  410.   status = 0;
  411.   FUNCTION (gsl_matrix, set) (m, 0, N + 1, (BASE) 1.2);
  412.   gsl_test (!status,
  413.         NAME (gsl_matrix) "_set traps 2nd index above upper bound");
  414.  
  415.   status = 0;
  416.   FUNCTION (gsl_matrix, set) (m, M, 0, (BASE) 1.2);
  417.   gsl_test (!status,
  418.         NAME (gsl_matrix) "_set traps 1st index at upper bound");
  419.  
  420.   status = 0;
  421.   FUNCTION (gsl_matrix, set) (m, 0, N, (BASE) 1.2);
  422.   gsl_test (!status,
  423.         NAME (gsl_matrix) "_set traps 2nd index at upper bound");
  424.  
  425.   status = 0;
  426.   x = FUNCTION (gsl_matrix, get) (m, i - 1, 0);
  427.   gsl_test (!status,
  428.         NAME (gsl_matrix) "_get traps 1st index below lower bound");
  429.   gsl_test (x != 0,
  430.      NAME (gsl_matrix) "_get returns zero for 1st index below lower bound");
  431.  
  432.   status = 0;
  433.   x = FUNCTION (gsl_matrix, get) (m, 0, j - 1);
  434.   gsl_test (!status,
  435.         NAME (gsl_matrix) "_get traps 2nd index below lower bound");
  436.   gsl_test (x != 0,
  437.      NAME (gsl_matrix) "_get returns zero for 2nd index below lower bound");
  438.  
  439.   status = 0;
  440.   x = FUNCTION (gsl_matrix, get) (m, M + 1, 0);
  441.   gsl_test (!status,
  442.         NAME (gsl_matrix) "_get traps 1st index above upper bound");
  443.   gsl_test (x != 0,
  444.      NAME (gsl_matrix) "_get returns zero for 1st index above upper bound");
  445.  
  446.   status = 0;
  447.   x = FUNCTION (gsl_matrix, get) (m, 0, N + 1);
  448.   gsl_test (!status,
  449.         NAME (gsl_matrix) "_get traps 2nd index above upper bound");
  450.   gsl_test (x != 0,
  451.      NAME (gsl_matrix) "_get returns zero for 2nd index above upper bound");
  452.  
  453.   status = 0;
  454.   x = FUNCTION (gsl_matrix, get) (m, M, 0);
  455.   gsl_test (!status,
  456.         NAME (gsl_matrix) "_get traps 1st index at upper bound");
  457.   gsl_test (x != 0,
  458.     NAME (gsl_matrix) "_get returns zero for 1st index at upper bound");
  459.  
  460.   status = 0;
  461.   x = FUNCTION (gsl_matrix, get) (m, 0, N);
  462.   gsl_test (!status,
  463.         NAME (gsl_matrix) "_get traps 2nd index at upper bound");
  464.   gsl_test (x != 0,
  465.     NAME (gsl_matrix) "_get returns zero for 2nd index at upper bound");
  466.  
  467.   FUNCTION (gsl_matrix, free) (m);
  468. }
  469.