474afd35394c502986859340acf2cb666b0af0c4
[fw/sdcc] / support / regression / tests / muldiv.c
1 /** Simple test for the mul/div/mod operations.
2
3     type: int, char, short, long
4     storage: static,
5     attr: volatile,
6 */
7 #include <testfwk.h>
8
9 void
10 testUnsignedModDiv(void)
11 {
12     {attr} {storage} unsigned {type} i;
13     unsigned {type} result;
14
15     i = 100;
16
17     result = i/3;
18     ASSERT(result == 33);
19
20     result = i/12;
21     ASSERT(result == 8);
22
23     result = i%7;
24     ASSERT(result == 2);
25
26     result = i%34;
27     ASSERT(result == 32);
28 }
29
30 void
31 testUnsignedMul(void)
32 {
33     {attr} {storage} unsigned {type} i;
34     unsigned {type} result;
35
36     i = 37;
37
38     LOG(("i*3 == 111 = %u\n", i*3));
39     result = i*3;
40     ASSERT(result == 111);
41
42     result = i*12;
43     ASSERT(result == ((unsigned {type})444));
44 }
45
46 void
47 testMul(void)
48 {
49     {attr} {storage} signed {type} i;
50     signed {type} result;
51
52     i = 5;
53
54     LOG(("i*5 == 25 = %u\n", (int)(i*5)));
55     result = i*5;
56     ASSERT(result == 25);
57     LOG(("i*-4 == -20 = %u\n", (int)(i*-4)));
58     ASSERT(i*-4 == -20);
59
60     i = -10;
61     LOG(("i*12 == -120 = %u\n", (int)(i*12)));
62     ASSERT(i*12 == -120);
63     LOG(("i*-3 == 30 = %u\n", (int)(i*-3)));
64     ASSERT(i*-3 == 30);
65 }
66
67 void
68 testDiv(void)
69 {
70     {attr} {storage} signed {type} i;
71
72     i = 100;
73     LOG(("i/5 == 20 = %u\n", (int)i/5));
74     ASSERT(i/5 == 20);
75     LOG(("i/-4 == -25 = %u\n", (int)i/-4));
76     ASSERT(i/-4 == -25);
77
78     i = -50;
79     LOG(("i/25 == -2 = %u\n", (int)i/25));
80     ASSERT(i/25 == -2);
81     LOG(("i/-12 == 4 = %u\n", (int)i/-12));
82     ASSERT(i/-12 == 4);
83 }
84
85 void
86 testMod(void)
87 {
88     {attr} {storage} signed {type} i;
89
90     // Disabled the LOG functions due to a bug in sdcc involving
91     // vaargs.
92     i = 100;
93     //    LOG(("i%%17 == 15 = %u\n", (int)(i%9)));
94     ASSERT(i%17 == 15);
95
96 #if MOD_SIGN_FOLLOWS_DIVIDEND
97     //    LOG(("i%%-7 == 2 = %u\n", (int)i%-7));
98     ASSERT(i%-7 == 2);
99
100     i = -49;
101     //    LOG(("i%%3 == -1 = %u\n", (int)i%3));
102     ASSERT(i%3 == -1);
103     //    LOG(("i%%-5 == -4 = %u\n", (int)i%-5));
104     ASSERT(i%-5 == -4);
105 #endif
106 }