|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.datasketches.cpc; |
| 21 | + |
| 22 | +import static org.testng.Assert.assertEquals; |
| 23 | +import static org.testng.Assert.fail; |
| 24 | + |
| 25 | +import java.lang.foreign.MemorySegment; |
| 26 | +import java.util.Random; |
| 27 | + |
| 28 | +import org.apache.datasketches.common.SketchesArgumentException; |
| 29 | +import org.apache.datasketches.common.Util; |
| 30 | +import org.testng.annotations.Test; |
| 31 | + |
| 32 | +/** |
| 33 | + * update(MemorySegment) decodes into the union's bit matrix where it can and uncompresses a |
| 34 | + * sketch where it cannot, so it must give the same result as update(heapify(seg)) in every case. |
| 35 | + */ |
| 36 | +public class CpcUnionSegmentUpdateTest { |
| 37 | + |
| 38 | + private static byte[] sketchBytes(final int lgK, final long from, final long to) { |
| 39 | + final CpcSketch sk = new CpcSketch(lgK); |
| 40 | + for (long i = from; i < to; i++) { sk.update(i); } |
| 41 | + return sk.toByteArray(); |
| 42 | + } |
| 43 | + |
| 44 | + /** Feeds the same images to both paths and requires byte-identical results. */ |
| 45 | + private static void assertSamePath(final String what, final int unionLgK, final byte[][] images) { |
| 46 | + final CpcUnion viaSegment = new CpcUnion(unionLgK); |
| 47 | + final CpcUnion viaSketch = new CpcUnion(unionLgK); |
| 48 | + for (final byte[] image : images) { |
| 49 | + viaSegment.update(MemorySegment.ofArray(image)); |
| 50 | + viaSketch.update(CpcSketch.heapify(MemorySegment.ofArray(image))); |
| 51 | + } |
| 52 | + final CpcSketch a = viaSegment.getResult(); |
| 53 | + final CpcSketch b = viaSketch.getResult(); |
| 54 | + assertEquals(a.getEstimate(), b.getEstimate(), 0.0, what + " estimate"); |
| 55 | + assertEquals(a.getLgK(), b.getLgK(), what + " lgK"); |
| 56 | + assertEquals(a.toByteArray(), b.toByteArray(), what + " serialized image"); |
| 57 | + } |
| 58 | + |
| 59 | + /** Every flavor of source, against a union that is still empty. */ |
| 60 | + @Test |
| 61 | + public void checkEachFlavorIntoFreshUnion() { |
| 62 | + for (int lgK = 4; lgK <= 14; lgK++) { |
| 63 | + final int k = 1 << lgK; |
| 64 | + //counts chosen to land in EMPTY, SPARSE, HYBRID, PINNED and SLIDING |
| 65 | + final int[] counts = {0, 1, 10, k / 16, k / 4, k, 2 * k, 3 * k, 6 * k, 20 * k}; |
| 66 | + for (final int n : counts) { |
| 67 | + assertSamePath("lgK=" + lgK + " n=" + n, lgK, |
| 68 | + new byte[][] {sketchBytes(lgK, 0, n)}); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + /** Sequences, so the union is exercised in every state a source can arrive into. */ |
| 74 | + @Test |
| 75 | + public void checkSequencesAcrossUnionStates() { |
| 76 | + for (final int lgK : new int[] {4, 8, 11, 12}) { |
| 77 | + final int k = 1 << lgK; |
| 78 | + final int[][] sequences = { |
| 79 | + {1, 1, 1}, //stays sparse |
| 80 | + {1, 10, k}, //sparse then graduates |
| 81 | + {k, 1}, //matrix first, then a sparse source |
| 82 | + {k / 16, k, 3 * k, 20 * k}, //ascending through the flavors |
| 83 | + {20 * k, 3 * k, k, k / 16}, //descending |
| 84 | + {0, k, 0, 3 * k}, //empties interleaved |
| 85 | + }; |
| 86 | + for (final int[] seq : sequences) { |
| 87 | + final byte[][] images = new byte[seq.length][]; |
| 88 | + long from = 0; |
| 89 | + for (int i = 0; i < seq.length; i++) { |
| 90 | + images[i] = sketchBytes(lgK, from, from + seq[i]); |
| 91 | + from += seq[i] / 2; //overlap, so the merges do real work |
| 92 | + } |
| 93 | + assertSamePath("lgK=" + lgK + " seq=" + java.util.Arrays.toString(seq), lgK, images); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + /** A source with a smaller lgK than the union forces the union to be reduced first. */ |
| 99 | + @Test |
| 100 | + public void checkSourceWithSmallerLgK() { |
| 101 | + for (final int unionLgK : new int[] {10, 12, 14}) { |
| 102 | + for (final int srcLgK : new int[] {4, 8, 9}) { |
| 103 | + if (srcLgK >= unionLgK) { continue; } |
| 104 | + final int srcK = 1 << srcLgK; |
| 105 | + for (final int n : new int[] {1, srcK / 4, srcK, 3 * srcK, 20 * srcK}) { |
| 106 | + //seed the union at its own lgK first, then merge the smaller source |
| 107 | + assertSamePath("unionLgK=" + unionLgK + " srcLgK=" + srcLgK + " n=" + n, unionLgK, |
| 108 | + new byte[][] {sketchBytes(unionLgK, 0, 1 << unionLgK), sketchBytes(srcLgK, 0, n)}); |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + /** Randomly fed sketches, which is where the column permutations get exercised hardest. */ |
| 115 | + @Test |
| 116 | + public void checkRandomInput() { |
| 117 | + final Random rnd = new Random(9876543L); |
| 118 | + for (final int lgK : new int[] {8, 11, 12}) { |
| 119 | + for (int trial = 0; trial < 6; trial++) { |
| 120 | + final byte[][] images = new byte[5][]; |
| 121 | + for (int i = 0; i < images.length; i++) { |
| 122 | + final CpcSketch sk = new CpcSketch(lgK); |
| 123 | + final int n = 1 + rnd.nextInt(40 << lgK); |
| 124 | + for (int j = 0; j < n; j++) { sk.update(rnd.nextLong()); } |
| 125 | + images[i] = sk.toByteArray(); |
| 126 | + } |
| 127 | + assertSamePath("random lgK=" + lgK + " trial=" + trial, lgK, images); |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + /** A mismatched seed must be rejected, exactly as the sketch path rejects it. */ |
| 133 | + @Test |
| 134 | + public void checkSeedMismatchIsRejected() { |
| 135 | + final byte[] image = sketchBytes(10, 0, 1000); |
| 136 | + final CpcUnion u = new CpcUnion(10, Util.DEFAULT_UPDATE_SEED + 1); |
| 137 | + try { |
| 138 | + u.update(MemorySegment.ofArray(image)); |
| 139 | + fail("expected a seed hash mismatch"); |
| 140 | + } catch (final SketchesArgumentException e) { |
| 141 | + //expected |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + /** Interleaving both entry points on one union must behave like using either alone. */ |
| 146 | + @Test |
| 147 | + public void checkInterleavedEntryPoints() { |
| 148 | + final int lgK = 11; |
| 149 | + final int k = 1 << lgK; |
| 150 | + final byte[][] images = new byte[6][]; |
| 151 | + for (int i = 0; i < images.length; i++) { |
| 152 | + images[i] = sketchBytes(lgK, (long) i * k, ((long) i * k) + (2 * k)); |
| 153 | + } |
| 154 | + |
| 155 | + final CpcUnion mixed = new CpcUnion(lgK); |
| 156 | + final CpcUnion sketchOnly = new CpcUnion(lgK); |
| 157 | + for (int i = 0; i < images.length; i++) { |
| 158 | + if ((i % 2) == 0) { |
| 159 | + mixed.update(MemorySegment.ofArray(images[i])); |
| 160 | + } else { |
| 161 | + mixed.update(CpcSketch.heapify(MemorySegment.ofArray(images[i]))); |
| 162 | + } |
| 163 | + sketchOnly.update(CpcSketch.heapify(MemorySegment.ofArray(images[i]))); |
| 164 | + } |
| 165 | + assertEquals(mixed.getResult().toByteArray(), sketchOnly.getResult().toByteArray()); |
| 166 | + } |
| 167 | +} |
0 commit comments