/**
  * Proxy setting properties to real objects.
  *
  * @param string $name  Property name.
  * @param mixed  $value Property value.
  *
  * @throws \UnexpectedValueException If property unknown.
  * @return float
  */
 public function __set($name, $value)
 {
     switch ($name) {
         case 'Diff_Timeout':
             $this->diff->setTimeout($value);
             break;
         case 'Diff_EditCost':
             $this->diff->setEditCost($value);
             break;
         case 'Match_Threshold':
             $this->match->setThreshold($value);
             break;
         case 'Match_Distance':
             $this->match->setDistance($value);
             break;
         case 'Match_MaxBits':
             $this->match->setMaxBits($value);
             break;
         case 'Patch_DeleteThreshold':
             $this->patch->setDeleteTreshold($value);
             break;
         case 'Patch_Margin':
             $this->patch->setMargin($value);
             break;
         default:
             throw new \UnexpectedValueException('Unknown property: ' . $name);
     }
 }
예제 #2
0
 public function testApply()
 {
     $this->m->setDistance(1000);
     $this->m->setThreshold(0.5);
     $this->p->setDeleteTreshold(0.5);
     // Null case.
     $patches = $this->p->make("", "");
     $this->assertEquals(array("Hello world.", array()), $this->p->apply($patches, "Hello world."));
     // Exact match.
     $patches = $this->p->make("The quick brown fox jumps over the lazy dog.", "That quick brown fox jumped over a lazy dog.");
     $this->assertEquals(array("That quick brown fox jumped over a lazy dog.", array(true, true)), $this->p->apply($patches, "The quick brown fox jumps over the lazy dog."));
     // Partial match.
     $this->assertEquals(array("That quick red rabbit jumped over a tired tiger.", array(true, true)), $this->p->apply($patches, "The quick red rabbit jumps over the tired tiger."));
     // Failed match.
     $this->assertEquals(array("I am the very model of a modern major general.", array(false, false)), $this->p->apply($patches, "I am the very model of a modern major general."));
     // Big delete, small change.
     $patches = $this->p->make("x1234567890123456789012345678901234567890123456789012345678901234567890y", "xabcy");
     $this->assertEquals(array("xabcy", array(true, true)), $this->p->apply($patches, "x123456789012345678901234567890-----++++++++++-----123456789012345678901234567890y"));
     // Big delete, big change 1.
     $patches = $this->p->make("x1234567890123456789012345678901234567890123456789012345678901234567890y", "xabcy");
     $this->assertEquals(array("xabc12345678901234567890---------------++++++++++---------------12345678901234567890y", array(false, true)), $this->p->apply($patches, "x12345678901234567890---------------++++++++++---------------12345678901234567890y"));
     // Big delete, big change 2.
     $this->p->setDeleteTreshold(0.6);
     $patches = $this->p->make("x1234567890123456789012345678901234567890123456789012345678901234567890y", "xabcy");
     $this->assertEquals(array("xabcy", array(true, true)), $this->p->apply($patches, "x12345678901234567890---------------++++++++++---------------12345678901234567890y"));
     $this->p->setDeleteTreshold(0.5);
     // Compensate for failed patch.
     $this->m->setDistance(0);
     $this->m->setThreshold(0.0);
     $patches = $this->p->make("abcdefghijklmnopqrstuvwxyz--------------------1234567890", "abcXXXXXXXXXXdefghijklmnopqrstuvwxyz--------------------1234567YYYYYYYYYY890");
     $this->assertEquals(array("ABCDEFGHIJKLMNOPQRSTUVWXYZ--------------------1234567YYYYYYYYYY890", array(false, true)), $this->p->apply($patches, "ABCDEFGHIJKLMNOPQRSTUVWXYZ--------------------1234567890"));
     $this->m->setDistance(1000);
     $this->m->setThreshold(0.5);
     // No side effects.
     $patches = $this->p->make("", "test");
     $patchesText = $this->p->toText($patches);
     $this->p->apply($patches, "");
     $this->assertEquals($patchesText, $this->p->toText($patches));
     // No side effects with major delete.
     $patches = $this->p->make("The quick brown fox jumps over the lazy dog.", "Woof");
     $patchesText = $this->p->toText($patches);
     $this->p->apply($patches, "The quick brown fox jumps over the lazy dog.");
     $this->assertEquals($patchesText, $this->p->toText($patches));
     // Edge exact match.
     $patches = $this->p->make("", "test");
     $this->assertEquals(array("test", array(true)), $this->p->apply($patches, ""));
     // Near edge exact match.
     $patches = $this->p->make("XY", "XtestY");
     $this->assertEquals(array("XtestY", array(true)), $this->p->apply($patches, "XY"));
     // Edge partial match.
     $patches = $this->p->make("y", "y123");
     $this->assertEquals(array("x123", array(true)), $this->p->apply($patches, "x"));
 }
예제 #3
0
 public function testBitap()
 {
     $this->m->setDistance(100);
     $this->m->setThreshold(0.5);
     // Exact matches.
     $this->assertEquals(5, $this->m->bitap("abcdefghijk", "fgh", 5));
     $this->assertEquals(5, $this->m->bitap("abcdefghijk", "fgh", 0));
     // Fuzzy matches.
     $this->assertEquals(4, $this->m->bitap("abcdefghijk", "efxhi", 0));
     $this->assertEquals(2, $this->m->bitap("abcdefghijk", "cdefxyhijk", 5));
     $this->assertEquals(-1, $this->m->bitap("abcdefghijk", "bxy", 1));
     // Overflow.
     $this->assertEquals(2, $this->m->bitap("123456789xx0", "3456789x0", 2));
     $this->assertEquals(0, $this->m->bitap("abcdef", "xxabc", 4));
     $this->assertEquals(3, $this->m->bitap("abcdef", "defyy", 4));
     $this->assertEquals(0, $this->m->bitap("abcdef", "xabcdefy", 0));
     // Threshold test.
     $this->m->setThreshold(0.4);
     $this->assertEquals(4, $this->m->bitap("abcdefghijk", "efxyhi", 1));
     $this->m->setThreshold(0.3);
     $this->assertEquals(-1, $this->m->bitap("abcdefghijk", "efxyhi", 1));
     $this->m->setThreshold(0.0);
     $this->assertEquals(1, $this->m->bitap("abcdefghijk", "bcdef", 1));
     $this->m->setThreshold(0.5);
     // Multiple select.
     $this->assertEquals(0, $this->m->bitap("abcdexyzabcde", "abccde", 3));
     $this->assertEquals(8, $this->m->bitap("abcdexyzabcde", "abccde", 5));
     // Distance test.
     // Strict location.
     $this->m->setDistance(10);
     $this->assertEquals(-1, $this->m->bitap("abcdefghijklmnopqrstuvwxyz", "abcdefg", 24));
     $this->assertEquals(0, $this->m->bitap("abcdefghijklmnopqrstuvwxyz", "abcdxxefg", 1));
     //Loose location.
     $this->m->setDistance(1000);
     $this->assertEquals(0, $this->m->bitap("abcdefghijklmnopqrstuvwxyz", "abcdefg", 24));
 }