public function testMake()
 {
     // Null case.
     $patches = $this->p->make("", "");
     $this->assertEquals("", $this->p->toText($patches));
     $text1 = "The quick brown fox jumps over the lazy dog.";
     $text2 = "That quick brown fox jumped over a lazy dog.";
     // Text2 + Text1 inputs.
     // The second patch must be "-21,17 +21,18", not "-22,17 +21,18" due to rolling context.
     $expected = "@@ -1,8 +1,7 @@\n Th\n-at\n+e\n  qui\n@@ -21,17 +21,18 @@\n jump\n-ed\n+s\n  over \n-a\n+the\n  laz\n";
     $patches = $this->p->make($text2, $text1);
     $this->assertEquals($expected, $this->p->toText($patches));
     // Text1 + Text2 inputs.
     $expected = "@@ -1,11 +1,12 @@\n Th\n-e\n+at\n  quick b\n@@ -22,18 +22,17 @@\n jump\n-s\n+ed\n  over \n-the\n+a\n  laz\n";
     $patches = $this->p->make($text1, $text2);
     $this->assertEquals($expected, $this->p->toText($patches));
     // Diff input.
     $diffs = $this->d->main($text1, $text2, false)->getChanges();
     $patches = $this->p->make($diffs);
     $this->assertEquals($expected, $this->p->toText($patches));
     // Text1+Diff inputs.
     $patches = $this->p->make($text1, $diffs);
     $this->assertEquals($expected, $this->p->toText($patches));
     // Text1+Text2+Diff inputs (deprecated).
     $patches = $this->p->make($text1, $text2, $diffs);
     $this->assertEquals($expected, $this->p->toText($patches));
     // Character encoding.
     $patches = $this->p->make("`1234567890-=[]\\;',./", "~!@#\$%^&*()_+{}|:\"<>?");
     $this->assertEquals("@@ -1,21 +1,21 @@\n-%601234567890-=%5B%5D%5C;',./\n+~!@#\$%25%5E&*()_+%7B%7D%7C:%22%3C%3E?\n", $this->p->toText($patches));
     // Character decoding.
     $diffs = array(array(Diff::DELETE, "`1234567890-=[]\\;',./"), array(Diff::INSERT, "~!@#\$%^&*()_+{}|:\"<>?"));
     $patches = $this->p->fromText("@@ -1,21 +1,21 @@\n-%601234567890-=%5B%5D%5C;',./\n+~!@#\$%25%5E&*()_+%7B%7D%7C:%22%3C%3E?\n");
     $this->assertEquals($diffs, $patches[0]->getChanges());
     // Long string with repeats.
     $text1 = "";
     for ($i = 0; $i < 100; $i++) {
         $text1 .= "abcdef";
     }
     $text2 = $text1 . "123";
     $expected = "@@ -573,28 +573,31 @@\n cdefabcdefabcdefabcdefabcdef\n+123\n";
     $patches = $this->p->make($text1, $text2);
     $this->assertEquals($expected, $this->p->toText($patches));
     // Test null inputs.
     try {
         $this->p->make(null, null);
         $this->fail();
     } catch (\InvalidArgumentException $e) {
     }
 }
 public function testDiffMainMemoryLeaks()
 {
     $text1 = file_get_contents(__DIR__ . '/fixtures/S_performance1.txt');
     $text2 = file_get_contents(__DIR__ . '/fixtures/S_performance2.txt');
     $n = 20;
     // Warm up
     $diff = new Diff();
     $diff->setTimeout(0);
     $diff->main($text1, $text2);
     unset($diff);
     $timeStart = microtime(1);
     $memoryStart = memory_get_usage();
     for ($i = 0; $i < $n; $i++) {
         $diff = new Diff();
         $diff->setTimeout(0);
         $diff->main($text1, $text2);
         unset($diff);
     }
     $timeElapsed = microtime(1) - $timeStart;
     $memoryUsage = (memory_get_usage() - $memoryStart) / 1024 / 1024;
     $this->assertLessThan(0.001, $memoryUsage);
     echo 'Elapsed time: ' . round($timeElapsed, 3) . PHP_EOL;
     echo 'Memory usage: ' . round($memoryUsage, 10) . PHP_EOL;
 }
Esempio n. 3
0
 /**
  * Given the location of the 'middle snake', split the diff in two parts and recurse.
  *
  * @param string $text1    Old string to be diffed.
  * @param string $text2    New string to be diffed.
  * @param int    $x        Index of split point in text1.
  * @param int    $y        Index of split point in text2.
  * @param int    $deadline Time at which to bail if not yet complete.
  *
  * @return array Array of diff arrays.
  */
 protected function bisectSplit($text1, $text2, $x, $y, $deadline)
 {
     $text1A = mb_substr($text1, 0, $x);
     $text2A = mb_substr($text2, 0, $y);
     $text1B = mb_substr($text1, $x);
     $text2B = mb_substr($text2, $y);
     // Compute both diffs serially.
     $diffA = new Diff();
     $diffA->main($text1A, $text2A, false, $deadline);
     $diffB = new Diff();
     $diffB->main($text1B, $text2B, false, $deadline);
     return array_merge($diffA->getChanges(), $diffB->getChanges());
 }
 /**
  * Find the differences between two texts.  Simplifies the problem by
  * stripping any common prefix or suffix off the texts before diffing.
  *
  * @param string $text1      Old string to be diffed.
  * @param string $text2      New string to be diffed.
  * @param bool   $checklines Optional speedup flag.  If present and false, then don't run
  *                           a line-level diff first to identify the changed areas.
  *                           Defaults to true, which does a faster, slightly less optimal diff.
  *
  * @throws \InvalidArgumentException If texts is null.
  * @return array Array of changes.
  */
 public function diff_main($text1, $text2, $checklines = true)
 {
     return $this->diff->main($text1, $text2, $checklines)->getChanges();
 }
 public function testMain()
 {
     // Perform a trivial diff.
     // Null case.
     $this->assertEquals(array(), $this->d->main("", "", false)->getChanges());
     // Equality.
     $this->assertEquals(array(array(Diff::EQUAL, "abc")), $this->d->main("abc", "abc", false)->getChanges());
     // Check '0' strings
     $this->assertEquals(array(array(Diff::EQUAL, "0"), array(Diff::INSERT, "X"), array(Diff::EQUAL, "12"), array(Diff::INSERT, "X"), array(Diff::EQUAL, "0"), array(Diff::INSERT, "X"), array(Diff::EQUAL, "34"), array(Diff::INSERT, "X"), array(Diff::EQUAL, "0")), $this->d->main("0120340", "0X12X0X34X0", false)->getChanges());
     $this->assertEquals(array(array(Diff::EQUAL, "0"), array(Diff::DELETE, "X"), array(Diff::EQUAL, "12"), array(Diff::DELETE, "X"), array(Diff::EQUAL, "0"), array(Diff::DELETE, "X"), array(Diff::EQUAL, "34"), array(Diff::DELETE, "X"), array(Diff::EQUAL, "0")), $this->d->main("0X12X0X34X0", "0120340", false)->getChanges());
     $this->assertEquals(array(array(Diff::DELETE, "Apple"), array(Diff::INSERT, "Banana"), array(Diff::EQUAL, "s are a"), array(Diff::INSERT, "lso"), array(Diff::EQUAL, " fruit.")), $this->d->main("Apples are a fruit.", "Bananas are also fruit.", false)->getChanges());
     $this->assertEquals(array(array(Diff::DELETE, "a"), array(Diff::INSERT, Utils::unicodeChr(0x680)), array(Diff::EQUAL, "x"), array(Diff::DELETE, "\t"), array(Diff::INSERT, "")), $this->d->main("ax\t", Utils::unicodeChr(0x680) . "x", false)->getChanges());
     // Overlaps.
     $this->assertEquals(array(array(Diff::DELETE, "1"), array(Diff::EQUAL, "a"), array(Diff::DELETE, "y"), array(Diff::EQUAL, "b"), array(Diff::DELETE, "2"), array(Diff::INSERT, "xab")), $this->d->main("1ayb2", "abxab", false)->getChanges());
     $this->assertEquals(array(array(Diff::INSERT, "xaxcx"), array(Diff::EQUAL, "abc"), array(Diff::DELETE, "y")), $this->d->main("abcy", "xaxcxabc", false)->getChanges());
     $this->assertEquals(array(array(Diff::DELETE, "ABCD"), array(Diff::EQUAL, "a"), array(Diff::DELETE, "="), array(Diff::INSERT, "-"), array(Diff::EQUAL, "bcd"), array(Diff::DELETE, "="), array(Diff::INSERT, "-"), array(Diff::EQUAL, "efghijklmnopqrs"), array(Diff::DELETE, "EFGHIJKLMNOefg")), $this->d->main("ABCDa=bcd=efghijklmnopqrsEFGHIJKLMNOefg", "a-bcd-efghijklmnopqrs", false)->getChanges());
     // Large equality.
     $this->assertEquals(array(array(Diff::INSERT, " "), array(Diff::EQUAL, "a"), array(Diff::INSERT, "nd"), array(Diff::EQUAL, " [[Pennsylvania]]"), array(Diff::DELETE, " and [[New")), $this->d->main("a [[Pennsylvania]] and [[New", " and [[Pennsylvania]]", false)->getChanges());
     // Timeout.
     // 100ms
     $this->d->setTimeout(0.1);
     $a = "`Twas brillig, and the slithy toves\nDid gyre and gimble in the wabe:\nAll mimsy were the borogoves,\nAnd the mome raths outgrabe.\n";
     $b = "I am the very model of a modern major general,\nI've information vegetable, animal, and mineral,\nI know the kings of England, and I quote the fights historical,\nFrom Marathon to Waterloo, in order categorical.\n";
     // Increase the text lengths by 1024 times to ensure a timeout.
     for ($i = 0; $i < 10; $i++) {
         $a .= $a;
         $b .= $b;
     }
     $startTime = microtime(1);
     $this->d->main($a, $b);
     $endTime = microtime(1);
     // Test that we took at least the timeout period.
     $this->assertGreaterThanOrEqual($this->d->getTimeout(), $endTime - $startTime);
     // Test that we didn't take forever (be forgiving).
     // Theoretically this test could fail very occasionally if the
     // OS task swaps or locks up for a second at the wrong moment.
     // TODO must be $this->d->getTimeout() * 2, but it need some optimization of linesToCharsMunge()
     $this->assertLessThan($this->d->getTimeout() * 15, $endTime - $startTime);
     $this->d->setTimeout(0);
     // Test the linemode speedup.
     // Must be long to pass the 100 char cutoff.
     // Simple line-mode.
     $a = str_repeat("1234567890\n", 13);
     $b = str_repeat("abcdefghij\n", 13);
     $this->assertEquals($this->d->main($a, $b, false)->getChanges(), $this->d->main($a, $b, true)->getChanges());
     // Single line-mode.
     $a = str_repeat("1234567890", 13);
     $b = str_repeat("abcdefghij", 13);
     $this->assertEquals($this->d->main($a, $b, false)->getChanges(), $this->d->main($a, $b, true)->getChanges());
     function rebuildtexts($diffs)
     {
         // Construct the two texts which made up the diff originally.
         $text1 = "";
         $text2 = "";
         foreach ($diffs as $change) {
             if ($change[0] != Diff::INSERT) {
                 $text1 .= $change[1];
             }
             if ($change[0] != Diff::DELETE) {
                 $text2 .= $change[1];
             }
         }
         return array($text1, $text2);
     }
     // Overlap line-mode.
     $a = str_repeat("1234567890\n", 13);
     $b = "abcdefghij\n1234567890\n1234567890\n1234567890\nabcdefghij\n1234567890\n1234567890\n1234567890\nabcdefghij\n1234567890\n1234567890\n1234567890\nabcdefghij\n";
     $this->assertEquals(rebuildtexts($this->d->main($a, $b, false)->getChanges()), rebuildtexts($this->d->main($a, $b, true)->getChanges()));
     // Test null inputs.
     try {
         $this->d->main(null, null);
         $this->fail();
     } catch (\InvalidArgumentException $e) {
     }
 }