<commit_before><?php
/* This class is part of the XP framework
 *
 * $Id$
 */
 
  uses('util.profiling.unittest.TestCase');

  /**
   * Tests ticks which we will use for code coverage.
   *
   * @see      php://register_tick_function
   * @see      php://unregister_tick_function
   * @purpose  Unit Test
   */
  class TicksTest extends TestCase {
    var
      $ticks= array();

    /**
     * Static initializer block. Resets (global) ticks declaration to zero 
     * so they will not interfere with ours.
     *
     * @model   static
     * @access  public
     */
    function __static() {
      declare(ticks= 0);
    }
    
    /**
     * Setup method. Sets up tick handling
     *
     * @access  public
     */
    function setUp() {
      set_error_handler(array(&$this, 'tick'));
      register_tick_function('trigger_error', NULL, E_USER_NOTICE);
    }
    
    /**
     * Teardown method. Unregisters tick handling.
     *
     * @access  public
     */
    function tearDown() {
      unregister_tick_function('trigger_error');
      restore_error_handler();
    }

    /**
     * Tick handler
     *
     * @access  protected
     * @param   int level
     * @param   string message
     * @param   string file
     * @param   int line
     */
    function tick($level, $message, $file, $line) {
      if (E_USER_NOTICE == $level) {
        $key= basename($file);
        if (!isset($this->ticks[$key])) $this->ticks[$key]= array();
        if (!isset($this->ticks[$key][$line])) $this->ticks[$key][$line]= 0;
        $this->ticks[$key][$line]++;
        return;
      }
      
      // Default error handler otherwise
      $e= error_reporting(0);
      $errors= xp::registry('errors');
      $errors[$file][$line][$msg]++;
      xp::registry('errors', $errors);
      error_reporting($e);
    }
    
    /**
     * Helper method
     *
     * @access  protected
     * @param   string file
     * @param   array<int, int> frequencies keys are line numbers, values are frequency
     */
    function assertTicks($file, $frequencies) {
      $this->assertEquals(
        $frequencies,
        $this->ticks[basename($file)]
      );      
    }
    
    /**
     * Tests the most basic form (which we will use for all other tests):
     * <code>
     *   declare(ticks= 1) {
     *     $line= __LINE__;
     *   }
     * </code>
     * We use the line to be able to reference this in the following 
     * assertions.
     *
     * In this case, two ticks should be produced:
     * <ol>
     *   <li>One for the assignment</li>
     *   <li>One for the closing curly bracket }</li>
     * </ol>
     *
     * @access  public
     */
    #[@test]
    function mostBasicForm() {
      declare(ticks= 1) {
        $line= __LINE__;      // tick
      }                       // tick

      $this->assertTicks(__FILE__, array(
        $line    => 1, 
        $line+ 1 => 1
      ));
    }

    /**
     * Tests an if / else statement where the condition in if() 
     * evaluates to true (thus the first block getting executed).
     *
     * @access  public
     */
    #[@test]
    function ifCondition() {
      declare(ticks= 1) {
        $line= __LINE__;      // tick
        if (TRUE) {
          $executed= TRUE;    // tick
        } else {              // tick
          $executed= FALSE;
        }                     // tick
      }                       // tick

      $this->assertTrue($executed);
      $this->assertTicks(__FILE__, array(
        $line    => 1, 
        $line+ 2 => 1,
        $line+ 3 => 1,
        $line+ 5 => 1,
        $line+ 6 => 1,
      ));
    }

    /**
     * Tests an if / else statement where the condition in if() 
     * evaluates to false (thus the second block getting executed).
     *
     * @access  public
     */
    #[@test]
    function elseCondition() {
      declare(ticks= 1) {
        $line= __LINE__;      // tick
        if (FALSE) {
          $executed= TRUE;    
        } else {
          $executed= FALSE;   // tick
        }                     // tick (2)
      }                       // tick

      $this->assertFalse($executed);
      $this->assertTicks(__FILE__, array(
        $line    => 1, 
        $line+ 4 => 1,
        $line+ 5 => 2,
        $line+ 6 => 1,
      ));
    }

    /**
     * Tests a for loop
     *
     * @access  public
     */
    #[@test]<commit_msg>- Add test ifConditionWithoutBlock() # Here again, the closing curly bracket belonging to the declare # block is producing two ticks. Why?<commit_after><?php
/* This class is part of the XP framework
 *
 * $Id$
 */
 
  uses('util.profiling.unittest.TestCase');

  /**
   * Tests ticks which we will use for code coverage.
   *
   * @see      php://register_tick_function
   * @see      php://unregister_tick_function
   * @purpose  Unit Test
   */
  class TicksTest extends TestCase {
    var
      $ticks= array();

    /**
     * Static initializer block. Resets (global) ticks declaration to zero 
     * so they will not interfere with ours.
     *
     * @model   static
     * @access  public
     */
    function __static() {
      declare(ticks= 0);
    }
    
    /**
     * Setup method. Sets up tick handling
     *
     * @access  public
     */
    function setUp() {
      set_error_handler(array(&$this, 'tick'));
      register_tick_function('trigger_error', NULL, E_USER_NOTICE);
    }
    
    /**
     * Teardown method. Unregisters tick handling.
     *
     * @access  public
     */
    function tearDown() {
      unregister_tick_function('trigger_error');
      restore_error_handler();
    }

    /**
     * Tick handler
     *
     * @access  protected
     * @param   int level
     * @param   string message
     * @param   string file
     * @param   int line
     */
    function tick($level, $message, $file, $line) {
      if (E_USER_NOTICE == $level) {
        $key= basename($file);
        if (!isset($this->ticks[$key])) $this->ticks[$key]= array();
        if (!isset($this->ticks[$key][$line])) $this->ticks[$key][$line]= 0;
        $this->ticks[$key][$line]++;
        return;
      }
      
      // Default error handler otherwise
      $e= error_reporting(0);
      $errors= xp::registry('errors');
      $errors[$file][$line][$msg]++;
      xp::registry('errors', $errors);
      error_reporting($e);
    }
    
    /**
     * Helper method
     *
     * @access  protected
     * @param   string file
     * @param   array<int, int> frequencies keys are line numbers, values are frequency
     */
    function assertTicks($file, $frequencies) {
      $this->assertEquals(
        $frequencies,
        $this->ticks[basename($file)]
      );      
    }
    
    /**
     * Tests the most basic form (which we will use for all other tests):
     * <code>
     *   declare(ticks= 1) {
     *     $line= __LINE__;
     *   }
     * </code>
     * We use the line to be able to reference this in the following 
     * assertions.
     *
     * In this case, two ticks should be produced:
     * <ol>
     *   <li>One for the assignment</li>
     *   <li>One for the closing curly bracket }</li>
     * </ol>
     *
     * @access  public
     */
    #[@test]
    function mostBasicForm() {
      declare(ticks= 1) {
        $line= __LINE__;      // tick
      }                       // tick

      $this->assertTicks(__FILE__, array(
        $line    => 1, 
        $line+ 1 => 1
      ));
    }

    /**
     * Tests an if / else statement where the condition in if() 
     * evaluates to true (thus the first block getting executed).
     *
     * @access  public
     */
    #[@test]
    function ifCondition() {
      declare(ticks= 1) {
        $line= __LINE__;      // tick
        if (TRUE) {
          $executed= TRUE;    // tick
        } else {              // tick
          $executed= FALSE;
        }                     // tick
      }                       // tick

      $this->assertTrue($executed);
      $this->assertTicks(__FILE__, array(
        $line    => 1, 
        $line+ 2 => 1,
        $line+ 3 => 1,
        $line+ 5 => 1,
        $line+ 6 => 1,
      ));
    }

    /**
     * Tests an if statement without the optional block (e.g. if (1) return;
     * instead of if (1) { return; }).
     *
     * @access  public
     */
    #[@test]
    function ifConditionWithoutBlock() {
      declare(ticks= 1) {
        $line= __LINE__;                            // tick 
        $executed= FALSE;                           // tick      
        if (TRUE) $executed= TRUE;                  // tick
      }                                             // tick (2?)

      $this->assertTrue($executed);
      $this->assertTicks(__FILE__, array(
        $line    => 1, 
        $line+ 1 => 1,
        $line+ 2 => 1,
        $line+ 3 => 2
      ));
    }

    /**
     * Tests an if / else statement where the condition in if() 
     * evaluates to false (thus the second block getting executed).
     *
     * @access  public
     */
    #[@test]
    function elseCondition() {
      declare(ticks= 1) {
        $line= __LINE__;      // tick
        if (FALSE) {
          $executed= TRUE;    
        } else {
          $executed= FALSE;   // tick
        }                     // tick (2)
      }                       // tick

      $this->assertFalse($executed);
      $this->assertTicks(__FILE__, array(
        $line    => 1, 
        $line+ 4 => 1,
        $line+ 5 => 2,
        $line+ 6 => 1,
      ));
    }

    /**
     * Tests a for loop
     *
     * @access  public
     */
    #[@test]
{'fraction_non_alphanumeric': 0.13599579611140306, 'fraction_numerical': 0.006831318970047294, 'mean_word_length': 1.5321979776476848, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 73, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5045072', 'n_tokens_mistral': 3008, 'n_tokens_neox': 2859, 'n_words': 761}