Imported Upstream version 2.9.0
[debian/cc1111] / 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", (int)(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 mark(void)
68 {
69 }
70
71 void
72 testDiv(void)
73 {
74     {attr} {storage} signed {type} i;
75
76     i = 100;
77     LOG(("i/5 == 20 = %u\n", (int)i/5));
78     ASSERT(i/5 == 20);
79     LOG(("i/-4 == -25 = %u\n", (int)i/-4));
80     mark();
81     ASSERT(i/-4 == -25);
82
83     i = -50;
84     LOG(("i/25 == -2 = %u\n", (int)i/25));
85     ASSERT(i/25 == -2);
86     LOG(("i/-12 == 4 = %u\n", (int)i/-12));
87     ASSERT(i/-12 == 4);
88     //power of 2
89     ASSERT(i/4 == -12);
90 }
91
92 void
93 testMod(void)
94 {
95     {attr} {storage} signed {type} i;
96
97     // Disabled the LOG functions due to a bug in sdcc involving
98     // vaargs.
99     i = 100;
100     //    LOG(("i%%17 == 15 = %u\n", (int)(i%9)));
101     ASSERT(i%17 == 15);
102
103     //    LOG(("i%%-7 == 2 = %u\n", (int)i%-7));
104     ASSERT(i%-7 == 2);
105     //power of 2
106     ASSERT(i%-8 == 4);
107
108     i = -49;
109     //    LOG(("i%%3 == -1 = %u\n", (int)i%3));
110     ASSERT(i%3 == -1);
111     //    LOG(("i%%-5 == -4 = %u\n", (int)i%-5));
112     ASSERT(i%-5 == -4);
113     //power of 2
114     ASSERT(i%4 == -1);
115 }