Test of Code for SitePoint Article
Evaluating assert(false);
assert(true);
assert('false');Waring: Assertion failed in example code on line 1.
Waring: Assertion false failed in example code on line 3.
Evaluating $one = 1;
$three = 3;
assert("$one + $one == $three");
assert('$one + $one == $three');Waring: Assertion 1 + 1 == 3 failed in example code on line 3.
Waring: Assertion $one + $one == $three failed in example code on line 4.
Evaluating define('MAX_CACHE_AGE', 60 * 60 * 24);
$timestamp = time() - 2 * 60 * 60 * 24; // make it 2 days old
assert('time() - $timestamp <= MAX_CACHE_AGE');Waring: Assertion time() - $timestamp <= MAX_CACHE_AGE failed in example code on line 3.
Evaluating define('XL', 'Extra Large');
class clothes {
var $size;
function clothes($s) {
$this->size = $s;
}
function size() {
return $this->size;
}
};
$socks = new clothes(XL);
assert('$socks->size() != XL');Waring: Assertion $socks->size() != XL failed in example code on line 12.
Evaluating function customer_exists($dummy) {
return false;
}
class shopping_cart {
function get_customer_id() {
return 1;
}
};
$cart = new shopping_cart;
assert('customer_exists($cart->get_customer_id())');Waring: Assertion customer_exists($cart->get_customer_id()) failed in example code on line 10.
Evaluating $some_variable = '1';
switch ($some_variable) {
case 'a':
//...
break;
case 'b':
//...
break;
//...
default:
assert('$some_variable != ' . "$some_variable");
break;
}Waring: Assertion $some_variable != 1 failed in example code on line 11.
Evaluating $fp = NULL;
assert('is_resource($fp)');Waring: Assertion is_resource($fp) failed in example code on line 2.
Evaluating function some_function($positive_int_parameter) {
assert('isset($positive_int_parameter)');
assert('is_int($positive_int_parameter)');
assert('$positive_int_parameter > 0');
//...
}
some_function($foo);
some_function('Hello');
some_function(-22);Waring: Assertion isset($positive_int_parameter) failed in example code on line 2.
Waring: Assertion is_int($positive_int_parameter) failed in example code on line 3.
Waring: Assertion $positive_int_parameter > 0 failed in example code on line 4.
Waring: Assertion is_int($positive_int_parameter) failed in example code on line 3.
Waring: Assertion $positive_int_parameter > 0 failed in example code on line 4.
Waring: Assertion $positive_int_parameter > 0 failed in example code on line 4.
Evaluating define('MAX_RESULT_LEN',10);
function process_string($dummy) {
return 'Here is a string longer than the max.';
}
$result = process_string($input_string);
assert('strlen($result) <= MAX_RESULT_LEN');Waring: Assertion strlen($result) <= MAX_RESULT_LEN failed in example code on line 6.
Evaluating function expensive_crunching_routine($i) {
return 1;
}
function alternate_expensive_crunching_routine($i) {
return 2;
}
$input = 0;
$output = expensive_crunching_routine($input);
assert('$output == alternate_expensive_crunching_routine($input)');Waring: Assertion $output == alternate_expensive_crunching_routine($input) failed in example code on line 9.
Evaluating $year_data = '1,2,1,2,0,0,2,0,0,2,0,0,2,2,2';
define('MIN_FIRST_MONTH',10);
assert(list($january, $ignore) = explode(',',$year_data));
assert('$january > MIN_FIRST_MONTH');Waring: Assertion $january > MIN_FIRST_MONTH failed in example code on line 4.