add test for linear congruential generator

This commit is contained in:
ivan 2017-11-06 21:39:12 -03:00
parent 3fabdfbe2f
commit 1b48369e27
4 changed files with 109 additions and 1 deletions

View File

@ -3,10 +3,11 @@ language: php
php:
- '5.6'
- '7.0'
- '7.1'
services:
- mysql
before_install:
- rvm use 2.2 --install --binary --fuzzy
- ruby --version
@ -27,5 +28,7 @@ before_install:
script:
- cd client
- npm test
- cd ../server
- ./run-tests.sh
- cd ../tests
- bacon init.rb

View File

@ -30,6 +30,8 @@ class Hashing {
$sqrt = sqrt($number);
$prime = true;
if($number <= 1) return false;
for($i = 2; $i <= $sqrt; $i++) {
if($number % $i === 0) {
$prime = false;

View File

@ -0,0 +1,73 @@
<?php
include_once 'libs/Hashing.php';
use PHPUnit\Framework\TestCase;
class HashingTest extends TestCase {
public function testShouldGenerateRandomToken() {
$token1 = Hashing::generateRandomToken();
$token2 = Hashing::generateRandomToken();
$this->assertNotEquals($token1, $token2);
}
public function testShouldHashAndVerifyPassword() {
$TEST_TIMES = 5;
for ($i = 0; $i < $TEST_TIMES; $i++) {
$password = Hashing::generateRandomToken();
$passwordHash = Hashing::hashPassword($password);
$this->assertTrue(Hashing::verifyPassword($password, $passwordHash));
$this->assertFalse(Hashing::verifyPassword('', $passwordHash));
$this->assertFalse(Hashing::verifyPassword($password, ''));
$this->assertFalse(Hashing::verifyPassword('', ''));
$this->assertFalse(Hashing::verifyPassword($password . 'a', $passwordHash));
$this->assertFalse(Hashing::verifyPassword($password, $passwordHash . 'a'));
}
}
public function testShouldGenerateNumber() {
$TEST_TIMES = 10;
for ($i = 0; $i < $TEST_TIMES; $i++) {
$min = $i*1000;
$max = $TEST_TIMES*6000;
$number1 = Hashing::generateRandomNumber($min, $max);
$number2 = Hashing::generateRandomNumber($min, $max);
$this->assertTrue($min < $number1 && $number1 < $max);
$this->assertTrue($min < $number2 && $number2 < $max);
$this->assertNotEquals($number1, $number2);
}
}
public function testShouldRecognizePrime() {
$primes = [2, 3, 5, 7, 11, 17, 53, 163, 379, 401, 443, 449, 701];
$nonPrimes = [0, 1, 4, 27, 40, 51, 155, 363, 381, 511, 703, 928];
foreach($primes as $number) $this->assertTrue(Hashing::isPrime($number));
foreach($nonPrimes as $number) $this->assertFalse(Hashing::isPrime($number));
}
public function testShouldGenerateRandsomPrime() {
$TEST_TIMES = 10;
for ($i = 0; $i < $TEST_TIMES; $i++) {
$min = $i*1000;
$max = $TEST_TIMES*6000;
$number1 = Hashing::generateRandomPrime($min, $max);
$number2 = Hashing::generateRandomPrime($min, $max);
$this->assertTrue($min < $number1 && $number1 < $max);
$this->assertTrue($min < $number2 && $number2 < $max);
$this->assertNotEquals($number1, $number2);
$this->assertTrue(Hashing::isPrime($number1));
$this->assertTrue(Hashing::isPrime($number2));
}
}
}

View File

@ -0,0 +1,30 @@
<?php
include_once 'libs/Hashing.php';
include_once 'libs/LinearCongruentialGenerator.php';
use PHPUnit\Framework\TestCase;
class LinearCongruentialGeneratorTest extends TestCase {
public function testAvoidCollisions() {
$TEST_TIMES = 10;
$GENERATE_TIMES = 500000;
$min = 100000;
$max = 999999;
for($i = 0; $i < $TEST_TIMES; $i++) {
$linearCongruentialGenerator = new LinearCongruentialGenerator();
$linearCongruentialGenerator->setRange($min, $max);
$linearCongruentialGenerator->setGap(Hashing::generateRandomPrime($min, $max));
$linearCongruentialGenerator->setFirst($linearCongruentialGenerator->generateFirst());
$used = [];
for($j = 0; $j < $GENERATE_TIMES; $j++) {
$generatedNumber = $linearCongruentialGenerator->generate($j);
$this->assertFalse(array_key_exists($generatedNumber, $used));
$used[$generatedNumber] = true;
}
}
}
}