Pick three bugfixes from next branch to trunk for inclusion in 4.5.0 RC2, as discusse...
[sdcc.git] / sdcc / support / regression / tests / muldiv.c.in
blob1df52656fddcd4918802199e07431ebb21c86229
1 /** Simple test for the mul/div/mod operations.
3 type: int, char, short, long
4 storage: static,
5 attr: volatile,
6 */
7 #include <testfwk.h>
9 void
10 testUnsignedModDiv(void)
12 #ifndef __SDCC_pdk14 // Lack of memory
13 #if !(defined (__SDCC_pdk15) && defined(__SDCC_STACK_AUTO)) // Lack of code memory
14 {attr} {storage} unsigned {type} i;
15 unsigned {type} result;
17 i = 100;
19 result = i/3;
20 ASSERT(result == 33);
22 result = i/12;
23 ASSERT(result == 8);
25 result = i%7;
26 ASSERT(result == 2);
28 result = i%34;
29 ASSERT(result == 32);
30 #endif
31 #endif
34 void
35 testUnsignedMul(void)
37 #ifndef __SDCC_pdk14 // Lack of memory
38 {attr} {storage} unsigned {type} i;
39 unsigned {type} result;
41 i = 37;
43 LOG(("i*3 == 111 = %u\n", (int)(i*3)));
44 result = i*3;
45 ASSERT(result == 111);
47 result = i*12;
48 ASSERT(result == ((unsigned {type})444));
49 #endif
52 void
53 testMul(void)
55 #ifndef __SDCC_pdk14 // Lack of memory
56 #if !(defined (__SDCC_pdk15) && defined(__SDCC_STACK_AUTO)) // Lack of code memory
57 {attr} {storage} signed {type} i;
58 signed {type} result;
60 i = 5;
62 LOG(("i*5 == 25 = %d\n", (int)(i*5)));
63 result = i*5;
64 ASSERT(result == 25);
65 LOG(("i*-4 == -20 = %d\n", (int)(i*-4)));
66 ASSERT(i*-4 == -20);
67 i = -10;
68 #ifndef __SDCC_pic16
69 LOG(("i*12 == -120 = %d\n", (int)(i*12)));
70 ASSERT(i*12 == -120);
71 LOG(("i*-3 == 30 = %d\n", (int)(i*-3)));
72 ASSERT(i*-3 == 30);
73 #endif
74 #endif
75 #endif
78 void mark(void)
82 void
83 testDiv(void)
85 #if !defined(__SDCC_pdk14) && !defined(__SDCC_pdk15) // Lack of memory
86 {attr} {storage} signed {type} i;
88 i = 100;
89 LOG(("i/5 == 20 = %d\n", (int)i/5));
90 ASSERT(i/5 == 20);
91 LOG(("i/-4 == -25 = %d\n", (int)i/-4));
92 mark();
93 ASSERT(i/-4 == -25);
95 i = -50;
96 LOG(("i/25 == -2 = %d\n", (int)i/25));
97 ASSERT(i/25 == -2);
98 LOG(("i/-12 == 4 = %d\n", (int)i/-12));
99 ASSERT(i/-12 == 4);
100 //power of 2
101 ASSERT(i/4 == -12);
102 #endif
105 void
106 test16to32(void)
108 #if !defined(__SDCC_pdk14) && !defined(__SDCC_pdk15) // Lack of memory
109 {attr} {storage} int i, j;
110 {attr} {storage} unsigned int ui, uj;
112 i = 42;
113 j = 42;
114 ASSERT((long)i * (long)j == 42l * 42l);
115 i = -i;
116 ASSERT((long)i * (long)j == -42l * 42l);
117 j = -j;
118 ASSERT((long)i * (long)j == -42l * -42l);
119 i = 2342;
120 j = 4223;
121 ASSERT((unsigned long)i * (unsigned long)j == 2342ul * 4223ul);
122 ASSERT((long)i * (long)j == 2342l * 4223l);
123 j = -j;
124 ASSERT((long)i * (long)j == 2342l * -4223l);
125 i = -i;
126 ASSERT((long)i * (long)j == -2342l * -4223l);
128 ui = 42;
129 uj = 42;
130 ASSERT((unsigned long)ui * (unsigned long)uj == 42ul * 42ul);
131 ui = 2342;
132 uj = 4223;
133 ASSERT((unsigned long)ui * (unsigned long)uj == 2342ul * 4223ul);
134 ui = 0xffff;
135 uj = 0x8000;
136 ASSERT((unsigned long)ui * (unsigned long)uj == 0xfffful * 0x8000ul);
137 #endif
140 void
141 testMod(void)
143 #if !defined(__SDCC_pdk14) && !defined(__SDCC_pdk15) // Lack of memory
144 {attr} {storage} signed {type} i;
146 // Disabled the LOG functions due to a bug in sdcc involving
147 // vaargs.
148 i = 100;
149 // LOG(("i%%17 == 15 = %u\n", (int)(i%9)));
150 ASSERT(i%17 == 15);
151 ASSERT(i%(unsigned char)19 == 5);
153 // LOG(("i%%-7 == 2 = %u\n", (int)i%-7));
154 ASSERT(i%-7 == 2);
155 //power of 2
156 ASSERT(i%-8 == 4);
158 i = -49;
159 // LOG(("i%%3 == -1 = %u\n", (int)i%3));
160 ASSERT(i%3 == -1);
161 // LOG(("i%%-5 == -4 = %u\n", (int)i%-5));
162 ASSERT(i%-5 == -4);
163 //power of 2
164 ASSERT(i%4 == -1);
165 #endif