Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Topic e1b #48

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Week 10 Solutions.
  • Loading branch information
MiddelkoopT committed Apr 6, 2014
commit 0cda517f986ec368afd33e64cce782670c6a7eb2
11 changes: 11 additions & 0 deletions Web/os.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
// OS Test Copyright 2014 by WebIS Spring 2014 License Apache 2.0

class OS {
public function osil(){
return "<osil></osil>";
}
}


?>
27 changes: 27 additions & 0 deletions Web/osTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
// OS Test Copyright 2014 by WebIS Spring 2014 License Apache 2.0

require_once 'Work-Cell-Scheduler/TDD/validator.php';
include 'Work-Cell-Scheduler/Config/local.php';
require_once 'os.php';

class WebOSTestCase extends WebIS\Validator {

protected static $__CLASS__=__CLASS__;

function testOsXML () {
// Problem: develop a class that provides an empty OSiL XML Document
// Reference: https://github.com/MiddelkoopT/Work-Cell-Scheduler/blob/master/Test/osTest.php
// Solution: properly formed xml document with the root node <osil></osil>

$os=new OS();
$this->assertEquals("<osil></osil>",$os->osil());
}

}


if (!defined('PHPUnit_MAIN_METHOD')) {
WebOSTestCase::main();
}
?>
204 changes: 204 additions & 0 deletions Web/osbasic.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
<?php
// OSbasics Copyright 2014 by WebIS Spring 2014 License Apache 2.0
echo "OSBasics\n";

// Problem 1: Write simple TDD using assert.

//assert(FALSE);
assert(TRUE);

// Problem 2: Test if two strings are equal, return TRUE if equal, FALSE if not.
// tddStringEquals("One","One") returns TRUE
// tddStringEquals("One","Two") returns FALSE

function tddStringEquals($a,$b){
return $a===$b;
}

assert(tddStringEquals("One","One")===TRUE);
assert(tddStringEquals("One","Two")===FALSE);

// Problem 3: Refactor so function does the assert instead
// tddAssertStringEquals()

function tddAssertStringEquals($a,$b){
assert($a===$b);
}

tddAssertStringEquals("One","One");
// tddAssertStringEquals("One","Two"); // Fails

// This is not so useful, poor errors.

// Problem 3: Refactor tddAssertStringEquals to assertEquals and assertNotEquals and throws an exception
// use $expected and $result for args.

function assertEquals($expected,$result) {
if(!($expected===$result)){
$message="assertEquasl: |$expected|$result|\n";
throw new Exception($message);
}
}

function assertNotEquals($expected,$result) {
if(($expected===$result)){
$message="assertNoeEquasl: |$expected|$result|\n";
throw new Exception($message);
}
}

assertEquals("One","One");
assertNotEquals("One","Two");

// Problem 4: Test Failure with try and catch block

$failed=FALSE;
try {
assertNotEquals("One","One"); // Fails
} catch (Exception $e){
// print "Caught Exception";
assert(is_a($e,'Exception'));
$failed=TRUE;
}
assert($failed);

// TDD is now usable.

// Problem 5: Create a function that returns an empty xml OSiL document generated by SimpleXMLElement object
// osil() should return the XML document as a string containing the XML header and <osil/>
// SimpleXMLElement is a class, look at the asXML method to return the string.
// Use the XML heredoc for the expected result

function osil() {
$xml=new SimpleXMLElement('<osil/>');
return $xml->asXML();
}

$xml=<<<XML
<?xml version="1.0"?>
<osil/>

XML;

assertEquals($xml,osil());

// Problem 6: Write assertContainsString($needle,$haystack)

function assertContainsString($needle,$haystack){
if(strpos($haystack,$needle)===FALSE){
$message="assertContainsString: |$needle|$haystack|";
throw new Exception($message);
}
}

assertContainsString('Needle','HayStack Needle Hay Hay');
$failed=FALSE;
try {
assertContainsString('Needle','All hay'); // Fails
} catch (Exception $e) {
$failed=TRUE;
}
assert($failed);

// Problem 7: The haystack can be an array of strings

function assertContains($needle,$haystack){
if(is_string($haystack)){
return assertContainsString($needle,$haystack);
}
foreach($haystack as $hay)
if(strpos($hay,$needle)!==FALSE){
return;
}
$message="assertContains: |$needle|$haystack|";
throw new Exception($message);
}

assertContains('Needle',array('HayStack Needle Hay Hay','Hay'));


// Problem 7: Run "OSSolverService -h" make sure it returns 0 and contains the string "OS Version: 2."
// use the exec function
// The program in ..\\..\\..\\bin\\OSSolverService.exe

// Make sure to test failure first.

exec("..\\..\\..\\bin\\OSSolverService.exe -h",$output,$result);
//print_r($output);
assertEquals(0,$result);
assertContains("OS Version: 2.",$output);

// Problem 8: Save the xml to test.xml and read it back to test it.
// reimplement osil() as a write() function.
// read

function write($file) {
$xml=new SimpleXMLElement('<osil/>');
return $xml->asXML($file);
}

write("test.xml");
$xml=file_get_contents("test.xml");
// print_r($xml);
assertContains('<osil/>',$xml);

// Problem 9: Create a function solve() "OSSolverService -osil test.xml" that throws an exception on failure
// Assume test.xml is there.
// Our problem should return a result of nonzero since it is not well formed.
// Actually it crashes.

function solve(){
exec("..\\..\\..\\bin\\OSSolverService.exe -osil test.xml -osrl solution.xml",$output,$result);
//print_r($output);
if($result!==0){
$message="solve: error $result\n".implode("\n",$output);
throw new Exception($message);
}
}

$failed=FALSE;
try {
solve();
} catch (Exception $e){
//print $e;
$failed=TRUE;
};
assert($failed);

// Problem 10:
// Generate a minimal osil document and solve() it
// <osil><instanceHeader/><instanceData><objectives><obj numberOfObjCoef="0"/></objectives></instanceData></osil>

$xml=new SimpleXMLElement('<osil/>');
$xml->addChild('instanceHeader');
$xml->addChild('instanceData')->addChild('objectives')->addChild('obj')->addAttribute('numberOfObjCoef',0);

//print $xml->asXML();
$xml->asXML('test.xml');
solve();

// Problem 11:
// Detect an error and report it.

// Problem 12: Verify the minimal solution() status is "optimal" and 0, return FALSE if not optimal.
// write the output in solve() to soution.xml with the "-osrl solution.xml" switch
// parse it with simplexml_load_file
// print_r is your friend.
// You must also often cast and simpleXMLElement to a string "(string)"

function solution(){
solve();
$osil=simplexml_load_file('solution.xml');
//print_r($osil->optimization);
$result=(string)$osil->optimization->solution->status->attributes()->type;
if($result!=='optimal'){
return FALSE;
}
return (int)(string)$osil->optimization->solution->objectives->values->obj;
}

assert(solution()===0);

echo "done.\n"

?>