~ubuntu-branches/ubuntu/wily/libjna-java/wily-proposed

« back to all changes in this revision

Viewing changes to test/com/sun/jna/MemoryTest.java

  • Committer: Bazaar Package Importer
  • Author(s): Michael Koch
  • Date: 2009-11-02 14:14:51 UTC
  • mfrom: (4.1.4 sid)
  • Revision ID: james.westby@ubuntu.com-20091102141451-j5hdwzzq5zwxrp4n
* New upstream release.
* Fixed debian/repack-source.sh to correctly use mktemp.
* Make libjna-java Depends on *-headless packages.
* Moved package under pkg-java control.
* Added debian/README.source to describe quilt.
* Added myself as Uploader.

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
package com.sun.jna;
14
14
 
15
15
import java.lang.ref.WeakReference;
 
16
import java.nio.Buffer;
 
17
import java.nio.ByteBuffer;
 
18
 
16
19
import junit.framework.TestCase;
17
20
 
18
21
public class MemoryTest extends TestCase {
31
34
        System.gc();
32
35
        long start = System.currentTimeMillis();
33
36
        assertFalse("Memory prematurely GC'd", flag[0]);
34
 
        // This check fails on IBM's J9, on which the weak ref
35
 
        // is cleared but the object not yet GC'd
36
 
        //assertNotNull("Memory prematurely GC'd", ref.get());
 
37
        assertNotNull("Base memory GC'd while shared memory extant", ref.get());
 
38
        // Avoid having IBM J9 prematurely nullify "shared"
 
39
        shared.setInt(0, 0);
 
40
 
37
41
        shared = null;
38
42
        System.gc();
39
43
        while (ref.get() != null) {
64
68
    }
65
69
 
66
70
    public void testAlignment() {
67
 
        final int SIZE = 128;
 
71
        final int SIZE = 1<<16;
68
72
        Memory base = new Memory(SIZE);
69
 
        for (int align=1;align < 8;align *= 2) {
 
73
        for (int align=1;align < SIZE;align *= 2) {
70
74
            Memory unaligned = base;
71
75
            long mask = ~((long)align - 1);
72
76
            if ((base.peer & mask) == base.peer)
73
77
                unaligned = (Memory)base.share(1, SIZE-1);
74
78
            Pointer aligned = unaligned.align(align);
75
 
            assertEquals("Memory not aligned",
 
79
            assertEquals("Memory not aligned (" + align + ")",
76
80
                         aligned.peer & mask, aligned.peer);
 
81
 
 
82
            assertSame("Alignment request on aligned memory should no-op",
 
83
                       aligned, ((Memory)aligned).align(align));
77
84
        }
 
85
    }
 
86
 
 
87
    public void testNegativeAlignment() {
 
88
        final int SIZE = 128;
 
89
        Memory base = new Memory(SIZE);
78
90
        try {
79
91
            base.align(-1);
80
92
            fail("Negative alignments not allowed");
82
94
        catch(IllegalArgumentException e) { }
83
95
    }
84
96
 
 
97
    public void testInvalidAlignment() {
 
98
        final int SIZE = 128;
 
99
        Memory base = new Memory(SIZE);
 
100
        int[] alignments = { 0, 3, 5, 9, 13 };
 
101
        for (int i=0;i < alignments.length;i++) {
 
102
            try {
 
103
                base.align(alignments[i]);
 
104
                fail("Power-of-two alignments required");
 
105
            }
 
106
            catch(IllegalArgumentException e) { }
 
107
        }
 
108
    }
 
109
 
 
110
    public void testAvoidGCWithExtantBuffer() throws Exception {
 
111
        Memory m = new Memory(1024);
 
112
        ByteBuffer b = m.getByteBuffer(0, m.getSize());
 
113
        WeakReference ref = new WeakReference(m);
 
114
        WeakReference bref = new WeakReference(b);
 
115
        m = null;
 
116
        System.gc();
 
117
        Memory.purge();
 
118
        for (int i=0;i < 100 && ref.get() != null;i++) {
 
119
            Thread.sleep(10);
 
120
            System.gc();
 
121
            Memory.purge();
 
122
        }
 
123
        assertNotNull("Memory GC'd while NIO Buffer still extant", ref.get());
 
124
 
 
125
        // Avoid IBM J9 optimization resulting in premature GC of buffer
 
126
        b.put((byte)0);
 
127
 
 
128
        b = null;
 
129
        System.gc();
 
130
        Memory.purge();
 
131
        for (int i=0;i < 100 && (bref.get() != null || ref.get() != null);i++) {
 
132
            Thread.sleep(10);
 
133
            System.gc();
 
134
            Memory.purge();
 
135
        }
 
136
        assertNull("Buffer not GC'd\n", bref.get());
 
137
        assertNull("Memory not GC'd after buffer GC'd\n", ref.get());
 
138
    }
 
139
 
85
140
    public static void main(String[] args) {
86
141
        junit.textui.TestRunner.run(MemoryTest.class);
87
142
    }