Skip to content

Commit 94347c4

Browse files
committed
EXP43-C: Add test case
1 parent ee22665 commit 94347c4

File tree

1 file changed

+81
-0
lines changed
  • c/cert/test/rules/EXP39-C

1 file changed

+81
-0
lines changed

c/cert/test/rules/EXP39-C/test.c

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
void test_incompatible_arithmetic() {
2+
float f = 0.0f;
3+
int *p = (int *)&f; // NON_COMPLIANT - arithmetic types are not compatible
4+
// with each other
5+
(*p)++;
6+
7+
short s[2];
8+
(int *)&s; // NON_COMPLIANT
9+
10+
(short(*)[4]) & s; // NON_COMPLIANT - array of size 2 is not compatible with
11+
// array of size 4 (n1570 6.7.6.2 paragraph 7)
12+
(short(*)[2]) & s; // COMPLIANT
13+
14+
// char may be signed or unsigned, and so is not compatible with either
15+
char c1;
16+
(signed char *)&c1; // NON_COMPLIANT
17+
(unsigned char *)&c1; // NON_COMPLIANT
18+
(char *)&c1; // NON_COMPLIANT
19+
20+
// int is defined as signed, so is compatible with all the signed versions
21+
// (long, short etc. are similar)
22+
int i1;
23+
(signed int *)&i1; // COMPLIANT
24+
(int *)&i1; // COMPLIANT
25+
(signed *)&i1; // COMPLIANT
26+
(unsigned int *)&i1; // NON_COMPLIANT
27+
(const int *)&i1; // NON_COMPLIANT
28+
}
29+
30+
struct {
31+
int a;
32+
} * s1;
33+
struct {
34+
int a;
35+
} * s2;
36+
struct S1 {
37+
int a;
38+
} * s3;
39+
struct S1 *s4;
40+
41+
// TODO test across files
42+
void test_incompatible_structs() {
43+
// s1 and s2 do not have tags, and are therefore not compatible
44+
s1 = s2; // NON_COMPLIANT
45+
// s3 tag is inconsistent with s1 tag
46+
s1 = s3; // NON_COMPLIANT
47+
s3 = s1; // NON_COMPLIANT
48+
// s4 tag is consistent with s3 tag
49+
s3 = s4; // COMPLIANT
50+
s4 = s3; // COMPLIANT
51+
}
52+
53+
enum E1 { E1A, E1B };
54+
enum E2 { E2A, E2B };
55+
56+
void test_enums() {
57+
enum E1 e1 = E1A;
58+
enum E2 e2 = e1; // COMPLIANT
59+
// Enums are also compatible with one of `char`, a signed integer type or an
60+
// unsigned integer type. It is implementation defined which is used, so
61+
// choose an appropriate type below for this test
62+
(int *)&e1; // COMPLIANT
63+
}
64+
65+
int *void_cast(void *v) { return (int *)v; }
66+
67+
void test_indirect_cast() {
68+
float f1 = 0.0f;
69+
void_cast(&f1); // NON_COMPLIANT
70+
int i1 = 0;
71+
void_cast(&i1); // COMPLIANT
72+
}
73+
74+
signed f(int y) { return y; }
75+
int g(signed int x) { return x; }
76+
77+
// 6.7.6.3 p15
78+
void test_compatible_functions() {
79+
signed (*f1)(int) = &g; // COMPLIANT
80+
int (*g1)(signed int) = &f; // COMPLIANT
81+
}

0 commit comments

Comments
 (0)