Skip to content

Commit 3154861

Browse files
committed
tests: Add an integration test for static arrays.
Turns out they were broken before rust-lang#456. Let's test it so it doesn't regress.
1 parent 3660d4d commit 3154861

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

bindgen-integration/cpp/Test.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
#include "Test.h"
22

3+
const int Test::COUNTDOWN[] = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
4+
const int* Test::COUNTDOWN_PTR = Test::COUNTDOWN;
5+
6+
const int* Test::countdown() {
7+
return COUNTDOWN;
8+
}
9+
310
const char* Test::name() {
411
return "Test";
512
}

bindgen-integration/cpp/Test.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ class Test {
77
static const char* name();
88
Test(int foo);
99
Test(double foo);
10+
11+
static const int COUNTDOWN[];
12+
static const int* COUNTDOWN_PTR;
13+
static const int* countdown();
1014
};
1115

1216
namespace testing {

bindgen-integration/src/lib.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,31 @@ mod bindings {
33
}
44

55
use std::ffi::CStr;
6+
use std::os::raw::c_int;
7+
8+
#[test]
9+
fn test_static_array() {
10+
let mut test = unsafe { bindings::Test_COUNTDOWN.as_ptr() };
11+
let expected = unsafe { bindings::Test_countdown()};
12+
let also_expected = unsafe { bindings::Test_COUNTDOWN_PTR };
13+
assert!(!test.is_null());
14+
assert_eq!(also_expected, expected, "Pointer value was bogus");
15+
assert_eq!(test, also_expected, "Array value was bogus");
16+
17+
println!("Test: {:?}", test);
18+
19+
let mut expected = 10;
20+
unsafe {
21+
loop {
22+
assert_eq!(*test, expected);
23+
if *test == 0 {
24+
break;
25+
}
26+
test = test.offset(1);
27+
expected -= 1;
28+
}
29+
}
30+
}
631

732
#[test]
833
fn test_static_method() {

0 commit comments

Comments
 (0)