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
106
107
108
109
110
111
112
113
114
115
116
|
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.prelude.query.test;
import com.yahoo.prelude.query.CompositeIndexedItem;
import com.yahoo.prelude.query.Item;
import com.yahoo.prelude.query.PureWeightedString;
import com.yahoo.prelude.query.WeightedSetItem;
import com.yahoo.prelude.query.WordItem;
import org.junit.jupiter.api.Test;
import java.nio.ByteBuffer;
import static org.junit.jupiter.api.Assertions.*;
public class WeightedSetItemTestCase {
@Test
void testTokenAPI() {
WeightedSetItem ws = new WeightedSetItem("index");
assertEquals(0, ws.getNumTokens());
assertNull(ws.getTokenWeight("bogus"));
// insert tokens
assertEquals(Integer.valueOf(1), ws.addToken("foo"));
assertEquals(Integer.valueOf(2), ws.addToken("bar", 2));
assertEquals(Integer.valueOf(3), ws.addToken("baz", 3));
// check state
assertEquals(3, ws.getNumTokens());
assertEquals(Integer.valueOf(1), ws.getTokenWeight("foo"));
assertEquals(Integer.valueOf(2), ws.getTokenWeight("bar"));
assertEquals(Integer.valueOf(3), ws.getTokenWeight("baz"));
// add duplicate tokens
assertEquals(Integer.valueOf(2), ws.addToken("foo", 2));
assertEquals(Integer.valueOf(3), ws.addToken("baz", 2));
// check state
assertEquals(3, ws.getNumTokens());
assertEquals(Integer.valueOf(2), ws.getTokenWeight("foo"));
assertEquals(Integer.valueOf(2), ws.getTokenWeight("bar"));
assertEquals(Integer.valueOf(3), ws.getTokenWeight("baz"));
// remove token
assertEquals(Integer.valueOf(2), ws.removeToken("bar"));
assertEquals(2, ws.getNumTokens());
assertNull(ws.getTokenWeight("bar"));
// remove non-existing token
assertNull(ws.removeToken("bogus"));
assertEquals(2, ws.getNumTokens());
}
@Test
void testNegativeWeight() {
WeightedSetItem ws = new WeightedSetItem("index");
assertEquals(Integer.valueOf(-10), ws.addToken("bad", -10));
assertEquals(1, ws.getNumTokens());
assertEquals(Integer.valueOf(-10), ws.getTokenWeight("bad"));
}
static class FakeWSItem extends CompositeIndexedItem {
public FakeWSItem() { setIndexName("index"); }
public ItemType getItemType() { return ItemType.WEIGHTEDSET; }
public String getName() { return "WEIGHTEDSET"; }
public int getNumWords() { return 1; }
public String getIndexedString() { return ""; }
public void add(String token, int weight) {
WordItem w = new WordItem(token, getIndexName());
w.setWeight(weight);
super.addItem(w);
}
}
@Test
void testEncoding() {
WeightedSetItem item = new WeightedSetItem("index");
// need 2 alternative reference encoding, as the encoding
// order is kept undefined to improve performance.
FakeWSItem ref1 = new FakeWSItem();
FakeWSItem ref2 = new FakeWSItem();
item.addToken("foo", 10);
item.addToken("bar", 20);
ref1.add("foo", 10);
ref1.add("bar", 20);
ref2.add("bar", 20);
ref2.add("foo", 10);
ByteBuffer actual = ByteBuffer.allocate(128);
ByteBuffer expect1 = ByteBuffer.allocate(128);
ByteBuffer expect2 = ByteBuffer.allocate(128);
expect1.put((byte) 15).put((byte) 2);
Item.putString("index", expect1);
new PureWeightedString("foo", 10).encode(expect1);
new PureWeightedString("bar", 20).encode(expect1);
expect2.put((byte) 15).put((byte) 2);
Item.putString("index", expect2);
new PureWeightedString("bar", 20).encode(expect2);
new PureWeightedString("foo", 10).encode(expect2);
assertEquals(3, item.encode(actual));
actual.flip();
expect1.flip();
expect2.flip();
if (actual.equals(expect1)) {
assertNotEquals(actual, expect2);
} else {
assertEquals(actual, expect2);
}
}
}
|