Archive for the ‘Programming’ Category

仍然很忙

Saturday, January 19th, 2008

做了一段时间的测试构架终于接近尾声了,中间学到了很多很多超级有价值的开发经验,有些小小的骄傲。软件测试自动化是一个很麻烦的活,一不小心就会陷入一个无间怪圈。因为测试一个软件不是简简单单给个 expected value 和 actual value 然后对比一下就可以的。有人说过,一个软件有多少个 Feature ,就有多少个针对这个 Feature 的测试。即使这样子讲,我也只能给打个半对。因为一个稍微大型一点的程序,任何两个或者多个 Feature 结合起来都有可能是一个新的 Feature。这样使得在一个软件中增加任何一个 Feature,就像蝶振效应一样,可以让测试成本几何加倍。现代的软件测试都被自动化了,使得整个状况稍微好转了一下,如果跟传统的人工测试相比的话。可是虽然把一个人工测试变成自动测试可以节省下一次的人力时间,但懒惰的测试工程师往往会把整个测试过程懒散地写成一个流水账。在软件功能变动频繁的状况下,这种自动化比人工测试更昂贵。所以这些以为自动化就是流水账的人到了最后总会选择手工测试,最终导致整个软件测试覆盖范围缩水,然后软件质量下降。一个好的自动化测试一定有它自己的 AI、随机性、模块化、可扩展性、可部署性、可搭建性等,整个测试成本会节省很多。到最后人工测试的部分可能就是几个自动化测试无法到达的死角而已。这就需要有一个很好的测试构架或者 Test Library,让这样的测试自动化变成可能。利用 .Net 的  Reflection 可以对 classes 和 members of a class、objects 和 properties of an object 进行归类、规划,让本身一维的(流水账)、二维的(加几个包含不同 Variant 的循环)自动化测试扩展到多维,达到增加测试覆盖范围的目的。现在我才发现 .Net 中利用 metadata 实现的 Reflection 在测试自动化上是如此得心应手。

唉,人只会在感兴趣的东西上孜孜不倦。因此论文还是没有进展,时间都忙在了工作上。是时候开始碰碰论文了,所以仍然很忙。

Video: Introduction to Functional Programming and F#

Thursday, November 29th, 2007

When I was in undergrad school, we used to have a course on functional language taught by Dr. Kahl. We learned the concept of functional programming and got to get assignments done in Haskell. Until the end of the course, I wasn’t sure enough if I’ve learned what it is.

It’s been a while since I graduated in 2005 and this new concept of programming wasn’t getting my attention until recently, I saw the coming of F#. Then today, I found a good video talking about the basic concept of this new form of coding, as well as F#. Take a look. :)

Use base class iterator in a derived class with a template

Thursday, October 18th, 2007

To tell the truth, I am still a newbie in C++. So I decided to take a evening and do a quick exercise. What I am trying to do is to write a class which uses the iterator from its ‘parent’ class:

 1: #include <vector>
 2: #include <iostream>
 3:  
 4: template <typename T>
 5: class myVector : public std::vector<T>
 6: {
 7: public:
 8:  void Test();
 9: };
 10:  
 11: template <typename T>
 12: void myVector<T>::Test()
 13: {
 14:  typename std::vector<T>::iterator i;
 15:  
 16:  for (i = this->begin(); i != this->end(); i++)
 17:  std::cout << *i << std::endl;
 18:  
 19:  return;
 20: }
 21:  
 22: int main()
 23: {
 24:  myVector<int> v;
 25:  v.push_back(1);
 26:  v.push_back(2);
 27:  v.push_back(3);
 28:  v.push_back(4);
 29:  v.Test();
 30:  return 0;
 31: }
 

The whole point here is the typename in line 14. That is the tricky part which took me a while to figure out. Without it the code won’t even compile. Now I can add all different kinds of sorting methods to myVector. Yes, I know it’s a bad idea. Just code for fun.

Don’t write multiple statements in one line

Saturday, October 13th, 2007

… for several reasons:

  1. It doesn’t boost the performance.
  2. It doesn’t reduce the memory usage.
  3. It is hard to set a breakpoint while debugging.
  4. It reduces the readability.
  5. It is nothing to show off.

Counter example:

Don’t write multiple statements in one line for that, it doesn’t boost the performance and reduce the memory usage, but hard to set a breakpoint while debugging as well as reducing the readability, plus it is nothing to show off.

Let PowerShell to summarize your IM history

Saturday, March 24th, 2007

(March 25, 2007) Update: Thanks to James Manning for the -passthru parameter to simply the command.

Windows PowerShell, also known as “Monad”, is a cool shell environment to replace old CMD shell under Windows. It is an object oriented shell and it allow easy manipulation of different types of objects interactively. Another advantage is the support of various data structures and formats, such as XML. Just like other good old shells, it also supports piping. The difference is that, instead of text stream, it sends objects through the pipes.

I always want to find out some information about my Live Messenger history. Now let’s see what PowerShell can do with it. In most cases, Live Messenger history is saved under “My Received Files\<Live ID with some numbers>\History” in your Document folder. Now let’s boot into PowerShell and “Set-Location” or ”cd” to this folder. If you type “Get-ChildItem” or “dir” you will see a bunch of .xml files listed.

Alright, let’s first try to find the top 10 friends with most letters:

Get-ChildItem | Sort-Object Length -descending | Select-Object -first 10

At this point, I assume you are already familiar with the concept of piping. Here it gets a list of files, sort it and display the first 10. That’s not too difficult to understand.

Actually those files are in XML format. That’s why it ends with .xml extension. One important XML element in these files is the message. Each message element corresponds to a sentence sent between me and my friend. Let’s see how PowerShell deals with XML files in this case. The following command lists the top 10 friends with most messages:

Get-ChildItem |
ForEach-Object {
    $_ |
    Add-Member noteproperty -name count -value ([XML] (Get-Content $_ -Encoding UTF8)).Log.Message.Count -passthru
} |
Sort-Object -property count -descending |
Select-Object -first 10 |
Format-Table count,Name

It first gets a list of files. For each file, it creates a new object $o. Then it adds the count of messages from the XML file as a property of $o, as well as the name of each file. Finally, it sorts the object based on number of messages and displays the top 10.

Another interesting XML element of Live Messenger history file is LastSessionID. I guess It is the number of chat sessions established by either me or my friend. With a little modification to the previous command, I can know the top 10 friends with most sessions:

Get-ChildItem |
ForEach-Object {
    $_ |
    Add-Member noteproperty -name count -value ([Int32] ([XML] (Get-Content $_ -Encoding UTF8)).Log.LastSessionID) -passthru
} |
Sort-Object -property count -descending |
Select-Object -first 10 |
Format-Table count,Name

A small change is that LastSessionsID is read as a string. So we have to convert it to a integer number using [Int32].

Other XML elements, such as Date, Time and even text in each message, are very informative too, as long as you can explorer them in a good way. Right now, there are other more information I can think of out of my head, e.g. the top 10 longest message you received, the first 10 person talked with you, the longest 10 chat sessions in your IM history and so on.

For updated information and tricks on PowerShell, here is a good resource. If you have used Live Messenger on two or more computers, you might also want to consider this history merger.

Guess who sends the most smiles to me? :)

Temporary Redirection Plugin for Wordpress

Tuesday, November 14th, 2006

When people visit His Story with URL www.wangxiaohu.com, I always want it automatically goes to wangxiaohu.com in order to be consistent. So I wrote this plugin for personal use.

It redirects the current URL to another URL with desired domain name. The source code is here:

The plugin first checks if HTTP_HOST equals $target_host (In my case, it’s “wangxiaohu.com“). If it doesn’t, the plugin simply replaces HTTP_HOST with $target_host and provides a 307 header for temporary redirection.

To install,

  1. Give $target_host a new value
  2. Rename the file to extension .php
  3. Upload it to Wordpress wp-content/plugins directory.

WARNING: This plugin is not guaranteed to be problem free. Use it at your own risk.

小虎版 Wordpress 插件两个

Thursday, August 10th, 2006

这几天老是不务正业。前两天琢磨着整个基于 MetaWeblog API 的 Wordpress 转发器(crossposter),后来发现实在时间有限,所以作罢。不过手还是很痒痒,老是想整点 Coding。算了,自己写不了就改改别人的吧。下面是下午刚改的两个 Wordpress 插件:

  • 小虎版 Counterize
    • 基于 Counterize 0.47
    • 增加了对包含了“baidu”、“sohu”等 user agent 的检测
    • 增加了 cz88.net 的 IP 地理位置显示
    • 如果 Admin 已登陆,则不记录 IP
    • 显示每个IP过去的访问次数
    • 基于 IP 地址的 Kill,而不是 ID

安装方法 先安装 Counterize 0.47,然后把 counterize.txt 改名为 .php 后缀,上传并替换原来的 counterize.php 。直接安装也可以,只是没有了那些红、绿、蓝的指示条。

安装方法 把 akismet.txt 改名为 .php 后缀,上传并替换原来的 akismet.php 。

注意 小虎对任何问题一概不主动不拒绝不负责,请三思而后下载!

Windows Live Spaces的MetaWeblog API的开启

Sunday, August 6th, 2006

类似Atom APILiveJournal APIMetaWeblog API是一种基于XML-RPC协议,允许通过第三方客户端来发表Blog的编程界面。现在,Windows Live Spaces也提供了对MetaWeblog API的支持。在Spaces里启用MetaWeblog API的设置步骤如下:

  1. 如果你还没有一个Passport帐号,需要在 http://www.passport.com 注册并获得
  2. 如果你还没有一个Space,需要在 http://spaces.live.com 注册并获得
  3. 进入你的Space,然后进入Settings,接着进入E-mail Publishing
  4. 打开E-mail Publishing功能
  5. 选择一个Secret Word

从先在开始,当你在设置MetaWeblog API第三方客户端的时候,用户名就是你的Space的名字,密码就是那个Secret Word。比如,如果Spaces的地址为 http://xiaohuwang.spaces.live.com ,那么用户名就是 xiaohuwang ,密码就是在第5个步骤里设置的那个Secret Word。最后,MetaWeblog API的请求地址为 https://storage.msn.com/storageservice/MetaWeblog.rpc

如果有兴趣开发基于 MetaWeblog API 的应用程序的话,可以访问参考MSDN的这个章节。(PS. 真的很想很想写个Wordpress到Live Spaces的转贴软件,可惜还要写硕士论文。命苦啊,为什么我不能做自己想做的事情?

ACM Student Membership

Wednesday, May 10th, 2006

I have just received my membership card from Association for Computing Machinary (ACM). My class is student, and this membership is provided due to my participation of 2005 ACM East Central North America Regional Programming Contest last year. I wrote about those two days during the contest at here and here, and the pictures can was placed here.

A Hint For Catching My Interest When Promoting A Computer Language

Friday, February 3rd, 2006

In CAS 703 class, it is asking to use two very different computer languages to implement an algorithm in the first assignment. These two languages are WEB, created by Don Knuth for literate programming; and Eiffel created by Bertrand Meyer, an object-oriented language featuring the concept of design by contract.

Unlike learning common computer languages, such as C/C++ and Perl, learning these two languages makes me head-ache, since I am not smart in learning languages and my TOFLE score is unspeakable. The paper for WEB and the documentation for Eiffel are fantastic and wonderful, but what I am actually looking for for my first step, is a piece of sample code less than 20 lines for each of them, and I don’t want to see just ‘Hello, World!‘.

Pardon me, I am lazy.


Chat with me. =)