Changed default documentation dir to share/doc/sdcc
[fw/sdcc] / support / regression / tests / muldiv.c
1 /** Simple test for the mul/div/mod operations.
2
3     type: int, char, short
4     storage: static,
5     attr: volatile,
6 */
7 #include <testfwk.h>
8
9 static 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 static 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 == (({type})444));
44 }
45
46 static 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     LOG(("30 == %u\n", (int)i*-3));
67 }
68
69 static void
70 testDiv(void)
71 {
72     {attr} {storage} signed {type} i;
73
74     i = 100;
75     LOG(("i/5 == 20 = %u\n", (int)i/5));
76     ASSERT(i/5 == 20);
77     LOG(("i/-4 == -25 = %u\n", (int)i/-4));
78     ASSERT(i/-4 == -25);
79
80     i = -50;
81     LOG(("i/25 == -2 = %u\n", (int)i/25));
82     ASSERT(i/25 == -2);
83     LOG(("i/-12 == 4 = %u\n", (int)i/-12));
84     ASSERT(i/-12 == 4);
85 }
86
87 static void
88 testMod(void)
89 {
90     {attr} {storage} signed {type} i;
91
92     // Disabled the LOG functions due to a bug in sdcc involving
93     // vaargs.
94     i = 100;
95     //    LOG(("i%%17 == 15 = %u\n", (int)(i%9)));
96     ASSERT(i%17 == 15);
97
98 #if MOD_SIGN_FOLLOWS_DIVIDEND
99     //    LOG(("i%%-7 == 2 = %u\n", (int)i%-7));
100     ASSERT(i%-7 == 2);
101
102     i = -49;
103     //    LOG(("i%%3 == -1 = %u\n", (int)i%3));
104     ASSERT(i%3 == -1);
105     //    LOG(("i%%-5 == -4 = %u\n", (int)i%-5));
106     ASSERT(i%-5 == -4);
107 #endif
108 }