Welcome to mirror list, hosted at ThFree Co, Russian Federation.

SearchableBaseNoteFragmentTest.java « fragment « android « notes « owncloud « niedermann « it « java « test « src « app - github.com/stefan-niedermann/nextcloud-notes.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 68912d0366ef2f69af2803a8d71ad662e9f585a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package it.niedermann.owncloud.notes.android.fragment;

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 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");
                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 Count Occurrences Fixed", 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()));
            Log.e("Test Count Occurrences Random", Arrays.toString(e.getStackTrace()));
        }
    }

    @Test
    public void testNullOrEmptyInput() {
        try {
            Method method = SearchableBaseNoteFragment.class.getDeclaredMethod("countOccurrences", String.class, String.class);
            method.setAccessible(true);

            int num;
            num = (int) method.invoke(null, null, "Hi");
            assertEquals(0, num);
            num = (int) method.invoke(null, "Hi my name is Mike Chester Wang", null);
            assertEquals(0, num);
            num = (int) method.invoke(null, "", "Hi");
            assertEquals(0, num);
            num = (int) method.invoke(null, "Hi my name is Mike Chester Wang", "");
            assertEquals(0, num);

        } catch (Exception e) {
            fail(Arrays.toString(e.getStackTrace()));
            Log.e("Test Null Or Empty Input", Arrays.toString(e.getStackTrace()));
        }
    }
}