asm{a,b}: section hax, finally
[uebersetzerbau-ss10.git] / asmb / asmbtest.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 extern unsigned char *asmb(unsigned char *a);
6
7 unsigned char *asmb_ref(unsigned char *s)
8 {
9         unsigned long i;
10         for (i=0; s[i]; i++) {
11                 unsigned char c=s[i];
12                 c += (c>='A' && c<='Z') ? 'a'-'A' : 0;
13                 s[i] = c;
14         }
15         return s;
16 }
17
18 static char ascii(char s) {
19         if(s < 0x20) return '.';
20         if(s > 0x7E) return '.';
21         return s;
22 }
23
24 static void hexdump(void *d, int len) {
25         unsigned char *data;
26         int i, off;
27         data = (unsigned char*)d;
28         for (off=0; off<len; off += 16) {
29                 printf("\t%08x  ",off);
30                 for(i=0; i<16; i++)
31                         if((i+off)>=len) printf("   ");
32                         else printf("%02x ",data[off+i]);
33
34                 printf(" ");
35                 for(i=0; i<16; i++)
36                         if((i+off)>=len) printf(" ");
37                         else printf("%c",ascii(data[off+i]));
38                 printf("\n");
39         }
40 }
41
42 #define NUM_TESTCASES 18
43 int main()
44 {
45         char *input[NUM_TESTCASES]={
46                 "AAaaB\0BBUUUUZZZZ",
47                 "AAaaBBB\0",
48                 "AaA\0ABBB",
49                 "A\0ABCDEF",
50                 "foofuuMUHkk", 
51                 "AbC", 
52                 "BLA|MUHMKUH|KA", 
53                 "ASDFNERABHDFKHDFKLGJAHGLKAHGLKASHGEARNAKLVNLVAANLSADJVHASDLGH", 
54                 "asdfABCDEFGHKL544", 
55                 "asdfA\0BCDEFGHKL5", 
56                 "foofuuMUHkk\0AAAA", 
57                 "AbC\0AAAAAAAAAAAA", 
58                 "BLA|MUHMKUH|KAA\0", 
59                 "ASDFASDFasdfasdfaBC\0AAAABBBBCCCC", 
60                 "ASDFASDFasdfasdfaBC0AAAABBBBCCCCmuhKA\0asASDFasdf",
61                 "ASas\0ASas",
62                 "asdfABCDEFGHKL54",
63                 "asdffvdfgerrggre\0",
64         };
65         int len[NUM_TESTCASES] = {
66                 16,
67                 8,
68                 8,
69                 8,
70                 11,
71                 3,
72                 14,
73                 61,
74                 17,
75                 16,
76                 16,
77                 16,
78                 16,
79                 32,
80                 48,
81                 9,
82                 16,
83                 17,
84         };
85         char *output_our, *output_ref;
86         char *input_our,  *input_ref;
87         int right=0, wrong=0, neither=0, i;
88
89         for(i = 0; i < NUM_TESTCASES; i++) {
90                 input_our = (char *) malloc (len[i]);
91                 input_ref = (char *) malloc (len[i]);
92
93                 (void) memcpy(input_our, input[i], len[i]+1);
94                 (void) memcpy(input_ref, input[i], len[i]+1);
95
96                 output_our = (char *) asmb((unsigned char *) input_our);
97                 output_ref = (char *) asmb_ref((unsigned char *) input_ref);
98
99                 if(memcmp(output_our,output_ref, len[i]) != 0) {
100                         if(strncmp(output_our, output_ref, len[i]) == 0) {
101                                 neither++;
102                                 printf("Testfall%02i nach Nullbyte ungleich\n", i);
103                         }
104                         else {
105                                 wrong++;
106                                 printf("Testfall%02i falsch!\n", i);
107                         }
108
109                         printf("Input(\"%s\"):\n", input[i]);
110                         hexdump(input[i], len[i]);
111
112                         printf("\nerwartet:\n");
113                         hexdump(output_ref, len[i]);
114
115                         printf("\ntatsaechliches Ergebnis:\n");
116                         hexdump(output_our, len[i]);
117                         printf("\n");
118                 }
119                 else {
120                         right++;
121                         printf("Testfall%02i korrekt\n", i);
122                 }
123                 free(input_our);
124                 free(input_ref);
125         }
126         printf("========\n%2i Testfaelle sind korrekt\n%2i Testfaelle sind nach dem Nullbyte ungleich\n"
127                         "%2i Testfaelle sind falsch\n", right, neither, wrong);
128         return 0;
129 }
130