// Path: java/com/google/zetasketch/internal/DifferenceDecoder.java
// public class DifferenceDecoder extends AbstractIntIterator {
// 
//   private final ByteSlice slice;
// 
//   private int last = 0;
// 
//   public DifferenceDecoder(ByteSlice slice) {
//     Preconditions.checkNotNull(slice);
//     this.slice = slice;
//   }
// 
//   @Override
//   public boolean hasNext() {
//     return slice.hasRemaining();
//   }
// 
//   @Override
//   public int nextInt() {
//     if (!hasNext()) {
//       throw new NoSuchElementException();
//     }
// 
//     last += slice.getNextVarInt();
//     return last;
//   }
// }
// 
// Path: java/com/google/zetasketch/testing/IntegerBitIterableSubject.java
// public class IntegerBitIterableSubject extends Subject {
// 
//   private static final Subject.Factory<IntegerBitIterableSubject, Iterable<Integer>> FACTORY =
//       IntegerBitIterableSubject::new;
// 
//   public static IntegerBitIterableSubject assertThat(Iterable<Integer> value) {
//     return Truth.assertAbout(FACTORY).that(value);
//   }
// 
//   public static IntegerBitIterableSubject assertThat(Iterator<Integer> value) {
//     return Truth.assertAbout(FACTORY).that(Lists.newArrayList(value));
//   }
// 
//   private final Iterable<Integer> actual;
// 
//   private IntegerBitIterableSubject(FailureMetadata failureMetadata, Iterable<Integer> subject) {
//     super(failureMetadata, subject);
//     this.actual = subject;
//   }
// 
//   @Override
//   public void isEqualTo(@Nullable Object other) {
//     if (other instanceof Iterable<?>) {
//       internalIsEqualTo((Iterable<?>) other);
//     } else if (other instanceof Iterator<?>) {
//       internalIsEqualTo(Lists.newArrayList((Iterator<?>) other));
//     } else {
//       failWithActual("expected to be equal to", other);
//     }
//   }
// 
//   private void internalIsEqualTo(Iterable<?> other) {
//     if (!Iterables.elementsEqual(this.actual, other)) {
//       this.failWithActual("expected to be equal to", toString(other));
//     }
//   }
// 
//   @Override
//   protected String actualCustomStringRepresentation() {
//     return toString(actual);
//   }
// 
//   private static String toString(Iterable<?> list) {
//     StringBuilder builder = new StringBuilder("[");
//     boolean first = true;
//     for (Object value : list) {
//       if (!first) {
//         builder.append(", ");
//       }
//       first = false;
// 
//       if (value instanceof Integer) {
//         builder.append("0b").append(Integer.toBinaryString(((Integer) value).intValue()));
//       } else {
//         builder.append(value);
//       }
//     }
//     builder.append("]");
//     return builder.toString();
//   }
// }
// 
// Path: java/com/google/zetasketch/testing/IntegerBitSubject.java
// public class IntegerBitSubject extends Subject {
// 
//   private static final Subject.Factory<IntegerBitSubject, Integer> FACTORY = IntegerBitSubject::new;
// 
//   public static IntegerBitSubject assertThat(Integer value) {
//     return Truth.assertAbout(FACTORY).that(value);
//   }
// 
//   private final Integer actual;
// 
//   private IntegerBitSubject(FailureMetadata failureMetadata, Integer subject) {
//     super(failureMetadata, subject);
//     this.actual = subject;
//   }
// 
//   @Override
//   public void isEqualTo(@Nullable Object other) {
//     Object displayObject =
//         (other instanceof Integer) ? toBinaryString(((Integer) other).intValue()) : other;
//     if (!this.actual.equals(other)) {
//       this.failWithActual("expected to be equal to", displayObject);
//     }
//   }
// 
//   @Override
//   protected String actualCustomStringRepresentation() {
//     return toBinaryString(getIntSubject());
//   }
// 
//   private static String toBinaryString(int value) {
//     return "0b" + Integer.toBinaryString(value);
//   }
// 
//   private int getIntSubject() {
//     return actual.intValue();
//   }
// }


import static com.google.common.truth.Truth.assertThat;
import com.google.zetasketch.internal.DifferenceDecoder;
import com.google.zetasketch.testing.IntegerBitIterableSubject;
import com.google.zetasketch.testing.IntegerBitSubject;
import it.unimi.dsi.fastutil.ints.IntIterators;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
/*
 * Copyright 2019 Google LLC
 *
 * Licensed 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
 *
 *     https://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 com.google.zetasketch.internal.hllplus;



/**
 * Unit tests for the HLL++ {@link SparseRepresentation}.
 *
 * <p>IMPORTANT: Note that these tests were introduced later and currently don't cover the full
 * functionality of SparseRepresentation. All code paths are covered by the more abstract {@link
 * com.google.zetasketch.HyperLogLogPlusPlusTest}, however.
 */
// TODO: Remove warning above once unit test coverage is complete.
@RunWith(JUnit4.class)
public final class SparseRepresentationTest {

  /**
   * Verifies that a sparse value with a higher precision is correctly downgraded to match the
   * representation.
   */
  @Test
  public void addSparseValue_HigherPrecision() {
    Representation repr = create(10, 13);
    Encoding.Sparse sparseEncoding = new Encoding.Sparse(11, 15);

    int sparseValue = 0b000000000011111;
    repr = repr.addSparseValue(sparseEncoding, sparseValue).compact();

    assertThat(repr.state.precision).isEqualTo(10);
    assertThat(repr.state.sparsePrecision).isEqualTo(13);
// Path: java/com/google/zetasketch/internal/DifferenceDecoder.java
// public class DifferenceDecoder extends AbstractIntIterator {
// 
//   private final ByteSlice slice;
// 
//   private int last = 0;
// 
//   public DifferenceDecoder(ByteSlice slice) {
//     Preconditions.checkNotNull(slice);
//     this.slice = slice;
//   }
// 
//   @Override
//   public boolean hasNext() {
//     return slice.hasRemaining();
//   }
// 
//   @Override
//   public int nextInt() {
//     if (!hasNext()) {
//       throw new NoSuchElementException();
//     }
// 
//     last += slice.getNextVarInt();
//     return last;
//   }
// }
// 
// Path: java/com/google/zetasketch/testing/IntegerBitIterableSubject.java
// public class IntegerBitIterableSubject extends Subject {
// 
//   private static final Subject.Factory<IntegerBitIterableSubject, Iterable<Integer>> FACTORY =
//       IntegerBitIterableSubject::new;
// 
//   public static IntegerBitIterableSubject assertThat(Iterable<Integer> value) {
//     return Truth.assertAbout(FACTORY).that(value);
//   }
// 
//   public static IntegerBitIterableSubject assertThat(Iterator<Integer> value) {
//     return Truth.assertAbout(FACTORY).that(Lists.newArrayList(value));
//   }
// 
//   private final Iterable<Integer> actual;
// 
//   private IntegerBitIterableSubject(FailureMetadata failureMetadata, Iterable<Integer> subject) {
//     super(failureMetadata, subject);
//     this.actual = subject;
//   }
// 
//   @Override
//   public void isEqualTo(@Nullable Object other) {
//     if (other instanceof Iterable<?>) {
//       internalIsEqualTo((Iterable<?>) other);
//     } else if (other instanceof Iterator<?>) {
//       internalIsEqualTo(Lists.newArrayList((Iterator<?>) other));
//     } else {
//       failWithActual("expected to be equal to", other);
//     }
//   }
// 
//   private void internalIsEqualTo(Iterable<?> other) {
//     if (!Iterables.elementsEqual(this.actual, other)) {
//       this.failWithActual("expected to be equal to", toString(other));
//     }
//   }
// 
//   @Override
//   protected String actualCustomStringRepresentation() {
//     return toString(actual);
//   }
// 
//   private static String toString(Iterable<?> list) {
//     StringBuilder builder = new StringBuilder("[");
//     boolean first = true;
//     for (Object value : list) {
//       if (!first) {
//         builder.append(", ");
//       }
//       first = false;
// 
//       if (value instanceof Integer) {
//         builder.append("0b").append(Integer.toBinaryString(((Integer) value).intValue()));
//       } else {
//         builder.append(value);
//       }
//     }
//     builder.append("]");
//     return builder.toString();
//   }
// }
// 
// Path: java/com/google/zetasketch/testing/IntegerBitSubject.java
// public class IntegerBitSubject extends Subject {
// 
//   private static final Subject.Factory<IntegerBitSubject, Integer> FACTORY = IntegerBitSubject::new;
// 
//   public static IntegerBitSubject assertThat(Integer value) {
//     return Truth.assertAbout(FACTORY).that(value);
//   }
// 
//   private final Integer actual;
// 
//   private IntegerBitSubject(FailureMetadata failureMetadata, Integer subject) {
//     super(failureMetadata, subject);
//     this.actual = subject;
//   }
// 
//   @Override
//   public void isEqualTo(@Nullable Object other) {
//     Object displayObject =
//         (other instanceof Integer) ? toBinaryString(((Integer) other).intValue()) : other;
//     if (!this.actual.equals(other)) {
//       this.failWithActual("expected to be equal to", displayObject);
//     }
//   }
// 
//   @Override
//   protected String actualCustomStringRepresentation() {
//     return toBinaryString(getIntSubject());
//   }
// 
//   private static String toBinaryString(int value) {
//     return "0b" + Integer.toBinaryString(value);
//   }
// 
//   private int getIntSubject() {
//     return actual.intValue();
//   }
// }


// Path: javatests/com/google/zetasketch/internal/hllplus/SparseRepresentationTest.java
import static com.google.common.truth.Truth.assertThat;
import com.google.zetasketch.internal.DifferenceDecoder;
import com.google.zetasketch.testing.IntegerBitIterableSubject;
import com.google.zetasketch.testing.IntegerBitSubject;
import it.unimi.dsi.fastutil.ints.IntIterators;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/*
 * Copyright 2019 Google LLC
 *
 * Licensed 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
 *
 *     https://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 com.google.zetasketch.internal.hllplus;



/**
 * Unit tests for the HLL++ {@link SparseRepresentation}.
 *
 * <p>IMPORTANT: Note that these tests were introduced later and currently don't cover the full
 * functionality of SparseRepresentation. All code paths are covered by the more abstract {@link
 * com.google.zetasketch.HyperLogLogPlusPlusTest}, however.
 */
// TODO: Remove warning above once unit test coverage is complete.
@RunWith(JUnit4.class)
public final class SparseRepresentationTest {

  /**
   * Verifies that a sparse value with a higher precision is correctly downgraded to match the
   * representation.
   */
  @Test
  public void addSparseValue_HigherPrecision() {
    Representation repr = create(10, 13);
    Encoding.Sparse sparseEncoding = new Encoding.Sparse(11, 15);

    int sparseValue = 0b000000000011111;
    repr = repr.addSparseValue(sparseEncoding, sparseValue).compact();

    assertThat(repr.state.precision).isEqualTo(10);
    assertThat(repr.state.sparsePrecision).isEqualTo(13);
    IntegerBitIterableSubject.assertThat(new DifferenceDecoder(repr.state.sparseData))