Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/tests/sum.c

    r2b9ddf2 r09687aa  
    1111// Created On       : Wed May 27 17:56:53 2015
    1212// Last Modified By : Peter A. Buhr
    13 // Last Modified On : Fri Jan 26 11:31:02 2018
    14 // Update Count     : 271
     13// Last Modified On : Thu Dec  7 09:13:36 2017
     14// Update Count     : 261
    1515//
    1616
    1717#include <fstream>
    18 #include <stdlib>
    1918
    2019void ?{}( int & c, zero_t ) { c = 0; }
     
    2928
    3029forall( otype T | sumable( T ) )                                                // use trait
    31 T sum( unsigned int size, T a[] ) {
    32         T total = 0;                                                                            // instantiate T from 0 by calling constructor
    33         for ( unsigned int i = 0; i < size; i += 1 )
    34                 total += a[i];                                                                  // select appropriate +
     30T sum( unsigned int n, T a[] ) {
     31        T total = 0;                                                                            // instantiate T, select 0
     32        for ( unsigned int i = 0; i < n; i += 1 )
     33                total += a[i];                                                                  // select +
    3534        return total;
    3635} // sum
     
    5655        } // for
    5756        sout | "sum from" | low | "to" | High | "is"
    58                  | sum( size, (unsigned char *)a ) | ", check" | (int)s | endl;
     57                | sum( size, (unsigned char *)a ) | ", check" | (int)s | endl;
    5958
    6059        int s = 0, a[size], v = low;
     
    6463        } // for
    6564        sout | "sum from" | low | "to" | High | "is"
    66                  | sum( size, (int *)a ) | ", check" | (int)s | endl;
     65                | sum( size, (int *)a ) | ", check" | (int)s | endl;
    6766
    6867        float s = 0.0f, a[size], v = low / 10.0f;
     
    7271        } // for
    7372        sout | "sum from" | low / 10.0f | "to" | High / 10.0f | "is"
    74                  | sum( size, (float *)a ) | ", check" | (float)s | endl;
     73                | sum( size, (float *)a ) | ", check" | (float)s | endl;
    7574
    7675        double s = 0.0, a[size], v = low / 10.0;
     
    8079        } // for
    8180        sout | "sum from" | low / 10.0 | "to" | High / 10.0 | "is"
    82                  | sum( size, (double *)a ) | ", check" | (double)s | endl;
     81                | sum( size, (double *)a ) | ", check" | (double)s | endl;
    8382
    8483        struct S { int i, j; };
     
    9998        } // for
    10099        sout | "sum from" | low | "to" | High | "is"
    101                  | sum( size, (S *)a ) | ", check" | (S)s | endl;
    102 
    103         forall( otype Impl | sumable( Impl ) )
    104         struct GS {
    105                 Impl * x, * y;
    106         };
    107         GS(int) gs;
    108         gs.x = anew( size );                                                            // create array storage for field
    109         s = 0; v = low;
    110         for ( int i = 0; i < size; i += 1, v += 1 ) {
    111                 s += (int)v;
    112                 gs.x[i] = (int)v;                                                               // set filed array in generic type
    113         } // for
    114         sout | "sum from" | low | "to" | High | "is"
    115                  | sum( size, gs.x ) | ", check" | (int)s | endl; // add filed array in generic type
     100                | sum( size, (S *)a ) | ", check" | (S)s | endl;
    116101} // main
    117102
Note: See TracChangeset for help on using the changeset viewer.