Bits of Learning

Learning sometimes happens in big jumps, but mostly in little tiny steps. I share my baby steps of learning here, mostly on topics around programming, programming languages, software engineering, and computing in general. But occasionally, even on other disciplines of engineering or even science. I mostly learn through examples and doing. And this place is a logbook of my experiences in learning something. You may find several things interesting here: little cute snippets of (hopefully useful) code, a bit of backing theory, and a lot of gyan on how learning can be so much fun.

Wednesday, September 24, 2008

Alien Number System

Here's something about the C++ program I wrote for implementing the alien number system which has an arbitrary set of characters as its digits. Seems to work for all test cases I tried. See Google Codejam sample problems for details:

A number system is represented by a sequence of characters which represent its digits. For example, the decimal number system (also called the base-10 number system) has the characters '0', '1', '2', '3', '4', '5', '6', '7', '8' and '9' as its digits. Representing the number system in this sequence also means the following:

  • The first digit (for decimal system, it's '0') is the additive idempotent number. In other words, the effect of adding it to any other number is the same number. In more worldly language, it represents the smallest natural number, or simply, the zero.
  • The first number is the multiplicative idempotent number. Which means that by multiplying it to any number, you get back the same number. Also, the difference between any two successive numbers in this number system is this number. In simplest possible terms, its value is one.
  • Definition of one gives the definition of the successor function. succ(x) = x + one.
  • Addition is repetitive application of the succ function. For instance, in decimal system:
add(3, 4) = succ (succ (succ (succ (3)))) = succ(succ(succ(4))) = succ(succ(5)) = succ(6) = 7.
  • Multiplication is a repetitive application of addition function between the number and itself.
multiply(3, 4) = add(3, add(3, add(3, 3))) = add(3, add(6))) = add(3, 9) = 12.

The above phenomena underlie the design of a basic number system. And that's what is done in the code below. Apart from the above, we use some optimisation by implementing the add and multiply functions to simulate the algorithms generally used in manual calculations.

Interface:


#ifndef NUMBERSYSTEM_H

#define NUMBERSYSTEM_H



#include <vector>

#include <map>

#include <string>



using namespace std;





class NumberSystem

{

private:

string m_Digits;

map< pair <char, char>, string> m_AddMap;

map< pair <char, char>, string> m_MultiplyMap;

public:

NumberSystem (string);

string add (string, string);

string add (char, char);

string add (string, char);

string multiply (char, char);

string multiply (string, string);



string succ (string);

string prev (string);



string getDigits ();

string getZero ();

char operator[] (unsigned int);

unsigned int size ();

};



string reverse (string);



#endif

Implementation

#include "NumberSystem.h"



NumberSystem::NumberSystem (string aDigits)

: m_Digits (aDigits)

{

// set up a lookup table to to speeden up the addition of two

// single digit numbers. Currently not used anywhere.

for (unsigned int i = 0; i < m_Digits.size (); i++)

{

for (unsigned int j = 0; j < m_Digits.size (); j++)

{

char a = m_Digits[i];

char b = m_Digits[j];



string sum = add (a, b);

pair <char, char> p = pair <char, char>(a, b);

m_AddMap[p] = sum;

}

}

// set up a lookup table to to speeden up the multiplication of two

// single digit numbers. Currently not used anywhere.

for (unsigned int i = 0; i < m_Digits.size (); i++)

{

for (unsigned int j = 0; j < m_Digits.size (); j++)

{

char a = m_Digits[i];

char b = m_Digits[j];



string product = multiply (a, b);

pair <char, char> p = pair <char, char>(a, b);

m_MultiplyMap[p] = product;

}

}

}





// Adds two single digit numbers.

string

NumberSystem::add (char a, char b)

{

string sum(" ");

sum[0] = a;

sum[1] = m_Digits[0];



int j = m_Digits.find (b);

for (unsigned int k = 1; k <= j; k++)

{

sum = succ (sum);

}

return sum;

}



// adds two numbers, one single digit, and another non-single digit.

string

NumberSystem::add (string a, char b)

{

string sum = a;



unsigned int j = m_Digits.find (b);

for (unsigned int k = 1; k <= j; k++)

{

sum = succ (sum);

}

return sum;

}



// adds two non-single digit numbers.

string

NumberSystem::add (string n1, string n2)

{



string a;

string b;

if (n1.size () >= n2.size ())

{

a = n1;

b = n2;

}

else

{

a = n2;

b = n1;

}



string sum;

char carry = m_Digits[0];

for (unsigned int i = 0; i < b.size (); i++)

{

string digitSum = add (a[i], b[i]);

digitSum = add (digitSum, carry);

sum = sum + digitSum[0];

if (digitSum.size () == 2)

{

carry = digitSum[1];

}

else

{

carry = m_Digits[0];

}

}

for (unsigned int i = b.size (); i < a.size (); i++)

{

string digitSum = add (add (a[i], m_Digits[0]), carry);

sum = sum + digitSum[0];

if (digitSum.size () == 2)

{

carry = digitSum[1];

}

else

{

carry = m_Digits[0];

}

}

if (carry != m_Digits[0])

{

sum = sum + carry;

}

return sum;

}



// given any number, it returns the successor of it.

string

NumberSystem::succ (string aNum)

{

if (aNum[0] == m_Digits[m_Digits.size () - 1])

{

aNum[0] = m_Digits[0];

}

else

{

aNum[0] = m_Digits[m_Digits.find (aNum[0]) + 1];

return aNum;

}

for (unsigned int i = 1; i < aNum.size (); i++)

{

if (aNum[i - 1] == m_Digits[0])

{

if (aNum[i] == m_Digits[m_Digits.size () - 1])

{

aNum[i] = m_Digits[0];

}

else

{

aNum[i] = m_Digits[m_Digits.find (aNum[i]) + 1];

return aNum;

}

}

}

if (aNum[aNum.size () - 1] == m_Digits[0])

{

aNum = aNum + m_Digits[1];

}

return aNum;

}



// given any number, this function returns a previous number. Not implemented

// as yet.

string

NumberSystem::prev (string aNum)

{

return aNum;

}



// multiplies two single-digit numbers.

string

NumberSystem::multiply (char aa, char bb)

{

string product (1, m_Digits[0]);

for (string counter (1, m_Digits[0]); counter != string (1, aa); counter = succ (counter))

{

product = add (product, bb);

}

return product;

}



//multiplies two non-single digit numbers.

string

NumberSystem::multiply (string a, string b)

{

vector <string> Products;

for (unsigned int i = 0; i < a.size (); i++)

{

char carry = m_Digits[0];

string product1;

for (unsigned int j = 0; j < i; j++)

{

product1 = product1 + (m_Digits[0]);

}

for (unsigned int j = 0; j < b.size (); j++)

{

string digitProduct = multiply (a[i], b[j]);

digitProduct = add (digitProduct, carry);

product1 = product1 + digitProduct[0];

if (digitProduct.size () == 2)

{

carry = digitProduct[1];

}

else if (digitProduct.size () == 1)

{

carry = m_Digits[0];

}

else

{

exit (1);

}

}

if (carry != m_Digits[0])

{

product1 = product1 + carry;

}

Products.push_back (product1);

}

string product (1, m_Digits[0]);

for (unsigned int i = 0; i < Products.size (); i++)

{

product = add (product, Products[i]);

}

return product;

}



// returns the digits of the number system.

string

NumberSystem::getDigits ()

{

return m_Digits;

}



// returns the zero value of the number system.

string

NumberSystem::getZero ()

{

return string (1, m_Digits[0]);

}



// returns the apos-th digit of the number system

char

NumberSystem::operator [] (unsigned int apos)

{

return m_Digits[apos];

}



// returns the number of digits in the number system.

unsigned int

NumberSystem::size ()

{

return m_Digits.size ();

}



// reverses a string. Used for user-interface purposes.

string reverse (const string s)

{

string o;

if (s.size ())

{

for (unsigned int i = 0; i < s.size (); i++)

{

o = o + s[s.size () - i - 1];

}

}

return o;

}



That's it!

Tuesday, September 23, 2008

Alien Number System

Here's the C++ program I wrote for implementing the alien number system which has an arbitrary set of characters as its digits. Seems to work for all test cases I tried. See Google Codejam sample problems for details:

#include
#include
#include

#include
#include <map>

using namespace std;

string reverse (string);

class NumberSystem
{
private:
string m_Digits;
map char, char, string> m_AddMap;
map char, char>, string> m_MultiplyMap;

public:
NumberSystem (string);
string add (string, string);
string add (
char, char);
string add (string,
char);
string multiply (
char, char);
string multiply (string, string);
string multiply1 (string, string);

string succ (string);
string prev (string);

string getDigits ();
string getZero ();
char operator[] (unsigned int);
unsigned int size ();
};

NumberSystem::NumberSystem (string aDigits)
: m_Digits (aDigits)
{
for (
unsigned int i = 0; i < m_Digits.size (); i++)
{
for (
unsigned int j = 0; j < m_Digits.size (); j++)
{
char a = m_Digits[i];
char b = m_Digits[j];

string sum = add (a, b);
pair
char, char> p = pair <char, char>(a, b);
m_AddMap[p] = sum;
}
}
for (
unsigned int i = 0; i < m_Digits.size (); i++)
{
for (
unsigned int j = 0; j < m_Digits.size (); j++)
{
char a = m_Digits[i];
char b = m_Digits[j];

string product = multiply (a, b);
pair <
char, char> p = pair <char, char>(a, b);
m_MultiplyMap[p] = product;
}
}
}

string
NumberSystem::add (
char a, char b)
{
string sum(
" ");
sum[
0] = a;
sum[
1] = m_Digits[0];

int j = m_Digits.find (b);
for (
unsigned int k = 1; k <= j; k++)
{
sum = succ (sum);
}
return sum;
}

string
NumberSystem::add (string a,
char b)
{
string sum = a;

unsigned int j = m_Digits.find (b);
for (
unsigned int k = 1; k <= j; k++)
{
sum = succ (sum);
}
return sum;
}

string
NumberSystem::add (string n1, string n2)
{

string a;
string b;
if (n1.size () >= n2.size ())
{
a = n1;
b = n2;
}
else

{
a = n2;
b = n1;
}

string sum;
char carry = m_Digits[0];
for (
unsigned int i = 0; i < b.size (); i++)
{
string digitSum = add (a[i], b[i]);
digitSum = add (digitSum, carry);
sum = sum + digitSum[
0];
if (digitSum.size () ==
2)
{
carry = digitSum[
1];
}
else

{
carry = m_Digits[
0];
}
}
for (
unsigned int i = b.size (); i < a.size (); i++)
{
string digitSum = add (add (a[i], m_Digits[
0]), carry);
sum = sum + digitSum[
0];
if (digitSum.size () ==
2)
{
carry = digitSum[
1];
}
else

{
carry = m_Digits[
0];
}
}
if (carry != m_Digits[
0])
{
sum = sum + carry;
}
return sum;
}

string
NumberSystem::succ (string aNum)
{
if (aNum[
0] == m_Digits[m_Digits.size () - 1])
{
aNum[
0] = m_Digits[0];
}
else

{
aNum[
0] = m_Digits[m_Digits.find (aNum[0]) + 1];
return aNum;
}
for (
unsigned int i = 1; i < aNum.size (); i++)
{
if (aNum[i -
1] == m_Digits[0])
{
if (aNum[i] == m_Digits[m_Digits.size () -
1])
{
aNum[i] = m_Digits[
0];
}
else

{
aNum[i] = m_Digits[m_Digits.find (aNum[i]) +
1];
return aNum;
}
}
}
if (aNum[aNum.size () -
1] == m_Digits[0])
{
aNum = aNum + m_Digits[
1];
}
return aNum;
}

string
NumberSystem::prev (string aNum)
{
return aNum;
}

string
NumberSystem::multiply (string a, string b)
{
string counter (
1, m_Digits[0]);
string product (
1, m_Digits[0]);

for (; counter != b; counter = succ (counter))
{
product = add (product, a);
}
return product;
}

string
NumberSystem::multiply (
char aa, char bb)
{
string product (
1, m_Digits[0]);
for (string counter (
1, m_Digits[0]); counter != string (1, aa); counter = succ (counter))
{
product = add (product, bb);
}
return product;
}

string
NumberSystem::multiply1 (string a, string b)
{
vector Products;
for (
unsigned int i = 0; i < a.size (); i++)
{
char carry = m_Digits[0];
string product1;
for (
unsigned int j = 0; j < i; j++)
{
product1 = product1 + (m_Digits[
0]);
}
for (
unsigned int j = 0; j < b.size (); j++)
{
string digitProduct = multiply (a[i], b[j]);
digitProduct = add (digitProduct, carry);
product1 = product1 + digitProduct[
0];
if (digitProduct.size () ==
2)
{
carry = digitProduct[
1];
}
else if (digitProduct.size () ==
1)
{
carry = m_Digits[
0];
}
else

{
exit (
1);
}
}
if (carry != m_Digits[
0])
{
product1 = product1 + carry;
}
Products.push_back (product1);
}
string product (
1, m_Digits[0]);
for (
unsigned int i = 0; i < Products.size (); i++)
{
product = add (product, Products[i]);
}
return product;
}

string
NumberSystem::getDigits ()
{
return m_Digits;
}

string
NumberSystem::getZero ()
{
return string (
1, m_Digits[0]);
}


char
NumberSystem::operator [] (unsigned int apos)
{
return m_Digits[apos];
}

unsigned int
NumberSystem::size ()
{
return m_Digits.size ();
}

string reverse (string s)
{
string o;
if (s.size ())
{
for (
unsigned int i = 0; i < s.size (); i++)
{
o = o + s[s.size () - i -
1];
}
}
return o;
}


class Converter
{
private:
string m_Input;
NumberSystem m_Source;
NumberSystem m_Target;
string m_Output;

map string, string> m_Map;
public:
Converter (string, NumberSystem, NumberSystem);
string getOutput ();
string convert ();
void makeMap ();
};

Converter::Converter (string aN, NumberSystem aS, NumberSystem aT)
: m_Input (aN)
, m_Source (aS)
, m_Target (aT)
{
makeMap ();
}

void
Converter::makeMap ()
{
if (m_Source.size () >= m_Target.size ())
{
string cOut = m_Target.getZero ();
for (
unsigned int i = 0; i < m_Source.size (); i++)
{
char c[2];
c[
0] = m_Source[i];
c[
1] = '\0';
m_Map[c] = cOut;
cOut = m_Target.succ (cOut);
}
}
else

{
string cIn = m_Source.getZero ();
for (
unsigned int i = 0; i < m_Target.size (); i++)
{
char c[2];
c[
0] = m_Target[i];
c[
1] = '\0';
m_Map[cIn] = c;
cIn = m_Source.succ (cIn);
}
}

// cout << "*******************************" << endl;
// for (map::iterator I = m_Map.begin (); I != m_Map.end (); I++)
// {
// cout << "Map[" << reverse ((*I).first) << "] = " << reverse ((*I).second) << endl;
// }
// cout << "*******************************" << endl;

}

string
Converter::getOutput ()
{
return m_Output;
}

string
Converter::convert ()
{
string output = m_Target.getZero ();
string nine (
1, m_Source[m_Source.size () - 1]);
string ten = m_Target.succ (m_Map[nine]);

for (
unsigned int i = 0; i < m_Input.size (); i++)
{
string product = m_Map[string (
1, m_Input[i])];
for (
unsigned int j = 0; j < i; j++)
{
product = m_Target.multiply (product, ten);
}
output = m_Target.add (output, product);
}
return output;
}

vector ;

getInput (istream & fin)
{
unsigned int n;
vector v;
fin >> n;
for (
unsigned int i = 0; i < n; i++)
{
string num;
string s;
string t;
fin >> num;
fin >> s;
fin >> t;

Converter c (reverse (num), s, t);
v.push_back (c);
}
return v;
}


int main (int argc, char ** argv)
{
/*
vector v;
if (argc < 2)
{
cout << "argc = " << argc << endl;
cout << "argv[0] = " << argv[0] << endl;
getInput (cin);

}
else
{
ifstream fin (argv[1]);
v = getInput (fin);
fin.close ();
}

for (unsigned int i = 0; i < v.size (); i++)
{
cout << "output = " << reverse (v[i].convert ()) << endl;
}
*/

/*
Counter c ("01");
for (unsigned int i = 0; i < 100; i++)
{
++c;
cout << reverse (c.getCurrentCount ()) << endl;
}
NumberSystem ns1 ("0123456789");
cout << "45 + 50 = " << reverse (ns1.add (reverse ("45"), reverse ("50"))) << endl;
cout << "4512222 * 50345 = " << reverse (ns1.multiply (reverse ("4512222"), reverse ("50345"))) << endl;
NumberSystem ns2 ("01");
*/

NumberSystem ns1 ("0123456789");
cout <<
"45113435 * 503112 = " <<>"45113435"), reverse ("503112"))) << endl;
getchar ();
cout <<
"45113435 * 503112 = " <<>"45113435"), reverse ("503112"))) << endl;

return
0;
}

Friday, November 30, 2007

Operator overloading

Don't overload the ostream & operator <<. Reason: You can't have this operator as a member function of a class (let me know if I am wrong). Therefore, there doesn't seem to be a way for using polymorphism by declaring these operators as virtual.

#include
#include
#include

using namespace std;

class some
{
protected:
string id;
public:
some (string aid){ id = aid; }
friend ostream & operator (ostream &, some &);
};

ostream & operator << (ostream & fout, some & asome)
{ fout <<
"Class some: " << id << endl;}


class someother : public some
{
public:
someother (string aid) : some (aid){}
friend ostream & operator << (ostream &, someother &);
};

ostream & operator << (ostream & fout, someother & asome)
{ fout <<
"Class someother: " << id << endl;}

int main ()
{
some A ("A");
some
B ("B");
some * C = new
someother ("C");
cout << style="color: rgb(0, 0, 0);">;
cout << B;
cout << (*C);
delete C;
return 0;
}

output:


Class some: A
Class some: B
Class some: C



As you can see above, the objective was to call the operator <<



#include
#include
#include
using namespace std;



class some
{
protected:
string id;
public:
some (string aid){ id = aid; }
friend ostream & operator << (ostream &, some &); virtual ostream &
print (ostream & fout)
{
fout
"Class some: " <<>return fout;
}
};


class someother : public some
{
public:
someother (string aid) : some (aid){}

virtual ostream &
print (ostream & fout)
{
fout <<
"Class someother: " <<>return fout;
}
};


int main ()
{
some
A ("A");
some
B ("B");
some * C = new
someother ("C");
A.
print (cout);
B.
print (cout);
(*C)
print (cout);
delete C;
return
0;
}

Thursday, August 23, 2007

Classes versus Functions

While working with an OO language like C++ or Java, we often stumble into an issue of choosing between a class and a function.

Let me illustrate the case. Say, we are writing a parser. A C-style way to do is to form it in the shape of a function, say parse (FILE * fin) , that returns the IR after parsing. The caller of this function would be responsible to do what he wants with the IR.

There are more one choice to do the same thing when working with an OO language. A class named Parser would possibly have a parse (FILE * fin) method returning the IR. Or it could have a constructor Parser (string FileName), and an argument-less method parse () returning the IR. The merits of choosing one over the other seem to me rather unimposing. The only difference between the two approaches is that the input for parsing is defined while making a call to the method in the former case, while, in the latter, it gets defined at the time of the creation of the parser object. It hardly matters!

For me, in a basic case, having a parser class is little more than syntactic sugar. May be, in a more advanced scenario, it helps having a class for the Parser, so that parser states can be encapsulated in private attributes. Among these, FileName is surely not one. It can always be passed to the parse method while calling.

I arbitrarily prefer having a parameterised method parse (FILE * File) or parse (string FileName), so that I can use the same parser object for parsing many times. Nothing fundamental about this choice.

Thursday, June 28, 2007

A Software Engineering Practice Problem

Suppose team A has built a module M, which is used by many of the products of A. Team B borrows M from A. While using it, they find a bug in M. In that process, they also create a fix which works for them. The question is: What should be the process by which the bug is reported to team A, and how should the fix be incorporated?

The potential difficulty is that there are many products that A had made which use M. Hence, if the fix is incorporated in the next version, all those products need to be regression tested.

Of course, it's a very common problem, and people must be doing something to solve it at their own level. Can we note down some common-sense technique here?

Friday, March 16, 2007

A Small Test Automation System

Here I describe a small test automation system that has come in handy for me. It's very crude and would obviously work for very small scale individual level software development. The kind of software it would work for are those which take an input in the form of a file or from standard input, and output it into the standard output. In particular language translators. However, I am sure that it covers a very broad ground. And a simple constraint of having to build your translator, so it is testable by this kind of testing system will automatically result in good programming practice. I can guarantee that it has yielded some bit of productivity rise for me, a significant increase in correctness as testing and bug catching was easier and hence done more freely, exhaustively and frequently, and hell lot of fun!

We create a directory named test in the directory where the program executable (let's call it prog) is placed. In this directory we create the following directories:
* input : The directory which contains all the inputs of the test cases
* output : The directory where the test harness will dump the outputs of running the prog on each test into a separate file of the same name (possibly with file name extension .out)
* expect : The directory where the expected output of each test case is placed in a separate file of the same name
(possibly with file name extension .exp)
* description : The directory where the description of each test case is placed in a separate file of the same name (possibly with file name extension .desc)

We work with the following scripts (written in your favourite scripting language). They are the following:
- createtest : This script asks for a test case name and creates the same. It will look for the input file of the same in the input directory, and will run the prog on it, dumping the output into a file of the same name in the expect directory after getting the user's consent about the correctness of the generated output.
- testTestCase : This script takes as an input a test case name, runs prog on the corresponding input file in input directory, and dumps the output into the output directory. Then it does a simple unix diff between the expected output (the file of the same name in the expect directory), and generated output (the file of the same name in the output directory). It plants the PASS or FAIL verdict into a file (with .log extension) into the current working directory.
- testTestSuite : This script takes as an input the name of a test suite file. The test suite file should contain the names of all the test cases to be tested in the test suite. The testTestSuite runs similar to testTestCase script on all the test cases. It plants its PASS or FAIL verdict for each test case into a file of the same name as the test suite.



createTest.sh
#/bin/sh

inputdir="./${1}/input/";
outputdir="./${1}/output/";
expectdir="./${1}/expect/";
descriptiondir="./${1}/description/";

if [ $# -ne 2 ]
then
echo "Usage - $0 app-name test-case"
exit 1
fi

testcasename=$2

ls ${descriptiondir}${testcasename}.desc
if [ "$?" = "0" ]
then
echo "Current Description: `cat ${descriptiondir}${testcasename}.desc`"
echo "Do you want to change the description? (y / n)"
read isNewDesc
if [ "$isNewDesc" = "y" ]
then
`rm ${descriptiondir}${testcasename}.desc`
grep "\/\/" ${inputdir}${testcasename}.kc >> ${descriptiondir}${testcasename}.desc
fi
else
grep "\/\/" ${inputdir}${testcasename}.kc >> ${descriptiondir}${testcasename}.desc
fi

ls ${inputdir}${testcasename}.kc
if [ "$?" = "0" ]
then
echo "Current Input: `cat ${inputdir}${testcasename}.kc`"
echo "do you want to change the input? (y / n)"
read isNewInput
if [ "$isNewInput" = "y" ]
then
`rm ${inputdir}${testcasename}.kc`
unset f
echo "type the test input data (to end the input, type 'eof' in the line following the last input line):"
while :
do
read f
if [ "$f" == eof ]
then
echo "Input data done"
break
fi
echo $f >> ${inputdir}${testcasename}.kc
done
fi
else
unset f
echo "type the test input data (to end the input, type 'eof' in the line following the last input line):"
while :
do
read f
if [ "$f" == eof ]
then
echo "Input data done"
break
fi
echo $f >> ${inputdir}${testcasename}.kc
done
fi

cat ${inputdir}${testcasename}.kc | ../${1} > ${expecteddir}${testcasename}.exp

./viewtest.sh $1 $testcasenam

testTestCase.sh
#/bin/sh

if [ $# -ne 2 ]
then
echo "Usage - $0 app-name test-case"
exit 1
fi

inputdir="./${1}/input/";
outputdir="./${1}/output/";
expectdir="./${1}/expect/";

CurrentIn="${inputdir}${1}.kc"
CurrentOut="${outputdir}${1}.out"
echo $CurrentIn
echo $CurrentOut

cat $CurrentIn | ../${1} > $CurrentOut

echo "Test result for application ${1} test-case ${2}"
CurrentExpected="${expectdir}${2}.exp"
CurrentOut="${outputdir}${2}.out"
echo "Comparing $CurrentExpected and $CurrentOut"
diff $CurrentExpected $CurrentOut > ${1}.${2}.log
if [ "$?" != "0" ]
then
echo "Test case $2: FAILED!"
else
echo "Test case $2: PASSED!"
fi


testTestSuite.sh

#/bin/sh

if [ $# -ne 2 ]
then
echo "Usage - $0 app-name test-suite"
exit 1
fi

inputdir="./${1}/input/";
outputdir="./${1}/output/";
expectdir="./${1}/expect/";

echo "testing application ${1} on test-suite ${2}"
while read f
do
CurrentIn="${inputdir}${f}.kc"
CurrentOut="${outputdir}${f}.out"
echo $CurrentIn
echo $CurrentOut
cat $CurrentIn | ../${1} > $CurrentOut
done < $2

if [ `ls ${2}.log` ]
then
rm ${2}.log
fi

echo "Test result for test-suite ${f}"
while read f
do
CurrentExpected="${expectdir}${f}.exp"
CurrentOut="${outputdir}${f}.out"
echo "Comparing $CurrentExpected and $CurrentOut"
diff $CurrentExpected $CurrentOut > temp
if [ "$?" != "0" ]
then
echo "Test case $f: FAILED!" >> ${1}/${2}.log
else
echo "Test case $f: PASSED!" >> ${1}/${2}.log
fi
done < $2
rm temp



A test suite
1
2
3
4
5
6
7
8
9
10
11
12
19
20
21
22
23
24
25
26

A test verdict:
Test case 1: PASSED!
Test case 2: PASSED!
Test case 3: PASSED!
Test case 4: PASSED!
Test case 5: PASSED!
Test case 6: PASSED!
Test case 7: PASSED!
Test case 8: PASSED!
Test case 9: PASSED!
Test case 10: PASSED!
Test case 11: PASSED!
Test case 12: PASSED!
Test case 19: PASSED!
Test case 20: PASSED!
Test case 21: FAILED!
Test case 22: FAILED!
Test case 23: FAILED!
Test case 24: FAILED!
Test case 25: FAILED!
Test case 26: PASSED!

A download page for this tool (with a more up to date source code and instructions for use)

Friday, February 23, 2007

Changing screen resolution

For a linux machine, the screen resolution can be changed by setting it in the following file:
/etc/X11/xorg.conf

In the section named 'Screens' add a 'Display' subsection. Something like:
Subsection "Display"
Depth 24
Virtual 1280 800
EndSubsection

It sets the screen resolution to 1280 x 800.

Tuesday, February 06, 2007

Automation for IISc

Have you ever felt frustrated to find the bathroom lamp broken and having to go all the way to the complaint cell to lodge a complaint? It's not the going which is the painful part, rather the remembering-to-go part which is painful. How nice would it be to be able to lodge the complaint right at the moment you notice or remember it? A bit of automation would definitely help, wouldn't it? If, just by clicking on a link, you could lodge your complaint and track its progress, it would bring in so much more sanity in our life. Another example: Booking the guest rooms and tracking the vacancies online etc etc. That would save us the trouble of waiting at the Hostel office at 4 PM a month before the occupation date (and then possibly getting disappointed).

I intend to maintain a list of ideas for automation that can be done in the day to day working in IISc. The idea can be picked up by the administrators for implementation. This is a very humble effort. The more difficult part -- that of implementing and deploying and getting them accepted by the IISc system -- remains by far the tougher part of the job. I consider it a viable idea to create proof-of-concept prototypes in the Software-Architecture course term-projects by CSA students. The analysis and design, and possibly the source-code, can then be reused to implement the deployed system. I think getting them implemented by professional software developers is very much a possibility. It shouldn't be unaffordable.

Foreseeable Problem
An obvious hurdle in getting this working is that of coupling this with a centralised database of IIScians. That system has to be 24 X 7 available, up-to-date, consistent and secure. While setting up of such a database would require some work, the development of the above systems can be done in parallel with some foresight.

Visitors to this post may contribute by:

  • Suggesting additional such problems
  • Providing links to already existing resources which can be reused
  • Providing the solutions themselves, partial or complete
  • Pointing out possible problems in implementation of these systems. The problems would preferably be technical, or at least apolitical.
Online Complaint Cell
It should be possible to place civil and electrical complaints online by all members of the campus. The system should allow tracking of the complaints.

Online Guest Room Booking
Making the process of guest-room booking online will make the process fast, trouble-free and transparent. It should be possible see the accommodation status of various guest-houses in the campus online.
(Think of the Railway, Airline, Hotel ticket booking systems we use so often).

Online Course Registration
Instructors may float their course on the department web page with a provision for students to apply for registration. The instructor may then go through the details of the student and decide to pass or reject the application. Depending on the availability of course/participants, the complete thing can be automatically scheduled (I had implemented a program that modelled this part as a graph-colouring problem to create a schedule, unless unschedulable).

Automated Web-Page Creation
It's so much easier to reach out to the world if you have your own official webpage. Being in IISc, and having to create webpages in geocities and googlepages is a lot less cool. Can't we have an equally convenient system which allows creation of our webpages in our departmental servers? The creation and editing should be easy with a couple of clicks of buttons.
(think of orkut, geocities, blogspot etc. which allow creation of whacky webpages with a few clicks of buttons.)

Research Publications Page
We could have a portal that gives an interface to manage our publications page. It allows us to add/modify/delete entries, organise them in different categories (conference, journal etc.). This could be a part of the automated web-page creation portal.


IISc Blog (eVoices)
I see it as some kind of an idealised 'Voices' where IIScians blog freely. The featured blogs can then be published in a hard-copy version of Voices.
(We had implemented a primitive version of this long time back, but it needed a lot more work to scale up).

Saturday, December 02, 2006

Algorithmic Design and Architectural Design

There are two types of design decisions we always seem to make while building large programs:
Architectural
Algorithmic

Architectural design decisions are decisions related to modularisation, division of responsibility, information accessibility and such things. For example, in a typical object oriented implementation, they would translate into decisions about class design, namespaces, fields and methods of a classes, inheritance hierarchy etc.

Algorithmic design is the choice of data-structure and control flow.

I feel, documenting architectural design decisions is best done offline, off code, in separate place. On the other hand algorithmic decision related documentation is best placed close to its implementation, as code comments.
Just an observation. Nothing biblical about it.

Monday, August 21, 2006

MS Ramaiah Polytechnic Talk

Last Saturday, I got a good experience of giving a lecture on 'Advances in Software Testing' in MS Ramaiah Polytechnic.

The invitation came from Mr. T. Shankar, a scientific officer in our department who was also one of the organisers of the Training the Trainers workshop in February this year. I had given a talk in that workshop which was received quite well.

While I was in middle of preparing for the lecture, I got this input from several sources that I could expect a fairly frigid audience. It was quite demoralising as I was hoping to present something really different than the usual stuff. It was quite ambitious and I was quite excited in the beginning. However, thinking about the possible response I would get for all that effort, I got quite psyched. I aborted the slide preparation in middle, and decided to make the whole thing impromptu. Going by what I expected, the lecture would be around 35-40 minutes long.

However on reaching there my experiences turned out to be quite to the contrary. I was warmly welcomed by the principal of the college. On entering the lecture hall I found a bunch of bringht young faces waiting for me. Quite gratifyingly, I could strike up a comfortable and friendly interacative conversation with the students. The lecture, that I thought wouldn't survive beyond 30-40 minutes, flowed smoothly for nearly two hours! It was such a terrific experience to evoke so much interactiveness with the students who'd been alleged to be uninterested. I felt like having won a victory in a battle. Added to this pure joy, my eog was also gratified as I was given a very generous thanks and a memento -- a clock, a bouquet, and a shawl. I felt really honoured.

Here are the slides.

Monday, August 07, 2006

Orientation talk

As every year, there's going on the orientation programme for the new comers to Computer Science and Automation department in IISc. It follows the usual format of introducing the new students to various aspects of the department : coursework, research, labs, computing environments, administrative procedures etc.

I presented a talk on Software Engineering in CSA this thursday.

Here's the link to the slides.

It was late in the evening just before dinner. The audience looked tired, majority of which were the organisers, and very few new students. I had worked pretty hard in preparing those slides. But I got a very uninterested look in the faces of the members of the audience. I scurried through the talk in a resembling mood!

Nevertheless, I always look forward to talking to new students.

JRE plugin for Firefox

To run applets, we need the Java Runtime plugin for the browser. The Java Runtime Environment (JRE) plugin for mozilla is available alongwith the JRE installation. For example, if the JRE is installed in the following location:

/usr/java/j2re1.4.2_10/

Then go it its plugin directory. You may find such directories:
ns4/ ns610/ ns610-gcc32/

All of them contain a file named libjavaplugin.so.

One of these is the shared object plugin that you want.

Now go to .mozilla/plugins/ directory in your home.
create a soft link of the above file here:
ln -s /usr/java/j2re1.4.2_10/plugin/i386/ns4/libjavaplugin.so .

If this happens to be the right plugin your firefox will now be able to run applets without trouble. If it's not, firefox will not run. If that happens just remove the
soft link:
unlink libjavaplugin.so from the mozilla/plugins directory and try the libjavaplugin.so files in the other directories in /usr/java/j2re1.4.2_10/plugin/i386/.

One of them should work.

Thursday, June 01, 2006

scp not working

Other machines were not able to connect to my laptop, either through scp or ssh.

The service called nifd which 'is a daemon which runs on Howl clients to monitor the state of a network interace. nifd must be running on systems that use autoidp and mDNSResponder to automatically obtain a Link-Local IPv4 address and do Zeroconf service discovery. nifd should not be running otherwise.'

Now whatever that means. The last sentence was vital. That service needed to be stopped. I stopped it. And the scp started working.

Sunday, May 28, 2006

CVS Repair

Today when I tried to commit the testing directory to cvs, there was a strange problem coming up. All commands including cvs commit were failing when cvs was trying to go into a directory demo/input/api1. It would report no such directory existed and would abort.

I realised that initially the directory testing/demo/data/input/ was created as testing/demo/input and was cvs added. Later when I realised that I would like to divide my data as input and output and intermediate. So, moved the whole directory into testing/demo/inputdemo/data after creating it. Since the CVS directory named testing/demo/input was already there in the CVS repository, this move of a directory created the problem. I should perhaps have followed the following sequence to create the whole thing:

mkdir testing/demo/data
cp -r testing/demo/input testing/demo/data
rm -f testing/demo/input/*
cvs remove testing/demo/input/*
cvs remove testing/demo/input
rm -rf testing/demo/data/input/api1/CVS
rm -rf testing/demo/data/input/CVS
cd testing/demo/data/input/api1
cvs add *
cd ..
cvs add api1
cd ..
cvs add input

OK! That's quite a long process. I don't think it would scale for a more complicated shift of directories within the working directory. I am sure there's another better way to do it. Nevertheless...

So, once the mistake was done (that is, shifting the directory without following the above sequence), I was persistently getting the above errors. I finally managed to get rid of it by the following process:

Went to the CVSROOT and saw that there's a directory testing/demo/input.
I moved it to testing/demo/data/

Then I came to the working directory, and went to the testing/demo/data/input/CVS
I opened the Repository file. In it, I saw that the path given was testing/demo/input. I changed it to testing/demo/data/input. I went to all the directories contained in testing/demo/data/ and changed this erroneous pointer to the repository.
I also edited the Entries file of the testing/demo/CVS directory to remove the 'input' entry from it. This now fixed the problem of cvs trying to look for this this directory in the testing/demo directory. I had to remove some spurious entries in the Entries files of one or two CVS directories withing this path.

This fixed the problems. My CVS commands are now working fine.

Related Blog:
Working with CVS