在NS2的源代碼包中包含了幾乎所有協議的測試代碼,全部存放在ns2\tcl\test目錄下,即test-suite-*.tcl這樣的文件。每個文件基本就對應一個協議,而且有一個對應的輸出目錄存放供比較用的運行結果。在運行test-suite-*.tcl之后將會產生一個 temp.rands文件,只要這個文件和相應目錄下的文件比較相一致就說明NS2的安裝是正確的。
如test-suite-adaptive-red.tcl這個文件的對應目錄就是test-output-adaptive-red。
每個測試用的tcl文件中都使用了一個或多個測試用例,因此output目錄下也對應有一個或者多個文件,這些供比較用的文件都是用gzip壓縮的,比較前應先解壓縮。通過ns test-suite-*.tcl這樣的命令可以知道每個TCL文件所支持的測試用例。使用ns test-suite-*.tcl test_name這樣的格式就可以調用指定的測試用例。
如Ns test-suite-adaptive-red.tcl red1
就指明了要調用red1這個測試用例,運行之后在測試目錄下生成temp.rands文件,將這個文件與test-output-adaptive-red\red1.Z解壓縮后的文件進行比較即可。
2測試代碼
在此測試目錄下同時提供了完整的測試Shell代碼,不過很遺憾在windows下沒法運行(需要cygwin),于是自己動手寫了下面的C++代碼進行完整的測試。
// NsTest.cpp : 定義控制臺應用程序的入口點。
//
#include "stdafx.h"
#include
#include
#include
using namespace std;
#define TEST_PATH "d:\\temp\\test\\"
#define FIND_MASK "d:\\temp\\test\\test-suite-*.tcl"
#define NS_PATH "d:\\research\\debug\\ns-2.31.exe"
void Compare(char* f1, char* f2)
{
// 比較兩個文件是否相同,之所以不用cmp進行比較,是因為在不同版本生成的數據中輸出格式可能會不一致,
// 主要是科學計數法輸出的不一致,如提供的原始數據為.2e-05,而生成的數據為.2e-005
ifstream s1(f1);
ifstream s2(f2);
if(s1.is_open() && s2.is_open())
{
char line1[2048], line2[2048];
int nLine = 0;
while(!s1.eof() && !s2.eof())
{
nLine++;
s1.getline(line1, 2048);
s2.getline(line2, 2048);
if(strcmp(line1, line2) == 0) continue;
double d[4];
sscanf(line1, "%lf %lf", &d[0], &d[1]);
sscanf(line2, "%lf %lf", &d[2], &d[3]);
if(fabs(d[0] - d[2]) > 0.00005 || fabs(d[1] - d[3]) > 0.005)
{
printf("%s and %s compare failed: \nline: %d\n%s\n%s\n", f1, f2, nLine, line1, line2);
s1.close();
s2.close();
exit(1);
}
}
if(s1.eof() && !s2.eof())
{
printf("%s and %s compare failed: s1.eof() && !s2.eof()", f1, f2);
s1.close();
s2.close();
exit(1);
}
else if(!s1.eof() && s2.eof())
{
printf("%s and %s compare failed: !s1.eof() && s2.eof()", f1, f2);
s1.close();
s2.close();
exit(1);
}
}
else
{
printf("compare file open failed: \n%s\n%s\n", f1, f2);
s1.close();
s2.close();
exit(1);
}
s1.close();
s2.close();
}
void Exec(char *cmd)
{
DWORD code = 0;
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
// Start the child process.
if( !CreateProcess( NULL, // No module name (use command line).
cmd, // Command line.
NULL, // Process handle not inheritable.
NULL, // Thread handle not inheritable.
FALSE, // Set handle inheritance to FALSE.
0, // No creation flags.
NULL, // Use parent's environment block.
TEST_PATH, // Use parent's starting directory.
&si, // Pointer to STARTUPINFO structure.
&pi ) // Pointer to PROCESS_INFORMATION structure.
原文轉自:http://www.anti-gravitydesign.com