From 3a9ec2ce72f2c4d02674a9f34e49565599ef1255 Mon Sep 17 00:00:00 2001 From: "Brian S. O'Neill" Date: Sat, 14 Oct 2006 17:49:42 +0000 Subject: Added merge sort tests. --- .../carbonado/cursor/TestMergeSortBuffer.java | 190 +++++++++++++++++++++ 1 file changed, 190 insertions(+) create mode 100644 src/test/java/com/amazon/carbonado/cursor/TestMergeSortBuffer.java (limited to 'src/test/java/com/amazon/carbonado') diff --git a/src/test/java/com/amazon/carbonado/cursor/TestMergeSortBuffer.java b/src/test/java/com/amazon/carbonado/cursor/TestMergeSortBuffer.java new file mode 100644 index 0000000..7554e8a --- /dev/null +++ b/src/test/java/com/amazon/carbonado/cursor/TestMergeSortBuffer.java @@ -0,0 +1,190 @@ +/* + * Copyright 2006 Amazon Technologies, Inc. or its affiliates. + * Amazon, Amazon.com and Carbonado are trademarks or registered trademarks + * of Amazon Technologies, Inc. or its affiliates. All rights reserved. + * + * 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 com.amazon.carbonado.cursor; + +import java.util.Comparator; +import java.util.NoSuchElementException; +import java.util.Random; + +import junit.framework.TestCase; +import junit.framework.TestSuite; + +import org.cojen.util.BeanComparator; + +import com.amazon.carbonado.*; +import com.amazon.carbonado.stored.*; + +/** + * Test case for {@link MergeSortBuffer}. + * + * @author Brian S O'Neill + */ +public class TestMergeSortBuffer extends TestCase { + private static final int SMALL_BUFFER_SIZE = 10; + private static final int MEDIUM_BUFFER_SIZE = 500; + private static final int LARGE_BUFFER_SIZE = 20000; + private static final int HUGE_BUFFER_SIZE = 4000000; + + public static void main(String[] args) { + junit.textui.TestRunner.run(suite()); + } + + public static TestSuite suite() { + return new TestSuite(TestMergeSortBuffer.class); + } + + private final Comparator mComparator; + + private Repository mRepository; + + public TestMergeSortBuffer(String name) { + super(name); + mComparator = BeanComparator.forClass(StorableTestBasic.class) + .orderBy("stringProp") + .caseSensitive() + .orderBy("intProp"); + } + + protected void setUp() { + mRepository = TestUtilities.buildTempRepository(); + } + + protected void tearDown() { + mRepository.close(); + mRepository = null; + } + + public void testEmptyBuffer() throws Exception { + Storage storage = mRepository.storageFor(StorableTestBasic.class); + SortBuffer buffer = new MergeSortBuffer(storage); + buffer.prepare(mComparator); + + assertEquals(0, buffer.size()); + assertEquals(false, buffer.iterator().hasNext()); + try { + buffer.iterator().next(); + fail(); + } catch (NoSuchElementException e) { + } + + buffer.sort(); + + assertEquals(0, buffer.size()); + assertEquals(false, buffer.iterator().hasNext()); + try { + buffer.iterator().next(); + fail(); + } catch (NoSuchElementException e) { + } + + buffer.close(); + } + + public void testSmallBuffer() throws Exception { + testBuffer(SMALL_BUFFER_SIZE); + } + + public void testMedium() throws Exception { + testBuffer(MEDIUM_BUFFER_SIZE); + } + + public void testMediumOdd() throws Exception { + testBuffer(MEDIUM_BUFFER_SIZE + 13); + } + + public void testLarge() throws Exception { + testBuffer(LARGE_BUFFER_SIZE); + } + + public void testLargeOdd() throws Exception { + testBuffer(LARGE_BUFFER_SIZE + 13); + } + + public void testHuge() throws Exception { + testBuffer(HUGE_BUFFER_SIZE); + } + + private void testBuffer(int size) throws Exception { + Storage storage = mRepository.storageFor(StorableTestBasic.class); + SortBuffer buffer = new MergeSortBuffer(storage); + buffer.prepare(mComparator); + + final long seed = 345891237L; + + Random rnd = new Random(seed); + + for (int i=0; i buffer) { + int count = 0; + StorableTestBasic last = null; + for (StorableTestBasic stb : buffer) { + if (last != null) { + int comparatorResult = mComparator.compare(last, stb); + if (comparatorResult > 0) { + System.out.println(last); + System.out.println(stb); + System.out.println(count); + } + assertTrue(comparatorResult <= 0); + } + last = stb; + count++; + } + assertEquals(buffer.size(), count); + } + + private StorableTestBasic generateRandomStorable(Storage storage, + Random rnd) + { + StorableTestBasic stb = storage.prepare(); + stb.setId(rnd.nextInt()); + stb.setStringProp(TestUtilities.sRandomText(0, 100, rnd)); + stb.setIntProp(rnd.nextInt()); + stb.setLongProp(rnd.nextLong()); + stb.setDoubleProp(rnd.nextDouble()); + return stb; + } +} -- cgit v1.2.3