JavaAlgorithms
Elementary and no so elementary Java algorithms
|
00001 00009 package sort; 00010 00011 import static org.junit.Assert.assertFalse; 00012 import static org.junit.Assert.assertTrue; 00013 import static org.junit.Assert.fail; 00014 00015 import java.io.BufferedInputStream; 00016 import java.io.BufferedOutputStream; 00017 import java.io.File; 00018 import java.io.FileInputStream; 00019 import java.io.FileOutputStream; 00020 import java.io.IOException; 00021 import java.io.ObjectInputStream; 00022 import java.io.ObjectOutputStream; 00023 import java.io.Serializable; 00024 import java.util.ArrayList; 00025 import java.util.Random; 00026 00027 import org.junit.Test; 00028 00037 public class TestMergeSortFile { 00038 // A bad solution, since the path is system dependent. 00039 private final static String dirPath = "/home/iank/tmp"; 00040 00050 public static class TestObj implements Comparable<TestObj>, Serializable { 00051 private static final long serialVersionUID = 1; 00052 final String key; 00053 final int val; 00054 00055 public TestObj(final String key, final int val) { 00056 this.key = key; 00057 this.val = val; 00058 } 00059 00060 @Override 00061 public int compareTo(TestObj o) { 00062 TestObj other = (TestObj) o; 00063 int compareRslt = this.key.compareTo(other.key); 00064 return compareRslt; 00065 } 00066 } 00067 00068 protected <T extends Comparable<T> & Serializable> void writeObjs(T[] objs, File outFile) throws IOException { 00069 FileOutputStream fileStr = null; 00070 BufferedOutputStream bufStr = null; 00071 ObjectOutputStream objOutStr = null; 00072 try { 00073 fileStr = new FileOutputStream(outFile); 00074 bufStr = new BufferedOutputStream(fileStr); 00075 objOutStr = new ObjectOutputStream(bufStr); 00076 for (T obj : objs) { 00077 objOutStr.writeObject(obj); 00078 } 00079 objOutStr.flush(); 00080 } finally { 00081 try { 00082 if (fileStr != null) { 00083 fileStr.close(); 00084 } 00085 if (bufStr != null) { 00086 bufStr.close(); 00087 } 00088 if (objOutStr != null) { 00089 objOutStr.close(); 00090 } 00091 } catch (IOException e) { 00092 } 00093 } 00094 } 00095 00096 protected <T extends Comparable<T> & Serializable> boolean testFile(File sortedFile) 00097 throws IOException, ClassNotFoundException { 00098 boolean fileSorted = false; 00099 FileInputStream fileStr = null; 00100 BufferedInputStream bufStr = null; 00101 ObjectInputStream objStr = null; 00102 try { 00103 fileStr = new FileInputStream(sortedFile); 00104 bufStr = new BufferedInputStream(fileStr); 00105 objStr = new ObjectInputStream(bufStr); 00106 ArrayList<T> objArray = new ArrayList<T>(); 00107 while (bufStr.available() > 0) { 00108 T obj = (T) objStr.readObject(); 00109 objArray.add(obj); 00110 } 00111 00112 int num = objArray.size(); 00113 fileSorted = true; 00114 if (num > 1) { 00115 for (int i = 0; i < num - 1; i++) { 00116 if (objArray.get(i).compareTo(objArray.get(i + 1)) > 0) { 00117 fileSorted = false; 00118 break; 00119 } 00120 } 00121 } 00122 } finally { 00123 try { 00124 if (fileStr != null) { 00125 fileStr.close(); 00126 } 00127 if (bufStr != null) { 00128 bufStr.close(); 00129 } 00130 if (objStr != null) { 00131 objStr.close(); 00132 } 00133 } catch (IOException e) { 00134 } 00135 } 00136 return fileSorted; 00137 } // testFile 00138 00139 protected void cleanupFiles(File inFile, File outFile) { 00140 if (inFile.exists()) { 00141 inFile.delete(); 00142 } 00143 if (outFile.exists()) { 00144 outFile.delete(); 00145 } 00146 } 00147 00155 public boolean testSortFile(final int numVals, final int bufSize) { 00156 final String inFileName = "randFile"; 00157 final String outFileName = "sortedFile"; 00158 final int seed = 127; 00159 00160 boolean rslt = false; 00161 Random rand = new Random(seed); 00162 Integer[] valArray = new Integer[numVals]; 00163 for (int i = 0; i < numVals; i++) { 00164 valArray[i] = new Integer(rand.nextInt()); 00165 } 00166 File randFile = new File(dirPath, inFileName); 00167 File outFile = new File(dirPath, outFileName); 00168 try { 00169 writeObjs(valArray, randFile); 00170 MergeSortFile<Integer> fileSort = new MergeSortFile<Integer>(bufSize); 00171 fileSort.sortFile(randFile, outFile); 00172 rslt = this.<Integer> testFile(outFile); 00173 } catch (IOException | ClassNotFoundException e) { 00174 System.err.println(e.getLocalizedMessage()); 00175 fail(e.getLocalizedMessage()); 00176 } finally { 00177 cleanupFiles(randFile, outFile); 00178 } 00179 return rslt; 00180 } 00181 00185 @Test 00186 public void testTheTester() { 00187 final String inFileName = "randFile"; 00188 final int seed = 127; 00189 final int numVals = 133; 00190 00191 Random rand = new Random(seed); 00192 Integer[] valArray = new Integer[numVals]; 00193 for (int i = 0; i < numVals; i++) { 00194 valArray[i] = new Integer(rand.nextInt()); 00195 } 00196 File randFile = new File(dirPath, inFileName); 00197 try { 00198 this.writeObjs(valArray, randFile); 00199 assertFalse("The file check does not work correctly", 00200 this.<Integer> testFile(randFile)); 00201 } catch (IOException | ClassNotFoundException e) { 00202 fail("testTheTester Error: " + e.getLocalizedMessage()); 00203 } finally { 00204 if (randFile.exists()) { 00205 randFile.delete(); 00206 } 00207 } 00208 } 00209 00214 @Test 00215 public void testEven() { 00216 assertTrue("Failed for the case where numVals mod bufSize == 0", 00217 testSortFile(90, 30)); 00218 } 00219 00220 @Test 00221 public void testOdd() { 00222 assertTrue("Failed for the case where numVals mod bufSize != 0", 00223 testSortFile(111, 30)); 00224 } 00225 00226 @Test 00227 public void testValsEqBuf() { 00228 assertTrue( 00229 "Failed for the case where the number of values is the same as the buffer", 00230 testSortFile(1000, 1000)); 00231 } 00232 00238 @Test 00239 public void testObjectSort() { 00240 final int seed = 127; 00241 final int bufSize = 1024; 00242 final int numObj = 3 * bufSize; 00243 final String inFileName = "randFile"; 00244 final String outFileName = "sortedFile"; 00245 00246 TestObj[] objVec = new TestObj[numObj]; 00247 00248 Random rand = new Random(seed); 00249 for (int i = 0; i < numObj; i++) { 00250 int randVal = rand.nextInt(); 00251 String key = Integer.toString(randVal); 00252 objVec[i] = new TestObj(key, randVal); 00253 } 00254 File randFile = new File(dirPath, inFileName); 00255 File outFile = new File(dirPath, outFileName); 00256 try { 00257 writeObjs(objVec, randFile); 00258 MergeSortFile<TestObj> fileSort = new MergeSortFile<TestObj>( 00259 bufSize); 00260 fileSort.sortFile(randFile, outFile); 00261 boolean rslt = this.<TestObj> testFile(outFile); 00262 assertTrue("Object test failed", rslt); 00263 } catch (IOException | ClassNotFoundException e) { 00264 System.err.println(e.getLocalizedMessage()); 00265 fail(e.getLocalizedMessage()); 00266 } finally { 00267 cleanupFiles(randFile, outFile); 00268 } 00269 } 00270 00271 }