Return-Path: Delivered-To: apmail-lucene-mahout-commits-archive@minotaur.apache.org Received: (qmail 58779 invoked from network); 11 Dec 2009 19:22:40 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 11 Dec 2009 19:22:40 -0000 Received: (qmail 91035 invoked by uid 500); 11 Dec 2009 19:22:40 -0000 Delivered-To: apmail-lucene-mahout-commits-archive@lucene.apache.org Received: (qmail 90953 invoked by uid 500); 11 Dec 2009 19:22:40 -0000 Mailing-List: contact mahout-commits-help@lucene.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: mahout-dev@lucene.apache.org Delivered-To: mailing list mahout-commits@lucene.apache.org Received: (qmail 90944 invoked by uid 99); 11 Dec 2009 19:22:40 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 11 Dec 2009 19:22:40 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 11 Dec 2009 19:22:38 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 6D89D23888E7; Fri, 11 Dec 2009 19:22:17 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r889777 - /lucene/mahout/trunk/matrix/src/test/java/org/apache/mahout/matrix/GenericSortingTest.java Date: Fri, 11 Dec 2009 19:22:17 -0000 To: mahout-commits@lucene.apache.org From: srowen@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20091211192217.6D89D23888E7@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: srowen Date: Fri Dec 11 19:22:16 2009 New Revision: 889777 URL: http://svn.apache.org/viewvc?rev=889777&view=rev Log: MAHOUT-219 Added: lucene/mahout/trunk/matrix/src/test/java/org/apache/mahout/matrix/GenericSortingTest.java Added: lucene/mahout/trunk/matrix/src/test/java/org/apache/mahout/matrix/GenericSortingTest.java URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/matrix/src/test/java/org/apache/mahout/matrix/GenericSortingTest.java?rev=889777&view=auto ============================================================================== --- lucene/mahout/trunk/matrix/src/test/java/org/apache/mahout/matrix/GenericSortingTest.java (added) +++ lucene/mahout/trunk/matrix/src/test/java/org/apache/mahout/matrix/GenericSortingTest.java Fri Dec 11 19:22:16 2009 @@ -0,0 +1,97 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.mahout.matrix; + +import org.apache.mahout.matrix.function.IntComparator; +import org.junit.Assert; +import org.junit.Test; + +public class GenericSortingTest extends Assert { + + private static class SomethingToSort implements Swapper, IntComparator { + private final int[] data; + + private SomethingToSort(int[] data) { + this.data = data; + } + + @Override + public void swap(int a, int b) { + int temp = data[a]; + data[a] = data[b]; + data[b] = temp; + } + + @Override + public int compare(int o1, int o2) { + if (data[o1] < data[o2]) { + return -1; + } else if (data[o1] > data[o2]) { + return 1; + } else { + return 0; + } + } + } + + @Test + public void testQuickSort() { + int[] td = new int[20]; + for (int x = 0; x < 20; x ++) { + td[x] = 20 - x; + } + SomethingToSort sts = new SomethingToSort(td); + GenericSorting.quickSort(0, 20, sts, sts); + for (int x = 0; x < 20; x ++) { + assertEquals(x+1, td[x]); + } + } + + private static class SomethingToSortStable implements Swapper, IntComparator { + private final String[] data; + + private SomethingToSortStable(String[] data) { + this.data = data; + } + + @Override + public void swap(int a, int b) { + String temp = data[a]; + data[a] = data[b]; + data[b] = temp; + } + + @Override + public int compare(int o1, int o2) { + return data[o1].compareTo(data[o2]); + } + } + + @Test + public void testMergeSort() { + String[] sd = {"z", "a", "a", "q", "1"}; + String[] correct = {"1", "a", "a", "q", "z"}; + + SomethingToSortStable sts = new SomethingToSortStable(sd); + GenericSorting.mergeSort(0, 5, sts, sts); + + for (int x = 0; x < 5; x ++) { + assertSame(correct[x], sd[x]); + } + } +}