Changeset 2b9ddf2


Ignore:
Timestamp:
Jan 26, 2018, 11:46:44 AM (4 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
aaron-thesis, arm-eh, cleanup-dtors, deferred_resn, demangler, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, resolv-new, with_gc
Children:
f4a6101
Parents:
728df66
Message:

add generic type to sum test

Location:
src/tests
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/tests/.expect/sum.txt

    r728df66 r2b9ddf2  
    44sum from 0.5 to 1.5 is 9.5, check 9.5
    55sum from 5 to 15 is 95 95, check 95 95
     6sum from 5 to 15 is 95, check 95
  • src/tests/sum.c

    r728df66 r2b9ddf2  
    1111// Created On       : Wed May 27 17:56:53 2015
    1212// Last Modified By : Peter A. Buhr
    13 // Last Modified On : Thu Dec  7 09:13:36 2017
    14 // Update Count     : 261
     13// Last Modified On : Fri Jan 26 11:31:02 2018
     14// Update Count     : 271
    1515//
    1616
    1717#include <fstream>
     18#include <stdlib>
    1819
    1920void ?{}( int & c, zero_t ) { c = 0; }
     
    2829
    2930forall( otype T | sumable( T ) )                                                // use trait
    30 T 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 +
     31T 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 +
    3435        return total;
    3536} // sum
     
    5556        } // for
    5657        sout | "sum from" | low | "to" | High | "is"
    57                 | sum( size, (unsigned char *)a ) | ", check" | (int)s | endl;
     58                 | sum( size, (unsigned char *)a ) | ", check" | (int)s | endl;
    5859
    5960        int s = 0, a[size], v = low;
     
    6364        } // for
    6465        sout | "sum from" | low | "to" | High | "is"
    65                 | sum( size, (int *)a ) | ", check" | (int)s | endl;
     66                 | sum( size, (int *)a ) | ", check" | (int)s | endl;
    6667
    6768        float s = 0.0f, a[size], v = low / 10.0f;
     
    7172        } // for
    7273        sout | "sum from" | low / 10.0f | "to" | High / 10.0f | "is"
    73                 | sum( size, (float *)a ) | ", check" | (float)s | endl;
     74                 | sum( size, (float *)a ) | ", check" | (float)s | endl;
    7475
    7576        double s = 0.0, a[size], v = low / 10.0;
     
    7980        } // for
    8081        sout | "sum from" | low / 10.0 | "to" | High / 10.0 | "is"
    81                 | sum( size, (double *)a ) | ", check" | (double)s | endl;
     82                 | sum( size, (double *)a ) | ", check" | (double)s | endl;
    8283
    8384        struct S { int i, j; };
     
    9899        } // for
    99100        sout | "sum from" | low | "to" | High | "is"
    100                 | sum( size, (S *)a ) | ", check" | (S)s | endl;
     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
    101116} // main
    102117
Note: See TracChangeset for help on using the changeset viewer.