]> git.lizzy.rs Git - rust.git/blob - src/rt/rust_test_helpers.c
Auto merge of #29045 - mseri:patch-4, r=nrc
[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 // MSVC doesn't allow empty structs or unions
139 #ifndef _MSC_VER
140 struct Empty {
141 };
142
143 void
144 rust_dbg_extern_empty_struct(struct ManyInts v1, struct Empty e, struct ManyInts v2) {
145     assert(v1.arg1 == v2.arg1 + 1);
146     assert(v1.arg2 == v2.arg2 + 1);
147     assert(v1.arg3 == v2.arg3 + 1);
148     assert(v1.arg4 == v2.arg4 + 1);
149     assert(v1.arg5 == v2.arg5 + 1);
150     assert(v1.arg6.one == v2.arg6.one + 1);
151     assert(v1.arg6.two == v2.arg6.two + 1);
152 }
153 #endif
154
155 intptr_t
156 rust_get_test_int() {
157   return 1;
158 }
159
160 /* Debug helpers strictly to verify ABI conformance.
161  *
162  * FIXME (#2665): move these into a testcase when the testsuite
163  * understands how to have explicit C files included.
164  */
165
166 struct quad {
167     uint64_t a;
168     uint64_t b;
169     uint64_t c;
170     uint64_t d;
171 };
172
173 struct floats {
174     double a;
175     uint8_t b;
176     double c;
177 };
178
179 struct quad
180 rust_dbg_abi_1(struct quad q) {
181     struct quad qq = { q.c + 1,
182                        q.d - 1,
183                        q.a + 1,
184                        q.b - 1 };
185     return qq;
186 }
187
188 struct floats
189 rust_dbg_abi_2(struct floats f) {
190     struct floats ff = { f.c + 1.0,
191                          0xff,
192                          f.a - 1.0 };
193     return ff;
194 }
195
196 int
197 rust_dbg_static_mut = 3;
198
199 void
200 rust_dbg_static_mut_check_four() {
201     assert(rust_dbg_static_mut == 4);
202 }
203
204 struct S {
205     uint64_t x;
206     uint64_t y;
207     uint64_t z;
208 };
209
210 uint64_t get_x(struct S s) {
211     return s.x;
212 }
213
214 uint64_t get_y(struct S s) {
215     return s.y;
216 }
217
218 uint64_t get_z(struct S s) {
219     return s.z;
220 }
221
222 uint64_t get_c_many_params(void *a, void *b, void *c, void *d, struct quad f) {
223     return f.c;
224 }