PHP Classes

File: Falcraft/examples/Data/Types/Leaf.php

Recommend this page to a friend!
  Classes of Asher Wolfstein   Abstract Data Types   Falcraft/examples/Data/Types/Leaf.php   Download  
File: Falcraft/examples/Data/Types/Leaf.php
Role: Example script
Content type: text/plain
Description: Leaf (Tree) Example
Class: Abstract Data Types
Set of abstract data types as pure PHP classes
Author: By
Last change:
Date: 9 years ago
Size: 2,788 bytes
 

Contents

Class file image Download
<?php

/* SOME TESTS ARE MEANT TO FAIL!
   If the entire script runs, it works. */

require_once('../../../Data/Types/Leaf.php');

use
Falcraft\Data\Types;

echo
"Falcraft\\Data\\Types\\Leaf.php Test\n";
echo
"---------------------------------\n\n";

echo
"Instantiation -- \n";

echo
" Basic Empty -> ";

$success = true;

$testLeaf = null;

try {
   
$testLeaf = new Types\Leaf();
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success!\n";
} else {
    echo
"Failure...\n";
}

echo
" Instantiation With Improper Values -> ";

$fail = true;

$testLeaf = $testArray = null;

try {
   
$testArray = array(5, 'notLeaf', 'what?' => 'delicious',);
   
$testLeaf = new Types\Leaf($testArray);
   
$fail = false;
} catch (\
Exception $e) {
   
}

if (
$fail) {
    echo
"Failure!\n";
} else {
    echo
"Success...\n";
}

echo
" Instantiation With Proper Values -> ";

$success = true;

$testLeaf = $testArray = null;

try {
   
$testArray = array(new Types\Leaf(), new Types\Leaf(), new Types\Leaf());
   
$testLeaf = new Types\Leaf($testArray);
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success!\n";
} else {
    echo
"Failure...\n";
}

echo
"\nReset\n";
echo
"-----\n\n";

unset(
$testLeaf);
unset(
$testArray);

echo
"New Leaf -> ";

$success = true;

$testLeaf = null;
$testIdentifier = null;

try {
   
$testLeaf = new Types\Leaf();
   
$testIdentifier = $testLeaf->getIdentifier();
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success! Identifier: $testIdentifier\n";
} else {
    echo
"Failure...\n";
}

echo
"Altering Leaf Prefix (root) and adding -> ";

$success = true;

$testLeaf1 = $testIdentifier1 = null;
$testLeaf2 = $testIdentifier2 = null;
$testIdentifier3 = null;

try {
   
$testLeaf1 = new Types\Leaf();
   
$testIdentifier1 = $testLeaf->getIdentifier();
   
$testLeaf2 = new Types\Leaf(array(), array('prefix' => 'Root',));
   
$testIdentifier2 = $testLeaf2->getIdentifier();
   
$testIdentifier3 = $testLeaf1->addLeaf($testLeaf2);
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success!\n";
    echo
" identifier1: $testIdentifier1\n";
    echo
" identifier2: $testIdentifier2\n";
    echo
" identifier3: $testIdentifier3\n";
} else {
    echo
"Failure...\n";
}

echo
"Did $identifier3 Generate? ";

$success = false;

try {
   
$success = $testLeaf1->isLeaf($identifier3);
} catch (\
Exception $e) {
   
}

if (
$success) {
    echo
"Success!\n";
} else {
    echo
"Failure...\n";
}

echo
"Data Setting and Retrieval -> ";

$d = 5;

try {
   
$testLeaf1->setData($d);
   
$d = 10;
   
$d = $testLeaf1->getData();
} catch (\
Exception $e) {
   
$d = 0;
}

if (
$d == 5) {
    echo
"Success!\n";
} else {
    echo
"Failure...\n";
}