use of org.apache.lucene.util.BytesRefBuilder in project elasticsearch by elastic.
the class BytesRefHashTests method testSize.
// START - tests borrowed from LUCENE
/**
* Test method for {@link org.apache.lucene.util.BytesRefHash#size()}.
*/
public void testSize() {
BytesRefBuilder ref = new BytesRefBuilder();
int num = scaledRandomIntBetween(2, 20);
for (int j = 0; j < num; j++) {
final int mod = 1 + randomInt(40);
for (int i = 0; i < 797; i++) {
String str;
do {
str = TestUtil.randomRealisticUnicodeString(random(), 1000);
} while (str.length() == 0);
ref.copyChars(str);
long count = hash.size();
long key = hash.add(ref.get());
if (key < 0)
assertEquals(hash.size(), count);
else
assertEquals(hash.size(), count + 1);
if (i % mod == 0) {
newHash();
}
}
}
hash.close();
}
use of org.apache.lucene.util.BytesRefBuilder in project elasticsearch by elastic.
the class CollectionUtilsTests method testSortAndDedupByteRefArray.
public void testSortAndDedupByteRefArray() {
SortedSet<BytesRef> set = new TreeSet<>();
final int numValues = scaledRandomIntBetween(0, 10000);
List<BytesRef> tmpList = new ArrayList<>();
BytesRefArray array = new BytesRefArray(Counter.newCounter());
for (int i = 0; i < numValues; i++) {
String s = randomRealisticUnicodeOfCodepointLengthBetween(1, 100);
set.add(new BytesRef(s));
tmpList.add(new BytesRef(s));
array.append(new BytesRef(s));
}
if (randomBoolean()) {
Collections.shuffle(tmpList, random());
for (BytesRef ref : tmpList) {
array.append(ref);
}
}
int[] indices = new int[array.size()];
for (int i = 0; i < indices.length; i++) {
indices[i] = i;
}
int numUnique = CollectionUtils.sortAndDedup(array, indices);
assertThat(numUnique, equalTo(set.size()));
Iterator<BytesRef> iterator = set.iterator();
BytesRefBuilder spare = new BytesRefBuilder();
for (int i = 0; i < numUnique; i++) {
assertThat(iterator.hasNext(), is(true));
assertThat(array.get(spare, indices[i]), equalTo(iterator.next()));
}
}
use of org.apache.lucene.util.BytesRefBuilder in project elasticsearch by elastic.
the class BytesRefHashTests method testAdd.
/**
* Test method for
* {@link org.apache.lucene.util.BytesRefHash#add(org.apache.lucene.util.BytesRef)}
* .
*/
public void testAdd() {
BytesRefBuilder ref = new BytesRefBuilder();
BytesRef scratch = new BytesRef();
int num = scaledRandomIntBetween(2, 20);
for (int j = 0; j < num; j++) {
Set<String> strings = new HashSet<>();
int uniqueCount = 0;
for (int i = 0; i < 797; i++) {
String str;
do {
str = TestUtil.randomRealisticUnicodeString(random(), 1000);
} while (str.length() == 0);
ref.copyChars(str);
long count = hash.size();
long key = hash.add(ref.get());
if (key >= 0) {
assertTrue(strings.add(str));
assertEquals(uniqueCount, key);
assertEquals(hash.size(), count + 1);
uniqueCount++;
} else {
assertFalse(strings.add(str));
assertTrue((-key) - 1 < count);
assertEquals(str, hash.get((-key) - 1, scratch).utf8ToString());
assertEquals(count, hash.size());
}
}
assertAllIn(strings, hash);
newHash();
}
hash.close();
}
use of org.apache.lucene.util.BytesRefBuilder in project lucene-solr by apache.
the class TestLegacyNumericUtils method testIntConversionAndOrdering.
public void testIntConversionAndOrdering() throws Exception {
// generate a series of encoded ints, each numerical one bigger than the one before
BytesRefBuilder act = new BytesRefBuilder();
BytesRefBuilder last = new BytesRefBuilder();
for (int i = -100000; i < 100000; i++) {
LegacyNumericUtils.intToPrefixCoded(i, 0, act);
if (last != null) {
// test if smaller
assertTrue("actual bigger than last (BytesRef)", last.get().compareTo(act.get()) < 0);
assertTrue("actual bigger than last (as String)", last.get().utf8ToString().compareTo(act.get().utf8ToString()) < 0);
}
// test is back and forward conversion works
assertEquals("forward and back conversion should generate same int", i, LegacyNumericUtils.prefixCodedToInt(act.get()));
// next step
last.copyBytes(act.get());
}
}
use of org.apache.lucene.util.BytesRefBuilder in project lucene-solr by apache.
the class TestLegacyNumericUtils method testLongConversionAndOrdering.
public void testLongConversionAndOrdering() throws Exception {
// generate a series of encoded longs, each numerical one bigger than the one before
BytesRefBuilder last = new BytesRefBuilder();
BytesRefBuilder act = new BytesRefBuilder();
for (long l = -100000L; l < 100000L; l++) {
LegacyNumericUtils.longToPrefixCoded(l, 0, act);
if (last != null) {
// test if smaller
assertTrue("actual bigger than last (BytesRef)", last.get().compareTo(act.get()) < 0);
assertTrue("actual bigger than last (as String)", last.get().utf8ToString().compareTo(act.get().utf8ToString()) < 0);
}
// test is back and forward conversion works
assertEquals("forward and back conversion should generate same long", l, LegacyNumericUtils.prefixCodedToLong(act.get()));
// next step
last.copyBytes(act);
}
}
Aggregations