Changeset 7780bd2
- Timestamp:
- Aug 5, 2021, 9:28:57 PM (3 years ago)
- Branches:
- ADT, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast-unique-expr, pthread-emulation, qualifiedEnum
- Children:
- b0b89a8
- Parents:
- aa173d8
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
tests/sum.cfa
raa173d8 r7780bd2 11 11 // Created On : Wed May 27 17:56:53 2015 12 12 // Last Modified By : Peter A. Buhr 13 // Last Modified On : T ue Jul 16 09:51:37 201914 // Update Count : 3 3613 // Last Modified On : Thu Aug 5 21:27:25 2021 14 // Update Count : 346 15 15 // 16 16 … … 20 20 trait sumable( T ) { 21 21 void ?{}( T &, zero_t ); // 0 literal constructor 22 void ?{}( T &, one_t ); // 1 literal constructor 22 23 T ?+?( T, T ); // assortment of additions 23 T ?+=?( T &, T ); 24 T ++?( T & ); 25 T ?++( T & ); 24 T ?+=?( T &, T ); // get pre/post ++ with += and one_t 26 25 }; // sumable 27 26 28 forall( T | sumable( T ) ) // use trait27 forall( T | sumable( T ) ) // use trait 29 28 T sum( size_t size, T a[] ) { 30 29 T total = 0; // initialize by 0 constructor … … 35 34 36 35 int main( void ) { 37 #if 038 36 const int low = 5, High = 15, size = High - low; 39 37 … … 94 92 S ?+?( S t1, S t2 ) { return (S){ t1.i + t2.i, t1.j + t2.j }; } 95 93 S ?+=?( S & t1, S t2 ) { t1 = t1 + t2; return t1; } 96 S ++?( S & t ) { t += (S){1}; return t; }97 S ?++( S & t ) { S temp = t; t += (S){1}; return temp; }98 94 ofstream & ?|?( ofstream & os, S v ) { return os | v.i | v.j; } 99 95 void ?|?( ofstream & os, S v ) { (ofstream &)(os | v); ends( os ); } 100 96 101 S s = (S){0}, a[size], v = { low, low };97 S s = 0, a[size], v = { low, low }; 102 98 for ( int i = 0; i < size; i += 1, v += (S){1} ) { 103 99 s += (S)v; … … 122 118 | sum( size, gs.x ) | ", check" | (int)s; // add field array in generic type 123 119 delete( gs.x ); 124 #else125 const int low = 5, High = 15, size = High - low;126 127 signed char s = 0, a[size], v = (char)low;128 for ( int i = 0; i < size; i += 1, v += 1hh ) {129 s += v;130 a[i] = v;131 } // for132 printf( "sum from %d to %d is %hhd, check %hhd\n", low, High,133 sum( size, (signed char *)a ), (signed char)s );134 135 unsigned char s = 0, a[size], v = low;136 for ( int i = 0; i < size; i += 1, v += 1hhu ) {137 s += (unsigned char)v;138 a[i] = (unsigned char)v;139 } // for140 printf( "sum from %d to %d is %hhu, check %hhu\n", low, High,141 sum( size, (unsigned char *)a ), (unsigned char)s );142 143 short int s = 0, a[size], v = low;144 for ( int i = 0; i < size; i += 1, v += 1h ) {145 s += (short int)v;146 a[i] = (short int)v;147 } // for148 printf( "sum from %d to %d is %hd, check %hd\n", low, High,149 sum( size, (short int *)a ), (short int)s );150 151 int s = 0, a[size], v = low;152 for ( int i = 0; i < size; i += 1, v += 1 ) {153 s += (int)v;154 a[i] = (int)v;155 } // for156 printf( "sum from %d to %d is %d, check %d\n", low, High,157 sum( size, (int *)a ), (int)s );158 159 float s = 0.0f, a[size], v = low / 10.0f;160 for ( int i = 0; i < size; i += 1, v += 0.1f ) {161 s += (float)v;162 a[i] = (float)v;163 } // for164 printf( "sum from %g to %g is %g, check %g\n", low / 10.0f, High / 10.0f,165 sum( size, (float *)a ), (float)s );166 167 double s = 0.0, a[size], v = low / 10.0;168 for ( int i = 0; i < size; i += 1, v += 0.1 ) {169 s += (double)v;170 a[i] = (double)v;171 } // for172 printf( "sum from %g to %g is %g, check %g\n", low / 10.0f, High / 10.0f,173 sum( size, (double *)a ), (double)s );174 175 struct S { int i, j; };176 void ?{}( S & s ) { s.[i, j] = 0; }177 void ?{}( S & s, int i ) { s.[i, j] = [i, 0]; }178 void ?{}( S & s, int i, int j ) { s.[i, j] = [i, j]; }179 void ?{}( S & s, zero_t ) { s.[i, j] = 0; }180 void ?{}( S & s, one_t ) { s.[i, j] = 1; }181 S ?+?( S t1, S t2 ) { return (S){ t1.i + t2.i, t1.j + t2.j }; }182 S ?+=?( S & t1, S t2 ) { t1 = t1 + t2; return t1; }183 S ++?( S & t ) { t += (S){1}; return t; }184 S ?++( S & t ) { S temp = t; t += (S){1}; return temp; }185 ofstream & ?|?( ofstream & os, S v ) { return os | v.i | v.j; }186 void ?|?( ofstream & os, S v ) { (ofstream &)(os | v); ends( os ); }187 188 S s = 0, a[size], v = { low, low };189 for ( int i = 0; i < size; i += 1, v += (S){1} ) {190 s += (S)v;191 a[i] = (S)v;192 } // for193 printf( "sum from %d to %d is %d %d, check %d %d\n", low, High,194 sum( size, (S *)a ).[i, j], s.[i, j] );195 196 forall( Impl | sumable( Impl ) )197 struct GS {198 Impl * x, * y;199 };200 GS(int) gs;201 // FIX ME, resolution problem with anew not picking up the LH type202 gs.x = (typeof(gs.x))anew( size ); // create array storage for field203 s = 0; v = low;204 for ( int i = 0; i < size; i += 1, v += 1 ) {205 s += (int)v;206 gs.x[i] = (int)v; // set field array in generic type207 } // for208 printf( "sum from %d to %d is %d, check %d\n", low, High,209 sum( size, gs.x ), (int)s ); // add field array in generic type210 delete( gs.x );211 #endif212 120 } // main 213 121
Note: See TracChangeset
for help on using the changeset viewer.