fixed some more testapps
[libfirm] / ir / be / test / harness.c
1 #include <stdio.h>
2 #define MAXPRINTABLE 128
3 #include "rantext.h"
4 #if ULTRIX || _AIX || _WIN32
5 #include <fcntl.h>
6 #else
7 #include <sys/fcntl.h>
8 #endif
9
10 double ran();
11 double ran2();
12 char   getranchar();
13 #define BUFFERSIZE      14500000
14 long int seedi;
15 #define COMPRESS 0
16 #define UNCOMPRESS 1
17
18 double prob_tab[MAXPRINTABLE][MAXPRINTABLE];
19
20 int
21 add_line(char* buf, int count, int num_letters, char letter)
22 {
23 int i;
24   for (i = count; i < (count + num_letters); i ++)
25   {
26         buf[i]=letter;
27   }
28         count=i;
29         buf[count]='\n';
30         count=count+1;
31         return(count);
32 }
33
34 fill_text_buffer(int count, char start_char, char* text_buffer)
35 {
36         long int total;         /* normalization */
37         int i,j;                /* array indexers */
38         char c1,c2;             /* character holders for level-three search */
39         int     writecount ;    /* Number of characters written */
40         int     bufindex;
41
42         /*
43          * For each ith, jth element in the frequency table, set the
44          * ith, jth, element of the probability table to the frequency
45          * table entry divided by the total.
46          */
47
48         for (i=0;i<128;i++)
49         {
50                 total = 0.0;
51                 for (j=0;j<128;j++)
52                 {
53                         total += freq_tab[i][j];
54                 }
55                 if (total<1)
56                         total=1;
57                 for (j=0;j<128;j++)
58                 {
59                         prob_tab[i][j] = (double)freq_tab[i][j]/(double)total;
60                 }
61         }
62         /*
63          * For each ith element in the probability table, make the
64          * jth elements cumulative in order to simplify 'getranchar'.
65         */
66
67         for (i=0;i<128;i++)
68         {
69                 for (j=1;j<128;j++)
70                 {
71                         prob_tab[i][j]+=prob_tab[i][j-1];
72                 }
73         }
74
75 #if SDEBUG
76         fprintf(stderr,
77                 "Probability table built, about to open file \n");
78         fflush(stderr) ;
79 #endif /* SDEBUG */
80
81         /*
82          * Start off the simulation with seed letter.
83          */
84
85         c1=start_char;
86
87         /*
88          * Get "count" characters and spit 'em out.
89          */
90
91         count-- ; /* pre-decrement for the final new line */
92
93         bufindex = 0 ;
94         while (count>0)
95         {
96                 c2=getranchar(c1,ran2());
97                 text_buffer[bufindex++]=c2 ;
98 #if SDEBUG>2
99                 fprintf(stderr,"Character number %d is %c\n",
100                         bufindex,c2) ;
101                 fflush(stderr) ;
102 #endif /* SDEBUG */
103                 c1=c2;
104                 count--;
105         }
106         /*
107          * Complete the last line
108          */
109
110         c2 = '\n' ;
111         text_buffer[bufindex++]=c2 ;
112 }
113
114 /* Routine For dumping contents of a buffer to the output
115         Jeff Reilly, 1/15/95                            */
116 print_buffer(int count, char* text_buffer)
117
118 {
119 int i;
120
121 for (i=0;i<count;i++)
122         printf("%c", text_buffer[i]);
123
124
125 }
126
127 char getranchar(c,rnno)
128 char c;
129 double rnno;
130
131 {
132         int mid, k;
133         int low=0;
134         int high=127;
135
136         /*
137  * Ascend the jth column (given by c1).
138  * if the cumulative probability exceeds the random number, then
139  * the current (char) i is the character to return.
140  */
141         if (rnno > prob_tab[c][127])
142                 return ('e');
143
144         for (k=0;k<7;k++)
145         {
146                 mid = (low+high)>>1;
147                 if (rnno < prob_tab[c][mid])
148                         high = mid;
149                 else if (rnno > prob_tab[c][mid])
150                         low = mid + 1;
151                 else    /* exact match found -unlikely */
152                         return ((char)mid);
153         }
154         return ((char)low);
155 }
156
157 double ran2()
158 {
159   seedi=((314157*seedi)+19)&0xffffff;
160   return ( (double) seedi/(double)0xffffff);
161 }
162
163 double ran()
164 /* See "Random Number Generators: Good Ones Are Hard To Find", */
165 /*     Park & Miller, CACM 31#10 October 1988 pages 1192-1201. */
166 /***********************************************************/
167 /* THIS IMPLEMENTATION REQUIRES AT LEAST 32 BIT INTEGERS ! */
168 /***********************************************************/
169 #define _A_MULTIPLIER  16807L
170 #define _M_MODULUS     2147483647L /* (2**31)-1 */
171 #define _Q_QUOTIENT    127773L     /* 2147483647 / 16807 */
172 #define _R_REMAINDER   2836L       /* 2147483647 % 16807 */
173 {
174         long lo;
175         long hi;
176         long test;
177
178         hi = seedi / _Q_QUOTIENT;
179         lo = seedi % _Q_QUOTIENT;
180         test = _A_MULTIPLIER * lo - _R_REMAINDER * hi;
181         if (test > 0) {
182                 seedi = test;
183         } else {
184                 seedi = test + _M_MODULUS;
185         }
186         return ( (float) seedi / _M_MODULUS);
187 }
188
189 compare_buffer(char* buf1, int count1, char* buf2, int count2)
190 {
191 if (count1 == count2)
192 {
193   printf("Files both have length %d\n", count1);
194   if (count1 > 0)
195   {
196     if ( (buf1[0] == buf2[0]) && (buf1[count1-1] == buf2[count2-1]) )
197     {
198       printf("First character (%c) and Last Character (%c) match. \n", buf1[0], buf1[count1-1]);
199     }
200     else
201     {
202       printf("First and last characters do not match.\n");
203       printf("%c does not match %c\n", buf1[0], buf2[0]);
204       printf("or %c does not match %c\n", buf1[count1-1], buf2[count2-1]);
205     }
206   }
207 }
208 else
209 {
210   printf("Warning: Files of differing lengths: %d and %d\n", count1, count2);
211 }
212
213
214 }
215
216 char    orig_text_buffer[BUFFERSIZE], comp_text_buffer[BUFFERSIZE], new_text_buffer[BUFFERSIZE];
217
218
219 int main(int argc, char *argv[])
220
221 {
222 int count, i, oper;
223 int comp_count, new_count;
224 char    start_char;
225 int N;
226 char C;
227
228         printf("SPEC 129.compress harness\n");
229         // for now...
230         return 0;
231
232         scanf("%i       %c      %li", &count, &start_char, &seedi);
233         printf("Initial File Size:%i    Start character:%c\n", count, start_char, seedi);
234         fill_text_buffer(count, start_char, orig_text_buffer);
235         for (i = 1; i <= 25; i++)
236         {
237           new_count=add_line(orig_text_buffer, count, i, start_char);
238           count=new_count;
239           oper=COMPRESS;
240           printf("The starting size is: %d\n", count);
241           //comp_count=spec_select_action(orig_text_buffer, count, oper, comp_text_buffer);
242           printf("The compressed size is: %d\n", comp_count);
243           oper=UNCOMPRESS;
244           //new_count=spec_select_action(comp_text_buffer, comp_count, oper, new_text_buffer);
245           printf("The compressed/uncompressed size is: %d\n", new_count);
246           compare_buffer(orig_text_buffer, count, new_text_buffer, new_count);
247         }
248 /* Remove comments for Debugging */
249 /*
250         printf("Original Text File:\n");
251         print_buffer(count, orig_text_buffer);
252         printf("New Text File:\n");
253         print_buffer(count, new_text_buffer); */
254
255         return 0;
256 }