1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
//! This module runs tests on the C abi as defined by Rust,
//! to detect whether Rust changed how it deals with zero-sized types.

use super::LibraryError;
use crate::std_types::{RBoxError, Tuple2, Tuple3};

// Types used in tests from this module
mod types;

mod functions;

#[macro_use]
mod c_abi_testing_macros;

use self::types::MyUnit;

pub use self::functions::{CAbiTestingFns, C_ABI_TESTING_FNS};

/// Tests that the abi (as defined by the compiler) of the functions in
/// CAbiTestingFns is the same as the caller's.
pub fn run_tests(funcs: &CAbiTestingFns) -> Result<(), LibraryError> {
    pair_tests(funcs)?;
    triple_tests(funcs)?;
    two_pair_tests(funcs)?;
    mixed_units_test(funcs)?;
    Ok(())
}

fn make_invalid_cabi_err<T>(expected: T, found: T) -> LibraryError
where
    T: std::fmt::Debug,
{
    LibraryError::InvalidCAbi {
        expected: RBoxError::from_debug(&expected),
        found: RBoxError::from_debug(&found),
    }
}

fn pair_tests(funcs: &CAbiTestingFns) -> Result<(), LibraryError> {
    check_roundtrip!(funcs, 0x1, (a = Tuple2(1, ())), ret_pair_a, take_pair_a);
    check_roundtrip!(funcs, 0x5, (a = Tuple2(5, ())), ret_pair_a, take_pair_a);

    check_roundtrip!(
        funcs,
        0x1_0000,
        (a = Tuple2((), 1)),
        ret_pair_b,
        take_pair_b
    );
    check_roundtrip!(
        funcs,
        0x5_0000,
        (a = Tuple2((), 5)),
        ret_pair_b,
        take_pair_b
    );
    Ok(())
}

fn triple_tests(funcs: &CAbiTestingFns) -> Result<(), LibraryError> {
    check_roundtrip!(
        funcs,
        0x1_0001_0000,
        (a = Tuple3((), 1, 1)),
        ret_triple_a,
        take_triple_a
    );
    check_roundtrip!(
        funcs,
        0x1_0005_0000,
        (a = Tuple3((), 5, 1)),
        ret_triple_a,
        take_triple_a
    );
    check_roundtrip!(
        funcs,
        0x7_0001_0000,
        (a = Tuple3((), 1, 7)),
        ret_triple_a,
        take_triple_a
    );
    check_roundtrip!(
        funcs,
        0x7_0003_0000,
        (a = Tuple3((), 3, 7)),
        ret_triple_a,
        take_triple_a
    );

    check_roundtrip!(
        funcs,
        0x1_0000_0001,
        (a = Tuple3(1, (), 1)),
        ret_triple_b,
        take_triple_b
    );
    check_roundtrip!(
        funcs,
        0x1_0000_0005,
        (a = Tuple3(5, (), 1)),
        ret_triple_b,
        take_triple_b
    );
    check_roundtrip!(
        funcs,
        0x7_0000_0001,
        (a = Tuple3(1, (), 7)),
        ret_triple_b,
        take_triple_b
    );
    check_roundtrip!(
        funcs,
        0x7_0000_0003,
        (a = Tuple3(3, (), 7)),
        ret_triple_b,
        take_triple_b
    );

    check_roundtrip!(
        funcs,
        0x1_0001,
        (a = Tuple3(1, 1, ())),
        ret_triple_c,
        take_triple_c
    );
    check_roundtrip!(
        funcs,
        0x1_0005,
        (a = Tuple3(5, 1, ())),
        ret_triple_c,
        take_triple_c
    );
    check_roundtrip!(
        funcs,
        0x7_0001,
        (a = Tuple3(1, 7, ())),
        ret_triple_c,
        take_triple_c
    );
    check_roundtrip!(
        funcs,
        0x7_0003,
        (a = Tuple3(3, 7, ())),
        ret_triple_c,
        take_triple_c
    );
    Ok(())
}

fn two_pair_tests(funcs: &CAbiTestingFns) -> Result<(), LibraryError> {
    let funcs = anon_struct! {
        ret_2_pairs_a:|n|(funcs.ret_2_pairs_a)(n).into_tuple(),
        ret_2_pairs_b:|n|(funcs.ret_2_pairs_b)(n).into_tuple(),
        take_2_pairs_a:funcs.take_2_pairs_a,
        take_2_pairs_b:funcs.take_2_pairs_b,
    };

    check_roundtrip!(
        funcs,
        0x1_0000_0005_0000,
        (a = Tuple2((), 5), b = Tuple2((), 1)),
        ret_2_pairs_a,
        take_2_pairs_a,
    );
    check_roundtrip!(
        funcs,
        0xF_0000_000A_0000,
        (a = Tuple2((), 10), b = Tuple2((), 15)),
        ret_2_pairs_a,
        take_2_pairs_a,
    );

    check_roundtrip!(
        funcs,
        0x0_0007_0000_0003,
        (a = Tuple2(3, ()), b = Tuple2(7, ())),
        ret_2_pairs_b,
        take_2_pairs_b,
    );
    check_roundtrip!(
        funcs,
        0x0_0002_0000_000B,
        (a = Tuple2(11, ()), b = Tuple2(2, ())),
        ret_2_pairs_b,
        take_2_pairs_b,
    );
    Ok(())
}

fn mixed_units_test(funcs: &CAbiTestingFns) -> Result<(), LibraryError> {
    let single_test = |n: u64| {
        let a = n as u16;
        let b = (n >> 16) as u16;
        let c = (n >> 32) as u16;
        let d = (n >> 48) as u16;

        let res = (funcs.mixed_units)(a, MyUnit, b, MyUnit, c, MyUnit, d);
        if res != n {
            Err(make_invalid_cabi_err(n, res))
        } else {
            Ok(())
        }
    };

    single_test(0x0)?;
    single_test(0x1)?;
    single_test(0x2_0003)?;
    single_test(0x4_0005_0006)?;
    single_test(0x7_0008_0009_000A)?;
    Ok(())
}