00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 package com.sleepycat.bind.tuple.test;
00011
00012 import junit.framework.Test;
00013 import junit.framework.TestCase;
00014 import junit.framework.TestSuite;
00015
00016 import com.sleepycat.bind.tuple.TupleOutput;
00017 import com.sleepycat.collections.test.DbTestUtil;
00018
00022 public class TupleOrderingTest extends TestCase {
00023
00024 private TupleOutput out;
00025 private byte[] prevBuf;
00026
00027 public static void main(String[] args)
00028 throws Exception {
00029
00030 junit.framework.TestResult tr =
00031 junit.textui.TestRunner.run(suite());
00032 if (tr.errorCount() > 0 ||
00033 tr.failureCount() > 0) {
00034 System.exit(1);
00035 } else {
00036 System.exit(0);
00037 }
00038 }
00039
00040 public static Test suite()
00041 throws Exception {
00042
00043 TestSuite suite = new TestSuite(TupleOrderingTest.class);
00044 return suite;
00045 }
00046
00047 public TupleOrderingTest(String name) {
00048
00049 super(name);
00050 }
00051
00052 public void setUp() {
00053
00054 DbTestUtil.printTestName("TupleOrderingTest." + getName());
00055 out = new TupleOutput();
00056 prevBuf = null;
00057 }
00058
00059 public void tearDown() {
00060
00061
00062 out = null;
00063 prevBuf = null;
00064 }
00065
00071 private void check() {
00072
00073 check(-1);
00074 }
00075
00076 private void check(int dataIndex) {
00077
00078 byte[] buf = new byte[out.size()];
00079 System.arraycopy(out.getBufferBytes(), out.getBufferOffset(),
00080 buf, 0, buf.length);
00081 if (prevBuf != null) {
00082 int errOffset = -1;
00083 int len = Math.min(prevBuf.length, buf.length);
00084 boolean areEqual = true;
00085 for (int i = 0; i < len; i += 1) {
00086 int val1 = prevBuf[i] & 0xFF;
00087 int val2 = buf[i] & 0xFF;
00088 if (val1 < val2) {
00089 areEqual = false;
00090 break;
00091 } else if (val1 > val2) {
00092 errOffset = i;
00093 break;
00094 }
00095 }
00096 if (areEqual) {
00097 if (prevBuf.length < buf.length) {
00098 areEqual = false;
00099 } else if (prevBuf.length > buf.length) {
00100 areEqual = false;
00101 errOffset = buf.length + 1;
00102 }
00103 }
00104 if (errOffset != -1 || areEqual) {
00105 StringBuffer msg = new StringBuffer();
00106 if (errOffset != -1) {
00107 msg.append("Left >= right at byte offset " + errOffset);
00108 } else if (areEqual) {
00109 msg.append("Bytes are equal");
00110 } else {
00111 throw new IllegalStateException();
00112 }
00113 msg.append("\nLeft hex bytes: ");
00114 for (int i = 0; i < prevBuf.length; i += 1) {
00115 msg.append(' ');
00116 int val = prevBuf[i] & 0xFF;
00117 if ((val & 0xF0) == 0) {
00118 msg.append('0');
00119 }
00120 msg.append(Integer.toHexString(val));
00121 }
00122 msg.append("\nRight hex bytes:");
00123 for (int i = 0; i < buf.length; i += 1) {
00124 msg.append(' ');
00125 int val = buf[i] & 0xFF;
00126 if ((val & 0xF0) == 0) {
00127 msg.append('0');
00128 }
00129 msg.append(Integer.toHexString(val));
00130 }
00131 if (dataIndex >= 0) {
00132 msg.append("\nData index: " + dataIndex);
00133 }
00134 fail(msg.toString());
00135 }
00136 }
00137 prevBuf = buf;
00138 out.reset();
00139 }
00140
00141 private void reset() {
00142
00143 prevBuf = null;
00144 out.reset();
00145 }
00146
00147 public void testString() {
00148
00149 final String[] DATA = {
00150 "", "a", "ab", "b", "bb", "bba",
00151 };
00152 for (int i = 0; i < DATA.length; i += 1) {
00153 out.writeString(DATA[i]);
00154 check(i);
00155 }
00156 reset();
00157 out.writeString("a");
00158 check();
00159 out.writeString("a");
00160 out.writeString("");
00161 check();
00162 out.writeString("a");
00163 out.writeString("");
00164 out.writeString("a");
00165 check();
00166 out.writeString("a");
00167 out.writeString("b");
00168 check();
00169 out.writeString("aa");
00170 check();
00171 out.writeString("b");
00172 check();
00173 }
00174
00175 public void testFixedString() {
00176
00177 final char[][] DATA = {
00178 {}, {'a'}, {'a', 'b'}, {'b'}, {'b', 'b'}, {0x7F}, {0xFF},
00179 };
00180 for (int i = 0; i < DATA.length; i += 1) {
00181 out.writeString(DATA[i]);
00182 check(i);
00183 }
00184 }
00185
00186 public void testChars() {
00187
00188 final char[][] DATA = {
00189 {}, {0}, {'a'}, {'a', 0}, {'a', 'b'}, {'b'}, {'b', 'b'},
00190 {0x7F}, {0x7F, 0}, {0xFF}, {0xFF, 0},
00191 };
00192 for (int i = 0; i < DATA.length; i += 1) {
00193 out.writeChars(DATA[i]);
00194 check(i);
00195 }
00196 }
00197
00198 public void testBytes() {
00199
00200 final char[][] DATA = {
00201 {}, {0}, {'a'}, {'a', 0}, {'a', 'b'}, {'b'}, {'b', 'b'},
00202 {0x7F}, {0xFF},
00203 };
00204 for (int i = 0; i < DATA.length; i += 1) {
00205 out.writeBytes(DATA[i]);
00206 check(i);
00207 }
00208 }
00209
00210 public void testBoolean() {
00211
00212 final boolean[] DATA = {
00213 false, true
00214 };
00215 for (int i = 0; i < DATA.length; i += 1) {
00216 out.writeBoolean(DATA[i]);
00217 check(i);
00218 }
00219 }
00220
00221 public void testUnsignedByte() {
00222
00223 final int[] DATA = {
00224 0, 1, 0x7F, 0xFF
00225 };
00226 for (int i = 0; i < DATA.length; i += 1) {
00227 out.writeUnsignedByte(DATA[i]);
00228 check(i);
00229 }
00230 }
00231
00232 public void testUnsignedShort() {
00233
00234 final int[] DATA = {
00235 0, 1, 0xFE, 0xFF, 0x800, 0x7FFF, 0xFFFF
00236 };
00237 for (int i = 0; i < DATA.length; i += 1) {
00238 out.writeUnsignedShort(DATA[i]);
00239 check(i);
00240 }
00241 }
00242
00243 public void testUnsignedInt() {
00244
00245 final long[] DATA = {
00246 0, 1, 0xFE, 0xFF, 0x800, 0x7FFF, 0xFFFF, 0x80000,
00247 0x7FFFFFFF, 0x80000000, 0xFFFFFFFF
00248 };
00249 for (int i = 0; i < DATA.length; i += 1) {
00250 out.writeUnsignedInt(DATA[i]);
00251 check(i);
00252 }
00253 }
00254
00255 public void testByte() {
00256
00257 final byte[] DATA = {
00258 Byte.MIN_VALUE, Byte.MIN_VALUE + 1,
00259 -1, 0, 1,
00260 Byte.MAX_VALUE - 1, Byte.MAX_VALUE,
00261 };
00262 for (int i = 0; i < DATA.length; i += 1) {
00263 out.writeByte(DATA[i]);
00264 check(i);
00265 }
00266 }
00267
00268 public void testShort() {
00269
00270 final short[] DATA = {
00271 Short.MIN_VALUE, Short.MIN_VALUE + 1,
00272 Byte.MIN_VALUE, Byte.MIN_VALUE + 1,
00273 -1, 0, 1,
00274 Byte.MAX_VALUE - 1, Byte.MAX_VALUE,
00275 Short.MAX_VALUE - 1, Short.MAX_VALUE,
00276 };
00277 for (int i = 0; i < DATA.length; i += 1) {
00278 out.writeShort(DATA[i]);
00279 check(i);
00280 }
00281 }
00282
00283 public void testInt() {
00284
00285 final int[] DATA = {
00286 Integer.MIN_VALUE, Integer.MIN_VALUE + 1,
00287 Short.MIN_VALUE, Short.MIN_VALUE + 1,
00288 Byte.MIN_VALUE, Byte.MIN_VALUE + 1,
00289 -1, 0, 1,
00290 Byte.MAX_VALUE - 1, Byte.MAX_VALUE,
00291 Short.MAX_VALUE - 1, Short.MAX_VALUE,
00292 Integer.MAX_VALUE - 1, Integer.MAX_VALUE,
00293 };
00294 for (int i = 0; i < DATA.length; i += 1) {
00295 out.writeInt(DATA[i]);
00296 check(i);
00297 }
00298 }
00299
00300 public void testLong() {
00301
00302 final long[] DATA = {
00303 Long.MIN_VALUE, Long.MIN_VALUE + 1,
00304 Integer.MIN_VALUE, Integer.MIN_VALUE + 1,
00305 Short.MIN_VALUE, Short.MIN_VALUE + 1,
00306 Byte.MIN_VALUE, Byte.MIN_VALUE + 1,
00307 -1, 0, 1,
00308 Byte.MAX_VALUE - 1, Byte.MAX_VALUE,
00309 Short.MAX_VALUE - 1, Short.MAX_VALUE,
00310 Integer.MAX_VALUE - 1, Integer.MAX_VALUE,
00311 Long.MAX_VALUE - 1, Long.MAX_VALUE,
00312 };
00313 for (int i = 0; i < DATA.length; i += 1) {
00314 out.writeLong(DATA[i]);
00315 check(i);
00316 }
00317 }
00318
00319 public void testFloat() {
00320
00321
00322
00323 final float[] DATA = {
00324 0, (float) 0.01, (float) 0.02, (float) 0.99,
00325 1, (float) 1.01, (float) 1.02, (float) 1.99,
00326 Byte.MAX_VALUE - 1, Byte.MAX_VALUE,
00327 Short.MAX_VALUE - 1, Short.MAX_VALUE,
00328 Integer.MAX_VALUE,
00329 Long.MAX_VALUE / 2, Long.MAX_VALUE,
00330 Float.MAX_VALUE,
00331 };
00332 for (int i = 0; i < DATA.length; i += 1) {
00333 out.writeFloat(DATA[i]);
00334 check(i);
00335 }
00336 }
00337
00338 public void testDouble() {
00339
00340
00341
00342 final double[] DATA = {
00343 0, 0.001, 0.002, 0.999,
00344 1, 1.001, 1.002, 1.999,
00345 Byte.MAX_VALUE - 1, Byte.MAX_VALUE,
00346 Short.MAX_VALUE - 1, Short.MAX_VALUE,
00347 Integer.MAX_VALUE - 1, Integer.MAX_VALUE,
00348 Long.MAX_VALUE / 2, Long.MAX_VALUE,
00349 Float.MAX_VALUE, Double.MAX_VALUE,
00350 };
00351 for (int i = 0; i < DATA.length; i += 1) {
00352 out.writeDouble(DATA[i]);
00353 check(i);
00354 }
00355 }
00356 }