Changeset 7416d46a for src/tests/sum.c


Ignore:
Timestamp:
Jan 30, 2018, 3:54:32 PM (6 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
633a642
Parents:
f792cb8 (diff), 42be3c3 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/tests/sum.c

    rf792cb8 r7416d46a  
    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.