diff --git a/app/src/androidTest/java/it/niedermann/owncloud/notes/persistence/NotesDatabaseTest.java b/app/src/androidTest/java/it/niedermann/owncloud/notes/persistence/NotesDatabaseTest.java index 8f3ce28f..4bdd7c18 100644 --- a/app/src/androidTest/java/it/niedermann/owncloud/notes/persistence/NotesDatabaseTest.java +++ b/app/src/androidTest/java/it/niedermann/owncloud/notes/persistence/NotesDatabaseTest.java @@ -156,6 +156,7 @@ public class NotesDatabaseTest { } assertTrue(exitFlag); + // TODO: The second parameter is annotated as @NonNull. This test is invalid. Please remove it categories = db.searchCategories(account.getId(), null); exitFlag = false; for (NavigationAdapter.NavigationItem categoryItem : categories) { @@ -531,9 +532,18 @@ public class NotesDatabaseTest { @Test public void test_14_getCategoryIdsByTitle() { try { -// long noteID = db.addNote(account.getId(), new CloudNote(1, Calendar.getInstance(), -// "woc", getCurDate() + " woc nmd testing", -// true, "aha", null)); + long noteID = db.addNote(account.getId(), new CloudNote(1, Calendar.getInstance(), + "hello1", getCurDate() + " Mike you look so cool.", + true, "Diary", null)); + long noteID2 = db.addNote(account.getId(), new CloudNote(1, Calendar.getInstance(), + "hello2", getCurDate() + " I'm so cool.", + true, "Mike's Diary", null)); + long noteID3 = db.addNote(account.getId(), new CloudNote(1, Calendar.getInstance(), + "hello3", getCurDate() + " Why Mike so cool.", + true, "Andy's Diary", null)); + long noteID4 = db.addNote(account.getId(), new CloudNote(1, Calendar.getInstance(), + "hello4", getCurDate() + " no person cooler than Mike.", + true, "Peter's Diary", null)); Method method_ids_by_title = NotesDatabase.class.getDeclaredMethod("getCategoryIdsByTitle", long.class, String.class); method_ids_by_title.setAccessible(true); @@ -544,29 +554,48 @@ public class NotesDatabaseTest { List categories = db.getCategories(account.getId()); Log.i("Test_14_getCategoryIdsByTitle", "size: " + categories.size()); // Log.i("Test_14_getCategoryIdsByTitle", "item: "+ categories.get(0).label); - String pattern = "Dia"; + String pattern = "'s Dia"; List matchList = new ArrayList<>(); for (NavigationAdapter.NavigationItem categoryItem : categories) { String catTitle = categoryItem.label; + if (pattern.length() > catTitle.length()) { + continue; + } Log.i("Test_14_getCategoryIdsByTitle", catTitle); - if (catTitle.contains(pattern)) { + boolean contain = true; + for (int i = 0; i < pattern.length(); i++) { + if (pattern.charAt(i) != catTitle.charAt(i)) { + contain = false; + } + } + if (contain) { + Log.e("###", catTitle); // She will be matched, store ID int catId = (int) method_id_by_title.invoke(db, account.getId(), catTitle, false); matchList.add(catId); } +// if (catTitle.contains(pattern)) { +// // She will be matched, store ID +// int catId = (int) method_id_by_title.invoke(db, account.getId(), catTitle, false); +// matchList.add(catId); +// } } // Now use our testing method List testList = (List) method_ids_by_title.invoke(db, account.getId(), pattern); - + Log.e("###", testList.size() + ""); // Compare (Sort to make sure the generated strings are equal to each other) Collections.sort(matchList); Collections.sort(testList); Log.i("Test_14_getCategoryIdsByTitle", matchList.toString()); Log.i("Test_14_getCategoryIdsByTitle", testList.toString()); - assertEquals(matchList.toString(), testList.toString()); -// db.deleteNote(noteID, DBStatus.VOID); + db.deleteNote(noteID, DBStatus.VOID); + db.deleteNote(noteID2, DBStatus.VOID); + db.deleteNote(noteID3, DBStatus.VOID); + db.deleteNote(noteID4, DBStatus.VOID); + + assertEquals(matchList.toString(), testList.toString()); } catch (Exception e) { fail(Arrays.toString(e.getStackTrace())); Log.e("Test_14_getCategoryIdsByTitle", Arrays.toString(e.getStackTrace())); diff --git a/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesDatabase.java b/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesDatabase.java index ad4c32af..61ae200d 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesDatabase.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesDatabase.java @@ -379,6 +379,7 @@ public class NotesDatabase extends AbstractNotesDatabase { // TODO: test @NonNull @WorkerThread + // the categories without note will not be returned public List getCategories(long accountId) { validateAccountId(accountId); String category_title = String.format("%s.%s", table_category, key_title); @@ -386,17 +387,9 @@ public class NotesDatabase extends AbstractNotesDatabase { String category_id = String.format("%s.%s", table_category, key_id); String category_accountId = String.format("%s.%s", table_category, key_account_id); String note_status = String.format("%s.%s", table_notes, key_status); - // Weird problem: If I use ? instead of concat directly, there is no result in the join table - // TODO: Find a way to use ? instead of concat. - // TODO: a bug here - // when no notes and have cat, inner join will not return this cat String rawQuery = "SELECT " + category_title + ", COUNT(*) FROM " + table_category + " INNER JOIN " + table_notes + " ON " + category_id + " = " + note_title + " WHERE " + category_accountId + " = ? AND " + note_status + " != ? GROUP BY " + category_id; -// String rawQuery = "SELECT " + category_title + ", COUNT(*) FROM " + table_category + " INNER JOIN " + table_notes + -// " ON " + category_id + " = " + note_title + " WHERE " + category_accountId + " = " + accountId + -// " GROUP BY " + category_id; - Cursor cursor = getReadableDatabase().rawQuery(rawQuery, new String[]{String.valueOf(accountId), DBStatus.LOCAL_DELETED.getTitle()}); List categories = new ArrayList<>(cursor.getCount()); while (cursor.moveToNext()) { @@ -918,13 +911,14 @@ public class NotesDatabase extends AbstractNotesDatabase { } // TODO: test + // TODO: not fuzzy search by testing bug? private List getCategoryIdsByTitle(long accountId, @NonNull String title) { validateAccountId(accountId); Cursor cursor = getReadableDatabase().query( table_category, new String[]{key_id}, key_title + " = ? OR " + key_title + " LIKE ? AND " + key_account_id + " = ? ", - new String[]{title, title + "/%", String.valueOf(accountId)}, + new String[]{title, title + "%", String.valueOf(accountId)}, key_id, null, key_id diff --git a/app/src/test/java/it/niedermann/owncloud/notes/android/fragment/SearchableBaseNoteFragmentTest.java b/app/src/test/java/it/niedermann/owncloud/notes/android/fragment/SearchableBaseNoteFragmentTest.java index 22881042..e5e7772a 100644 --- a/app/src/test/java/it/niedermann/owncloud/notes/android/fragment/SearchableBaseNoteFragmentTest.java +++ b/app/src/test/java/it/niedermann/owncloud/notes/android/fragment/SearchableBaseNoteFragmentTest.java @@ -1,24 +1,80 @@ package it.niedermann.owncloud.notes.android.fragment; -import it.niedermann.owncloud.notes.android.fragment.SearchableBaseNoteFragment; - import android.util.Log; import org.junit.Test; import java.lang.reflect.Method; import java.util.Arrays; +import java.util.Random; import static org.junit.Assert.*; public class SearchableBaseNoteFragmentTest { + @Test - public void testCountOccurrences() { + public void testCountOccurrencesFixed() { try { Method method = SearchableBaseNoteFragment.class.getDeclaredMethod("countOccurrences", String.class, String.class); method.setAccessible(true); + for (int count = 0; count <= 15; ++count) { + StringBuilder sb = new StringBuilder("Mike Chester Wang"); + Random rand = new Random(); + for (int i = 0; i < count; ++i) { + sb.append(sb); + } + long startTime = System.currentTimeMillis(); + int num = (int) method.invoke(null, sb.toString(), "Chester"); + long endTime = System.currentTimeMillis(); + System.out.println("Fixed Version"); + System.out.println("Total Time: " + (endTime - startTime) + " ms"); + System.out.println("Total Times: " + num); + System.out.println("String Size: " + (sb.length() / 1024) + " K"); + assertEquals((int) Math.pow(2, count), num); + System.out.println(); + + if (endTime - startTime > 10) { + fail("The algorithm spends too much time."); + } + } + + } catch (Exception e) { + fail(Arrays.toString(e.getStackTrace())); + Log.e("Test_12_getCategoryIdByTitle", Arrays.toString(e.getStackTrace())); + } + } + + @Test + public void testCountOccurrencesRandom() { + try { + Method method = SearchableBaseNoteFragment.class.getDeclaredMethod("countOccurrences", String.class, String.class); + method.setAccessible(true); + + for (int count = 10; count <= 15; ++count) { + StringBuilder sb = new StringBuilder("Mike Chester Wang"); + Random rand = new Random(); + for (int i = 0; i < count * 100; ++i) { + sb.append(rand.nextDouble()); + if (i % 100 == 0) { + sb.append("flag"); + } + } + + long startTime = System.currentTimeMillis(); + int num = (int) method.invoke(null, sb.toString(), String.valueOf(rand.nextInt(100))); + long endTime = System.currentTimeMillis(); + System.out.println("Random Version"); + System.out.println("Total Time: " + (endTime - startTime) + " ms"); + System.out.println("Total Times: " + num); + System.out.println("String Size: " + (sb.length() / 1024) + " K"); + System.out.println(); + + if (endTime - startTime > 10) { + fail("The algorithm spends too much time."); + } + } } catch (Exception e) { fail(Arrays.toString(e.getStackTrace()));