PHP Classes

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

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

Contents

Class file image Download
<?php

require_once('../../../Data/Types/Type.php');
require_once(
'../../../Data/Types/Restrictions.php');
require_once(
'../../../Data/Types/RestrictedStack.php');

use
Falcraft\Data\Types;
use
Falcraft\Data\Types\Type;

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

echo
"Various Instantiations -> \n";
echo
" \$testRestrictions = new Types\\Restrictions(array(Type::BASIC_INT)) -> ";

$success = true;

$testRestrictions = null;

try {
   
$testRestrictions = new Types\Restrictions(array(Type::BASIC_INT));
} catch (\
Exception $e) {
   
$success = false;
}

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

echo
" \$testRestrictedStack = new Types\\RestrictedStack() -> ";

$success = true;

$testRestrictedStack = null;

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

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

echo
" \$testRestrictedStack = new Types\\RestrictedStack(null, null, array('strict' => true)) -> ";

$success = true;

$testRestrictedStack = null;

try {
   
$testRestrictedStack = new Types\RestrictedStack(null, null, array('strict' => true));
} catch (\
Exception $e) {
   
$success = false;
}

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

echo
" \$testRestrictedStack = new Types\\RestrictedStack(
                null,
                new Types\Restrictions(array(Type::BASIC_INT)),
                array('strict' => true)) -> "
;

$success = true;

$testRestrictedStack = null;

try {
   
$testRestrictedStack = new Types\RestrictedStack(
       
null,
        new
Types\Restrictions(array(Type::BASIC_INT)),
        array(
'strict' => true));
} catch (\
Exception $e) {
   
$success = false;
}

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

echo
" \$testRestrictedStack = new Types\RestrictedStack(
                array(1, 2, 3, 'string',),
                \$testRestrictions) -> "
;
               
$fail = true;

$testRestrictedStack = null;

try {
   
$testRestrictedStack = new Types\RestrictedStack(
        array(
1, 2, 3, 'string',),
       
$testRestrictions);
   
$fail = false;
} catch (\
Exception $e) {
   
}

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

echo
" \$testRestrictedStack = new Types\RestrictedStack(
                array(1, 2, 3, 'string',),
                \$testRestrictions,
                array('strict' => true)) -> "
;
               
$fail = true;

$testRestrictedStack = null;

try {
   
$testRestrictedStack = new Types\RestrictedStack(
        array(
1, 2, 3, 'string',),
       
$testRestrictions,
        array(
'strict' => true));
   
$fail = false;
} catch (\
Exception $e) {
   
}

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

echo
" \$testRestrictedStack = new Types\RestrictedStack(
                array(1, 2, 3, 4, 5,),
                \$testRestrictions,
                array('strict' => true)) -> "
;
               
$success = true;

$testRestrictedStack = null;

try {
   
$testRestrictedStack = new Types\RestrictedStack(
        array(
1, 2, 3, 4, 5,),
       
$testRestrictions,
        array(
'strict' => true));
} catch (\
Exception $e) {
   
$success = false;
}

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

echo
"\nStack Operations --\n\n";

echo
"Instantiate Stack 2 and Push (1) -> ";

$success = true;

$testRestrictedStack2 = null;

try {
   
$testRestrictedStack2 = new Types\RestrictedStack(array(), $testRestrictions);
   
$testRestrictedStack2->push(1);
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success!\n\n";
    echo
"Stack Internals -- \n\n";
   
var_dump($testRestrictedStack2->getList());
    echo
"\n";
} else {
    echo
"Failure...\n";
}

echo
"Push Stack 1 (6, 7, 8) -> ";

$success = true;

try {
   
$testRestrictedStack->push(6, 7, 8);
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success!\n\n";
    echo
"Stack Internals -- \n\n";
   
var_dump($testRestrictedStack->getList());
    echo
"\n";
} else {
    echo
"Failure...\n";
}

echo
"Top -> ";

$success = true;

$val = null;

try {
   
$val = $testRestrictedStack->top();
} catch (\
Exception $e) {
   
$success = false;
}

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

echo
"Pop -> ";

$success = true;

$val = null;

try {
   
$val = $testRestrictedStack->pop();
} catch (\
Exception $e) {
   
$success = false;
}

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

class
testObject {
    public
$publicProp;
}

$obj = new testObject();
$obj->publicProp = 3;
$testRestrictedStackReference = new Types\RestrictedStack(
    array(),
    new
Types\Restrictions(array(Type::TYPED_OBJECT), array('testObject')),
    array(
'strict' => true));

echo
"Push Reference -> ";

$success = true;

//try {
   
$testRestrictedStackReference->pushReference($obj);
//} catch (\Exception $e) {
// $success = false;
//}

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

echo
"Test Reference: ";

$success = true;

try {
   
$obj->publicProp = 5;
    echo
"\n"; var_dump($testRestrictedStackReference->getList()); echo "\n";
} catch (\
Exception $e) {
   
$success = false;
}

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

echo
"PopReference -> ";

$success = true;

$val = null;

try {
   
$obj->publicProp = 7;
   
$val = $testRestrictedStackReference->popReference();
} catch(\
Exception $e) {
   
$success = false;
}

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

echo
"Testing Array Functionality -- \n";
echo
" ( [] ) -> ";

$success = true;

try {
   
$testRestrictedStack[] = 12;
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success!\n\n";
    echo
"Stack Internals --\n\n";
   
var_dump($testRestrictedStack->getList());
    echo
"\n";
} else {
    echo
"Failure...\n";
}

echo
" ( [3] = 20 ) -> ";

$success = true;

try {
   
$testRestrictedStack[3] = 20;
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success!\n\n";
    echo
"Stack Internals --\n\n";
   
var_dump($testRestrictedStack->getList());
    echo
"\n";
} else {
    echo
"Failure...\n";
}

echo
"Stack Roll Operations -- \n\n";
echo
" Pre-Roll -> \n\n";
var_dump($testRestrictedStack->getList());
echo
"\n";

echo
" ::roll(2) -> ";

$success = true;

try {
   
$testRestrictedStack->roll(2);
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success!\n\n";
    echo
"Stack Internals --\n\n";
   
var_dump($testRestrictedStack->getList());
    echo
"\n";
} else {
    echo
"Failure...\n";
}

echo
" ::roll(-5) -> ";

$success = true;

try {
   
$testRestrictedStack->roll(-5);
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success!\n\n";
    echo
"Stack Internals --\n\n";
   
var_dump($testRestrictedStack->getList());
    echo
"\n";
} else {
    echo
"Failure...\n";
}