Skip to content

Commit 92a3086

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

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-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: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,29 @@ 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+
let mut expected = 10;
18+
unsafe {
19+
loop {
20+
assert_eq!(*test, expected);
21+
if *test == 0 {
22+
break;
23+
}
24+
test = test.offset(1);
25+
expected -= 1;
26+
}
27+
}
28+
}
629

730
#[test]
831
fn test_static_method() {

0 commit comments

Comments
 (0)