CatRank.setupRequest

This commit is contained in:
David Renshaw 2014-06-24 10:38:30 -04:00
parent b5168b17e5
commit 9203047327
3 changed files with 47 additions and 3 deletions

View file

@ -27,7 +27,7 @@ public class CatRank
}
static final String URL_PREFIX = "http://example.com";
static final Text.Reader URL_PREFIX = new Text.Reader("http://example.com");
public Integer setupRequest(Common.FastRand rng, SearchResultList.Builder request) {
int count = rng.nextLessThan(1000);
@ -38,9 +38,39 @@ public class CatRank
SearchResult.Builder result = list.get(i);
result.setScore(1000.0 - (double)i);
int urlSize = rng.nextLessThan(100);
int urlPrefixLength = URL_PREFIX.size();
StringBuilder url = new StringBuilder();
url.append(URL_PREFIX);
for (int j = 0; j < urlSize; j++) {
url.append('a' + rng.nextLessThan(26));
}
// ...
result.setUrl(new Text.Reader(url.toString()));
boolean isCat = rng.nextLessThan(8) == 0;
boolean isDog = rng.nextLessThan(8) == 0;
if (isCat && !isDog) {
goodCount += 1;
}
StringBuilder snippet = new StringBuilder(" ");
int prefix = rng.nextLessThan(20);
for (int j = 0; j < prefix; ++j) {
snippet.append(Common.WORDS[rng.nextLessThan(Common.WORDS.length)]);
}
if (isCat) { snippet.append("cat "); }
if (isDog) { snippet.append("dog "); }
int suffix = rng.nextLessThan(20);
for (int j = 0; j < suffix; ++j) {
snippet.append(Common.WORDS[rng.nextLessThan(Common.WORDS.length)]);
}
result.setSnippet(new Text.Reader(snippet.toString()));
}
return goodCount;
}
@ -83,4 +113,9 @@ public class CatRank
return goodCount == expectedGoodCount;
}
public static void main(String[] args) {
CatRank testCase = new CatRank();
testCase.execute(args, SearchResultList.factory, SearchResultList.factory);
}
}

View file

@ -43,5 +43,10 @@ public class Common {
}
public static String[] WORDS = {
"foo ", "bar ", "baz ", "qux ", "quux ", "corge ", "grault ", "garply ", "waldo ", "fred ",
"plugh ", "xyzzy ", "thud "
};
}

View file

@ -26,6 +26,10 @@ public final class Text {
}
}
public final int size() {
return this.size;
}
@Override
public final String toString() {
byte[] bytes = new byte[this.size];