package com.github.tonivade.claudb.persistence;

import static java.util.concurrent.ThreadLocalRandom.current;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import java.nio.ByteBuffer;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

class ByteUtilsTest {

  @ParameterizedTest
  @MethodSource("intValues")
  void intToArray(int x) {
    byte[] array = ByteUtils.toByteArray(x);
    int y = ByteUtils.byteArrayToInt(array);

    assertThat(x, equalTo(y));
  }

  @ParameterizedTest
  @MethodSource("longValues")
  void longToArray(long x) {
    byte[] array = ByteUtils.toByteArray(x);
    long y = ByteUtils.byteArrayToLong(array);

    assertThat(x, equalTo(y));
  }

  @ParameterizedTest
  @MethodSource("lengthValues")
  void lengthToArray(int x) {
    byte[] array = ByteUtils.lengthToByteArray(x);
    int y = ByteUtils.byteArrayToLength(IntStream.range(0, array.length).map(i -> array[i]).iterator()::next);
    int z = ByteUtils.byteArrayToLength(ByteBuffer.wrap(array)::get);

    assertThat(x, equalTo(y));
    assertThat(x, equalTo(z));
  }

  static IntStream intValues() {
    return IntStream.generate(() -> current().nextInt()).limit(100);
  }

  static IntStream lengthValues() {
    return IntStream.generate(() -> Math.abs(current().nextInt())).limit(100);
  }

  static LongStream longValues() {
    return LongStream.generate(() -> current().nextLong()).limit(100);
  }
}

@@ -1,8 +1,152 @@
+/*%0A * Copyright (c) 2015-2022, Antonio Gabriel Mu%C3%B1oz Conejo %3Cantoniogmc at gmail dot com%3E%0A * Distributed under the terms of the MIT License%0A */%0A
 package 
@@ -346,16 +346,17 @@
 qualTo;%0A
+%0A
 import j
@@ -442,24 +442,25 @@
 LongStream;%0A
+%0A
 import org.j
@@ -982,32 +982,904 @@
 ualTo(y));%0A  %7D%0A%0A
+  @ParameterizedTest%0A  @MethodSource(%22smallLengthValues%22)%0A  void smallLengthToArray(int x) %7B%0A    byte%5B%5D array = ByteUtils.lengthToByteArray(x);%0A    assertThat(array.length, equalTo(1));%0A    int y = ByteUtils.byteArrayToLength(IntStream.range(0, array.length).map(i -%3E array%5Bi%5D).iterator()::next);%0A    int z = ByteUtils.byteArrayToLength(ByteBuffer.wrap(array)::get);%0A%0A    assertThat(x, equalTo(y));%0A    assertThat(x, equalTo(z));%0A  %7D%0A%0A  @ParameterizedTest%0A  @MethodSource(%22mediumLengthValues%22)%0A  void mediumLengthToArray(int x) %7B%0A    byte%5B%5D array = ByteUtils.lengthToByteArray(x);%0A    assertThat(array.length, equalTo(2));%0A    int y = ByteUtils.byteArrayToLength(IntStream.range(0, array.length).map(i -%3E array%5Bi%5D).iterator()::next);%0A    int z = ByteUtils.byteArrayToLength(ByteBuffer.wrap(array)::get);%0A%0A    assertThat(x, equalTo(y));%0A    assertThat(x, equalTo(z));%0A  %7D%0A%0A
   @Parameterized
@@ -1988,32 +1988,74 @@
 ToByteArray(x);%0A
+    assertThat(array.length, equalTo(5));%0A
     int y = Byte
@@ -2312,32 +2312,32 @@
 m intValues() %7B%0A
-
     return IntSt
@@ -2386,32 +2386,297 @@
 imit(100);%0A  %7D%0A%0A
+  static IntStream smallLengthValues() %7B%0A    return IntStream.generate(() -%3E Math.abs(current().nextInt(0x40))).limit(100);%0A  %7D%0A%0A  static IntStream mediumLengthValues() %7B%0A    return IntStream.generate(() -%3E Math.abs(current().nextInt(0x40, 0x80))).limit(100);%0A  %7D%0A%0A
   static IntStre

improve ByteUtils tests