Skip to content

Commit 33c783f

Browse files
committed
BridJ: fixed STL list.empty / back / front.
1 parent bc51743 commit 33c783f

2 files changed

Lines changed: 27 additions & 20 deletions

File tree

src/main/java/org/bridj/cpp/std/list.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ protected void deleteNode(list_node<T> node) {
148148
free(Pointer.getAddress(node, list_node.class));
149149
}
150150

151+
private Pointer<list_node<T>> node() {
152+
return (Pointer) Pointer.getPointer(this).as(list_node.class);
153+
}
154+
151155
@Deprecated
152156
@Field(0)
153157
public Pointer<list_node<T>> next() {
@@ -183,13 +187,13 @@ private void checkNotEmpty() {
183187
throw new NoSuchElementException();
184188
}
185189
public boolean empty() {
186-
return next() != null;
190+
return Pointer.getPeer(next()) == Pointer.getPeer(node());
187191
}
188-
public T front() {
192+
public T back() {
189193
checkNotEmpty();
190194
return next().get().get();
191195
}
192-
public T back() {
196+
public T front() {
193197
checkNotEmpty();
194198
Pointer<list_node<T>> prev = prev(), next = next();
195199
Pointer<list_node<T>> nextPrev = next == null ? null : next.get().prev();

src/test/java/org/bridj/STLTest.java

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -88,27 +88,30 @@ public void testVector() throws Exception {
8888
int_vector_push_back.apply(nativeVector, -1);
8989
assertEquals("bad size after push_back", n + 1, bridjVector.size());
9090
assertEquals("bad back", -1, (int)bridjVector.back());
91+
int_vector_push_back.apply(nativeVector, -2);
92+
assertEquals("bad size after push_back", n + 2, bridjVector.size());
93+
assertEquals("bad back", -2, (int)bridjVector.back());
9194
}
9295

9396
@Test
9497
public void testList() throws Exception {
95-
try {
96-
int n = 10;
97-
Pointer<?> nativeList = new_int_list.apply();
98-
list<Integer> bridjList = new list<Integer>((Pointer)nativeList, Integer.class);
99-
assertTrue("bad empty()", bridjList.empty());
100-
int_list_push_back.apply(nativeList, 66);
101-
102-
int[] values = nativeList.getInts(20);
103-
long[] ptrs = nativeList.getSizeTs(10);
104-
long peer = nativeList.getPeer();
105-
for (int i = n; --i != 0;) {
106-
int_list_push_back.apply(nativeList, i);
107-
assertEquals("bad back() at index " + i, i, (int)bridjList.back());
108-
}
109-
} catch (Throwable th) {
110-
th.printStackTrace();
111-
assertNull(th);
98+
int n = 10;
99+
Pointer<?> nativeList = new_int_list.apply();
100+
list<Integer> bridjList = new list<Integer>((Pointer)nativeList, Integer.class);
101+
assertTrue("bad empty()", bridjList.empty());
102+
// assertEquals("bad size()", 0, bridjList.size());
103+
int_list_push_back.apply(nativeList, 66);
104+
assertFalse("bad empty()", bridjList.empty());
105+
assertEquals("first front()", 66, (int)bridjList.front());
106+
assertEquals("first back()", 66, (int)bridjList.back());
107+
108+
int_list_push_back.apply(nativeList, 65);
109+
assertEquals("second front()", 66, (int)bridjList.front());
110+
assertEquals("second back()", 65, (int)bridjList.back());
111+
112+
for (int i = n; --i != 0;) {
113+
int_list_push_back.apply(nativeList, i);
114+
assertEquals("bad back() at index " + i, i, (int)bridjList.back());
112115
}
113116
}
114117
}

0 commit comments

Comments
 (0)