]> git.lizzy.rs Git - rust.git/blob - src/rt/rust_test_helpers.c
auto merge of #15061 : pnkfelix/rust/fsk-fix-issue-10846, r=nikomatsakis
[rust.git] / src / rt / rust_test_helpers.c
1 // Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // Helper functions used only in tests
12
13 #include <stdint.h>
14 #include <assert.h>
15
16 // These functions are used in the unit tests for C ABI calls.
17
18 uint32_t
19 rust_dbg_extern_identity_u32(uint32_t u) {
20     return u;
21 }
22
23 uint64_t
24 rust_dbg_extern_identity_u64(uint64_t u) {
25     return u;
26 }
27
28 double
29 rust_dbg_extern_identity_double(double u) {
30     return u;
31 }
32
33 char
34 rust_dbg_extern_identity_u8(char u) {
35     return u;
36 }
37
38 typedef void *(*dbg_callback)(void*);
39
40 void *
41 rust_dbg_call(dbg_callback cb, void *data) {
42     return cb(data);
43 }
44
45 void rust_dbg_do_nothing() { }
46
47 struct TwoU8s {
48     uint8_t one;
49     uint8_t two;
50 };
51
52 struct TwoU8s
53 rust_dbg_extern_return_TwoU8s() {
54     struct TwoU8s s;
55     s.one = 10;
56     s.two = 20;
57     return s;
58 }
59
60 struct TwoU8s
61 rust_dbg_extern_identity_TwoU8s(struct TwoU8s u) {
62     return u;
63 }
64
65 struct TwoU16s {
66     uint16_t one;
67     uint16_t two;
68 };
69
70 struct TwoU16s
71 rust_dbg_extern_return_TwoU16s() {
72     struct TwoU16s s;
73     s.one = 10;
74     s.two = 20;
75     return s;
76 }
77
78 struct TwoU16s
79 rust_dbg_extern_identity_TwoU16s(struct TwoU16s u) {
80     return u;
81 }
82
83 struct TwoU32s {
84     uint32_t one;
85     uint32_t two;
86 };
87
88 struct TwoU32s
89 rust_dbg_extern_return_TwoU32s() {
90     struct TwoU32s s;
91     s.one = 10;
92     s.two = 20;
93     return s;
94 }
95
96 struct TwoU32s
97 rust_dbg_extern_identity_TwoU32s(struct TwoU32s u) {
98     return u;
99 }
100
101 struct TwoU64s {
102     uint64_t one;
103     uint64_t two;
104 };
105
106 struct TwoU64s
107 rust_dbg_extern_return_TwoU64s() {
108     struct TwoU64s s;
109     s.one = 10;
110     s.two = 20;
111     return s;
112 }
113
114 struct TwoU64s
115 rust_dbg_extern_identity_TwoU64s(struct TwoU64s u) {
116     return u;
117 }
118
119 struct TwoDoubles {
120     double one;
121     double two;
122 };
123
124 struct TwoDoubles
125 rust_dbg_extern_identity_TwoDoubles(struct TwoDoubles u) {
126     return u;
127 }
128
129 struct ManyInts {
130     int8_t arg1;
131     int16_t arg2;
132     int32_t arg3;
133     int16_t arg4;
134     int8_t arg5;
135     struct TwoU8s arg6;
136 };
137
138 struct Empty {
139 };
140
141 void
142 rust_dbg_extern_empty_struct(struct ManyInts v1, struct Empty e, struct ManyInts v2) {
143     assert(v1.arg1 == v2.arg1 + 1);
144     assert(v1.arg2 == v2.arg2 + 1);
145     assert(v1.arg3 == v2.arg3 + 1);
146     assert(v1.arg4 == v2.arg4 + 1);
147     assert(v1.arg5 == v2.arg5 + 1);
148     assert(v1.arg6.one == v2.arg6.one + 1);
149     assert(v1.arg6.two == v2.arg6.two + 1);
150 }
151
152 intptr_t
153 rust_get_test_int() {
154   return 1;
155 }
156
157 /* Debug helpers strictly to verify ABI conformance.
158  *
159  * FIXME (#2665): move these into a testcase when the testsuite
160  * understands how to have explicit C files included.
161  */
162
163 struct quad {
164     uint64_t a;
165     uint64_t b;
166     uint64_t c;
167     uint64_t d;
168 };
169
170 struct floats {
171     double a;
172     uint8_t b;
173     double c;
174 };
175
176 struct quad
177 rust_dbg_abi_1(struct quad q) {
178     struct quad qq = { q.c + 1,
179                        q.d - 1,
180                        q.a + 1,
181                        q.b - 1 };
182     return qq;
183 }
184
185 struct floats
186 rust_dbg_abi_2(struct floats f) {
187     struct floats ff = { f.c + 1.0,
188                          0xff,
189                          f.a - 1.0 };
190     return ff;
191 }
192
193 int
194 rust_dbg_static_mut;
195
196 int rust_dbg_static_mut = 3;
197
198 void
199 rust_dbg_static_mut_check_four() {
200     assert(rust_dbg_static_mut == 4);
201 }
202
203 struct S {
204     uint64_t x;
205     uint64_t y;
206     uint64_t z;
207 };
208
209 uint64_t get_x(struct S s) {
210     return s.x;
211 }
212
213 uint64_t get_y(struct S s) {
214     return s.y;
215 }
216
217 uint64_t get_z(struct S s) {
218     return s.z;
219 }