1 /* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.inputmethod.latin.makedict; 18 19 import android.test.AndroidTestCase; 20 import android.test.suitebuilder.annotation.LargeTest; 21 import android.util.Log; 22 23 import java.io.File; 24 import java.io.FileOutputStream; 25 import java.io.IOException; 26 import java.io.OutputStream; 27 import java.util.ArrayList; 28 import java.util.Random; 29 30 /** 31 * Unit tests for SparseTable. 32 */ 33 @LargeTest 34 public class SparseTableTests extends AndroidTestCase { 35 private static final String TAG = SparseTableTests.class.getSimpleName(); 36 37 private final Random mRandom; 38 private final ArrayList<Integer> mRandomIndex; 39 40 private static final int DEFAULT_SIZE = 10000; 41 private static final int BLOCK_SIZE = 8; 42 43 public SparseTableTests() { 44 this(System.currentTimeMillis(), DEFAULT_SIZE); 45 } 46 47 public SparseTableTests(final long seed, final int tableSize) { 48 super(); 49 Log.d(TAG, "Seed for test is " + seed + ", size is " + tableSize); 50 mRandom = new Random(seed); 51 mRandomIndex = new ArrayList<Integer>(tableSize); 52 for (int i = 0; i < tableSize; ++i) { 53 mRandomIndex.add(SparseTable.NOT_EXIST); 54 } 55 } 56 57 public void testSet() { 58 final SparseTable table = new SparseTable(16, BLOCK_SIZE, 1); 59 table.set(0, 3, 6); 60 table.set(0, 8, 16); 61 for (int i = 0; i < 16; ++i) { 62 if (i == 3 || i == 8) { 63 assertEquals(i * 2, table.get(0, i)); 64 } else { 65 assertEquals(SparseTable.NOT_EXIST, table.get(0, i)); 66 } 67 } 68 } 69 70 private void generateRandomIndex(final int size, final int prop) { 71 for (int i = 0; i < size; ++i) { 72 if (mRandom.nextInt(100) < prop) { 73 mRandomIndex.set(i, mRandom.nextInt()); 74 } else { 75 mRandomIndex.set(i, SparseTable.NOT_EXIST); 76 } 77 } 78 } 79 80 private void runTestRandomSet() { 81 final SparseTable table = new SparseTable(DEFAULT_SIZE, BLOCK_SIZE, 1); 82 int elementCount = 0; 83 for (int i = 0; i < DEFAULT_SIZE; ++i) { 84 if (mRandomIndex.get(i) != SparseTable.NOT_EXIST) { 85 table.set(0, i, mRandomIndex.get(i)); 86 elementCount++; 87 } 88 } 89 90 Log.d(TAG, "table size = " + table.getLookupTableSize() + " + " 91 + table.getContentTableSize()); 92 Log.d(TAG, "the table has " + elementCount + " elements"); 93 for (int i = 0; i < DEFAULT_SIZE; ++i) { 94 assertEquals(table.get(0, i), (int)mRandomIndex.get(i)); 95 } 96 97 // flush and reload 98 OutputStream lookupOutStream = null; 99 OutputStream contentOutStream = null; 100 try { 101 final File lookupIndexFile = File.createTempFile("testRandomSet", ".small"); 102 final File contentFile = File.createTempFile("testRandomSet", ".big"); 103 lookupOutStream = new FileOutputStream(lookupIndexFile); 104 contentOutStream = new FileOutputStream(contentFile); 105 table.write(lookupOutStream, new OutputStream[] { contentOutStream }); 106 lookupOutStream.flush(); 107 contentOutStream.flush(); 108 final SparseTable newTable = SparseTable.readFromFiles(lookupIndexFile, 109 new File[] { contentFile }, BLOCK_SIZE); 110 for (int i = 0; i < DEFAULT_SIZE; ++i) { 111 assertEquals(table.get(0, i), newTable.get(0, i)); 112 } 113 } catch (IOException e) { 114 Log.d(TAG, "IOException while flushing and realoding", e); 115 } finally { 116 if (lookupOutStream != null) { 117 try { 118 lookupOutStream.close(); 119 } catch (IOException e) { 120 Log.d(TAG, "IOException while closing the stream", e); 121 } 122 } 123 if (contentOutStream != null) { 124 try { 125 contentOutStream.close(); 126 } catch (IOException e) { 127 Log.d(TAG, "IOException while closing contentStream.", e); 128 } 129 } 130 } 131 } 132 133 public void testRandomSet() { 134 for (int i = 0; i <= 100; i += 10) { 135 generateRandomIndex(DEFAULT_SIZE, i); 136 runTestRandomSet(); 137 } 138 } 139 140 public void testMultipleContents() { 141 final int numOfContents = 5; 142 generateRandomIndex(DEFAULT_SIZE, 20); 143 final SparseTable table = new SparseTable(DEFAULT_SIZE, BLOCK_SIZE, numOfContents); 144 for (int i = 0; i < mRandomIndex.size(); ++i) { 145 if (mRandomIndex.get(i) != SparseTable.NOT_EXIST) { 146 for (int j = 0; j < numOfContents; ++j) { 147 table.set(j, i, mRandomIndex.get(i)); 148 } 149 } 150 } 151 152 OutputStream lookupOutStream = null; 153 OutputStream[] contentsOutStream = new OutputStream[numOfContents]; 154 try { 155 final File lookupIndexFile = File.createTempFile("testMultipleContents", "small"); 156 lookupOutStream = new FileOutputStream(lookupIndexFile); 157 final File[] contentFiles = new File[numOfContents]; 158 for (int i = 0; i < numOfContents; ++i) { 159 contentFiles[i] = File.createTempFile("testMultipleContents", "big" + i); 160 contentsOutStream[i] = new FileOutputStream(contentFiles[i]); 161 } 162 table.write(lookupOutStream, contentsOutStream); 163 lookupOutStream.flush(); 164 for (int i = 0; i < numOfContents; ++i) { 165 contentsOutStream[i].flush(); 166 } 167 final SparseTable newTable = SparseTable.readFromFiles(lookupIndexFile, contentFiles, 168 BLOCK_SIZE); 169 for (int i = 0; i < numOfContents; ++i) { 170 for (int j = 0; j < DEFAULT_SIZE; ++j) { 171 assertEquals(table.get(i, j), newTable.get(i, j)); 172 } 173 } 174 } catch (IOException e) { 175 Log.d(TAG, "IOException while flushing and reloading", e); 176 } finally { 177 if (lookupOutStream != null) { 178 try { 179 lookupOutStream.close(); 180 } catch (IOException e) { 181 Log.d(TAG, "IOException while closing the stream", e); 182 } 183 } 184 for (int i = 0; i < numOfContents; ++i) { 185 if (contentsOutStream[i] != null) { 186 try { 187 contentsOutStream[i].close(); 188 } catch (IOException e) { 189 Log.d(TAG, "IOException while closing the stream.", e); 190 } 191 } 192 } 193 } 194 } 195 } 196