]> git.proxmox.com Git - rustc.git/blame - src/rt/rust_test_helpers.c
Imported Upstream version 1.9.0+dfsg1
[rustc.git] / src / rt / rust_test_helpers.c
CommitLineData
1a4d82fc
JJ
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>
9cc50fc6 15#include <stdarg.h>
1a4d82fc
JJ
16
17// These functions are used in the unit tests for C ABI calls.
18
19uint32_t
20rust_dbg_extern_identity_u32(uint32_t u) {
21 return u;
22}
23
24uint64_t
25rust_dbg_extern_identity_u64(uint64_t u) {
26 return u;
27}
28
29double
30rust_dbg_extern_identity_double(double u) {
31 return u;
32}
33
34char
35rust_dbg_extern_identity_u8(char u) {
36 return u;
37}
38
39typedef void *(*dbg_callback)(void*);
40
41void *
42rust_dbg_call(dbg_callback cb, void *data) {
43 return cb(data);
44}
45
46void rust_dbg_do_nothing() { }
47
48struct TwoU8s {
49 uint8_t one;
50 uint8_t two;
51};
52
53struct TwoU8s
54rust_dbg_extern_return_TwoU8s() {
55 struct TwoU8s s;
56 s.one = 10;
57 s.two = 20;
58 return s;
59}
60
61struct TwoU8s
62rust_dbg_extern_identity_TwoU8s(struct TwoU8s u) {
63 return u;
64}
65
66struct TwoU16s {
67 uint16_t one;
68 uint16_t two;
69};
70
71struct TwoU16s
72rust_dbg_extern_return_TwoU16s() {
73 struct TwoU16s s;
74 s.one = 10;
75 s.two = 20;
76 return s;
77}
78
79struct TwoU16s
80rust_dbg_extern_identity_TwoU16s(struct TwoU16s u) {
81 return u;
82}
83
84struct TwoU32s {
85 uint32_t one;
86 uint32_t two;
87};
88
89struct TwoU32s
90rust_dbg_extern_return_TwoU32s() {
91 struct TwoU32s s;
92 s.one = 10;
93 s.two = 20;
94 return s;
95}
96
97struct TwoU32s
98rust_dbg_extern_identity_TwoU32s(struct TwoU32s u) {
99 return u;
100}
101
102struct TwoU64s {
103 uint64_t one;
104 uint64_t two;
105};
106
107struct TwoU64s
108rust_dbg_extern_return_TwoU64s() {
109 struct TwoU64s s;
110 s.one = 10;
111 s.two = 20;
112 return s;
113}
114
115struct TwoU64s
116rust_dbg_extern_identity_TwoU64s(struct TwoU64s u) {
117 return u;
118}
119
120struct TwoDoubles {
121 double one;
122 double two;
123};
124
125struct TwoDoubles
126rust_dbg_extern_identity_TwoDoubles(struct TwoDoubles u) {
127 return u;
128}
129
130struct ManyInts {
131 int8_t arg1;
132 int16_t arg2;
133 int32_t arg3;
134 int16_t arg4;
135 int8_t arg5;
136 struct TwoU8s arg6;
137};
138
62682a34
SL
139// MSVC doesn't allow empty structs or unions
140#ifndef _MSC_VER
1a4d82fc
JJ
141struct Empty {
142};
143
144void
145rust_dbg_extern_empty_struct(struct ManyInts v1, struct Empty e, struct ManyInts v2) {
146 assert(v1.arg1 == v2.arg1 + 1);
147 assert(v1.arg2 == v2.arg2 + 1);
148 assert(v1.arg3 == v2.arg3 + 1);
149 assert(v1.arg4 == v2.arg4 + 1);
150 assert(v1.arg5 == v2.arg5 + 1);
151 assert(v1.arg6.one == v2.arg6.one + 1);
152 assert(v1.arg6.two == v2.arg6.two + 1);
153}
62682a34 154#endif
1a4d82fc
JJ
155
156intptr_t
157rust_get_test_int() {
158 return 1;
159}
160
7453a54e
SL
161char *
162rust_get_null_ptr() {
163 return 0;
164}
165
1a4d82fc
JJ
166/* Debug helpers strictly to verify ABI conformance.
167 *
168 * FIXME (#2665): move these into a testcase when the testsuite
169 * understands how to have explicit C files included.
170 */
171
172struct quad {
173 uint64_t a;
174 uint64_t b;
175 uint64_t c;
176 uint64_t d;
177};
178
179struct floats {
180 double a;
181 uint8_t b;
182 double c;
183};
184
185struct quad
186rust_dbg_abi_1(struct quad q) {
187 struct quad qq = { q.c + 1,
188 q.d - 1,
189 q.a + 1,
190 q.b - 1 };
191 return qq;
192}
193
194struct floats
195rust_dbg_abi_2(struct floats f) {
196 struct floats ff = { f.c + 1.0,
197 0xff,
198 f.a - 1.0 };
199 return ff;
200}
201
202int
62682a34 203rust_dbg_static_mut = 3;
1a4d82fc
JJ
204
205void
206rust_dbg_static_mut_check_four() {
207 assert(rust_dbg_static_mut == 4);
208}
209
210struct S {
211 uint64_t x;
212 uint64_t y;
213 uint64_t z;
214};
215
216uint64_t get_x(struct S s) {
217 return s.x;
218}
219
220uint64_t get_y(struct S s) {
221 return s.y;
222}
223
224uint64_t get_z(struct S s) {
225 return s.z;
226}
b039eaaf
SL
227
228uint64_t get_c_many_params(void *a, void *b, void *c, void *d, struct quad f) {
229 return f.c;
230}
9cc50fc6
SL
231
232// Calculates the average of `(x + y) / n` where x: i64, y: f64. There must be exactly n pairs
233// passed as variadic arguments.
234double rust_interesting_average(uint64_t n, ...) {
235 va_list pairs;
236 double sum = 0.0;
237 int i;
238 va_start(pairs, n);
239 for(i = 0; i < n; i += 1) {
240 sum += (double)va_arg(pairs, int64_t);
241 sum += va_arg(pairs, double);
242 }
243 va_end(pairs);
244 return sum / n;
245}
54a0048b
SL
246
247int32_t rust_int8_to_int32(int8_t x) {
248 return (int32_t)x;
249}