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