1+ #include < cstdint>
2+
3+ // Global variables for testing
4+ std::uint8_t g1 = 5 ;
5+ std::uint8_t g2 = 10 ;
6+ std::uint16_t g3 = 100 ;
7+ std::uint32_t g4 = 1000 ;
8+ std::int8_t g5 = -5 ;
9+ std::int32_t g6 = -1000 ;
10+ float g7 = 3 .14f ;
11+
12+ constexpr std::int32_t f1 (std::int32_t i) {
13+ return i * i;
14+ }
15+
16+ void test_binary_arithmetic_operations () {
17+ std::uint8_t l1 = 5 ;
18+ std::uint8_t l2 = 10 ;
19+ std::uint16_t l3 = 100 ;
20+ std::uint32_t l4 = 1000 ;
21+ std::int8_t l5 = -5 ;
22+ std::int32_t l6 = -1000 ;
23+
24+ l1 + l2; // NON_COMPLIANT - u8 + u8 -> signed int
25+ l1 * l2; // NON_COMPLIANT - u8 * u8 -> signed int
26+ l1 - l2; // NON_COMPLIANT - u8 - u8 -> signed int
27+ l1 / l2; // NON_COMPLIANT - u8 / u8 -> signed int
28+ l1 % l2; // NON_COMPLIANT - u8 % u8 -> signed int
29+ l1 & l2; // NON_COMPLIANT - u8 & u8 -> signed int
30+ l1 | l2; // NON_COMPLIANT - u8 | u8 -> signed int
31+ l1 ^ l2; // NON_COMPLIANT - u8 ^ u8 -> signed int
32+
33+ static_cast <std::uint32_t >(l1) + l2; // COMPLIANT - l2 -> unsigned int
34+ l1 + static_cast <std::uint32_t >(l2); // COMPLIANT - l1 -> unsigned int
35+
36+ l6 * l5; // COMPLIANT - l5 -> signed int
37+ l4 / l1; // COMPLIANT - l1 -> unsigned int
38+ }
39+
40+ void test_assignment_operations () {
41+ std::uint8_t l1 = 5 ;
42+ std::uint8_t l2 = 10 ;
43+ std::uint32_t l3 = 1000 ;
44+
45+ l1 += l2; // NON_COMPLIANT - same as l1 + l2
46+ l1 -= l2; // NON_COMPLIANT - same as l1 - l2
47+ l1 *= l2; // NON_COMPLIANT - same as l1 * l2
48+ l1 /= l2; // NON_COMPLIANT - same as l1 / l2
49+ l1 %= l2; // NON_COMPLIANT - same as l1 % l2
50+ l1 &= l2; // NON_COMPLIANT - same as l1 & l2
51+ l1 |= l2; // NON_COMPLIANT - same as l1 | l2
52+ l1 ^= l2; // NON_COMPLIANT - same as l1 ^ l2
53+
54+ l1 += static_cast <std::uint32_t >(l2); // COMPLIANT - l1 -> unsigned int
55+ l1 += l3; // COMPLIANT - l1 -> unsigned int
56+ }
57+
58+ void test_comparison_operations () {
59+ std::int32_t l1 = -1000 ;
60+ std::uint32_t l2 = 1000 ;
61+ std::uint8_t l3 = 5 ;
62+ std::uint16_t l4 = 100 ;
63+
64+ l1 > l2; // NON_COMPLIANT - l1 -> unsigned int
65+ l1 < l2; // NON_COMPLIANT - l1 -> unsigned int
66+ l1 >= l2; // NON_COMPLIANT - l1 -> unsigned int
67+ l1 <= l2; // NON_COMPLIANT - l1 -> unsigned int
68+ l1 == l2; // NON_COMPLIANT - l1 -> unsigned int
69+ l1 != l2; // NON_COMPLIANT - l1 -> unsigned int
70+
71+ l3 > l4; // NON_COMPLIANT - l3 and l4 -> signed int
72+ l3 < l4; // NON_COMPLIANT - l3 and l4 -> signed int
73+ }
74+
75+ void test_conditional_operator () {
76+ bool l1 = true ;
77+ std::uint8_t l2 = 5 ;
78+ std::uint8_t l3 = 10 ;
79+ std::uint16_t l4 = 100 ;
80+
81+ l1 ? l2 : l3; // COMPLIANT - no conversion
82+ l1 ? l2 : l4; // NON_COMPLIANT - l2 and l4 -> signed int
83+ }
84+
85+ void test_shift_operations () {
86+ std::uint8_t l1 = 5 ;
87+ std::uint32_t l2 = 1000 ;
88+
89+ l1 << 2 ; // NON_COMPLIANT - l1 -> signed int
90+ l1 >> 1 ; // NON_COMPLIANT - l1 -> signed int
91+ l2 << 2 ; // COMPLIANT
92+ l2 >> 1 ; // COMPLIANT
93+ }
94+
95+ void test_unary_operations () {
96+ std::uint8_t l1 = 5 ;
97+ std::uint32_t l2 = 1000 ;
98+ std::int8_t l3 = -5 ;
99+
100+ ~l1; // NON_COMPLIANT - l1 -> signed int
101+ ~l2; // COMPLIANT
102+ -l1; // NON_COMPLIANT - l1 -> signed int
103+ -l3; // COMPLIANT - l3 is signed
104+ +l1; // NON_COMPLIANT - l1 -> signed int
105+ }
106+
107+ void test_increment_decrement () {
108+ std::uint8_t l1 = 5 ;
109+ std::uint16_t l2 = 100 ;
110+
111+ l1++; // COMPLIANT - rule does not apply
112+ ++l1; // COMPLIANT - rule does not apply
113+ l1--; // COMPLIANT - rule does not apply
114+ --l1; // COMPLIANT - rule does not apply
115+ l2++; // COMPLIANT - rule does not apply
116+ ++l2; // COMPLIANT - rule does not apply
117+ }
118+
119+ void test_array_subscript () {
120+ int l1[10 ];
121+ std::uint8_t l2 = 5 ;
122+
123+ l1[l2]; // COMPLIANT - rule does not apply
124+ }
125+
126+ void test_exception_compile_time_constants () {
127+ std::uint32_t l1 = 1000 ;
128+ float l2 = 3 .14f ;
129+ std::int32_t l3 = 5 ;
130+
131+ l1 - 1 ; // COMPLIANT - exception #1
132+ l1 + 42 ; // COMPLIANT - exception #1
133+ l2 += 1 ; // COMPLIANT - exception #2
134+ l2 += 0x10001 ; // COMPLIANT - exception #2
135+ l3 + f1 (10 ); // COMPLIANT - exception #1
136+ l2 + f1 (10 ); // COMPLIANT - exception #2
137+ }
138+
139+ void test_floating_point_conversions () {
140+ float l1;
141+ std::uint32_t l2;
142+
143+ l1 += l2; // NON_COMPLIANT - l2 -> floating
144+ l1 *= l2; // NON_COMPLIANT - l2 -> floating
145+ l1 /= l2; // NON_COMPLIANT - l2 -> floating
146+ }
0 commit comments