PHP Classes

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

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

Contents

Class file image Download
<?php

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

use
Falcraft\Data\Types;

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

echo
"Instantiation -> ";

$success = true;

$testStack = null;

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

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

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

$success = true;

$list = null;

try {
   
$testQueue->push('test1', 'test2', 'test3');
   
$list = $testQueue->getQueue();
    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 = $testQueue->pop();
} catch (\
Exception $e) {
   
$success = false;
}

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

echo
"testQueue[] Functionality -> ";

$success = true;

$list = null;

try {
   
$testQueue[] = 'autoData';
   
$list = $testQueue->getQueue();
    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 {
   
$testQueue[1] = 'replaceData';
} catch (\
Exception $e) {

}

var_dump($testQueue->getQueue());

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

echo
"Top -> ";

$success = true;

$val = null;

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

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

echo
"BottomReference -> ";

$success = true;

$val = null;

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

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

echo
"PopReference -> ";

$success = true;

$val = null;

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

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