Saturday, November 28, 2009

Google's Go - a systems programming language



November 10, Google release it's  new systems programming language called Go.  According to the website http://golang.org Go has the following outstanding features:-

  • simple in term of programming syntax, less typing
  • fast  compilers produce fast code fast. Typical Go builds take a fraction of a second yet the resulting programs run nearly as quickly as comparable C or C++ code.
  • safe in type safe and memory safe. Go has pointers but no pointer arithmetic. For random access, use slices, which know their limits.
  • concurrent is a core part of the language. Go promotes writing systems and servers as sets of lightweight communicating processes, called goroutines, with strong support from the language. Run thousands of goroutines if you want—and say good-bye to stack overflows.
  • fun to work with. Go has fast builds, clean syntax, garbage collection, methods for any type, and run-time reflection. It feels like a dynamic language but has the speed and safety of a static language. It's a joy to use
  • open source, Go is realised under the BSD licence.

After watching a video on Google TechTalk by one of the archtechts of the language, I thought of giving a try. I have installed it on my Ubuntu, with no difficulty at all, and have done a few example. I have not done anything worth showing, but during the course I will blog more about it.

Visit their site,http://golang.org, and see if you can make use of this language.

Introduction to TDD with PHPUnit


What is TDD?

Test Drive Development [TDD] is an approach to development which combines test-first development where you write a test before you write just enough production code to fulfill that test and refactoring. So basically you first decide the functions or sections of your code you want to test. Then write test for these functions for different scenarios with expected values. Then write the implement the functions while checking if they meet the different scenarios you captured in the tests, if not refactoring is done till all tests have passed.

Why use TDD?

  • by focusing on the different test cases first, developer(s) focuses on the functionality more than the implementation
  • it allows the developer to focus on a small section of the implementations at a time till its working as expected
  • can lead to more modularized, flexible, and extensible code
  • no more code is written than necessary to pass a failing test case, automated tests tend to cover every code path
  • while it is true that more code is required with TDD than without TDD because of the unit test code, total code implementation time is typically shorter

TDD Example with PHP

There are many ways in which one can implement a TDD. In this example, we will create a simple calculator with the four basic operations that is (add, subtract, multiply and divide).
But after looking around and looking back to my exprience, it looks like there is one way that most developers agree on:-
  1. design your class/API
  2. create a test suite
  3. run the test, and in this case all tests should fail
  4. implement the class
  5. run the test, an this time some test will pass and some will fail
  6. fix failures or errors
  7. repeat 5 and 6 till all tests have passed

Download Source Code

Designing the class/API

This basically mean designing the skeleton of the class, so the class body with its functions signatures without the actually implementation
/* === calculator.php === */
class Calculator{
/**
* Constructor
* NOTE: if you are using function __constructor() can also be used for older version of PHP
* @return unknown_type
*/
function Calculator(){

}

/**
* add two numbers (a + b)
* @param $a an integer
* @param $b an integer
* @return a + b or null otherwise
*/
function add($a, $b){

}

/**
* subtact b from a numbers (a - b)
* @param $a an integer
* @param $b an integer
* @return a - b or null otherwise
*/
function subtract($a, $b){

}

/**
* multipy a and b numbers (a x b)
* @param $a an integer
* @param $b an integer
* @return a x b or null otherwise
*/
function  multiply($a, $b){

}

/**
* divide a by b numbers (a / b)
* @param $a an integer
* @param $b an integer
* @return a / b or null otherwise
*/
function  multiply($a, $b){

}
}

Creating the test class

To test a function there is two (or more depending on the test used) things that are  needed, value returned by the function and the values expected. Then using a once of the many assert functions to establish if the test has passed or not. Look at the comments on the code.

/* === calculatortest.php === */
require_once 'calculator.php';
require_once 'PHPUnit.php';

class CalculatorTest extends PHPUnit_TestCase{
 /**
  * Constructor
  * NOTE: if you are using function __constructor() can also be used for older version of PHP
  * @return unknown_type
  */
 var $calc;

 function CalculatorTest($name){
  $this->PHPUnit_TestCase($name);
 }

 /**
  * Used to set up and default values
  *
  * (non-PHPdoc)
  * @see PHPUnit/PHPUnit_TestCase#setUp()
  */
 function setUp(){
  // creating the Calculator object
  $this->calc = new Calculator();
 }

 /**
  * Used to destroy values that where set in the setUp()
  *
  * (non-PHPdoc)
  * @see PHPUnit/PHPUnit_TestCase#tearDown()
  */
 function tearDown(){
  // destroying the  calculator object 
  unset($this->calc);
 }

 /**
  * Testing add two numbers (a + b)
  */
 function testAdd(){
  $actual = $this->calc->add(2,3);
  $expected = 5;
  //checking if value returned value is what we expected
  $this->assertEquals($expected, $actual);
 }

 /**
  * Testing subtact b from a numbers (a - b)
  */
 function testSubtract(){
  $actual = $this->calc->subtract(5,2);
  $expected = 3;
  //checking if value returned value is what we expected
  $this->assertEquals($expected, $actual);
 }

 /**
  * Testing multipy a and b numbers (a x b)
  */
 function  testMultiply(){
  $actual = $this->calc->multiply(5,2);
  $expected = 10;
  //checking if value returned value is what we expected
  $this->assertEquals($expected, $actual);
 }

 /**
  * Testing divide a by b numbers where b=0
  */
 function  testDivideByZero(){
  $actual = $this->calc->divide(3,0);
  $expected = NULL;
  //checking if value returned value is what we expected
  $this->assertEquals($expected, $actual);
 }

 /**
  * Testing divide a by b numbers (a / b)
  */
 function  testDivide(){
  $actual = $this->calc->divide(10,2);
  $expected = 5;
  //checking if value returned value is what we expected
  $this->assertEquals($expected, $actual);
 }
}

Write the Testing

Now we need to write the code to actual run the code
/* ==== test.php ==== */

require_once 'calculatortest.php';
require_once 'PHPUnit.php';

/* the value passed duing the instantiating must be the name of the test class */
$suite  = new PHPUnit_TestSuite("CalculatorTest");
/*rin the test*/
$result = PHPUnit::run($suite);
/*dispay the result */
echo $result->toString();

Running the test

Start up your terminal and run the test.php with php or if run it through your apache and view the result on the browser
eferuzi@feruzi /v/w/phpunit> php5 test.php
TestCase CalculatorTest->testDivideByZero() passed
TestCase CalculatorTest->testAdd() failed: expected 5, actual  in /var/www/phpunit/calculatortest.php:56
TestCase CalculatorTest->testSubtract() failed: expected 3, actual  in /var/www/phpunit/calculatortest.php:66
TestCase CalculatorTest->testMultiply() failed: expected 10, actual  in /var/www/phpunit/calculatortest.php:76
TestCase CalculatorTest->testDivide() failed: expected 5, actual  in /var/www/phpunit/calculatortest.php:96
As you can see we have one passing because the functions are not returning anything at the moment and that test checks if NULL is returned.

Implementations of the Calculator

So now we add implementation into the functions in the Calculator class

/* === calculatortest.php === */


require_once 'calculator.php';
require_once 'PHPUnit.php';

class CalculatorTest extends PHPUnit_TestCase{
 /**
  * Constructor
  * NOTE: if you are using function __constructor() can also be used for older version of PHP
  * @return unknown_type
  */
 var $calc;

 function CalculatorTest($name){
  $this->PHPUnit_TestCase($name);
 }

 /**
  * Used to set up and default values
  *
  * (non-PHPdoc)
  * @see PHPUnit/PHPUnit_TestCase#setUp()
  */
 function setUp(){
  // creating the Calculator object
  $this->calc = new Calculator();
 }

 /**
  * Used to destroy values that where set in the setUp()
  *
  * (non-PHPdoc)
  * @see PHPUnit/PHPUnit_TestCase#tearDown()
  */
 function tearDown(){
  // destroying the  calculator object
  unset($this->calc);
 }

 /**
  * Testing add two numbers (a + b)
  */
 function testAdd(){
  $actual = $this->calc->add(2,3);
  $expected = 5;
  //checking if value returned value is what we expected
  $this->assertEquals($expected, $actual);
 }

 /**
  * Testing the add method with wrong or none numerical input
  */
 function testAddInvalidValues(){
  $actual = $this->calc->add('ugali',3);
  //checking if value returned value is what we expected
  $this->assertNull($actual);
 }


 /**
  * Testing subtact b from a numbers (a - b)
  */
 function testSubtract(){
  $actual = $this->calc->subtract(5,2);
  $expected = 3;
  //checking if value returned value is what we expected
  $this->assertEquals($expected, $actual);
 }

 /**
  * Testing multipy a and b numbers (a x b)
  */
 function  testMultiply(){
  $actual = $this->calc->multiply(5,2);
  $expected = 10;
  //checking if value returned value is what we expected
  $this->assertEquals($expected, $actual);
 }

 /**
  * Testing divide a by b numbers where b=0
  */
 function  testDivideByZero(){
  $actual = $this->calc->divide(3,0);
  $expected = NULL;
  //checking if value returned value is what we expected
  $this->assertEquals($expected, $actual);
 }

 /**
  * Testing divide a by b numbers (a / b)
  */
 function  testDivide(){
  $actual = $this->calc->divide(10,2);
  $expected = 5;
  //checking if value returned value is what we expected
  $this->assertEquals($expected, $actual);
 }

Re-run the test

Re-run the test again and this time you should see some more passes if not refactor your code till you get all passes.

eferuzi@feruzi /v/w/phpunit> php5 test.php
TestCase CalculatorTest->testAdd() passed
TestCase CalculatorTest->testAddInvalidValues() passed
TestCase CalculatorTest->testSubtract() passed
TestCase CalculatorTest->testMultiply() passed
TestCase CalculatorTest->testDivideByZero() passed
TestCase CalculatorTest->testDivide() passed

Download Source Code

More information:-

  • TDD:
    • http://en.wikipedia.org/wiki/Test-driven_development
    • http://www.extremeprogramming.org/rules.html
  • PHPUnit:
    • http://www.phpunit.de/
Do not hesitate to ask me questions.

Sunday, November 22, 2009

kiGM : A Terminology Tool




To explain kiGM one has to start with GlossMasterGlossMaster is the fabulous multi-lingual terminology tool developed for ANLoc, the African Network for Localization. kiGM is a very lightweght version GlossMaster with an aim of allowing user with low or no internet connection to work and sycn data with GlossMaster periodically.GlossMaster. kiGM has a verys small subset of the functionality that Glossmaster.

Please come in and help.