Changeset 90152a4 for tests/sum.c
- Timestamp:
- Aug 27, 2018, 4:40:34 PM (7 years ago)
- 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. - File:
-
- 1 moved
-
tests/sum.c (moved) (moved from src/tests/sum.c ) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
tests/sum.c
rf9feab8 r90152a4 11 11 // Created On : Wed May 27 17:56:53 2015 12 12 // Last Modified By : Peter A. Buhr 13 // Last Modified On : Sun Oct 29 10:29:12 201714 // Update Count : 2 6013 // Last Modified On : Thu Aug 2 08:03:09 2018 14 // Update Count : 279 15 15 // 16 16 17 #include <fstream> 17 #include <fstream.hfa> 18 #include <stdlib.hfa> 18 19 19 void ?{}( int & c, zero_t ) { c = 0; } 20 void ?{}( int & c, zero_t ) { c = 0; } // not in prelude 20 21 21 22 trait sumable( otype T ) { 22 void ?{}( T &, zero_t ); // constructor from 0 literal23 void ?{}( T &, zero_t ); // 0 literal constructor 23 24 T ?+?( T, T ); // assortment of additions 24 25 T ?+=?( T &, T ); … … 28 29 29 30 forall( otype T | sumable( T ) ) // use trait 30 T sum( unsigned int n, T a[] ) {31 T total = 0; // in stantiate T, select 032 for ( unsigned int i = 0; i < n; i += 1 )33 total += a[i]; // select +31 T 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 + 34 35 return total; 35 36 } // sum … … 55 56 } // for 56 57 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; 58 59 59 60 int s = 0, a[size], v = low; … … 63 64 } // for 64 65 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; 66 67 67 68 float s = 0.0f, a[size], v = low / 10.0f; … … 71 72 } // for 72 73 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; 74 75 75 76 double s = 0.0, a[size], v = low / 10.0; … … 79 80 } // for 80 81 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; 82 83 83 84 struct S { int i, j; }; 84 85 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; } 88 90 S ?+?( S t1, S t2 ) { return (S){ t1.i + t2.i, t1.j + t2.j }; } 89 91 S ?+=?( S & t1, S t2 ) { t1 = t1 + t2; return t1; } 90 92 S ++?( S & t ) { t += (S){1}; return t; } 91 93 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; } 93 95 94 96 S s = (S){0}, a[size], v = { low, low }; … … 98 100 } // for 99 101 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 ); 101 118 } // main 102 119
Note:
See TracChangeset
for help on using the changeset viewer.