GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — lmdbjava/src/test/java/org/lmdbjava/ComparatorTest.java at master · lmdbjava/lmdbjava Expand file tree/ Copy pathComparatorTest.javaMore file actions 273 lines (230 loc) · 10.7 KB / Copy pathComparatorTest.javaFile metadata and controls273 lines (230 loc) · 10.7 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 /* * Copyright © 2016-2025 The LmdbJava Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.lmdbjava; import static io.netty.buffer.PooledByteBufAllocator.DEFAULT; import static java.nio.charset.StandardCharsets.US_ASCII; import static org.assertj.core.api.Assertions.assertThat; import static org.lmdbjava.ByteArrayProxy.PROXY_BA; import static org.lmdbjava.ByteBufProxy.PROXY_NETTY; import static org.lmdbjava.ByteBufferProxy.PROXY_OPTIMAL; import static org.lmdbjava.ComparatorResult.EQUAL_TO; import static org.lmdbjava.ComparatorResult.GREATER_THAN; import static org.lmdbjava.ComparatorResult.LESS_THAN; import static org.lmdbjava.DirectBufferProxy.PROXY_DB; import com.google.common.primitives.SignedBytes; import com.google.common.primitives.UnsignedBytes; import io.netty.buffer.ByteBuf; import java.nio.ByteBuffer; import java.util.Arrays; import java.util.Comparator; import java.util.stream.Stream; import org.agrona.DirectBuffer; import org.agrona.concurrent.UnsafeBuffer; import org.junit.jupiter.api.extension.ExtensionContext; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.ArgumentsProvider; import org.junit.jupiter.params.provider.ArgumentsSource; import org.junit.jupiter.params.support.ParameterDeclarations; /** Tests comparator functions are consistent across buffers. */ public final class ComparatorTest { // H = 1 (high), L = 0 (low), X = byte not set in buffer private static final byte[] HL = buffer(1, 0); private static final byte[] HLLLLLLL = buffer(1, 0, 0, 0, 0, 0, 0, 0); private static final byte[] HLLLLLLX = buffer(1, 0, 0, 0, 0, 0, 0); private static final byte[] HX = buffer(1); private static final byte[] LH = buffer(0, 1); private static final byte[] LHLLLLLL = buffer(0, 1, 0, 0, 0, 0, 0, 0); private static final byte[] LL = buffer(0, 0); private static final byte[] LLLLLLLL = buffer(0, 0, 0, 0, 0, 0, 0, 0); private static final byte[] LLLLLLLX = buffer(0, 0, 0, 0, 0, 0, 0); private static final byte[] LX = buffer(0); private static final byte[] XX = buffer(); static class MyArgumentProvider implements ArgumentsProvider { @Override public Stream<? extends Arguments> provideArguments( ParameterDeclarations parameters, ExtensionContext context) { return Stream.of( Arguments.argumentSet("StringRunner", new StringRunner()), Arguments.argumentSet("DirectBufferRunner", new DirectBufferRunner()), Arguments.argumentSet("ByteArrayRunner", new ByteArrayRunner()), Arguments.argumentSet("UnsignedByteArrayRunner", new UnsignedByteArrayRunner()), Arguments.argumentSet("ByteBufferRunner", new ByteBufferRunner()), Arguments.argumentSet("NettyRunner", new NettyRunner()), Arguments.argumentSet("GuavaUnsignedBytes", new GuavaUnsignedBytes()), Arguments.argumentSet("GuavaSignedBytes", new GuavaSignedBytes())); } } private static byte[] buffer(final int... bytes) { final byte[] array = new byte[bytes.length]; for (int i = 0; i < bytes.length; i++) { array[i] = (byte) bytes[i]; } return array; } @ParameterizedTest @ArgumentsSource(MyArgumentProvider.class) void atLeastOneBufferHasEightBytes(final ComparatorRunner comparator) { assertThat(TestUtils.compare(comparator, HLLLLLLL, LLLLLLLL)).isEqualTo(GREATER_THAN); assertThat(TestUtils.compare(comparator, LLLLLLLL, HLLLLLLL)).isEqualTo(LESS_THAN); assertThat(TestUtils.compare(comparator, LHLLLLLL, LLLLLLLL)).isEqualTo(GREATER_THAN); assertThat(TestUtils.compare(comparator, LLLLLLLL, LHLLLLLL)).isEqualTo(LESS_THAN); assertThat(TestUtils.compare(comparator, LLLLLLLL, LLLLLLLX)).isEqualTo(GREATER_THAN); assertThat(TestUtils.compare(comparator, LLLLLLLX, LLLLLLLL)).isEqualTo(LESS_THAN); assertThat(TestUtils.compare(comparator, HLLLLLLL, HLLLLLLX)).isEqualTo(GREATER_THAN); assertThat(TestUtils.compare(comparator, HLLLLLLX, HLLLLLLL)).isEqualTo(LESS_THAN); assertThat(TestUtils.compare(comparator, HLLLLLLX, LHLLLLLL)).isEqualTo(GREATER_THAN); assertThat(TestUtils.compare(comparator, LHLLLLLL, HLLLLLLX)).isEqualTo(LESS_THAN); } @ParameterizedTest @ArgumentsSource(MyArgumentProvider.class) void buffersOfTwoBytes(final ComparatorRunner comparator) { assertThat(TestUtils.compare(comparator, LL, XX)).isEqualTo(GREATER_THAN); assertThat(TestUtils.compare(comparator, XX, LL)).isEqualTo(LESS_THAN); assertThat(TestUtils.compare(comparator, LL, LX)).isEqualTo(GREATER_THAN); assertThat(TestUtils.compare(comparator, LX, LL)).isEqualTo(LESS_THAN); assertThat(TestUtils.compare(comparator, LH, LX)).isEqualTo(GREATER_THAN); assertThat(TestUtils.compare(comparator, LX, HL)).isEqualTo(LESS_THAN); assertThat(TestUtils.compare(comparator, HX, LL)).isEqualTo(GREATER_THAN); assertThat(TestUtils.compare(comparator, LH, HX)).isEqualTo(LESS_THAN); } @ParameterizedTest @ArgumentsSource(MyArgumentProvider.class) void equalBuffers(final ComparatorRunner comparator) { assertThat(TestUtils.compare(comparator, LL, LL)).isEqualTo(EQUAL_TO); assertThat(TestUtils.compare(comparator, HX, HX)).isEqualTo(EQUAL_TO); assertThat(TestUtils.compare(comparator, LH, LH)).isEqualTo(EQUAL_TO); assertThat(TestUtils.compare(comparator, LL, LL)).isEqualTo(EQUAL_TO); assertThat(TestUtils.compare(comparator, LX, LX)).isEqualTo(EQUAL_TO); assertThat(TestUtils.compare(comparator, HLLLLLLL, HLLLLLLL)).isEqualTo(EQUAL_TO); assertThat(TestUtils.compare(comparator, HLLLLLLX, HLLLLLLX)).isEqualTo(EQUAL_TO); assertThat(TestUtils.compare(comparator, LHLLLLLL, LHLLLLLL)).isEqualTo(EQUAL_TO); assertThat(TestUtils.compare(comparator, LLLLLLLL, LLLLLLLL)).isEqualTo(EQUAL_TO); assertThat(TestUtils.compare(comparator, LLLLLLLX, LLLLLLLX)).isEqualTo(EQUAL_TO); } /** Tests {@link ByteArrayProxy}. */ private static final class ByteArrayRunner implements ComparatorRunner { @Override public int compare(final byte[] o1, final byte[] o2) { final Comparator<byte[]> c = PROXY_BA.getComparator(); return c.compare(o1, o2); } } /** Tests {@link ByteArrayProxy} (unsigned). */ private static final class UnsignedByteArrayRunner implements ComparatorRunner { @Override public int compare(final byte[] o1, final byte[] o2) { final Comparator<byte[]> c = PROXY_BA.getComparator(); return c.compare(o1, o2); } } /** Tests {@link ByteBufferProxy}. */ private static final class ByteBufferRunner implements ComparatorRunner { @Override public int compare(final byte[] o1, final byte[] o2) { final Comparator<ByteBuffer> c = PROXY_OPTIMAL.getComparator(); // Convert arrays to buffers that are larger than the array, with // limit set at the array length. One buffer bigger than the other. ByteBuffer o1b = arrayToBuffer(o1, o1.length * 3); ByteBuffer o2b = arrayToBuffer(o2, o2.length * 2); final int result = c.compare(o1b, o2b); // Now swap which buffer is bigger o1b = arrayToBuffer(o1, o1.length * 2); o2b = arrayToBuffer(o2, o2.length * 3); final int result2 = c.compare(o1b, o2b); assertThat(result2).isEqualTo(result); // Now try with buffers sized to the array. o1b = ByteBuffer.wrap(o1); o2b = ByteBuffer.wrap(o2); final int result3 = c.compare(o1b, o2b); assertThat(result3).isEqualTo(result); return result; } private ByteBuffer arrayToBuffer(final byte[] arr, final int bufferCapacity) { if (bufferCapacity < arr.length) { throw new IllegalArgumentException("bufferCapacity < arr.length"); } final byte[] newArr = Arrays.copyOf(arr, bufferCapacity); final ByteBuffer byteBuffer = ByteBuffer.wrap(newArr); byteBuffer.limit(arr.length); byteBuffer.position(0); return byteBuffer; } } /** Tests {@link DirectBufferProxy}. */ private static final class DirectBufferRunner implements ComparatorRunner { @Override public int compare(final byte[] o1, final byte[] o2) { final DirectBuffer o1b = new UnsafeBuffer(o1); final DirectBuffer o2b = new UnsafeBuffer(o2); final Comparator<DirectBuffer> c = PROXY_DB.getComparator(); return c.compare(o1b, o2b); } } /** Tests using Guava's {@link SignedBytes} comparator. */ private static final class GuavaSignedBytes implements ComparatorRunner { @Override public int compare(final byte[] o1, final byte[] o2) { final Comparator<byte[]> c = SignedBytes.lexicographicalComparator(); return c.compare(o1, o2); } } /** Tests using Guava's {@link UnsignedBytes} comparator. */ private static final class GuavaUnsignedBytes implements ComparatorRunner { @Override public int compare(final byte[] o1, final byte[] o2) { final Comparator<byte[]> c = UnsignedBytes.lexicographicalComparator(); return c.compare(o1, o2); } } /** Tests {@link ByteBufProxy}. */ private static final class NettyRunner implements ComparatorRunner { @Override public int compare(final byte[] o1, final byte[] o2) { final ByteBuf o1b = DEFAULT.directBuffer(o1.length); final ByteBuf o2b = DEFAULT.directBuffer(o2.length); o1b.writeBytes(o1); o2b.writeBytes(o2); final Comparator<ByteBuf> c = PROXY_NETTY.getComparator(); return c.compare(o1b, o2b); } } /** * Tests {@link String} by providing a reference implementation of what a comparator involving * ASCII-encoded bytes should return. */ private static final class StringRunner implements ComparatorRunner { @Override public int compare(final byte[] o1, final byte[] o2) { final String o1s = new String(o1, US_ASCII); final String o2s = new String(o2, US_ASCII); return o1s.compareTo(o2s); } } /** Interface that can test a {@link BufferProxy} <code>compare</code> method. */ private interface ComparatorRunner extends Comparator<byte[]> { /** * Convert the passed byte arrays into the proxy's relevant buffer type and then invoke the * comparator. * * @param o1 lhs buffer content * @param o2 rhs buffer content * @return as per {@link Comparable} */ @Override int compare(byte[] o1, byte[] o2); } } |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |