forked from apache/datasketches-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReqCompactorTest.java
More file actions
169 lines (148 loc) · 6 KB
/
Copy pathReqCompactorTest.java
File metadata and controls
169 lines (148 loc) · 6 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.datasketches.req;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import java.lang.foreign.MemorySegment;
import org.apache.datasketches.common.positional.PositionalSegment;
import org.apache.datasketches.req.FloatBuffer;
import org.apache.datasketches.req.ReqCompactor;
import org.apache.datasketches.req.ReqSerDe;
import org.apache.datasketches.req.ReqSketch;
import org.apache.datasketches.req.ReqSerDe.Compactor;
import org.testng.annotations.Test;
/**
* @author Lee Rhodes
*/
public class ReqCompactorTest {
final ReqSketchTest reqSketchTest = new ReqSketchTest();
@Test
public void checkNearestEven() {
assertEquals(ReqCompactor.nearestEven(-0.9f), 0);
}
@Test
public void checkGetters() {
final boolean up = true;
final boolean hra = true;
final ReqSketch sk = reqSketchTest.loadSketch( 20, 1, 120, up, hra, 0);
final ReqCompactor c = sk.getCompactors().get(0);
c.getCoin();
final long state = c.getState();
assertEquals(state, 1L);
assertEquals(c.getNumSections(), 3);
assertEquals(c.getSectionSize(), 20);
}
@Test
public void checkSerDe() {
checkSerDeImpl(12, false);
checkSerDeImpl(12, true);
}
private static void checkSerDeImpl(final int k, final boolean hra) {
final ReqCompactor c1 = new ReqCompactor((byte)0, hra, k, null);
final int nomCap = 2 * 3 * k;
final int cap = 2 * nomCap;
final int delta = nomCap;
final FloatBuffer fbuf = c1.getBuffer();
for (int i = 1; i <= nomCap; i++) {
fbuf.append(i); //compactor doesn't have a direct update() method
}
final float minV = 1;
final float maxV = nomCap;
final float sectionSizeFlt = c1.getSectionSizeFlt();
final int sectionSize = c1.getSectionSize();
final int numSections = c1.getNumSections();
final long state = c1.getState();
final int lgWeight = c1.getLgWeight();
final boolean c1hra = c1.isHighRankAccuracy();
final boolean sorted = fbuf.isSorted();
final byte[] c1ser = c1.toByteArray();
//now deserialize
final PositionalSegment posSeg = PositionalSegment.wrap(MemorySegment.ofArray(c1ser));
final Compactor compactor = ReqSerDe.extractCompactor(posSeg, sorted, c1hra);
final ReqCompactor c2 = compactor.reqCompactor;
assertEquals(compactor.minItem, minV);
assertEquals(compactor.maxItem, maxV);
assertEquals(compactor.count, nomCap);
assertEquals(c2.getSectionSizeFlt(), sectionSizeFlt);
assertEquals(c2.getSectionSize(), sectionSize);
assertEquals(c2.getNumSections(), numSections);
assertEquals(c2.getState(), state);
assertEquals(c2.getLgWeight(), lgWeight);
assertEquals(c2.isHighRankAccuracy(), c1hra);
final FloatBuffer fbuf2 = c2.getBuffer();
assertEquals(fbuf2.getCapacity(), cap);
assertEquals(fbuf2.getDelta(), delta);
assertTrue(fbuf.isEqualTo(fbuf2));
}
@Test
public void checkSerDeWithNegativeValues() {
checkSerDeNegativeImpl(12, false);
checkSerDeNegativeImpl(12, true);
}
private static void checkSerDeNegativeImpl(final int k, final boolean hra) {
final ReqCompactor c1 = new ReqCompactor((byte)0, hra, k, null);
final int nomCap = 2 * 3 * k;
final FloatBuffer fbuf = c1.getBuffer();
for (int i = 1; i <= nomCap; i++) {
fbuf.append(-i); //all negative values
}
final byte[] c1ser = c1.toByteArray();
final PositionalSegment posSeg = PositionalSegment.wrap(MemorySegment.ofArray(c1ser));
final Compactor compactor = ReqSerDe.extractCompactor(posSeg, fbuf.isSorted(), hra);
assertEquals(compactor.minItem, -nomCap);
assertEquals(compactor.maxItem, -1f);
}
@Test
public void checkSerDeWithMixedValues() {
checkSerDeMixedImpl(12, false);
checkSerDeMixedImpl(12, true);
}
private static void checkSerDeMixedImpl(final int k, final boolean hra) {
final ReqCompactor c1 = new ReqCompactor((byte)0, hra, k, null);
final int nomCap = 2 * 3 * k;
final int half = nomCap / 2;
final FloatBuffer fbuf = c1.getBuffer();
for (int i = 0; i < nomCap; i++) {
fbuf.append(i - half); // range: -half to half-1
}
final byte[] c1ser = c1.toByteArray();
final PositionalSegment posSeg = PositionalSegment.wrap(MemorySegment.ofArray(c1ser));
final Compactor compactor = ReqSerDe.extractCompactor(posSeg, fbuf.isSorted(), hra);
assertEquals(compactor.minItem, (float) -half);
assertEquals(compactor.maxItem, (float) (half - 1));
}
@Test
public void checkSerDeWithInfiniteValues() {
checkSerDeInfiniteImpl(12, false);
checkSerDeInfiniteImpl(12, true);
}
private static void checkSerDeInfiniteImpl(final int k, final boolean hra) {
final ReqCompactor c1 = new ReqCompactor((byte)0, hra, k, null);
final FloatBuffer fbuf = c1.getBuffer();
fbuf.append(Float.POSITIVE_INFINITY);
fbuf.append(1);
fbuf.append(Float.NEGATIVE_INFINITY);
fbuf.append(-1);
final byte[] c1ser = c1.toByteArray();
final PositionalSegment posSeg = PositionalSegment.wrap(MemorySegment.ofArray(c1ser));
final Compactor compactor = ReqSerDe.extractCompactor(posSeg, fbuf.isSorted(), hra);
assertEquals(compactor.minItem, Float.NEGATIVE_INFINITY);
assertEquals(compactor.maxItem, Float.POSITIVE_INFINITY);
}
}