From de0046999a9074fec3e01e85ed9fed599b30b7fd Mon Sep 17 00:00:00 2001 From: furplag Date: Thu, 6 Jul 2023 18:44:26 +0900 Subject: [PATCH 1/2] fix #745 fallback to empty array if the array of IP addresses contains nothing when using "random_int($min, $max)" . Signed-off-by: furplag --- lib/Service/NegativeSampleGenerator.php | 2 +- tests/Unit/Service/NegativeSampleGeneratorTest.php | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/Service/NegativeSampleGenerator.php b/lib/Service/NegativeSampleGenerator.php index 99a3eecf..a74a1a7a 100644 --- a/lib/Service/NegativeSampleGenerator.php +++ b/lib/Service/NegativeSampleGenerator.php @@ -71,7 +71,7 @@ private function getUniqueIPsPerUser(Dataset $positives): array { private function generateFromRealData(array $uidVec, array $uniqueIps): array { return array_merge( $uidVec, - $uniqueIps[random_int(0, count($uniqueIps) - 1)] + empty($uniqueIps) ? array() : $uniqueIps[random_int(0, count($uniqueIps) - 1)] ); } diff --git a/tests/Unit/Service/NegativeSampleGeneratorTest.php b/tests/Unit/Service/NegativeSampleGeneratorTest.php index c04073e5..76fa1d3e 100644 --- a/tests/Unit/Service/NegativeSampleGeneratorTest.php +++ b/tests/Unit/Service/NegativeSampleGeneratorTest.php @@ -131,6 +131,17 @@ public function testGenerateMultipleShuffledFromLimitedUnique(): void { 'sample has a unique IP' ); } + + $positives = new Unlabeled([ + array_merge(self::decToBitArray(1, 16), self::decToBitArray(1, 32)), + array_merge(self::decToBitArray(2, 16), self::decToBitArray(1, 32)), + array_merge(self::decToBitArray(3, 16), self::decToBitArray(1, 32)), + array_merge(self::decToBitArray(4, 16), self::decToBitArray(1, 32)), + ]); + + $result = $this->generator->generateShuffledFromPositiveSamples($positives, 5); + + self::assertCount(5, $result); } /** From 4faa3deda309b0f556b2154092aa4559d8ce22fc Mon Sep 17 00:00:00 2001 From: furplag Date: Fri, 7 Jul 2023 08:56:23 +0900 Subject: [PATCH 2/2] fix empty array following the coding style . Signed-off-by: furplag --- lib/Service/NegativeSampleGenerator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Service/NegativeSampleGenerator.php b/lib/Service/NegativeSampleGenerator.php index a74a1a7a..3705fc37 100644 --- a/lib/Service/NegativeSampleGenerator.php +++ b/lib/Service/NegativeSampleGenerator.php @@ -71,7 +71,7 @@ private function getUniqueIPsPerUser(Dataset $positives): array { private function generateFromRealData(array $uidVec, array $uniqueIps): array { return array_merge( $uidVec, - empty($uniqueIps) ? array() : $uniqueIps[random_int(0, count($uniqueIps) - 1)] + empty($uniqueIps) ? [] : $uniqueIps[random_int(0, count($uniqueIps) - 1)] ); }