Changeset 90152a4 for tests/sum.c


Ignore:
Timestamp:
Aug 27, 2018, 4:40:34 PM (7 years ago)
Author:
Rob Schluntz <rschlunt@…>
Branches:
ADT, arm-eh, ast-experimental, cleanup-dtors, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
b7c89aa
Parents:
f9feab8 (diff), 305581d (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' into cleanup-dtors

File:
1 moved

Legend:

Unmodified
Added
Removed
  • tests/sum.c

    rf9feab8 r90152a4  
    1111// Created On       : Wed May 27 17:56:53 2015
    1212// Last Modified By : Peter A. Buhr
    13 // Last Modified On : Sun Oct 29 10:29:12 2017
    14 // Update Count     : 260
     13// Last Modified On : Thu Aug  2 08:03:09 2018
     14// Update Count     : 279
    1515//
    1616
    17 #include <fstream>
     17#include <fstream.hfa>
     18#include <stdlib.hfa>
    1819
    19 void ?{}( int & c, zero_t ) { c = 0; }
     20void ?{}( int & c, zero_t ) { c = 0; }                                  // not in prelude
    2021
    2122trait sumable( otype T ) {
    22         void ?{}( T &, zero_t );                                                        // constructor from 0 literal
     23        void ?{}( T &, zero_t );                                                        // 0 literal constructor
    2324        T ?+?( T, T );                                                                          // assortment of additions
    2425        T ?+=?( T &, T );
     
    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( size_t size, T a[] ) {
     32        T total = 0;                                                                            // initialize by 0 constructor
     33        for ( size_t 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; };
    8485        void ?{}( S & s ) { s.[i, j] = 0; }
    85         void ?{}( S & s, int i, int j ) { s.[i,j] = [i, j]; }
    86         void ?{}( S & s, zero_t ) { s.[i,j] = 0; }
    87         void ?{}( S & s, one_t ) { s.[i,j] = 1; }
     86        void ?{}( S & s, int i ) { s.[i, j] = [i, 0]; }
     87        void ?{}( S & s, int i, int j ) { s.[i, j] = [i, j]; }
     88        void ?{}( S & s, zero_t ) { s.[i, j] = 0; }
     89        void ?{}( S & s, one_t ) { s.[i, j] = 1; }
    8890        S ?+?( S t1, S t2 ) { return (S){ t1.i + t2.i, t1.j + t2.j }; }
    8991        S ?+=?( S & t1, S t2 ) { t1 = t1 + t2; return t1; }
    9092        S ++?( S & t ) { t += (S){1}; return t; }
    9193        S ?++( S & t ) { S temp = t; t += (S){1}; return temp; }
    92         ofstream * ?|?( ofstream * os, S v ) { return os | v.i | v.j; }
     94        ofstream & ?|?( ofstream & os, S v ) { return os | v.i | v.j; }
    9395
    9496        S s = (S){0}, a[size], v = { low, low };
     
    98100        } // for
    99101        sout | "sum from" | low | "to" | High | "is"
    100                 | sum( size, (S *)a ) | ", check" | (S)s | endl;
     102                 | sum( size, (S *)a ) | ", check" | (S)s | endl;
     103
     104        forall( otype Impl | sumable( Impl ) )
     105        struct GS {
     106                Impl * x, * y;
     107        };
     108        GS(int) gs;
     109        gs.x = anew( size );                                                            // create array storage for field
     110        s = 0; v = low;
     111        for ( int i = 0; i < size; i += 1, v += 1 ) {
     112                s += (int)v;
     113                gs.x[i] = (int)v;                                                               // set field array in generic type
     114        } // for
     115        sout | "sum from" | low | "to" | High | "is"
     116                 | sum( size, gs.x ) | ", check" | (int)s | endl; // add field array in generic type
     117        delete( gs.x );
    101118} // main
    102119
Note: See TracChangeset for help on using the changeset viewer.