PHP Classes

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

Recommend this page to a friend!
  Classes of Asher Wolfstein   Abstract Data Types   Falcraft/examples/Data/Types/Stack.php   Download  
File: Falcraft/examples/Data/Types/Stack.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: 9 years ago
Size: 3,294 bytes
 

Contents

Class file image Download
<?php

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

use
Falcraft\Data\Types;

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

echo
"Instantiation -> ";

$success = true;

$testStack = null;

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

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

echo
"Push ('test1', 'test2', 'test3') -> ";

$success = true;

$list = null;

try {
   
$testStack->push('test1', 'test2', 'test3');
   
$list = $testStack->getStack();
    if (!
in_array('test2', $list)) {
       
$success = false;
    }
} catch (\
Exception $e) {
   
$success = false;
}

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

echo
"Pop -> ";

$success = true;
$val = null;

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

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

echo
"testStack[] Functionality -> ";

$success = true;

$list = null;

try {
   
$testStack[] = 'autoData';
   
$list = $testStack->getStack();
    if (!
in_array('autoData', $list)) {
       
$success = false;
    }
} catch (\
Exception $e) {
   
$success = false;
}

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

echo
"Replace Data [] -> \n";

try {
   
$testStack[1] = 'replaceData';
} catch (\
Exception $e) {

}

var_dump($testStack->getStack());

echo
"\nRoll -- ";
try {
   
$testStack->push('roll1', 'roll2', 'roll3');
    echo
" Pre-Roll: \n";
   
var_dump($testStack->getStack());
    echo
"\n roll(2): \n";
   
$testStack->roll(2);
   
var_dump($testStack->getStack());
    echo
"\n roll(-1): \n";
   
$testStack->roll(-1);
   
var_dump($testStack->getStack());
    echo
"\n roll(3): \n";
   
$testStack->roll(3);
   
var_dump($testStack->getStack());
} catch (\
Exception $e) {
    echo
"Failure\n\n";
}

echo
"Top -> ";

$success = true;
$val = null;

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

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

class
testObject {
    public
$publicProp;
   
}

$obj = new testObject();
$obj->publicProp = 3;

echo
"Push Reference -> ";

$success = true;

try {
   
$testStack->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($testStack->getStack()); echo "\n";
} catch (\
Exception $e) {
   
$success = false;
}

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

echo
"TopReference -> ";

$success = true;

$val = null;

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

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

echo
"PopReference -> ";

$success = true;

$val = null;

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

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