|
| 1 | +/* |
| 2 | + * Copyright (C) 2018 Muhammad Tayyab Akram |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.mta.tehreer.internal.collections; |
| 18 | + |
| 19 | +import com.mta.tehreer.collections.IntListTestSuite; |
| 20 | +import com.mta.tehreer.internal.Memory; |
| 21 | + |
| 22 | +import org.junit.After; |
| 23 | +import org.junit.Before; |
| 24 | + |
| 25 | +import java.nio.ByteBuffer; |
| 26 | +import java.nio.ByteOrder; |
| 27 | +import java.nio.IntBuffer; |
| 28 | +import java.nio.LongBuffer; |
| 29 | + |
| 30 | +public class UIntPtrBufferIntListTest extends IntListTestSuite { |
| 31 | + private long pointer; |
| 32 | + |
| 33 | + private void setUpInts() { |
| 34 | + int[] sample = new int[] { |
| 35 | + 0x00000000, 0x0E38E38E, 0x1C71C71C, 0x2AAAAAAA, 0x38E38E38, |
| 36 | + 0x471C71C6, 0x55555554, 0x638E38E2, 0x71C71C70, 0x7FFFFFFE |
| 37 | + }; |
| 38 | + int capacity = sample.length * 4; |
| 39 | + |
| 40 | + this.pointer = Memory.allocate(capacity); |
| 41 | + this.expected = sample; |
| 42 | + this.actual = new UIntPtrBufferIntList(null, pointer, sample.length); |
| 43 | + |
| 44 | + ByteBuffer byteBuffer = Memory.buffer(pointer, capacity).order(ByteOrder.nativeOrder()); |
| 45 | + IntBuffer intBuffer = byteBuffer.asIntBuffer(); |
| 46 | + intBuffer.put(sample); |
| 47 | + } |
| 48 | + |
| 49 | + private void setUpLongs() { |
| 50 | + long[] sample = new long[] { |
| 51 | + 0x00000000, 0x0E38E38E, 0x1C71C71C, 0x2AAAAAAA, 0x38E38E38, |
| 52 | + 0x471C71C6, 0x55555554, 0x638E38E2, 0x71C71C70, 0x7FFFFFFE |
| 53 | + }; |
| 54 | + int capacity = sample.length * 8; |
| 55 | + |
| 56 | + this.pointer = Memory.allocate(capacity); |
| 57 | + this.expected = new int[] { 0x00000000, 0x0E38E38E, 0x1C71C71C, 0x2AAAAAAA, 0x38E38E38, |
| 58 | + 0x471C71C6, 0x55555554, 0x638E38E2, 0x71C71C70, 0x7FFFFFFE }; |
| 59 | + this.actual = new UIntPtrBufferIntList(null, pointer, sample.length); |
| 60 | + |
| 61 | + ByteBuffer byteBuffer = Memory.buffer(pointer, capacity).order(ByteOrder.nativeOrder()); |
| 62 | + LongBuffer longBuffer = byteBuffer.asLongBuffer(); |
| 63 | + longBuffer.put(sample); |
| 64 | + } |
| 65 | + |
| 66 | + @Before |
| 67 | + public void setUp() { |
| 68 | + switch (Memory.pointerSize()) { |
| 69 | + case 4: |
| 70 | + setUpInts(); |
| 71 | + break; |
| 72 | + |
| 73 | + case 8: |
| 74 | + setUpLongs(); |
| 75 | + break; |
| 76 | + |
| 77 | + default: |
| 78 | + throw new UnsupportedOperationException(); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + @After |
| 83 | + public void tearDown() { |
| 84 | + Memory.dispose(pointer); |
| 85 | + } |
| 86 | +} |
0 commit comments