blob: 2df91047cdf83e418c67e14b3e5052875ea4d3f3 [file] [log] [blame]
Chris Craik4cbf5ef2015-09-01 15:30:57 -07001/*
2 * Copyright (C) 2015 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 */
16package com.android.test.uibench;
17
Seigo Nonaka47bc9482017-06-16 09:56:18 -070018import android.icu.text.UnicodeSet;
19import android.icu.text.UnicodeSetIterator;
20
21import java.util.ArrayList;
Chris Craik4cbf5ef2015-09-01 15:30:57 -070022import java.util.Random;
23
24public class TextUtils {
25 private static final int STRING_COUNT = 200;
Seigo Nonaka47bc9482017-06-16 09:56:18 -070026 private static final int SIMPLE_STRING_LENGTH = 10; // in code points
27
28 private static String[] UnicodeSetToArray(UnicodeSet set) {
29 final UnicodeSetIterator iterator = new UnicodeSetIterator(set);
30 final ArrayList<String> out = new ArrayList<>(set.size());
31 while (iterator.next()) {
32 out.add(iterator.getString());
33 }
34 return out.toArray(new String[out.size()]);
35 }
Chris Craik4cbf5ef2015-09-01 15:30:57 -070036
37 /**
38 * Create word of random assortment of lower/upper case letters
39 */
40 private static String randomWord(Random random, int length) {
41 String result = "";
42 for (int j = 0; j < length; j++) {
43 // add random letter
44 int base = random.nextInt(2) == 0 ? 'A' : 'a';
45 result += (char)(random.nextInt(26) + base);
46 }
47 return result;
48 }
49
Seigo Nonaka47bc9482017-06-16 09:56:18 -070050 /**
51 * Create word from a random assortment of a given set of codepoints, given as strings.
52 */
53 private static String randomWordFromStringSet(Random random, int length, String[] stringSet) {
54 final StringBuilder sb = new StringBuilder(length);
55 final int setLength = stringSet.length;
56 for (int j = 0; j < length; j++) {
57 sb.append(stringSet[random.nextInt(setLength)]);
58 }
59 return sb.toString();
60 }
61
Chris Craik4cbf5ef2015-09-01 15:30:57 -070062 public static String[] buildSimpleStringList() {
Stan Ilievbf256272017-01-26 13:26:08 -050063 return buildSimpleStringList(SIMPLE_STRING_LENGTH);
64 }
65
Seigo Nonaka47bc9482017-06-16 09:56:18 -070066 public static String[] buildEmojiStringList() {
67 return buildEmojiStringList(SIMPLE_STRING_LENGTH);
68 }
69
70 public static String[] buildHanStringList() {
71 return buildHanStringList(SIMPLE_STRING_LENGTH);
72 }
73
74 public static String[] buildLongStringList() {
75 return buildLongStringList(SIMPLE_STRING_LENGTH);
76 }
77
Stan Ilievbf256272017-01-26 13:26:08 -050078 public static String[] buildSimpleStringList(int stringLength) {
Chris Craik4cbf5ef2015-09-01 15:30:57 -070079 String[] strings = new String[STRING_COUNT];
80 Random random = new Random(0);
81 for (int i = 0; i < strings.length; i++) {
Stan Ilievbf256272017-01-26 13:26:08 -050082 strings[i] = randomWord(random, stringLength);
Chris Craik4cbf5ef2015-09-01 15:30:57 -070083 }
84 return strings;
85 }
86
Seigo Nonaka47bc9482017-06-16 09:56:18 -070087 private static String[] buildStringListFromUnicodeSet(int stringLength, UnicodeSet set) {
88 final String[] strings = new String[STRING_COUNT];
89 final Random random = new Random(0);
90 final String[] stringSet = UnicodeSetToArray(set);
91 for (int i = 0; i < strings.length; i++) {
92 strings[i] = randomWordFromStringSet(random, stringLength, stringSet);
93 }
94 return strings;
95 }
96
97 public static String[] buildEmojiStringList(int stringLength) {
98 return buildStringListFromUnicodeSet(stringLength, new UnicodeSet("[:emoji:]"));
99 }
100
101 public static String[] buildHanStringList(int stringLength) {
102 return buildStringListFromUnicodeSet(stringLength, new UnicodeSet("[\\u4E00-\\u9FA0]"));
103 }
104
105 public static String[] buildLongStringList(int stringLength) {
106 final int WORD_COUNT = 100;
107 final String[] strings = new String[STRING_COUNT];
108 final Random random = new Random(0);
109 for (int i = 0; i < strings.length; i++) {
110 final StringBuilder sb = new StringBuilder((stringLength + 1) * WORD_COUNT);
111 for (int j = 0; j < WORD_COUNT; ++j) {
112 if (j != 0) {
113 sb.append(' ');
114 }
115 sb.append(randomWord(random, stringLength));
116 }
117 strings[i] = sb.toString();
118 }
119 return strings;
120 }
121
Chris Craik4cbf5ef2015-09-01 15:30:57 -0700122 // a small number of strings reused frequently, expected to hit
123 // in the word-granularity text layout cache
124 static final String[] CACHE_HIT_STRINGS = new String[] {
125 "a",
126 "small",
127 "number",
128 "of",
129 "strings",
130 "reused",
131 "frequently"
132 };
133
134 private static final int WORDS_IN_PARAGRAPH = 150;
135
136 // misses are fairly long 'words' to ensure they miss
137 private static final int PARAGRAPH_MISS_MIN_LENGTH = 4;
138 private static final int PARAGRAPH_MISS_MAX_LENGTH = 9;
139
140 static String[] buildParagraphListWithHitPercentage(int hitPercentage) {
141 if (hitPercentage < 0 || hitPercentage > 100) throw new IllegalArgumentException();
142
143 String[] strings = new String[STRING_COUNT];
144 Random random = new Random(0);
145 for (int i = 0; i < strings.length; i++) {
146 String result = "";
147 for (int word = 0; word < WORDS_IN_PARAGRAPH; word++) {
148 if (word != 0) {
149 result += " ";
150 }
151 if (random.nextInt(100) < hitPercentage) {
152 // add a common word, which is very likely to hit in the cache
153 result += CACHE_HIT_STRINGS[random.nextInt(CACHE_HIT_STRINGS.length)];
154 } else {
155 // construct a random word, which will *most likely* miss
156 int length = PARAGRAPH_MISS_MIN_LENGTH;
157 length += random.nextInt(PARAGRAPH_MISS_MAX_LENGTH - PARAGRAPH_MISS_MIN_LENGTH);
158
159 result += randomWord(random, length);
160 }
161 }
162 strings[i] = result;
163 }
164
165 return strings;
166 }
167}