1 8 9 package com.sleepycat.bind.tuple.test; 10 11 import junit.framework.Test; 12 import junit.framework.TestCase; 13 import junit.framework.TestSuite; 14 15 import com.sleepycat.bind.tuple.TupleOutput; 16 import com.sleepycat.collections.test.DbTestUtil; 17 18 21 public class TupleOrderingTest extends TestCase { 22 23 private TupleOutput out; 24 private byte[] prevBuf; 25 26 public static void main(String [] args) 27 throws Exception { 28 29 junit.framework.TestResult tr = 30 junit.textui.TestRunner.run(suite()); 31 if (tr.errorCount() > 0 || 32 tr.failureCount() > 0) { 33 System.exit(1); 34 } else { 35 System.exit(0); 36 } 37 } 38 39 public static Test suite() 40 throws Exception { 41 42 TestSuite suite = new TestSuite(TupleOrderingTest.class); 43 return suite; 44 } 45 46 public TupleOrderingTest(String name) { 47 48 super(name); 49 } 50 51 public void setUp() { 52 53 DbTestUtil.printTestName("TupleOrderingTest." + getName()); 54 out = new TupleOutput(); 55 prevBuf = null; 56 } 57 58 public void tearDown() { 59 60 61 out = null; 62 prevBuf = null; 63 } 64 65 70 private void check() { 71 72 check(-1); 73 } 74 75 private void check(int dataIndex) { 76 77 byte[] buf = new byte[out.size()]; 78 System.arraycopy(out.getBufferBytes(), out.getBufferOffset(), 79 buf, 0, buf.length); 80 if (prevBuf != null) { 81 int errOffset = -1; 82 int len = Math.min(prevBuf.length, buf.length); 83 boolean areEqual = true; 84 for (int i = 0; i < len; i += 1) { 85 int val1 = prevBuf[i] & 0xFF; 86 int val2 = buf[i] & 0xFF; 87 if (val1 < val2) { 88 areEqual = false; 89 break; 90 } else if (val1 > val2) { 91 errOffset = i; 92 break; 93 } 94 } 95 if (areEqual) { 96 if (prevBuf.length < buf.length) { 97 areEqual = false; 98 } else if (prevBuf.length > buf.length) { 99 areEqual = false; 100 errOffset = buf.length + 1; 101 } 102 } 103 if (errOffset != -1 || areEqual) { 104 StringBuffer msg = new StringBuffer (); 105 if (errOffset != -1) { 106 msg.append("Left >= right at byte offset " + errOffset); 107 } else if (areEqual) { 108 msg.append("Bytes are equal"); 109 } else { 110 throw new IllegalStateException (); 111 } 112 msg.append("\nLeft hex bytes: "); 113 for (int i = 0; i < prevBuf.length; i += 1) { 114 msg.append(' '); 115 int val = prevBuf[i] & 0xFF; 116 if ((val & 0xF0) == 0) { 117 msg.append('0'); 118 } 119 msg.append(Integer.toHexString(val)); 120 } 121 msg.append("\nRight hex bytes:"); 122 for (int i = 0; i < buf.length; i += 1) { 123 msg.append(' '); 124 int val = buf[i] & 0xFF; 125 if ((val & 0xF0) == 0) { 126 msg.append('0'); 127 } 128 msg.append(Integer.toHexString(val)); 129 } 130 if (dataIndex >= 0) { 131 msg.append("\nData index: " + dataIndex); 132 } 133 fail(msg.toString()); 134 } 135 } 136 prevBuf = buf; 137 out.reset(); 138 } 139 140 private void reset() { 141 142 prevBuf = null; 143 out.reset(); 144 } 145 146 public void testString() { 147 148 final String [] DATA = { 149 "", "a", "ab", "b", "bb", "bba", 150 }; 151 for (int i = 0; i < DATA.length; i += 1) { 152 out.writeString(DATA[i]); 153 check(i); 154 } 155 reset(); 156 out.writeString("a"); 157 check(); 158 out.writeString("a"); 159 out.writeString(""); 160 check(); 161 out.writeString("a"); 162 out.writeString(""); 163 out.writeString("a"); 164 check(); 165 out.writeString("a"); 166 out.writeString("b"); 167 check(); 168 out.writeString("aa"); 169 check(); 170 out.writeString("b"); 171 check(); 172 } 173 174 public void testFixedString() { 175 176 final char[][] DATA = { 177 {}, {'a'}, {'a', 'b'}, {'b'}, {'b', 'b'}, {0x7F}, {0xFF}, 178 }; 179 for (int i = 0; i < DATA.length; i += 1) { 180 out.writeString(DATA[i]); 181 check(i); 182 } 183 } 184 185 public void testChars() { 186 187 final char[][] DATA = { 188 {}, {0}, {'a'}, {'a', 0}, {'a', 'b'}, {'b'}, {'b', 'b'}, 189 {0x7F}, {0x7F, 0}, {0xFF}, {0xFF, 0}, 190 }; 191 for (int i = 0; i < DATA.length; i += 1) { 192 out.writeChars(DATA[i]); 193 check(i); 194 } 195 } 196 197 public void testBytes() { 198 199 final char[][] DATA = { 200 {}, {0}, {'a'}, {'a', 0}, {'a', 'b'}, {'b'}, {'b', 'b'}, 201 {0x7F}, {0xFF}, 202 }; 203 for (int i = 0; i < DATA.length; i += 1) { 204 out.writeBytes(DATA[i]); 205 check(i); 206 } 207 } 208 209 public void testBoolean() { 210 211 final boolean[] DATA = { 212 false, true 213 }; 214 for (int i = 0; i < DATA.length; i += 1) { 215 out.writeBoolean(DATA[i]); 216 check(i); 217 } 218 } 219 220 public void testUnsignedByte() { 221 222 final int[] DATA = { 223 0, 1, 0x7F, 0xFF 224 }; 225 for (int i = 0; i < DATA.length; i += 1) { 226 out.writeUnsignedByte(DATA[i]); 227 check(i); 228 } 229 } 230 231 public void testUnsignedShort() { 232 233 final int[] DATA = { 234 0, 1, 0xFE, 0xFF, 0x800, 0x7FFF, 0xFFFF 235 }; 236 for (int i = 0; i < DATA.length; i += 1) { 237 out.writeUnsignedShort(DATA[i]); 238 check(i); 239 } 240 } 241 242 public void testUnsignedInt() { 243 244 final long[] DATA = { 245 0, 1, 0xFE, 0xFF, 0x800, 0x7FFF, 0xFFFF, 0x80000, 246 0x7FFFFFFF, 0x80000000, 0xFFFFFFFF 247 }; 248 for (int i = 0; i < DATA.length; i += 1) { 249 out.writeUnsignedInt(DATA[i]); 250 check(i); 251 } 252 } 253 254 public void testByte() { 255 256 final byte[] DATA = { 257 Byte.MIN_VALUE, Byte.MIN_VALUE + 1, 258 -1, 0, 1, 259 Byte.MAX_VALUE - 1, Byte.MAX_VALUE, 260 }; 261 for (int i = 0; i < DATA.length; i += 1) { 262 out.writeByte(DATA[i]); 263 check(i); 264 } 265 } 266 267 public void testShort() { 268 269 final short[] DATA = { 270 Short.MIN_VALUE, Short.MIN_VALUE + 1, 271 Byte.MIN_VALUE, Byte.MIN_VALUE + 1, 272 -1, 0, 1, 273 Byte.MAX_VALUE - 1, Byte.MAX_VALUE, 274 Short.MAX_VALUE - 1, Short.MAX_VALUE, 275 }; 276 for (int i = 0; i < DATA.length; i += 1) { 277 out.writeShort(DATA[i]); 278 check(i); 279 } 280 } 281 282 public void testInt() { 283 284 final int[] DATA = { 285 Integer.MIN_VALUE, Integer.MIN_VALUE + 1, 286 Short.MIN_VALUE, Short.MIN_VALUE + 1, 287 Byte.MIN_VALUE, Byte.MIN_VALUE + 1, 288 -1, 0, 1, 289 Byte.MAX_VALUE - 1, Byte.MAX_VALUE, 290 Short.MAX_VALUE - 1, Short.MAX_VALUE, 291 Integer.MAX_VALUE - 1, Integer.MAX_VALUE, 292 }; 293 for (int i = 0; i < DATA.length; i += 1) { 294 out.writeInt(DATA[i]); 295 check(i); 296 } 297 } 298 299 public void testLong() { 300 301 final long[] DATA = { 302 Long.MIN_VALUE, Long.MIN_VALUE + 1, 303 Integer.MIN_VALUE, Integer.MIN_VALUE + 1, 304 Short.MIN_VALUE, Short.MIN_VALUE + 1, 305 Byte.MIN_VALUE, Byte.MIN_VALUE + 1, 306 -1, 0, 1, 307 Byte.MAX_VALUE - 1, Byte.MAX_VALUE, 308 Short.MAX_VALUE - 1, Short.MAX_VALUE, 309 Integer.MAX_VALUE - 1, Integer.MAX_VALUE, 310 Long.MAX_VALUE - 1, Long.MAX_VALUE, 311 }; 312 for (int i = 0; i < DATA.length; i += 1) { 313 out.writeLong(DATA[i]); 314 check(i); 315 } 316 } 317 318 public void testFloat() { 319 320 322 final float[] DATA = { 323 0, Float.MIN_VALUE, 2 * Float.MIN_VALUE, 324 (float) 0.01, (float) 0.02, (float) 0.99, 325 1, (float) 1.01, (float) 1.02, (float) 1.99, 326 Byte.MAX_VALUE - 1, Byte.MAX_VALUE, 327 Short.MAX_VALUE - 1, Short.MAX_VALUE, 328 Integer.MAX_VALUE, 329 Long.MAX_VALUE / 2, Long.MAX_VALUE, 330 Float.MAX_VALUE, 331 Float.POSITIVE_INFINITY, 332 Float.NaN, 333 }; 334 for (int i = 0; i < DATA.length; i += 1) { 335 out.writeFloat(DATA[i]); 336 check(i); 337 } 338 } 339 340 public void testDouble() { 341 342 344 final double[] DATA = { 345 0, Double.MIN_VALUE, 2 * Double.MIN_VALUE, 346 0.001, 0.002, 0.999, 347 1, 1.001, 1.002, 1.999, 348 Byte.MAX_VALUE - 1, Byte.MAX_VALUE, 349 Short.MAX_VALUE - 1, Short.MAX_VALUE, 350 Integer.MAX_VALUE - 1, Integer.MAX_VALUE, 351 Long.MAX_VALUE / 2, Long.MAX_VALUE, 352 Float.MAX_VALUE, Double.MAX_VALUE, 353 Double.POSITIVE_INFINITY, 354 Double.NaN, 355 }; 356 for (int i = 0; i < DATA.length; i += 1) { 357 out.writeDouble(DATA[i]); 358 check(i); 359 } 360 } 361 362 public void testSortedFloat() { 363 364 final float[] DATA = { 365 Float.NEGATIVE_INFINITY, 366 (- Float.MAX_VALUE), 367 Long.MIN_VALUE, 368 Long.MIN_VALUE / 2, 369 Integer.MIN_VALUE, 370 Short.MIN_VALUE, 371 Short.MIN_VALUE + 1, 372 Byte.MIN_VALUE, 373 Byte.MIN_VALUE + 1, 374 (float) -1.99, 375 (float) -1.02, 376 (float) -1.01, 377 -1, 378 (float) -0.99, 379 (float) -0.02, 380 (float) -0.01, 381 2 * (- Float.MIN_VALUE), 382 (- Float.MIN_VALUE), 383 0, 384 Float.MIN_VALUE, 385 2 * Float.MIN_VALUE, 386 (float) 0.01, 387 (float) 0.02, 388 (float) 0.99, 389 1, 390 (float) 1.01, 391 (float) 1.02, 392 (float) 1.99, 393 Byte.MAX_VALUE - 1, 394 Byte.MAX_VALUE, 395 Short.MAX_VALUE - 1, 396 Short.MAX_VALUE, 397 Integer.MAX_VALUE, 398 Long.MAX_VALUE / 2, 399 Long.MAX_VALUE, 400 Float.MAX_VALUE, 401 Float.POSITIVE_INFINITY, 402 Float.NaN, 403 }; 404 for (int i = 0; i < DATA.length; i += 1) { 405 out.writeSortedFloat(DATA[i]); 406 check(i); 407 } 408 } 409 410 public void testSortedDouble() { 411 412 final double[] DATA = { 413 Double.NEGATIVE_INFINITY, 414 (- Double.MAX_VALUE), 415 (- Float.MAX_VALUE), 416 Long.MIN_VALUE, 417 Long.MIN_VALUE / 2, 418 Integer.MIN_VALUE, 419 Short.MIN_VALUE, 420 Short.MIN_VALUE + 1, 421 Byte.MIN_VALUE, 422 Byte.MIN_VALUE + 1, 423 -1.999, 424 -1.002, 425 -1.001, 426 -1, 427 -0.999, 428 -0.002, 429 -0.001, 430 2 * (- Double.MIN_VALUE), 431 (- Double.MIN_VALUE), 432 0, 433 Double.MIN_VALUE, 434 2 * Double.MIN_VALUE, 435 0.001, 436 0.002, 437 0.999, 438 1, 439 1.001, 440 1.002, 441 1.999, 442 Byte.MAX_VALUE - 1, 443 Byte.MAX_VALUE, 444 Short.MAX_VALUE - 1, 445 Short.MAX_VALUE, 446 Integer.MAX_VALUE - 1, 447 Integer.MAX_VALUE, 448 Long.MAX_VALUE / 2, 449 Long.MAX_VALUE, 450 Float.MAX_VALUE, 451 Double.MAX_VALUE, 452 Double.POSITIVE_INFINITY, 453 Double.NaN, 454 }; 455 for (int i = 0; i < DATA.length; i += 1) { 456 out.writeSortedDouble(DATA[i]); 457 check(i); 458 } 459 } 460 } 461 | Popular Tags |